OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/base/platform/semaphore.h" | 5 #include "src/base/platform/semaphore.h" |
6 | 6 |
7 #if V8_OS_MACOSX | 7 #if V8_OS_MACOSX |
8 #include <mach/mach_init.h> | 8 #include <mach/mach_init.h> |
9 #include <mach/task.h> | 9 #include <mach/task.h> |
10 #endif | 10 #endif |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 int result = sem_wait(&native_handle_); | 113 int result = sem_wait(&native_handle_); |
114 if (result == 0) return; // Semaphore was signalled. | 114 if (result == 0) return; // Semaphore was signalled. |
115 // Signal caused spurious wakeup. | 115 // Signal caused spurious wakeup. |
116 DCHECK_EQ(-1, result); | 116 DCHECK_EQ(-1, result); |
117 DCHECK_EQ(EINTR, errno); | 117 DCHECK_EQ(EINTR, errno); |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 bool Semaphore::WaitFor(const TimeDelta& rel_time) { | 122 bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
123 #if V8_OS_NACL | |
124 // PNaCL doesn't support sem_timedwait, do ugly busy waiting. | |
125 ElapsedTimer timer; | |
126 timer.Start(); | |
127 do { | |
128 int result = sem_trywait(&native_handle_); | |
129 if (result == 0) return true; | |
130 DCHECK(errno == EAGAIN || errno == EINTR); | |
131 } while (!timer.HasExpired(rel_time)); | |
132 return false; | |
133 #else | |
134 // Compute the time for end of timeout. | 123 // Compute the time for end of timeout. |
135 const Time time = Time::NowFromSystemTime() + rel_time; | 124 const Time time = Time::NowFromSystemTime() + rel_time; |
136 const struct timespec ts = time.ToTimespec(); | 125 const struct timespec ts = time.ToTimespec(); |
137 | 126 |
138 // Wait for semaphore signalled or timeout. | 127 // Wait for semaphore signalled or timeout. |
139 while (true) { | 128 while (true) { |
140 int result = sem_timedwait(&native_handle_, &ts); | 129 int result = sem_timedwait(&native_handle_, &ts); |
141 if (result == 0) return true; // Semaphore was signalled. | 130 if (result == 0) return true; // Semaphore was signalled. |
142 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 131 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
143 if (result > 0) { | 132 if (result > 0) { |
144 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. | 133 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. |
145 errno = result; | 134 errno = result; |
146 result = -1; | 135 result = -1; |
147 } | 136 } |
148 #endif | 137 #endif |
149 if (result == -1 && errno == ETIMEDOUT) { | 138 if (result == -1 && errno == ETIMEDOUT) { |
150 // Timed out while waiting for semaphore. | 139 // Timed out while waiting for semaphore. |
151 return false; | 140 return false; |
152 } | 141 } |
153 // Signal caused spurious wakeup. | 142 // Signal caused spurious wakeup. |
154 DCHECK_EQ(-1, result); | 143 DCHECK_EQ(-1, result); |
155 DCHECK_EQ(EINTR, errno); | 144 DCHECK_EQ(EINTR, errno); |
156 } | 145 } |
157 #endif | |
158 } | 146 } |
159 | 147 |
160 #elif V8_OS_WIN | 148 #elif V8_OS_WIN |
161 | 149 |
162 Semaphore::Semaphore(int count) { | 150 Semaphore::Semaphore(int count) { |
163 DCHECK(count >= 0); | 151 DCHECK(count >= 0); |
164 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); | 152 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); |
165 DCHECK(native_handle_ != NULL); | 153 DCHECK(native_handle_ != NULL); |
166 } | 154 } |
167 | 155 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 DCHECK(result == WAIT_OBJECT_0); | 196 DCHECK(result == WAIT_OBJECT_0); |
209 return true; | 197 return true; |
210 } | 198 } |
211 } | 199 } |
212 } | 200 } |
213 | 201 |
214 #endif // V8_OS_MACOSX | 202 #endif // V8_OS_MACOSX |
215 | 203 |
216 } // namespace base | 204 } // namespace base |
217 } // namespace v8 | 205 } // namespace v8 |
OLD | NEW |