From 6680723e663ce279fcf56355ff17513f07397a53 Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Wed, 6 Aug 2025 18:27:58 -0700 Subject: [PATCH] Implement pthread_mutex_t using SRWLOCK on Windows SRWLOCK is faster than CRITICAL_SECTION. MSVC implements the C11 mtx_t and C++ std::mutex using SRWLOCK. Change-Id: I144d2f53aeaa135030ea8c057a2ae8543e6c91b8 --- src/utils/thread_utils.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/thread_utils.c b/src/utils/thread_utils.c index 9149bc9e..5b06d6e2 100644 --- a/src/utils/thread_utils.c +++ b/src/utils/thread_utils.c @@ -27,11 +27,11 @@ WEBP_ASSUME_UNSAFE_INDEXABLE_ABI #include typedef HANDLE pthread_t; -typedef CRITICAL_SECTION pthread_mutex_t; #if _WIN32_WINNT < 0x0600 #error _WIN32_WINNT must target Windows Vista / Server 2008 or newer. #endif +typedef SRWLOCK pthread_mutex_t; typedef CONDITION_VARIABLE pthread_cond_t; #ifndef WINAPI_FAMILY_PARTITION @@ -93,22 +93,22 @@ static int pthread_join(pthread_t thread, void** value_ptr) { // Mutex static int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) { (void)mutexattr; - InitializeCriticalSectionEx(mutex, /*dwSpinCount=*/0, /*Flags=*/0); + InitializeSRWLock(mutex); return 0; } static int pthread_mutex_lock(pthread_mutex_t* const mutex) { - EnterCriticalSection(mutex); + AcquireSRWLockExclusive(mutex); return 0; } static int pthread_mutex_unlock(pthread_mutex_t* const mutex) { - LeaveCriticalSection(mutex); + ReleaseSRWLockExclusive(mutex); return 0; } static int pthread_mutex_destroy(pthread_mutex_t* const mutex) { - DeleteCriticalSection(mutex); + (void)mutex; return 0; } @@ -131,7 +131,7 @@ static int pthread_cond_signal(pthread_cond_t* const condition) { static int pthread_cond_wait(pthread_cond_t* const condition, pthread_mutex_t* const mutex) { - const int ok = SleepConditionVariableCS(condition, mutex, INFINITE); + const int ok = SleepConditionVariableSRW(condition, mutex, INFINITE, 0); return !ok; }