Commit a2cf9790 authored by Robert Sesek's avatar Robert Sesek Committed by Commit Bot

Switch base::Semaphore to use dispatch_semaphore_t on Mac.

The dispatch_semaphore_t is a higher-level, more-efficient semaphore
primitive if the cross-process capabilities of semaphore_t are not
needed.

Bug: chromium:1012386
Change-Id: I9cc6f025f00159f9424c054a3395542b9db00b89
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1848211Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64254}
parent f5dac714
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#if V8_OS_MACOSX #if V8_OS_MACOSX
#include <dlfcn.h> #include <dlfcn.h>
#include <mach/mach.h>
#endif #endif
#if V8_OS_LINUX #if V8_OS_LINUX
......
...@@ -5,8 +5,7 @@ ...@@ -5,8 +5,7 @@
#include "src/base/platform/semaphore.h" #include "src/base/platform/semaphore.h"
#if V8_OS_MACOSX #if V8_OS_MACOSX
#include <mach/mach_init.h> #include <dispatch/dispatch.h>
#include <mach/task.h>
#endif #endif
#include <errno.h> #include <errno.h>
...@@ -21,53 +20,23 @@ namespace base { ...@@ -21,53 +20,23 @@ namespace base {
#if V8_OS_MACOSX #if V8_OS_MACOSX
Semaphore::Semaphore(int count) { Semaphore::Semaphore(int count) {
kern_return_t result = semaphore_create( native_handle_ = dispatch_semaphore_create(count);
mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count); DCHECK(native_handle_);
DCHECK_EQ(KERN_SUCCESS, result);
USE(result);
} }
Semaphore::~Semaphore() { dispatch_release(native_handle_); }
Semaphore::~Semaphore() { void Semaphore::Signal() { dispatch_semaphore_signal(native_handle_); }
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() { void Semaphore::Wait() {
while (true) { dispatch_semaphore_wait(native_handle_, DISPATCH_TIME_FOREVER);
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) { bool Semaphore::WaitFor(const TimeDelta& rel_time) {
TimeTicks now = TimeTicks::Now(); dispatch_time_t timeout =
TimeTicks end = now + rel_time; dispatch_time(DISPATCH_TIME_NOW, rel_time.InNanoseconds());
while (true) { return dispatch_semaphore_wait(native_handle_, timeout) == 0;
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 #elif V8_OS_POSIX
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#endif #endif
#if V8_OS_MACOSX #if V8_OS_MACOSX
#include <mach/semaphore.h> // NOLINT #include <dispatch/dispatch.h> // NOLINT
#elif V8_OS_POSIX #elif V8_OS_POSIX
#include <semaphore.h> // NOLINT #include <semaphore.h> // NOLINT
#endif #endif
...@@ -50,7 +50,7 @@ class V8_BASE_EXPORT Semaphore final { ...@@ -50,7 +50,7 @@ class V8_BASE_EXPORT Semaphore final {
bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT; bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
#if V8_OS_MACOSX #if V8_OS_MACOSX
using NativeHandle = semaphore_t; using NativeHandle = dispatch_semaphore_t;
#elif V8_OS_POSIX #elif V8_OS_POSIX
using NativeHandle = sem_t; using NativeHandle = sem_t;
#elif V8_OS_WIN #elif V8_OS_WIN
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment