Skip to content

Commit fdf1db6

Browse files
committed
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. Remove #include <errno.h>. It should have been removed along with the pthread_trylock() function in commit d77922a. Change-Id: If3e5e5ccc9f34ef002971ef0d9fc6db8a6b0b650
1 parent fed80e2 commit fdf1db6

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

vpx_util/vpx_pthread.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ extern "C" {
2626
#define NOMINMAX
2727
#undef WIN32_LEAN_AND_MEAN
2828
#define WIN32_LEAN_AND_MEAN
29-
#include <errno.h> // NOLINT
3029
#include <process.h> // NOLINT
3130
#include <stddef.h> // NOLINT
3231
#include <windows.h> // NOLINT
3332
typedef HANDLE pthread_t;
34-
typedef CRITICAL_SECTION pthread_mutex_t;
33+
typedef SRWLOCK pthread_mutex_t;
3534

3635
#if _WIN32_WINNT < 0x0600
3736
#error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
@@ -81,31 +80,30 @@ static INLINE int pthread_create(pthread_t *const thread, const void *attr,
8180

8281
static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
8382
(void)value_ptr;
84-
return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
85-
WAIT_OBJECT_0 ||
83+
return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 ||
8684
CloseHandle(thread) == 0);
8785
}
8886

8987
// Mutex
9088
static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
9189
void *mutexattr) {
9290
(void)mutexattr;
93-
InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
91+
InitializeSRWLock(mutex);
9492
return 0;
9593
}
9694

9795
static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
98-
EnterCriticalSection(mutex);
96+
AcquireSRWLockExclusive(mutex);
9997
return 0;
10098
}
10199

102100
static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
103-
LeaveCriticalSection(mutex);
101+
ReleaseSRWLockExclusive(mutex);
104102
return 0;
105103
}
106104

107105
static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
108-
DeleteCriticalSection(mutex);
106+
(void)mutex;
109107
return 0;
110108
}
111109

@@ -134,8 +132,7 @@ static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
134132

135133
static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
136134
pthread_mutex_t *const mutex) {
137-
int ok;
138-
ok = SleepConditionVariableCS(condition, mutex, INFINITE);
135+
const int ok = SleepConditionVariableSRW(condition, mutex, INFINITE, 0);
139136
return !ok;
140137
}
141138
#else // _WIN32

0 commit comments

Comments
 (0)