Flutter Engine
The Flutter Engine
semaphore.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/synchronization/semaphore.h"
6
7#include "flutter/fml/build_config.h"
8#include "flutter/fml/logging.h"
9
10#if FML_OS_MACOSX
11#include <dispatch/dispatch.h>
12
13namespace fml {
14
15class PlatformSemaphore {
16 public:
17 explicit PlatformSemaphore(uint32_t count)
18 : sem_(dispatch_semaphore_create(count)), initial_(count) {}
19
21 for (uint32_t i = 0; i < initial_; ++i) {
22 Signal();
23 }
24 if (sem_ != nullptr) {
25 dispatch_release(reinterpret_cast<dispatch_object_t>(sem_));
26 sem_ = nullptr;
27 }
28 }
29
30 bool IsValid() const { return sem_ != nullptr; }
31
32 bool Wait() {
33 if (sem_ == nullptr) {
34 return false;
35 }
36 return dispatch_semaphore_wait(sem_, DISPATCH_TIME_FOREVER) == 0;
37 }
38
39 bool TryWait() {
40 if (sem_ == nullptr) {
41 return false;
42 }
43
44 return dispatch_semaphore_wait(sem_, DISPATCH_TIME_NOW) == 0;
45 }
46
47 void Signal() {
48 if (sem_ != nullptr) {
49 dispatch_semaphore_signal(sem_);
50 }
51 }
52
53 private:
54 dispatch_semaphore_t sem_;
55 const uint32_t initial_;
56
57 FML_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
58};
59
60} // namespace fml
61
62#elif FML_OS_WIN
63#include <windows.h>
64
65namespace fml {
66
67class PlatformSemaphore {
68 public:
69 explicit PlatformSemaphore(uint32_t count)
70 : _sem(CreateSemaphore(NULL, count, LONG_MAX, NULL)) {}
71
73 if (_sem != nullptr) {
74 CloseHandle(_sem);
75 _sem = nullptr;
76 }
77 }
78
79 bool IsValid() const { return _sem != nullptr; }
80
81 bool Wait() {
82 if (_sem == nullptr) {
83 return false;
84 }
85
86 return WaitForSingleObject(_sem, INFINITE) == WAIT_OBJECT_0;
87 }
88
89 bool TryWait() {
90 if (_sem == nullptr) {
91 return false;
92 }
93
94 return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0;
95 }
96
97 void Signal() {
98 if (_sem != nullptr) {
99 ReleaseSemaphore(_sem, 1, NULL);
100 }
101 }
102
103 private:
104 HANDLE _sem;
105
106 FML_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
107};
108
109} // namespace fml
110
111#else
112#include <semaphore.h>
113#include "flutter/fml/eintr_wrapper.h"
114
115namespace fml {
116
118 public:
119 explicit PlatformSemaphore(uint32_t count)
120 : valid_(::sem_init(&sem_, 0 /* not shared */, count) == 0) {}
121
123 if (valid_) {
124 int result = ::sem_destroy(&sem_);
125 (void)result;
126 // Can only be EINVAL which should not be possible since we checked for
127 // validity.
128 FML_DCHECK(result == 0);
129 }
130 }
131
132 bool IsValid() const { return valid_; }
133
134 bool Wait() {
135 if (!valid_) {
136 return false;
137 }
138
139 return FML_HANDLE_EINTR(::sem_wait(&sem_)) == 0;
140 }
141
142 bool TryWait() {
143 if (!valid_) {
144 return false;
145 }
146
147 return FML_HANDLE_EINTR(::sem_trywait(&sem_)) == 0;
148 }
149
150 void Signal() {
151 if (!valid_) {
152 return;
153 }
154
155 ::sem_post(&sem_);
156
157 return;
158 }
159
160 private:
161 bool valid_;
162 sem_t sem_;
163
164 FML_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
165};
166
167} // namespace fml
168
169#endif
170
171namespace fml {
172
174
175Semaphore::~Semaphore() = default;
176
177bool Semaphore::IsValid() const {
178 return impl_->IsValid();
179}
180
182 return impl_->Wait();
183}
184
186 return impl_->TryWait();
187}
188
190 return impl_->Signal();
191}
192
193} // namespace fml
int count
Definition: FontMgrTest.cpp:50
PlatformSemaphore(uint32_t count)
Definition: semaphore.cc:119
bool IsValid() const
Definition: semaphore.cc:132
bool IsValid() const
Check if the underlying semaphore handle could be created. Failure modes are platform specific and ma...
Definition: semaphore.cc:177
~Semaphore()
Destroy the counting semaphore.
void Signal()
Increment the count by one. Any pending Waits will be resolved at this point.
Definition: semaphore.cc:189
bool TryWait()
Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.
Definition: semaphore.cc:185
Semaphore(uint32_t count)
Initializes the counting semaphore to a specified start count.
Definition: semaphore.cc:173
bool Wait()
Decrements the count and waits indefinitely if the value is less than zero for a Signal.
Definition: semaphore.cc:181
GAsyncResult * result
#define FML_HANDLE_EINTR(x)
Definition: eintr_wrapper.h:20
#define FML_DCHECK(condition)
Definition: logging.h:103
Definition: ascii_trie.cc:9
void * HANDLE
Definition: windows_types.h:36