| 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 | |
| 8 | #include "include/private/SkSemaphore.h" |
| 9 | #include "src/core/SkLeanWindows.h" |
| 10 | |
| 11 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 12 | #include <dispatch/dispatch.h> |
| 13 | |
| 14 | struct SkSemaphore::OSSemaphore { |
| 15 | dispatch_semaphore_t fSemaphore; |
| 16 | |
| 17 | OSSemaphore() { fSemaphore = dispatch_semaphore_create(0/*initial count*/); } |
| 18 | ~OSSemaphore() { dispatch_release(fSemaphore); } |
| 19 | |
| 20 | void signal(int n) { while (n --> 0) { dispatch_semaphore_signal(fSemaphore); } } |
| 21 | void wait() { dispatch_semaphore_wait(fSemaphore, DISPATCH_TIME_FOREVER); } |
| 22 | }; |
| 23 | #elif defined(SK_BUILD_FOR_WIN) |
| 24 | struct SkSemaphore::OSSemaphore { |
| 25 | HANDLE fSemaphore; |
| 26 | |
| 27 | OSSemaphore() { |
| 28 | fSemaphore = CreateSemaphore(nullptr /*security attributes, optional*/, |
| 29 | 0 /*initial count*/, |
| 30 | MAXLONG /*max count*/, |
| 31 | nullptr /*name, optional*/); |
| 32 | } |
| 33 | ~OSSemaphore() { CloseHandle(fSemaphore); } |
| 34 | |
| 35 | void signal(int n) { |
| 36 | ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, optional*/); |
| 37 | } |
| 38 | void wait() { WaitForSingleObject(fSemaphore, INFINITE/*timeout in ms*/); } |
| 39 | }; |
| 40 | #else |
| 41 | // It's important we test for Mach before this. This code will compile but not work there. |
| 42 | #include <errno.h> |
| 43 | #include <semaphore.h> |
| 44 | struct SkSemaphore::OSSemaphore { |
| 45 | sem_t fSemaphore; |
| 46 | |
| 47 | OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial count*/); } |
| 48 | ~OSSemaphore() { sem_destroy(&fSemaphore); } |
| 49 | |
| 50 | void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } } |
| 51 | void wait() { |
| 52 | // Try until we're not interrupted. |
| 53 | while(sem_wait(&fSemaphore) == -1 && errno == EINTR); |
| 54 | } |
| 55 | }; |
| 56 | #endif |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | SkSemaphore::~SkSemaphore() { |
| 61 | delete fOSSemaphore; |
| 62 | } |
| 63 | |
| 64 | void SkSemaphore::osSignal(int n) { |
| 65 | fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); |
| 66 | fOSSemaphore->signal(n); |
| 67 | } |
| 68 | |
| 69 | void SkSemaphore::osWait() { |
| 70 | fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; }); |
| 71 | fOSSemaphore->wait(); |
| 72 | } |
| 73 | |
| 74 | bool SkSemaphore::try_wait() { |
| 75 | int count = fCount.load(std::memory_order_relaxed); |
| 76 | if (count > 0) { |
| 77 | return fCount.compare_exchange_weak(count, count-1, std::memory_order_acquire); |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |