Commit 8ce8b7f0 authored by Loo Rong Jie's avatar Loo Rong Jie Committed by Commit Bot

[base] Migrate Mutex from CRITICAL_SECTION to SRWLOCK

SRWLOCK is a faster and lightweight alternative of CRITICAL_SECTION for
non-recursive use case.

Bug: chromium:592752
Change-Id: Ie97cd9cee2d50a95f316b41c30e953f586b06c99
Reviewed-on: https://chromium-review.googlesource.com/520828Reviewed-by: 's avatarJochen Eisinger <jochen@chromium.org>
Commit-Queue: Loo Rong Jie <loorongjie@gmail.com>
Cr-Commit-Position: refs/heads/master@{#45658}
parent 4b7ce144
...@@ -134,7 +134,8 @@ void ConditionVariable::NotifyAll() { ...@@ -134,7 +134,8 @@ void ConditionVariable::NotifyAll() {
void ConditionVariable::Wait(Mutex* mutex) { void ConditionVariable::Wait(Mutex* mutex) {
mutex->AssertHeldAndUnmark(); mutex->AssertHeldAndUnmark();
SleepConditionVariableCS(&native_handle_, &mutex->native_handle(), INFINITE); SleepConditionVariableSRW(&native_handle_, &mutex->native_handle(), INFINITE,
0);
mutex->AssertUnheldAndMark(); mutex->AssertUnheldAndMark();
} }
...@@ -142,8 +143,8 @@ void ConditionVariable::Wait(Mutex* mutex) { ...@@ -142,8 +143,8 @@ void ConditionVariable::Wait(Mutex* mutex) {
bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) { bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) {
int64_t msec = rel_time.InMilliseconds(); int64_t msec = rel_time.InMilliseconds();
mutex->AssertHeldAndUnmark(); mutex->AssertHeldAndUnmark();
BOOL result = SleepConditionVariableCS( BOOL result = SleepConditionVariableSRW(
&native_handle_, &mutex->native_handle(), static_cast<DWORD>(msec)); &native_handle_, &mutex->native_handle(), static_cast<DWORD>(msec), 0);
#ifdef DEBUG #ifdef DEBUG
if (!result) { if (!result) {
// On failure, we only expect the CV to timeout. Any other error value means // On failure, we only expect the CV to timeout. Any other error value means
......
...@@ -76,42 +76,88 @@ static V8_INLINE bool TryLockNativeHandle(pthread_mutex_t* mutex) { ...@@ -76,42 +76,88 @@ static V8_INLINE bool TryLockNativeHandle(pthread_mutex_t* mutex) {
return true; return true;
} }
#elif V8_OS_WIN
static V8_INLINE void InitializeNativeHandle(PCRITICAL_SECTION cs) { Mutex::Mutex() {
InitializeCriticalSection(cs); InitializeNativeHandle(&native_handle_);
#ifdef DEBUG
level_ = 0;
#endif
} }
static V8_INLINE void InitializeRecursiveNativeHandle(PCRITICAL_SECTION cs) { Mutex::~Mutex() {
InitializeCriticalSection(cs); DestroyNativeHandle(&native_handle_);
DCHECK_EQ(0, level_);
} }
static V8_INLINE void DestroyNativeHandle(PCRITICAL_SECTION cs) { void Mutex::Lock() {
DeleteCriticalSection(cs); LockNativeHandle(&native_handle_);
AssertUnheldAndMark();
} }
static V8_INLINE void LockNativeHandle(PCRITICAL_SECTION cs) { void Mutex::Unlock() {
EnterCriticalSection(cs); AssertHeldAndUnmark();
UnlockNativeHandle(&native_handle_);
} }
static V8_INLINE void UnlockNativeHandle(PCRITICAL_SECTION cs) { bool Mutex::TryLock() {
LeaveCriticalSection(cs); if (!TryLockNativeHandle(&native_handle_)) {
return false;
}
AssertUnheldAndMark();
return true;
} }
static V8_INLINE bool TryLockNativeHandle(PCRITICAL_SECTION cs) { RecursiveMutex::RecursiveMutex() {
return TryEnterCriticalSection(cs) != FALSE; InitializeRecursiveNativeHandle(&native_handle_);
#ifdef DEBUG
level_ = 0;
#endif
} }
#endif // V8_OS_POSIX
RecursiveMutex::~RecursiveMutex() {
DestroyNativeHandle(&native_handle_);
DCHECK_EQ(0, level_);
}
Mutex::Mutex() {
InitializeNativeHandle(&native_handle_); void RecursiveMutex::Lock() {
LockNativeHandle(&native_handle_);
#ifdef DEBUG
DCHECK_LE(0, level_);
level_++;
#endif
}
void RecursiveMutex::Unlock() {
#ifdef DEBUG
DCHECK_LT(0, level_);
level_--;
#endif
UnlockNativeHandle(&native_handle_);
}
bool RecursiveMutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) {
return false;
}
#ifdef DEBUG
DCHECK_LE(0, level_);
level_++;
#endif
return true;
}
#elif V8_OS_WIN
Mutex::Mutex() : native_handle_(SRWLOCK_INIT) {
#ifdef DEBUG #ifdef DEBUG
level_ = 0; level_ = 0;
#endif #endif
...@@ -119,25 +165,24 @@ Mutex::Mutex() { ...@@ -119,25 +165,24 @@ Mutex::Mutex() {
Mutex::~Mutex() { Mutex::~Mutex() {
DestroyNativeHandle(&native_handle_);
DCHECK_EQ(0, level_); DCHECK_EQ(0, level_);
} }
void Mutex::Lock() { void Mutex::Lock() {
LockNativeHandle(&native_handle_); AcquireSRWLockExclusive(&native_handle_);
AssertUnheldAndMark(); AssertUnheldAndMark();
} }
void Mutex::Unlock() { void Mutex::Unlock() {
AssertHeldAndUnmark(); AssertHeldAndUnmark();
UnlockNativeHandle(&native_handle_); ReleaseSRWLockExclusive(&native_handle_);
} }
bool Mutex::TryLock() { bool Mutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) { if (!TryAcquireSRWLockExclusive(&native_handle_)) {
return false; return false;
} }
AssertUnheldAndMark(); AssertUnheldAndMark();
...@@ -146,7 +191,7 @@ bool Mutex::TryLock() { ...@@ -146,7 +191,7 @@ bool Mutex::TryLock() {
RecursiveMutex::RecursiveMutex() { RecursiveMutex::RecursiveMutex() {
InitializeRecursiveNativeHandle(&native_handle_); InitializeCriticalSection(&native_handle_);
#ifdef DEBUG #ifdef DEBUG
level_ = 0; level_ = 0;
#endif #endif
...@@ -154,13 +199,13 @@ RecursiveMutex::RecursiveMutex() { ...@@ -154,13 +199,13 @@ RecursiveMutex::RecursiveMutex() {
RecursiveMutex::~RecursiveMutex() { RecursiveMutex::~RecursiveMutex() {
DestroyNativeHandle(&native_handle_); DeleteCriticalSection(&native_handle_);
DCHECK_EQ(0, level_); DCHECK_EQ(0, level_);
} }
void RecursiveMutex::Lock() { void RecursiveMutex::Lock() {
LockNativeHandle(&native_handle_); EnterCriticalSection(&native_handle_);
#ifdef DEBUG #ifdef DEBUG
DCHECK_LE(0, level_); DCHECK_LE(0, level_);
level_++; level_++;
...@@ -173,12 +218,12 @@ void RecursiveMutex::Unlock() { ...@@ -173,12 +218,12 @@ void RecursiveMutex::Unlock() {
DCHECK_LT(0, level_); DCHECK_LT(0, level_);
level_--; level_--;
#endif #endif
UnlockNativeHandle(&native_handle_); LeaveCriticalSection(&native_handle_);
} }
bool RecursiveMutex::TryLock() { bool RecursiveMutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) { if (!TryEnterCriticalSection(&native_handle_)) {
return false; return false;
} }
#ifdef DEBUG #ifdef DEBUG
...@@ -188,5 +233,7 @@ bool RecursiveMutex::TryLock() { ...@@ -188,5 +233,7 @@ bool RecursiveMutex::TryLock() {
return true; return true;
} }
#endif // V8_OS_POSIX
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
...@@ -57,7 +57,7 @@ class V8_BASE_EXPORT Mutex final { ...@@ -57,7 +57,7 @@ class V8_BASE_EXPORT Mutex final {
#if V8_OS_POSIX #if V8_OS_POSIX
typedef pthread_mutex_t NativeHandle; typedef pthread_mutex_t NativeHandle;
#elif V8_OS_WIN #elif V8_OS_WIN
typedef CRITICAL_SECTION NativeHandle; typedef SRWLOCK NativeHandle;
#endif #endif
NativeHandle& native_handle() { NativeHandle& native_handle() {
...@@ -153,7 +153,11 @@ class V8_BASE_EXPORT RecursiveMutex final { ...@@ -153,7 +153,11 @@ class V8_BASE_EXPORT RecursiveMutex final {
bool TryLock() WARN_UNUSED_RESULT; bool TryLock() WARN_UNUSED_RESULT;
// The implementation-defined native handle type. // The implementation-defined native handle type.
typedef Mutex::NativeHandle NativeHandle; #if V8_OS_POSIX
typedef pthread_mutex_t NativeHandle;
#elif V8_OS_WIN
typedef CRITICAL_SECTION NativeHandle;
#endif
NativeHandle& native_handle() { NativeHandle& native_handle() {
return native_handle_; return native_handle_;
......
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