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() {
void ConditionVariable::Wait(Mutex* mutex) {
mutex->AssertHeldAndUnmark();
SleepConditionVariableCS(&native_handle_, &mutex->native_handle(), INFINITE);
SleepConditionVariableSRW(&native_handle_, &mutex->native_handle(), INFINITE,
0);
mutex->AssertUnheldAndMark();
}
......@@ -142,8 +143,8 @@ void ConditionVariable::Wait(Mutex* mutex) {
bool ConditionVariable::WaitFor(Mutex* mutex, const TimeDelta& rel_time) {
int64_t msec = rel_time.InMilliseconds();
mutex->AssertHeldAndUnmark();
BOOL result = SleepConditionVariableCS(
&native_handle_, &mutex->native_handle(), static_cast<DWORD>(msec));
BOOL result = SleepConditionVariableSRW(
&native_handle_, &mutex->native_handle(), static_cast<DWORD>(msec), 0);
#ifdef DEBUG
if (!result) {
// 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) {
return true;
}
#elif V8_OS_WIN
static V8_INLINE void InitializeNativeHandle(PCRITICAL_SECTION cs) {
InitializeCriticalSection(cs);
Mutex::Mutex() {
InitializeNativeHandle(&native_handle_);
#ifdef DEBUG
level_ = 0;
#endif
}
static V8_INLINE void InitializeRecursiveNativeHandle(PCRITICAL_SECTION cs) {
InitializeCriticalSection(cs);
Mutex::~Mutex() {
DestroyNativeHandle(&native_handle_);
DCHECK_EQ(0, level_);
}
static V8_INLINE void DestroyNativeHandle(PCRITICAL_SECTION cs) {
DeleteCriticalSection(cs);
void Mutex::Lock() {
LockNativeHandle(&native_handle_);
AssertUnheldAndMark();
}
static V8_INLINE void LockNativeHandle(PCRITICAL_SECTION cs) {
EnterCriticalSection(cs);
void Mutex::Unlock() {
AssertHeldAndUnmark();
UnlockNativeHandle(&native_handle_);
}
static V8_INLINE void UnlockNativeHandle(PCRITICAL_SECTION cs) {
LeaveCriticalSection(cs);
bool Mutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) {
return false;
}
AssertUnheldAndMark();
return true;
}
static V8_INLINE bool TryLockNativeHandle(PCRITICAL_SECTION cs) {
return TryEnterCriticalSection(cs) != FALSE;
RecursiveMutex::RecursiveMutex() {
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
level_ = 0;
#endif
......@@ -119,25 +165,24 @@ Mutex::Mutex() {
Mutex::~Mutex() {
DestroyNativeHandle(&native_handle_);
DCHECK_EQ(0, level_);
}
void Mutex::Lock() {
LockNativeHandle(&native_handle_);
AcquireSRWLockExclusive(&native_handle_);
AssertUnheldAndMark();
}
void Mutex::Unlock() {
AssertHeldAndUnmark();
UnlockNativeHandle(&native_handle_);
ReleaseSRWLockExclusive(&native_handle_);
}
bool Mutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) {
if (!TryAcquireSRWLockExclusive(&native_handle_)) {
return false;
}
AssertUnheldAndMark();
......@@ -146,7 +191,7 @@ bool Mutex::TryLock() {
RecursiveMutex::RecursiveMutex() {
InitializeRecursiveNativeHandle(&native_handle_);
InitializeCriticalSection(&native_handle_);
#ifdef DEBUG
level_ = 0;
#endif
......@@ -154,13 +199,13 @@ RecursiveMutex::RecursiveMutex() {
RecursiveMutex::~RecursiveMutex() {
DestroyNativeHandle(&native_handle_);
DeleteCriticalSection(&native_handle_);
DCHECK_EQ(0, level_);
}
void RecursiveMutex::Lock() {
LockNativeHandle(&native_handle_);
EnterCriticalSection(&native_handle_);
#ifdef DEBUG
DCHECK_LE(0, level_);
level_++;
......@@ -173,12 +218,12 @@ void RecursiveMutex::Unlock() {
DCHECK_LT(0, level_);
level_--;
#endif
UnlockNativeHandle(&native_handle_);
LeaveCriticalSection(&native_handle_);
}
bool RecursiveMutex::TryLock() {
if (!TryLockNativeHandle(&native_handle_)) {
if (!TryEnterCriticalSection(&native_handle_)) {
return false;
}
#ifdef DEBUG
......@@ -188,5 +233,7 @@ bool RecursiveMutex::TryLock() {
return true;
}
#endif // V8_OS_POSIX
} // namespace base
} // namespace v8
......@@ -57,7 +57,7 @@ class V8_BASE_EXPORT Mutex final {
#if V8_OS_POSIX
typedef pthread_mutex_t NativeHandle;
#elif V8_OS_WIN
typedef CRITICAL_SECTION NativeHandle;
typedef SRWLOCK NativeHandle;
#endif
NativeHandle& native_handle() {
......@@ -153,7 +153,11 @@ class V8_BASE_EXPORT RecursiveMutex final {
bool TryLock() WARN_UNUSED_RESULT;
// 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() {
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