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
OSSemaphoreSkSemaphore::OSSemaphore17 OSSemaphore() { fSemaphore = dispatch_semaphore_create(0/*initial count*/); }
~OSSemaphoreSkSemaphore::OSSemaphore18 ~OSSemaphore() { dispatch_release(fSemaphore); }
19
signalSkSemaphore::OSSemaphore20 void signal(int n) { while (n --> 0) { dispatch_semaphore_signal(fSemaphore); } }
waitSkSemaphore::OSSemaphore21 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
OSSemaphoreSkSemaphore::OSSemaphore27 OSSemaphore() {
28 fSemaphore = CreateSemaphore(nullptr /*security attributes, optional*/,
29 0 /*initial count*/,
30 MAXLONG /*max count*/,
31 nullptr /*name, optional*/);
32 }
~OSSemaphoreSkSemaphore::OSSemaphore33 ~OSSemaphore() { CloseHandle(fSemaphore); }
34
signalSkSemaphore::OSSemaphore35 void signal(int n) {
36 ReleaseSemaphore(fSemaphore, n, nullptr/*returns previous count, optional*/);
37 }
waitSkSemaphore::OSSemaphore38 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
OSSemaphoreSkSemaphore::OSSemaphore47 OSSemaphore() { sem_init(&fSemaphore, 0/*cross process?*/, 0/*initial count*/); }
~OSSemaphoreSkSemaphore::OSSemaphore48 ~OSSemaphore() { sem_destroy(&fSemaphore); }
49
signalSkSemaphore::OSSemaphore50 void signal(int n) { while (n --> 0) { sem_post(&fSemaphore); } }
waitSkSemaphore::OSSemaphore51 void wait() {
52 // Try until we're not interrupted.
53 while(sem_wait(&fSemaphore) == -1 && errno == EINTR);
54 }
55 };
56 #endif
57
58 ///////////////////////////////////////////////////////////////////////////////
59
~SkSemaphore()60 SkSemaphore::~SkSemaphore() {
61 delete fOSSemaphore;
62 }
63
osSignal(int n)64 void SkSemaphore::osSignal(int n) {
65 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
66 fOSSemaphore->signal(n);
67 }
68
osWait()69 void SkSemaphore::osWait() {
70 fOSSemaphoreOnce([this] { fOSSemaphore = new OSSemaphore; });
71 fOSSemaphore->wait();
72 }
73
try_wait()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