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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright 2013 the V8 project 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 "src/base/platform/semaphore.h"
#if V8_OS_MACOSX
#include <mach/mach_init.h>
#include <mach/task.h>
#endif
#include <errno.h>
#include "src/base/logging.h"
#include "src/base/platform/elapsed-timer.h"
#include "src/base/platform/time.h"
namespace v8 {
namespace base {
#if V8_OS_MACOSX
Semaphore::Semaphore(int count) {
kern_return_t result = semaphore_create(
mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count);
DCHECK_EQ(KERN_SUCCESS, result);
USE(result);
}
Semaphore::~Semaphore() {
kern_return_t result = semaphore_destroy(mach_task_self(), native_handle_);
DCHECK_EQ(KERN_SUCCESS, result);
USE(result);
}
void Semaphore::Signal() {
kern_return_t result = semaphore_signal(native_handle_);
DCHECK_EQ(KERN_SUCCESS, result);
USE(result);
}
void Semaphore::Wait() {
while (true) {
kern_return_t result = semaphore_wait(native_handle_);
if (result == KERN_SUCCESS) return; // Semaphore was signalled.
DCHECK_EQ(KERN_ABORTED, result);
}
}
bool Semaphore::WaitFor(const TimeDelta& rel_time) {
TimeTicks now = TimeTicks::Now();
TimeTicks end = now + rel_time;
while (true) {
mach_timespec_t ts;
if (now >= end) {
// Return immediately if semaphore was not signalled.
ts.tv_sec = 0;
ts.tv_nsec = 0;
} else {
ts = (end - now).ToMachTimespec();
}
kern_return_t result = semaphore_timedwait(native_handle_, ts);
if (result == KERN_SUCCESS) return true; // Semaphore was signalled.
if (result == KERN_OPERATION_TIMED_OUT) return false; // Timeout.
DCHECK_EQ(KERN_ABORTED, result);
now = TimeTicks::Now();
}
}
#elif V8_OS_POSIX
Semaphore::Semaphore(int count) {
DCHECK(count >= 0);
#if V8_LIBC_GLIBC
// sem_init in glibc prior to 2.1 does not zero out semaphores.
memset(&native_handle_, 0, sizeof(native_handle_));
#endif
int result = sem_init(&native_handle_, 0, count);
DCHECK_EQ(0, result);
USE(result);
}
Semaphore::~Semaphore() {
int result = sem_destroy(&native_handle_);
DCHECK_EQ(0, result);
USE(result);
}
void Semaphore::Signal() {
int result = sem_post(&native_handle_);
DCHECK_EQ(0, result);
USE(result);
}
void Semaphore::Wait() {
while (true) {
int result = sem_wait(&native_handle_);
if (result == 0) return; // Semaphore was signalled.
// Signal caused spurious wakeup.
DCHECK_EQ(-1, result);
DCHECK_EQ(EINTR, errno);
}
}
bool Semaphore::WaitFor(const TimeDelta& rel_time) {
#if V8_OS_NACL
// PNaCL doesn't support sem_timedwait, do ugly busy waiting.
ElapsedTimer timer;
timer.Start();
do {
int result = sem_trywait(&native_handle_);
if (result == 0) return true;
DCHECK(errno == EAGAIN || errno == EINTR);
} while (!timer.HasExpired(rel_time));
return false;
#else
// Compute the time for end of timeout.
const Time time = Time::NowFromSystemTime() + rel_time;
const struct timespec ts = time.ToTimespec();
// Wait for semaphore signalled or timeout.
while (true) {
int result = sem_timedwait(&native_handle_, &ts);
if (result == 0) return true; // Semaphore was signalled.
#if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4)
if (result > 0) {
// sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1.
errno = result;
result = -1;
}
#endif
if (result == -1 && errno == ETIMEDOUT) {
// Timed out while waiting for semaphore.
return false;
}
// Signal caused spurious wakeup.
DCHECK_EQ(-1, result);
DCHECK_EQ(EINTR, errno);
}
#endif
}
#elif V8_OS_WIN
Semaphore::Semaphore(int count) {
DCHECK(count >= 0);
native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL);
DCHECK(native_handle_ != NULL);
}
Semaphore::~Semaphore() {
BOOL result = CloseHandle(native_handle_);
DCHECK(result);
USE(result);
}
void Semaphore::Signal() {
LONG dummy;
BOOL result = ReleaseSemaphore(native_handle_, 1, &dummy);
DCHECK(result);
USE(result);
}
void Semaphore::Wait() {
DWORD result = WaitForSingleObject(native_handle_, INFINITE);
DCHECK(result == WAIT_OBJECT_0);
USE(result);
}
bool Semaphore::WaitFor(const TimeDelta& rel_time) {
TimeTicks now = TimeTicks::Now();
TimeTicks end = now + rel_time;
while (true) {
int64_t msec = (end - now).InMilliseconds();
if (msec >= static_cast<int64_t>(INFINITE)) {
DWORD result = WaitForSingleObject(native_handle_, INFINITE - 1);
if (result == WAIT_OBJECT_0) {
return true;
}
DCHECK(result == WAIT_TIMEOUT);
now = TimeTicks::Now();
} else {
DWORD result = WaitForSingleObject(
native_handle_, (msec < 0) ? 0 : static_cast<DWORD>(msec));
if (result == WAIT_TIMEOUT) {
return false;
}
DCHECK(result == WAIT_OBJECT_0);
return true;
}
}
}
#endif // V8_OS_MACOSX
} // namespace base
} // namespace v8
|