forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore.cc
165 lines (120 loc) · 2.97 KB
/
semaphore.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/synchronization/semaphore.h"
#include "lib/fxl/build_config.h"
#include "lib/fxl/logging.h"
#if OS_MACOSX
#include <dispatch/dispatch.h>
namespace flutter {
class PlatformSemaphore {
public:
explicit PlatformSemaphore(uint32_t count)
: _sem(dispatch_semaphore_create(count)), _initial(count) {}
~PlatformSemaphore() {
for (uint32_t i = 0; i < _initial; ++i) {
Signal();
}
if (_sem != nullptr) {
dispatch_release(reinterpret_cast<dispatch_object_t>(_sem));
_sem = nullptr;
}
}
bool IsValid() const { return _sem != nullptr; }
bool TryWait() {
if (_sem == nullptr) {
return false;
}
return dispatch_semaphore_wait(_sem, DISPATCH_TIME_NOW) == 0;
}
void Signal() {
if (_sem != nullptr) {
dispatch_semaphore_signal(_sem);
}
}
private:
dispatch_semaphore_t _sem;
const uint32_t _initial;
FXL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
};
} // namespace flutter
#elif OS_WIN
#include <windows.h>
namespace flutter {
class PlatformSemaphore {
public:
explicit PlatformSemaphore(uint32_t count)
: _sem(CreateSemaphore(NULL, count, LONG_MAX, NULL)) {}
~PlatformSemaphore() {
if (_sem != nullptr) {
CloseHandle(_sem);
_sem = nullptr;
}
}
bool IsValid() const { return _sem != nullptr; }
bool TryWait() {
if (_sem == nullptr) {
return false;
}
return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0;
}
void Signal() {
if (_sem != nullptr) {
ReleaseSemaphore(_sem, 1, NULL);
}
}
private:
HANDLE _sem;
FXL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
};
} // namespace flutter
#else
#include <semaphore.h>
#include "lib/fxl/files/eintr_wrapper.h"
namespace flutter {
class PlatformSemaphore {
public:
explicit PlatformSemaphore(uint32_t count)
: valid_(::sem_init(&sem_, 0 /* not shared */, count) == 0) {}
~PlatformSemaphore() {
if (valid_) {
int result = ::sem_destroy(&sem_);
// Can only be EINVAL which should not be possible since we checked for
// validity.
FXL_DCHECK(result == 0);
}
}
bool IsValid() const { return valid_; }
bool TryWait() {
if (!valid_) {
return false;
}
return HANDLE_EINTR(::sem_trywait(&sem_)) == 0;
}
void Signal() {
if (!valid_) {
return;
}
::sem_post(&sem_);
return;
}
private:
bool valid_;
sem_t sem_;
FXL_DISALLOW_COPY_AND_ASSIGN(PlatformSemaphore);
};
} // namespace flutter
#endif
namespace flutter {
Semaphore::Semaphore(uint32_t count) : _impl(new PlatformSemaphore(count)) {}
Semaphore::~Semaphore() = default;
bool Semaphore::IsValid() const {
return _impl->IsValid();
}
bool Semaphore::TryWait() {
return _impl->TryWait();
}
void Semaphore::Signal() {
return _impl->Signal();
}
} // namespace flutter