Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/base/platform/semaphore.h" |
| 6 | |
| 7 | #if V8_OS_MACOSX |
| 8 | #include <mach/mach_init.h> |
| 9 | #include <mach/task.h> |
| 10 | #endif |
| 11 | |
| 12 | #include <errno.h> |
| 13 | |
| 14 | #include "src/base/logging.h" |
| 15 | #include "src/base/platform/elapsed-timer.h" |
| 16 | #include "src/base/platform/time.h" |
| 17 | |
| 18 | namespace v8 { |
| 19 | namespace base { |
| 20 | |
| 21 | #if V8_OS_MACOSX |
| 22 | |
| 23 | Semaphore::Semaphore(int count) { |
| 24 | kern_return_t result = semaphore_create( |
| 25 | mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); |
| 26 | DCHECK_EQ(KERN_SUCCESS, result); |
| 27 | USE(result); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | Semaphore::~Semaphore() { |
| 32 | kern_return_t result = semaphore_destroy(mach_task_self(), native_handle_); |
| 33 | DCHECK_EQ(KERN_SUCCESS, result); |
| 34 | USE(result); |
| 35 | } |
| 36 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | void Semaphore::Signal() { |
| 38 | kern_return_t result = semaphore_signal(native_handle_); |
| 39 | DCHECK_EQ(KERN_SUCCESS, result); |
| 40 | USE(result); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void Semaphore::Wait() { |
| 45 | while (true) { |
| 46 | kern_return_t result = semaphore_wait(native_handle_); |
| 47 | if (result == KERN_SUCCESS) return; // Semaphore was signalled. |
| 48 | DCHECK_EQ(KERN_ABORTED, result); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 54 | TimeTicks now = TimeTicks::Now(); |
| 55 | TimeTicks end = now + rel_time; |
| 56 | while (true) { |
| 57 | mach_timespec_t ts; |
| 58 | if (now >= end) { |
| 59 | // Return immediately if semaphore was not signalled. |
| 60 | ts.tv_sec = 0; |
| 61 | ts.tv_nsec = 0; |
| 62 | } else { |
| 63 | ts = (end - now).ToMachTimespec(); |
| 64 | } |
| 65 | kern_return_t result = semaphore_timedwait(native_handle_, ts); |
| 66 | if (result == KERN_SUCCESS) return true; // Semaphore was signalled. |
| 67 | if (result == KERN_OPERATION_TIMED_OUT) return false; // Timeout. |
| 68 | DCHECK_EQ(KERN_ABORTED, result); |
| 69 | now = TimeTicks::Now(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #elif V8_OS_POSIX |
| 74 | |
| 75 | Semaphore::Semaphore(int count) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 76 | // The sem_init() does not check for alignment of the native handle. |
| 77 | // Unaligned native handle can later cause a failure in semaphore signal. |
| 78 | // Check the alignment here to catch the failure earlier. |
| 79 | // Context: crbug.com/605349. |
| 80 | #if V8_OS_AIX |
| 81 | // On aix sem_t is of type int |
| 82 | const uintptr_t kSemaphoreAlignmentMask = sizeof(int) - 1; |
| 83 | #else |
| 84 | const uintptr_t kSemaphoreAlignmentMask = sizeof(void*) - 1; |
| 85 | #endif |
| 86 | CHECK_EQ( |
| 87 | 0, reinterpret_cast<uintptr_t>(&native_handle_) & |
| 88 | kSemaphoreAlignmentMask); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 89 | DCHECK(count >= 0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | #if V8_LIBC_GLIBC |
| 91 | // sem_init in glibc prior to 2.1 does not zero out semaphores. |
| 92 | memset(&native_handle_, 0, sizeof(native_handle_)); |
| 93 | #endif |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 94 | int result = sem_init(&native_handle_, 0, count); |
| 95 | DCHECK_EQ(0, result); |
| 96 | USE(result); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | Semaphore::~Semaphore() { |
| 101 | int result = sem_destroy(&native_handle_); |
| 102 | DCHECK_EQ(0, result); |
| 103 | USE(result); |
| 104 | } |
| 105 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | void Semaphore::Signal() { |
| 107 | int result = sem_post(&native_handle_); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 108 | CHECK_EQ(0, result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | void Semaphore::Wait() { |
| 113 | while (true) { |
| 114 | int result = sem_wait(&native_handle_); |
| 115 | if (result == 0) return; // Semaphore was signalled. |
| 116 | // Signal caused spurious wakeup. |
| 117 | DCHECK_EQ(-1, result); |
| 118 | DCHECK_EQ(EINTR, errno); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 124 | #if V8_OS_NACL |
| 125 | // PNaCL doesn't support sem_timedwait, do ugly busy waiting. |
| 126 | ElapsedTimer timer; |
| 127 | timer.Start(); |
| 128 | do { |
| 129 | int result = sem_trywait(&native_handle_); |
| 130 | if (result == 0) return true; |
| 131 | DCHECK(errno == EAGAIN || errno == EINTR); |
| 132 | } while (!timer.HasExpired(rel_time)); |
| 133 | return false; |
| 134 | #else |
| 135 | // Compute the time for end of timeout. |
| 136 | const Time time = Time::NowFromSystemTime() + rel_time; |
| 137 | const struct timespec ts = time.ToTimespec(); |
| 138 | |
| 139 | // Wait for semaphore signalled or timeout. |
| 140 | while (true) { |
| 141 | int result = sem_timedwait(&native_handle_, &ts); |
| 142 | if (result == 0) return true; // Semaphore was signalled. |
| 143 | #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
| 144 | if (result > 0) { |
| 145 | // sem_timedwait in glibc prior to 2.3.4 returns the errno instead of -1. |
| 146 | errno = result; |
| 147 | result = -1; |
| 148 | } |
| 149 | #endif |
| 150 | if (result == -1 && errno == ETIMEDOUT) { |
| 151 | // Timed out while waiting for semaphore. |
| 152 | return false; |
| 153 | } |
| 154 | // Signal caused spurious wakeup. |
| 155 | DCHECK_EQ(-1, result); |
| 156 | DCHECK_EQ(EINTR, errno); |
| 157 | } |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | #elif V8_OS_WIN |
| 162 | |
| 163 | Semaphore::Semaphore(int count) { |
| 164 | DCHECK(count >= 0); |
| 165 | native_handle_ = ::CreateSemaphoreA(NULL, count, 0x7fffffff, NULL); |
| 166 | DCHECK(native_handle_ != NULL); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | Semaphore::~Semaphore() { |
| 171 | BOOL result = CloseHandle(native_handle_); |
| 172 | DCHECK(result); |
| 173 | USE(result); |
| 174 | } |
| 175 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | void Semaphore::Signal() { |
| 177 | LONG dummy; |
| 178 | BOOL result = ReleaseSemaphore(native_handle_, 1, &dummy); |
| 179 | DCHECK(result); |
| 180 | USE(result); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | void Semaphore::Wait() { |
| 185 | DWORD result = WaitForSingleObject(native_handle_, INFINITE); |
| 186 | DCHECK(result == WAIT_OBJECT_0); |
| 187 | USE(result); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | bool Semaphore::WaitFor(const TimeDelta& rel_time) { |
| 192 | TimeTicks now = TimeTicks::Now(); |
| 193 | TimeTicks end = now + rel_time; |
| 194 | while (true) { |
| 195 | int64_t msec = (end - now).InMilliseconds(); |
| 196 | if (msec >= static_cast<int64_t>(INFINITE)) { |
| 197 | DWORD result = WaitForSingleObject(native_handle_, INFINITE - 1); |
| 198 | if (result == WAIT_OBJECT_0) { |
| 199 | return true; |
| 200 | } |
| 201 | DCHECK(result == WAIT_TIMEOUT); |
| 202 | now = TimeTicks::Now(); |
| 203 | } else { |
| 204 | DWORD result = WaitForSingleObject( |
| 205 | native_handle_, (msec < 0) ? 0 : static_cast<DWORD>(msec)); |
| 206 | if (result == WAIT_TIMEOUT) { |
| 207 | return false; |
| 208 | } |
| 209 | DCHECK(result == WAIT_OBJECT_0); |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #endif // V8_OS_MACOSX |
| 216 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 217 | } // namespace base |
| 218 | } // namespace v8 |