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 @@
#if V8_OS_MACOSX
#include <dlfcn.h>
#include <mach/mach.h>
#endif
#if V8_OS_LINUX
......
......@@ -5,8 +5,7 @@
#include "src/base/platform/semaphore.h"
#if V8_OS_MACOSX
#include <mach/mach_init.h>
#include <mach/task.h>
#include <dispatch/dispatch.h>
#endif
#include <errno.h>
......@@ -21,53 +20,23 @@ namespace base {
#if V8_OS_MACOSX
Semaphore::Semaphore(int count) {
kern_return_t result = semaphore_create(
mach_task_self(), &native_handle_, SYNC_POLICY_FIFO, count);
DCHECK_EQ(KERN_SUCCESS, result);
USE(result);
native_handle_ = dispatch_semaphore_create(count);
DCHECK(native_handle_);
}
Semaphore::~Semaphore() { dispatch_release(native_handle_); }
Semaphore::~Semaphore() {
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::Signal() { dispatch_semaphore_signal(native_handle_); }
void Semaphore::Wait() {
while (true) {
kern_return_t result = semaphore_wait(native_handle_);
if (result == KERN_SUCCESS) return; // Semaphore was signalled.
DCHECK_EQ(KERN_ABORTED, result);
}
dispatch_semaphore_wait(native_handle_, DISPATCH_TIME_FOREVER);
}
bool Semaphore::WaitFor(const TimeDelta& rel_time) {
TimeTicks now = TimeTicks::Now();
TimeTicks end = now + rel_time;
while (true) {
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();
}
dispatch_time_t timeout =
dispatch_time(DISPATCH_TIME_NOW, rel_time.InNanoseconds());
return dispatch_semaphore_wait(native_handle_, timeout) == 0;
}
#elif V8_OS_POSIX
......
......@@ -12,7 +12,7 @@
#endif
#if V8_OS_MACOSX
#include <mach/semaphore.h> // NOLINT
#include <dispatch/dispatch.h> // NOLINT
#elif V8_OS_POSIX
#include <semaphore.h> // NOLINT
#endif
......@@ -50,7 +50,7 @@ class V8_BASE_EXPORT Semaphore final {
bool WaitFor(const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT;
#if V8_OS_MACOSX
using NativeHandle = semaphore_t;
using NativeHandle = dispatch_semaphore_t;
#elif V8_OS_POSIX
using NativeHandle = sem_t;
#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