Replace Semaphore with std::counting_semaphore
Replace the custom platform-specific Semaphore wrapper in
src/util/semaphore.{h,cc} with C++20 std::counting_semaphore<> in
src/util/auto_reset_event.h, and remove the obsolete files from the
build.
Using std::counting_semaphore<> rather than std::binary_semaphore
matches the previous Semaphore semantics and avoids exceeding max()
when multiple waiters are signaled concurrently.
Change-Id: Ic57fe29bdd8c13b04181079c0f46825fddee6d65
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29860
Reviewed-by: Matt Stark <msta@google.com>
Commit-Queue: Takuto Ikuta <tikuta@google.com>
diff --git a/build/gen.py b/build/gen.py
index 4163a60..a1ed2b5 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -893,7 +893,6 @@
'src/util/atomic_write.cc',
'src/util/exe_path.cc',
'src/util/msg_loop.cc',
- 'src/util/semaphore.cc',
'src/util/sys_info.cc',
'src/util/ticks.cc',
'src/util/worker_pool.cc',
diff --git a/src/util/auto_reset_event.h b/src/util/auto_reset_event.h
index 5e040a8..7fce403 100644
--- a/src/util/auto_reset_event.h
+++ b/src/util/auto_reset_event.h
@@ -6,22 +6,22 @@
#define UTIL_AUTO_RESET_EVENT_H_
#include <atomic>
+#include <semaphore>
#include "base/logging.h"
-#include "util/semaphore.h"
// From http://preshing.com/20150316/semaphores-are-surprisingly-versatile/,
-// but using V8's Semaphore.
+// using C++20 std::counting_semaphore.
class AutoResetEvent {
private:
// status_ == 1: Event object is signaled.
// status_ == 0: Event object is reset and no threads are waiting.
// status_ == -N: Event object is reset and N threads are waiting.
- std::atomic<int> status_;
- Semaphore semaphore_;
+ std::atomic<int> status_{0};
+ std::counting_semaphore<> semaphore_{0};
public:
- AutoResetEvent() : status_(0), semaphore_(0) {}
+ AutoResetEvent() = default;
void Signal() {
int old_status = status_.load(std::memory_order_relaxed);
@@ -38,14 +38,14 @@
// status_. old_status has been updated. Retry the CAS loop.
}
if (old_status < 0)
- semaphore_.Signal(); // Release one waiting thread.
+ semaphore_.release(); // Release one waiting thread.
}
void Wait() {
int old_status = status_.fetch_sub(1, std::memory_order_acquire);
DCHECK_LE(old_status, 1);
if (old_status < 1) {
- semaphore_.Wait();
+ semaphore_.acquire();
}
}
};
diff --git a/src/util/semaphore.cc b/src/util/semaphore.cc
deleted file mode 100644
index afed45e..0000000
--- a/src/util/semaphore.cc
+++ /dev/null
@@ -1,91 +0,0 @@
-// Copyright 2013 the V8 project authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-//
-// Based on
-// https://cs.chromium.org/chromium/src/v8/src/base/platform/semaphore.cc
-
-#include "util/semaphore.h"
-
-#include "base/logging.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#endif
-
-#if defined(OS_MACOSX)
-
-Semaphore::Semaphore(int count) {
- native_handle_ = dispatch_semaphore_create(count);
- DCHECK(native_handle_);
-}
-
-Semaphore::~Semaphore() {
- dispatch_release(native_handle_);
-}
-
-void Semaphore::Signal() {
- dispatch_semaphore_signal(native_handle_);
-}
-
-void Semaphore::Wait() {
- dispatch_semaphore_wait(native_handle_, DISPATCH_TIME_FOREVER);
-}
-
-#elif defined(OS_POSIX)
-
-Semaphore::Semaphore(int count) {
- DCHECK_GE(count, 0);
- int result = sem_init(&native_handle_, 0, count);
- DCHECK_EQ(0, result);
-}
-
-Semaphore::~Semaphore() {
- int result = sem_destroy(&native_handle_);
- DCHECK_EQ(0, result);
-}
-
-void Semaphore::Signal() {
- int result = sem_post(&native_handle_);
- // This check may fail with <libc-2.21, which we use on the try bots, if the
- // semaphore is destroyed while sem_post is still executed. A work around is
- // to extend the lifetime of the semaphore.
- CHECK_EQ(0, result);
-}
-
-void Semaphore::Wait() {
- while (true) {
- int result = sem_wait(&native_handle_);
- if (result == 0)
- return; // Semaphore was signalled.
- // Signal caused spurious wakeup.
- DCHECK_EQ(-1, result);
- DCHECK_EQ(EINTR, errno);
- }
-}
-
-#elif defined(OS_WIN)
-
-Semaphore::Semaphore(int count) {
- DCHECK_GE(count, 0);
- native_handle_ = ::CreateSemaphoreA(nullptr, count, 0x7FFFFFFF, nullptr);
- DCHECK(native_handle_);
-}
-
-Semaphore::~Semaphore() {
- BOOL result = CloseHandle(native_handle_);
- DCHECK(result);
-}
-
-void Semaphore::Signal() {
- LONG dummy;
- BOOL result = ReleaseSemaphore(native_handle_, 1, &dummy);
- DCHECK(result);
-}
-
-void Semaphore::Wait() {
- DWORD result = WaitForSingleObject(native_handle_, INFINITE);
- DCHECK(result == WAIT_OBJECT_0);
-}
-
-#endif
diff --git a/src/util/semaphore.h b/src/util/semaphore.h
deleted file mode 100644
index 13a0992..0000000
--- a/src/util/semaphore.h
+++ /dev/null
@@ -1,55 +0,0 @@
-// Copyright 2013 the V8 project authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-//
-// Based on
-// https://cs.chromium.org/chromium/src/v8/src/base/platform/semaphore.h
-
-#ifndef UTIL_SEMAPHORE_H_
-#define UTIL_SEMAPHORE_H_
-
-#include "util/build_config.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#elif defined(OS_MACOSX)
-#include <dispatch/dispatch.h>
-#elif defined(OS_ZOS)
-#include "zos-semaphore.h"
-#elif defined(OS_POSIX)
-#include <semaphore.h>
-#else
-#error Port.
-#endif
-
-class Semaphore {
- public:
- explicit Semaphore(int count);
- ~Semaphore();
-
- // Increments the semaphore counter.
- void Signal();
-
- // Decrements the semaphore counter if it is positive, or blocks until it
- // becomes positive and then decrements the counter.
- void Wait();
-
-#if defined(OS_MACOSX)
- using NativeHandle = dispatch_semaphore_t;
-#elif defined(OS_POSIX)
- using NativeHandle = sem_t;
-#elif defined(OS_WIN)
- using NativeHandle = HANDLE;
-#endif
-
- NativeHandle& native_handle() { return native_handle_; }
- const NativeHandle& native_handle() const { return native_handle_; }
-
- private:
- NativeHandle native_handle_;
-
- Semaphore(const Semaphore&) = delete;
- Semaphore& operator=(const Semaphore&) = delete;
-};
-
-#endif // UTIL_SEMAPHORE_H_