mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 8 | #include "../private/SkLeanWindows.h" |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 9 | #include "../private/SkSemaphore.h" |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 10 | |
| 11 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 12 | #include <mach/mach.h> |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 13 | struct SkBaseSemaphore::OSSemaphore { |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 14 | semaphore_t fSemaphore; |
| 15 | |
| 16 | OSSemaphore() { |
mtklein | 2a4685f | 2015-06-17 11:49:04 -0700 | [diff] [blame] | 17 | semaphore_create(mach_task_self(), &fSemaphore, SYNC_POLICY_LIFO, 0/*initial count*/); |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 18 | } |
| 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) |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 25 | struct SkBaseSemaphore::OSSemaphore { |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 26 | HANDLE fSemaphore; |
| 27 | |
| 28 | OSSemaphore() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | fSemaphore = CreateSemaphore(nullptr /*security attributes, optional*/, |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 30 | 0 /*initial count*/, |
| 31 | MAXLONG /*max count*/, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | nullptr /*name, optional*/); |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 33 | } |
| 34 | ~OSSemaphore() { CloseHandle(fSemaphore); } |
| 35 | |
| 36 | void signal(int n) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, optional*/); |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 38 | } |
| 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> |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 45 | struct SkBaseSemaphore::OSSemaphore { |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 46 | 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 | |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 59 | /////////////////////////////////////////////////////////////////////////////// |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 60 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 61 | void SkBaseSemaphore::osSignal(int n) { |
| 62 | fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); |
| 63 | fOSSemaphore->signal(n); |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 64 | } |
| 65 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 66 | void SkBaseSemaphore::osWait() { |
| 67 | fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); |
| 68 | fOSSemaphore->wait(); |
mtklein | 61fa22b | 2015-06-17 10:50:25 -0700 | [diff] [blame] | 69 | } |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 70 | |
mtklein | 42846ed | 2016-05-05 10:57:37 -0700 | [diff] [blame] | 71 | void SkBaseSemaphore::cleanup() { |
| 72 | delete fOSSemaphore; |
herb | 7f0a3d7 | 2015-09-24 07:34:49 -0700 | [diff] [blame] | 73 | } |