[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/520828 Reviewed-by: Jochen Eisinger <jochen@chromium.org> Commit-Queue: Loo Rong Jie <loorongjie@gmail.com> Cr-Commit-Position: refs/heads/master@{#45658}
This commit is contained in:
parent
4b7ce1446d
commit
8ce8b7f022
@ -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,39 +76,6 @@ 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);
|
||||
}
|
||||
|
||||
|
||||
static V8_INLINE void InitializeRecursiveNativeHandle(PCRITICAL_SECTION cs) {
|
||||
InitializeCriticalSection(cs);
|
||||
}
|
||||
|
||||
|
||||
static V8_INLINE void DestroyNativeHandle(PCRITICAL_SECTION cs) {
|
||||
DeleteCriticalSection(cs);
|
||||
}
|
||||
|
||||
|
||||
static V8_INLINE void LockNativeHandle(PCRITICAL_SECTION cs) {
|
||||
EnterCriticalSection(cs);
|
||||
}
|
||||
|
||||
|
||||
static V8_INLINE void UnlockNativeHandle(PCRITICAL_SECTION cs) {
|
||||
LeaveCriticalSection(cs);
|
||||
}
|
||||
|
||||
|
||||
static V8_INLINE bool TryLockNativeHandle(PCRITICAL_SECTION cs) {
|
||||
return TryEnterCriticalSection(cs) != FALSE;
|
||||
}
|
||||
|
||||
#endif // V8_OS_POSIX
|
||||
|
||||
|
||||
Mutex::Mutex() {
|
||||
InitializeNativeHandle(&native_handle_);
|
||||
@ -188,5 +155,85 @@ bool RecursiveMutex::TryLock() {
|
||||
return true;
|
||||
}
|
||||
|
||||
#elif V8_OS_WIN
|
||||
|
||||
Mutex::Mutex() : native_handle_(SRWLOCK_INIT) {
|
||||
#ifdef DEBUG
|
||||
level_ = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Mutex::~Mutex() {
|
||||
DCHECK_EQ(0, level_);
|
||||
}
|
||||
|
||||
|
||||
void Mutex::Lock() {
|
||||
AcquireSRWLockExclusive(&native_handle_);
|
||||
AssertUnheldAndMark();
|
||||
}
|
||||
|
||||
|
||||
void Mutex::Unlock() {
|
||||
AssertHeldAndUnmark();
|
||||
ReleaseSRWLockExclusive(&native_handle_);
|
||||
}
|
||||
|
||||
|
||||
bool Mutex::TryLock() {
|
||||
if (!TryAcquireSRWLockExclusive(&native_handle_)) {
|
||||
return false;
|
||||
}
|
||||
AssertUnheldAndMark();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
RecursiveMutex::RecursiveMutex() {
|
||||
InitializeCriticalSection(&native_handle_);
|
||||
#ifdef DEBUG
|
||||
level_ = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
RecursiveMutex::~RecursiveMutex() {
|
||||
DeleteCriticalSection(&native_handle_);
|
||||
DCHECK_EQ(0, level_);
|
||||
}
|
||||
|
||||
|
||||
void RecursiveMutex::Lock() {
|
||||
EnterCriticalSection(&native_handle_);
|
||||
#ifdef DEBUG
|
||||
DCHECK_LE(0, level_);
|
||||
level_++;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void RecursiveMutex::Unlock() {
|
||||
#ifdef DEBUG
|
||||
DCHECK_LT(0, level_);
|
||||
level_--;
|
||||
#endif
|
||||
LeaveCriticalSection(&native_handle_);
|
||||
}
|
||||
|
||||
|
||||
bool RecursiveMutex::TryLock() {
|
||||
if (!TryEnterCriticalSection(&native_handle_)) {
|
||||
return false;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
DCHECK_LE(0, level_);
|
||||
level_++;
|
||||
#endif
|
||||
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_;
|
||||
|
Loading…
Reference in New Issue
Block a user