| 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 |
| 11 | 11 |
| 12 #include <errno.h> | 12 #include <errno.h> |
| 13 | 13 |
| 14 #include "src/base/logging.h" | 14 #include "src/base/logging.h" |
| 15 #include "src/base/platform/elapsed-timer.h" |
| 15 #include "src/base/platform/time.h" | 16 #include "src/base/platform/time.h" |
| 16 | 17 |
| 17 namespace v8 { | 18 namespace v8 { |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 #if V8_OS_MACOSX | 21 #if V8_OS_MACOSX |
| 21 | 22 |
| 22 Semaphore::Semaphore(int count) { | 23 Semaphore::Semaphore(int count) { |
| 23 kern_return_t result = semaphore_create( | 24 kern_return_t result = semaphore_create( |
| 24 mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); | 25 mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 int result = sem_wait(&native_handle_); | 100 int result = sem_wait(&native_handle_); |
| 100 if (result == 0) return; // Semaphore was signalled. | 101 if (result == 0) return; // Semaphore was signalled. |
| 101 // Signal caused spurious wakeup. | 102 // Signal caused spurious wakeup. |
| 102 DCHECK_EQ(-1, result); | 103 DCHECK_EQ(-1, result); |
| 103 DCHECK_EQ(EINTR, errno); | 104 DCHECK_EQ(EINTR, errno); |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 | 108 |
| 108 bool Semaphore::WaitFor(const TimeDelta& rel_time) { | 109 bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 110 #if V8_OS_NACL |
| 111 // PNaCL doesn't support sem_timedwait, do ugly busy waiting. |
| 112 ElapsedTimer timer; |
| 113 timer.Start(); |
| 114 do { |
| 115 int result = sem_trywait(&native_handle_); |
| 116 if (result == 0) return true; |
| 117 DCHECK(errno == EAGAIN || error == EINTR); |
| 118 } while (!timer.HasExpired(rel_time)); |
| 119 return false; |
| 120 #else |
| 109 // Compute the time for end of timeout. | 121 // Compute the time for end of timeout. |
| 110 const Time time = Time::NowFromSystemTime() + rel_time; | 122 const Time time = Time::NowFromSystemTime() + rel_time; |
| 111 const struct timespec ts = time.ToTimespec(); | 123 const struct timespec ts = time.ToTimespec(); |
| 112 | 124 |
| 113 // Wait for semaphore signalled or timeout. | 125 // Wait for semaphore signalled or timeout. |
| 114 while (true) { | 126 while (true) { |
| 115 int result = sem_timedwait(&native_handle_, &ts); | 127 int result = sem_timedwait(&native_handle_, &ts); |
| 116 if (result == 0) return true; // Semaphore was signalled. | 128 if (result == 0) return true; // Semaphore was signalled. |
| 117 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 129 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
| 118 if (result > 0) { | 130 if (result > 0) { |
| 119 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. | 131 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. |
| 120 errno = result; | 132 errno = result; |
| 121 result = -1; | 133 result = -1; |
| 122 } | 134 } |
| 123 #endif | 135 #endif |
| 124 if (result == -1 && errno == ETIMEDOUT) { | 136 if (result == -1 && errno == ETIMEDOUT) { |
| 125 // Timed out while waiting for semaphore. | 137 // Timed out while waiting for semaphore. |
| 126 return false; | 138 return false; |
| 127 } | 139 } |
| 128 // Signal caused spurious wakeup. | 140 // Signal caused spurious wakeup. |
| 129 DCHECK_EQ(-1, result); | 141 DCHECK_EQ(-1, result); |
| 130 DCHECK_EQ(EINTR, errno); | 142 DCHECK_EQ(EINTR, errno); |
| 131 } | 143 } |
| 144 #endif |
| 132 } | 145 } |
| 133 | 146 |
| 134 #elif V8_OS_WIN | 147 #elif V8_OS_WIN |
| 135 | 148 |
| 136 Semaphore::Semaphore(int count) { | 149 Semaphore::Semaphore(int count) { |
| 137 DCHECK(count >= 0); | 150 DCHECK(count >= 0); |
| 138 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); | 151 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); |
| 139 DCHECK(native_handle_ != NULL); | 152 DCHECK(native_handle_ != NULL); |
| 140 } | 153 } |
| 141 | 154 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 } | 195 } |
| 183 DCHECK(result == WAIT_OBJECT_0); | 196 DCHECK(result == WAIT_OBJECT_0); |
| 184 return true; | 197 return true; |
| 185 } | 198 } |
| 186 } | 199 } |
| 187 } | 200 } |
| 188 | 201 |
| 189 #endif // V8_OS_MACOSX | 202 #endif // V8_OS_MACOSX |
| 190 | 203 |
| 191 } } // namespace v8::base | 204 } } // namespace v8::base |
| OLD | NEW |