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