From 59fb6407f5be9b2e02d1fe47a45522905c2fed36 Mon Sep 17 00:00:00 2001 From: Clemens Hammacher Date: Tue, 19 Feb 2019 14:26:42 +0100 Subject: [PATCH] [base] Clean up mutex code Since the comments are copied from the std types anyway, we should also point to them to make clear that the semantic is intentially equivalent. Also, remove two unused methods and avoid an unneeded nullptr check. R=mlippautz@chromium.org Bug: v8:8834 Change-Id: Idcb5a1b8b2b3bb0786807828a96e085df963a8f0 Reviewed-on: https://chromium-review.googlesource.com/c/1477224 Reviewed-by: Michael Lippautz Commit-Queue: Clemens Hammacher Cr-Commit-Position: refs/heads/master@{#59692} --- src/base/platform/mutex.h | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/base/platform/mutex.h b/src/base/platform/mutex.h index a69eee0bc6..4ee0deb2cf 100644 --- a/src/base/platform/mutex.h +++ b/src/base/platform/mutex.h @@ -20,7 +20,7 @@ namespace v8 { namespace base { // ---------------------------------------------------------------------------- -// Mutex +// Mutex - a replacement for std::mutex // // This class is a synchronization primitive that can be used to protect shared // data from being simultaneously accessed by multiple threads. A mutex offers @@ -106,9 +106,8 @@ typedef LazyStaticInstance, #define LAZY_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER - // ----------------------------------------------------------------------------- -// RecursiveMutex +// RecursiveMutex - a replacement for std::recursive_mutex // // This class is a synchronization primitive that can be used to protect shared // data from being simultaneously accessed by multiple threads. A recursive @@ -158,13 +157,6 @@ class V8_BASE_EXPORT RecursiveMutex final { typedef CRITICAL_SECTION NativeHandle; #endif - NativeHandle& native_handle() { - return native_handle_; - } - const NativeHandle& native_handle() const { - return native_handle_; - } - private: NativeHandle native_handle_; #ifdef DEBUG @@ -210,16 +202,20 @@ template class LockGuard final { public: explicit LockGuard(Mutex* mutex) : mutex_(mutex) { - if (Behavior == NullBehavior::kRequireNotNull || mutex_ != nullptr) { - mutex_->Lock(); - } + if (has_mutex()) mutex_->Lock(); } ~LockGuard() { - if (mutex_ != nullptr) mutex_->Unlock(); + if (has_mutex()) mutex_->Unlock(); } private: - Mutex* mutex_; + Mutex* const mutex_; + + bool V8_INLINE has_mutex() const { + DCHECK_IMPLIES(Behavior == NullBehavior::kRequireNotNull, + mutex_ != nullptr); + return Behavior == NullBehavior::kRequireNotNull || mutex_ != nullptr; + } DISALLOW_COPY_AND_ASSIGN(LockGuard); };