| 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/time.h" | 15 #include "src/base/platform/time.h" |
| 16 | 16 |
| 17 namespace v8 { | 17 namespace v8 { |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 #if V8_OS_MACOSX | 20 #if V8_OS_MACOSX |
| 21 | 21 |
| 22 Semaphore::Semaphore(int count) { | 22 Semaphore::Semaphore(int count) { |
| 23 kern_return_t result = semaphore_create( | 23 kern_return_t result = semaphore_create( |
| 24 mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); | 24 mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); |
| 25 ASSERT_EQ(KERN_SUCCESS, result); | 25 DCHECK_EQ(KERN_SUCCESS, result); |
| 26 USE(result); | 26 USE(result); |
| 27 } | 27 } |
| 28 | 28 |
| 29 | 29 |
| 30 Semaphore::~Semaphore() { | 30 Semaphore::~Semaphore() { |
| 31 kern_return_t result = semaphore_destroy(mach_task_self(), native_handle_); | 31 kern_return_t result = semaphore_destroy(mach_task_self(), native_handle_); |
| 32 ASSERT_EQ(KERN_SUCCESS, result); | 32 DCHECK_EQ(KERN_SUCCESS, result); |
| 33 USE(result); | 33 USE(result); |
| 34 } | 34 } |
| 35 | 35 |
| 36 | 36 |
| 37 void Semaphore::Signal() { | 37 void Semaphore::Signal() { |
| 38 kern_return_t result = semaphore_signal(native_handle_); | 38 kern_return_t result = semaphore_signal(native_handle_); |
| 39 ASSERT_EQ(KERN_SUCCESS, result); | 39 DCHECK_EQ(KERN_SUCCESS, result); |
| 40 USE(result); | 40 USE(result); |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 void Semaphore::Wait() { | 44 void Semaphore::Wait() { |
| 45 while (true) { | 45 while (true) { |
| 46 kern_return_t result = semaphore_wait(native_handle_); | 46 kern_return_t result = semaphore_wait(native_handle_); |
| 47 if (result == KERN_SUCCESS) return; // Semaphore was signalled. | 47 if (result == KERN_SUCCESS) return; // Semaphore was signalled. |
| 48 ASSERT_EQ(KERN_ABORTED, result); | 48 DCHECK_EQ(KERN_ABORTED, result); |
| 49 } | 49 } |
| 50 } | 50 } |
| 51 | 51 |
| 52 | 52 |
| 53 bool Semaphore::WaitFor(const TimeDelta& rel_time) { | 53 bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 54 TimeTicks now = TimeTicks::Now(); | 54 TimeTicks now = TimeTicks::Now(); |
| 55 TimeTicks end = now + rel_time; | 55 TimeTicks end = now + rel_time; |
| 56 while (true) { | 56 while (true) { |
| 57 mach_timespec_t ts; | 57 mach_timespec_t ts; |
| 58 if (now >= end) { | 58 if (now >= end) { |
| 59 // Return immediately if semaphore was not signalled. | 59 // Return immediately if semaphore was not signalled. |
| 60 ts.tv_sec = 0; | 60 ts.tv_sec = 0; |
| 61 ts.tv_nsec = 0; | 61 ts.tv_nsec = 0; |
| 62 } else { | 62 } else { |
| 63 ts = (end - now).ToMachTimespec(); | 63 ts = (end - now).ToMachTimespec(); |
| 64 } | 64 } |
| 65 kern_return_t result = semaphore_timedwait(native_handle_, ts); | 65 kern_return_t result = semaphore_timedwait(native_handle_, ts); |
| 66 if (result == KERN_SUCCESS) return true; // Semaphore was signalled. | 66 if (result == KERN_SUCCESS) return true; // Semaphore was signalled. |
| 67 if (result == KERN_OPERATION_TIMED_OUT) return false; // Timeout. | 67 if (result == KERN_OPERATION_TIMED_OUT) return false; // Timeout. |
| 68 ASSERT_EQ(KERN_ABORTED, result); | 68 DCHECK_EQ(KERN_ABORTED, result); |
| 69 now = TimeTicks::Now(); | 69 now = TimeTicks::Now(); |
| 70 } | 70 } |
| 71 } | 71 } |
| 72 | 72 |
| 73 #elif V8_OS_POSIX | 73 #elif V8_OS_POSIX |
| 74 | 74 |
| 75 Semaphore::Semaphore(int count) { | 75 Semaphore::Semaphore(int count) { |
| 76 ASSERT(count >= 0); | 76 DCHECK(count >= 0); |
| 77 int result = sem_init(&native_handle_, 0, count); | 77 int result = sem_init(&native_handle_, 0, count); |
| 78 ASSERT_EQ(0, result); | 78 DCHECK_EQ(0, result); |
| 79 USE(result); | 79 USE(result); |
| 80 } | 80 } |
| 81 | 81 |
| 82 | 82 |
| 83 Semaphore::~Semaphore() { | 83 Semaphore::~Semaphore() { |
| 84 int result = sem_destroy(&native_handle_); | 84 int result = sem_destroy(&native_handle_); |
| 85 ASSERT_EQ(0, result); | 85 DCHECK_EQ(0, result); |
| 86 USE(result); | 86 USE(result); |
| 87 } | 87 } |
| 88 | 88 |
| 89 | 89 |
| 90 void Semaphore::Signal() { | 90 void Semaphore::Signal() { |
| 91 int result = sem_post(&native_handle_); | 91 int result = sem_post(&native_handle_); |
| 92 ASSERT_EQ(0, result); | 92 DCHECK_EQ(0, result); |
| 93 USE(result); | 93 USE(result); |
| 94 } | 94 } |
| 95 | 95 |
| 96 | 96 |
| 97 void Semaphore::Wait() { | 97 void Semaphore::Wait() { |
| 98 while (true) { | 98 while (true) { |
| 99 int result = sem_wait(&native_handle_); | 99 int result = sem_wait(&native_handle_); |
| 100 if (result == 0) return; // Semaphore was signalled. | 100 if (result == 0) return; // Semaphore was signalled. |
| 101 // Signal caused spurious wakeup. | 101 // Signal caused spurious wakeup. |
| 102 ASSERT_EQ(-1, result); | 102 DCHECK_EQ(-1, result); |
| 103 ASSERT_EQ(EINTR, errno); | 103 DCHECK_EQ(EINTR, errno); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 bool Semaphore::WaitFor(const TimeDelta& rel_time) { | 108 bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 109 // Compute the time for end of timeout. | 109 // Compute the time for end of timeout. |
| 110 const Time time = Time::NowFromSystemTime() + rel_time; | 110 const Time time = Time::NowFromSystemTime() + rel_time; |
| 111 const struct timespec ts = time.ToTimespec(); | 111 const struct timespec ts = time.ToTimespec(); |
| 112 | 112 |
| 113 // Wait for semaphore signalled or timeout. | 113 // Wait for semaphore signalled or timeout. |
| 114 while (true) { | 114 while (true) { |
| 115 int result = sem_timedwait(&native_handle_, &ts); | 115 int result = sem_timedwait(&native_handle_, &ts); |
| 116 if (result == 0) return true; // Semaphore was signalled. | 116 if (result == 0) return true; // Semaphore was signalled. |
| 117 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 117 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
| 118 if (result > 0) { | 118 if (result > 0) { |
| 119 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. | 119 // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. |
| 120 errno = result; | 120 errno = result; |
| 121 result = -1; | 121 result = -1; |
| 122 } | 122 } |
| 123 #endif | 123 #endif |
| 124 if (result == -1 && errno == ETIMEDOUT) { | 124 if (result == -1 && errno == ETIMEDOUT) { |
| 125 // Timed out while waiting for semaphore. | 125 // Timed out while waiting for semaphore. |
| 126 return false; | 126 return false; |
| 127 } | 127 } |
| 128 // Signal caused spurious wakeup. | 128 // Signal caused spurious wakeup. |
| 129 ASSERT_EQ(-1, result); | 129 DCHECK_EQ(-1, result); |
| 130 ASSERT_EQ(EINTR, errno); | 130 DCHECK_EQ(EINTR, errno); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 #elif V8_OS_WIN | 134 #elif V8_OS_WIN |
| 135 | 135 |
| 136 Semaphore::Semaphore(int count) { | 136 Semaphore::Semaphore(int count) { |
| 137 ASSERT(count >= 0); | 137 DCHECK(count >= 0); |
| 138 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); | 138 native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); |
| 139 ASSERT(native_handle_ != NULL); | 139 DCHECK(native_handle_ != NULL); |
| 140 } | 140 } |
| 141 | 141 |
| 142 | 142 |
| 143 Semaphore::~Semaphore() { | 143 Semaphore::~Semaphore() { |
| 144 BOOL result = CloseHandle(native_handle_); | 144 BOOL result = CloseHandle(native_handle_); |
| 145 ASSERT(result); | 145 DCHECK(result); |
| 146 USE(result); | 146 USE(result); |
| 147 } | 147 } |
| 148 | 148 |
| 149 | 149 |
| 150 void Semaphore::Signal() { | 150 void Semaphore::Signal() { |
| 151 LONG dummy; | 151 LONG dummy; |
| 152 BOOL result = ReleaseSemaphore(native_handle_, 1, &dummy); | 152 BOOL result = ReleaseSemaphore(native_handle_, 1, &dummy); |
| 153 ASSERT(result); | 153 DCHECK(result); |
| 154 USE(result); | 154 USE(result); |
| 155 } | 155 } |
| 156 | 156 |
| 157 | 157 |
| 158 void Semaphore::Wait() { | 158 void Semaphore::Wait() { |
| 159 DWORD result = WaitForSingleObject(native_handle_, INFINITE); | 159 DWORD result = WaitForSingleObject(native_handle_, INFINITE); |
| 160 ASSERT(result == WAIT_OBJECT_0); | 160 DCHECK(result == WAIT_OBJECT_0); |
| 161 USE(result); | 161 USE(result); |
| 162 } | 162 } |
| 163 | 163 |
| 164 | 164 |
| 165 bool Semaphore::WaitFor(const TimeDelta& rel_time) { | 165 bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 166 TimeTicks now = TimeTicks::Now(); | 166 TimeTicks now = TimeTicks::Now(); |
| 167 TimeTicks end = now + rel_time; | 167 TimeTicks end = now + rel_time; |
| 168 while (true) { | 168 while (true) { |
| 169 int64_t msec = (end - now).InMilliseconds(); | 169 int64_t msec = (end - now).InMilliseconds(); |
| 170 if (msec >= static_cast<int64_t>(INFINITE)) { | 170 if (msec >= static_cast<int64_t>(INFINITE)) { |
| 171 DWORD result = WaitForSingleObject(native_handle_, INFINITE - 1); | 171 DWORD result = WaitForSingleObject(native_handle_, INFINITE - 1); |
| 172 if (result == WAIT_OBJECT_0) { | 172 if (result == WAIT_OBJECT_0) { |
| 173 return true; | 173 return true; |
| 174 } | 174 } |
| 175 ASSERT(result == WAIT_TIMEOUT); | 175 DCHECK(result == WAIT_TIMEOUT); |
| 176 now = TimeTicks::Now(); | 176 now = TimeTicks::Now(); |
| 177 } else { | 177 } else { |
| 178 DWORD result = WaitForSingleObject( | 178 DWORD result = WaitForSingleObject( |
| 179 native_handle_, (msec < 0) ? 0 : static_cast<DWORD>(msec)); | 179 native_handle_, (msec < 0) ? 0 : static_cast<DWORD>(msec)); |
| 180 if (result == WAIT_TIMEOUT) { | 180 if (result == WAIT_TIMEOUT) { |
| 181 return false; | 181 return false; |
| 182 } | 182 } |
| 183 ASSERT(result == WAIT_OBJECT_0); | 183 DCHECK(result == WAIT_OBJECT_0); |
| 184 return true; | 184 return true; |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 | 188 |
| 189 #endif // V8_OS_MACOSX | 189 #endif // V8_OS_MACOSX |
| 190 | 190 |
| 191 } } // namespace v8::base | 191 } } // namespace v8::base |
| OLD | NEW |