blob: 1ebe51b15c1bc024703aad0d26ac833c9a0720ed [file] [log] [blame]
mtklein61fa22b2015-06-17 10:50:25 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
halcanary4dbbd042016-06-07 17:21:10 -07008#include "../private/SkLeanWindows.h"
herb7f0a3d72015-09-24 07:34:49 -07009#include "../private/SkSemaphore.h"
mtklein61fa22b2015-06-17 10:50:25 -070010
11#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
12 #include <mach/mach.h>
sclittled9f5d202016-05-04 18:23:30 -070013 struct SkBaseSemaphore::OSSemaphore {
mtklein61fa22b2015-06-17 10:50:25 -070014 semaphore_t fSemaphore;
15
16 OSSemaphore() {
mtklein2a4685f2015-06-17 11:49:04 -070017 semaphore_create(mach_task_self(), &fSemaphore, SYNC_POLICY_LIFO, 0/*initial count*/);
mtklein61fa22b2015-06-17 10:50:25 -070018 }
19 ~OSSemaphore() { semaphore_destroy(mach_task_self(), fSemaphore); }
20
21 void signal(int n) { while (n --> 0) { semaphore_signal(fSemaphore); } }
22 void wait() { semaphore_wait(fSemaphore); }
23 };
24#elif defined(SK_BUILD_FOR_WIN32)
sclittled9f5d202016-05-04 18:23:30 -070025 struct SkBaseSemaphore::OSSemaphore {
mtklein61fa22b2015-06-17 10:50:25 -070026 HANDLE fSemaphore;
27
28 OSSemaphore() {
halcanary96fcdcc2015-08-27 07:41:13 -070029 fSemaphore = CreateSemaphore(nullptr /*security attributes, optional*/,
mtklein61fa22b2015-06-17 10:50:25 -070030 0 /*initial count*/,
31 MAXLONG /*max count*/,
halcanary96fcdcc2015-08-27 07:41:13 -070032 nullptr /*name, optional*/);
mtklein61fa22b2015-06-17 10:50:25 -070033 }
34 ~OSSemaphore() { CloseHandle(fSemaphore); }
35
36 void signal(int n) {
halcanary96fcdcc2015-08-27 07:41:13 -070037 ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, optional*/);
mtklein61fa22b2015-06-17 10:50:25 -070038 }
39 void wait() { WaitForSingleObject(fSemaphore, INFINITE/*timeout in ms*/); }
40 };
41#else
42 // It's important we test for Mach before this. This code will compile but not work there.
43 #include <errno.h>
44 #include <semaphore.h>
sclittled9f5d202016-05-04 18:23:30 -070045 struct SkBaseSemaphore::OSSemaphore {
mtklein61fa22b2015-06-17 10:50:25 -070046 sem_t fSemaphore;
47
48 OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial count*/); }
49 ~OSSemaphore() { sem_destroy(&fSemaphore); }
50
51 void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } }
52 void wait() {
53 // Try until we're not interrupted.
54 while(sem_wait(&fSemaphore) == -1 && errno == EINTR);
55 }
56 };
57#endif
58
herb7f0a3d72015-09-24 07:34:49 -070059///////////////////////////////////////////////////////////////////////////////
mtklein61fa22b2015-06-17 10:50:25 -070060
mtklein42846ed2016-05-05 10:57:37 -070061void SkBaseSemaphore::osSignal(int n) {
62 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
63 fOSSemaphore->signal(n);
mtklein61fa22b2015-06-17 10:50:25 -070064}
65
mtklein42846ed2016-05-05 10:57:37 -070066void SkBaseSemaphore::osWait() {
67 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
68 fOSSemaphore->wait();
mtklein61fa22b2015-06-17 10:50:25 -070069}
herb7f0a3d72015-09-24 07:34:49 -070070
mtklein42846ed2016-05-05 10:57:37 -070071void SkBaseSemaphore::cleanup() {
72 delete fOSSemaphore;
herb7f0a3d72015-09-24 07:34:49 -070073}