Skip to content

Commit dc0e305

Browse files
LeszekSwirskiV8 LUCI CQ
authored andcommitted
[base] Use absl::Mutex for SharedMutex
std::shared_mutex doesn't seem very performance optimized in libc++, and we see it spending a lot of time in the kernel even under low contention. Mac only for now, since this is the only platform where we use std::shared_mutex, but we should try unifying with other platforms too. Change-Id: I984692918dd611dd8595059345b31531f1fe3071 Bug: 383975879 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6088023 Commit-Queue: Michael Lippautz <[email protected]> Auto-Submit: Leszek Swirski <[email protected]> Reviewed-by: Michael Lippautz <[email protected]> Cr-Commit-Position: refs/heads/main@{#97768}
1 parent 846f470 commit dc0e305

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6649,6 +6649,8 @@ v8_component("v8_libbase") {
66496649

66506650
deps = [ ":v8_config_headers" ]
66516651

6652+
public_deps = [ ":v8_abseil" ]
6653+
66526654
if (current_os == "zos") {
66536655
public_configs += [ ":zoslib_config" ]
66546656
deps += [ ":zoslib" ]

DEPS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ include_rules = [
526526
'+absl/container/flat_hash_set.h',
527527
'+absl/container/btree_map.h',
528528
'+absl/status',
529+
'+absl/synchronization/mutex.h',
529530
# Some abseil features are explicitly banned.
530531
'-absl/types/any.h', # Requires RTTI.
531532
'-absl/types/flags', # Requires RTTI.

src/base/platform/mutex.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,36 +231,38 @@ bool RecursiveMutex::TryLock() {
231231
SharedMutex::SharedMutex() = default;
232232
SharedMutex::~SharedMutex() = default;
233233

234-
void SharedMutex::LockShared() {
234+
void SharedMutex::LockShared() ABSL_SHARED_LOCK_FUNCTION(native_handle_) {
235235
DCHECK(TryHoldSharedMutex(this));
236-
native_handle_.lock_shared();
236+
native_handle_.ReaderLock();
237237
}
238238

239-
void SharedMutex::LockExclusive() {
239+
void SharedMutex::LockExclusive() ABSL_EXCLUSIVE_LOCK_FUNCTION(native_handle_) {
240240
DCHECK(TryHoldSharedMutex(this));
241-
native_handle_.lock();
241+
native_handle_.Lock();
242242
}
243243

244-
void SharedMutex::UnlockShared() {
244+
void SharedMutex::UnlockShared() ABSL_UNLOCK_FUNCTION(native_handle_) {
245245
DCHECK(TryReleaseSharedMutex(this));
246-
native_handle_.unlock_shared();
246+
native_handle_.ReaderUnlock();
247247
}
248248

249-
void SharedMutex::UnlockExclusive() {
249+
void SharedMutex::UnlockExclusive() ABSL_UNLOCK_FUNCTION(native_handle_) {
250250
DCHECK(TryReleaseSharedMutex(this));
251-
native_handle_.unlock();
251+
native_handle_.Unlock();
252252
}
253253

254-
bool SharedMutex::TryLockShared() {
254+
bool SharedMutex::TryLockShared()
255+
ABSL_SHARED_TRYLOCK_FUNCTION(true, native_handle_) {
255256
DCHECK(SharedMutexNotHeld(this));
256-
bool result = native_handle_.try_lock_shared();
257+
bool result = native_handle_.ReaderTryLock();
257258
if (result) DCHECK(TryHoldSharedMutex(this));
258259
return result;
259260
}
260261

261-
bool SharedMutex::TryLockExclusive() {
262+
bool SharedMutex::TryLockExclusive()
263+
ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true, native_handle_) {
262264
DCHECK(SharedMutexNotHeld(this));
263-
bool result = native_handle_.try_lock();
265+
bool result = native_handle_.TryLock();
264266
if (result) DCHECK(TryHoldSharedMutex(this));
265267
return result;
266268
}

src/base/platform/mutex.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "include/v8config.h"
1111

1212
#if V8_OS_DARWIN
13-
#include <shared_mutex>
13+
#include "absl/synchronization/mutex.h"
1414
#endif
1515

1616
#if V8_OS_POSIX
@@ -296,7 +296,7 @@ class V8_BASE_EXPORT SharedMutex final {
296296
// pthread_rwlock_t is broken on MacOS when signals are being sent to the
297297
// process (see https://crbug.com/v8/11399).
298298
// We thus use std::shared_mutex on MacOS, which does not have this problem.
299-
using NativeHandle = std::shared_mutex;
299+
using NativeHandle = absl::Mutex;
300300
#elif V8_OS_POSIX
301301
using NativeHandle = pthread_rwlock_t;
302302
#elif V8_OS_WIN

0 commit comments

Comments
 (0)