diff options
author | wm4 <wm4@nowhere> | 2016-05-24 18:45:40 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2016-05-24 19:02:22 +0200 |
commit | b9cc33de58c3e58a93c5e5316e5734922f5a44b9 (patch) | |
tree | 60862bc79e8bdb76bfba9cb1d5d7e7cb9191d403 /osdep/win32/pthread.c | |
parent | 8ff96f0216884f9bd3c60857e629a25aaa8704de (diff) | |
download | mpv-b9cc33de58c3e58a93c5e5316e5734922f5a44b9.tar.bz2 mpv-b9cc33de58c3e58a93c5e5316e5734922f5a44b9.tar.xz |
win32: pthread: use SRW locks by default
SRW locks are available since Windows Vista. They work essentially like
Linux futexes. In particular, they can be statically initialized, and do
not require deinitialization. This makes them ideal for implementing
PTHREAD_MUTEX_INITIALIZER.
We still need CRITICAL_SECTION for recursive mutexes.
Diffstat (limited to 'osdep/win32/pthread.c')
-rw-r--r-- | osdep/win32/pthread.c | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/osdep/win32/pthread.c b/osdep/win32/pthread.c index 4838df48b7..b215e46ba2 100644 --- a/osdep/win32/pthread.c +++ b/osdep/win32/pthread.c @@ -32,35 +32,40 @@ int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) int pthread_mutex_destroy(pthread_mutex_t *mutex) { - DeleteCriticalSection(&mutex->cs); + if (mutex->use_cs) + DeleteCriticalSection(&mutex->lock.cs); return 0; } int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr) { - InitializeCriticalSection(&mutex->cs); + mutex->use_cs = attr && (*attr & PTHREAD_MUTEX_RECURSIVE); + if (mutex->use_cs) { + InitializeCriticalSection(&mutex->lock.cs); + } else { + InitializeSRWLock(&mutex->lock.srw); + } return 0; } int pthread_mutex_lock(pthread_mutex_t *mutex) { - if (mutex->static_mutex) { - BOOL pending; - if (!InitOnceBeginInitialize(&mutex->static_init, 0, &pending, NULL)) - abort(); - if (pending) { - InitializeCriticalSection(&mutex->cs); - InitOnceComplete(&mutex->static_init, 0, NULL); - } + if (mutex->use_cs) { + EnterCriticalSection(&mutex->lock.cs); + } else { + AcquireSRWLockExclusive(&mutex->lock.srw); } - EnterCriticalSection(&mutex->cs); return 0; } int pthread_mutex_unlock(pthread_mutex_t *mutex) { - LeaveCriticalSection(&mutex->cs); + if (mutex->use_cs) { + LeaveCriticalSection(&mutex->lock.cs); + } else { + ReleaseSRWLockExclusive(&mutex->lock.srw); + } return 0; } @@ -68,7 +73,13 @@ static int cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, DWORD ms) { - return SleepConditionVariableCS(cond, &mutex->cs, ms) ? 0 : ETIMEDOUT; + BOOL res; + if (mutex->use_cs) { + res = SleepConditionVariableCS(cond, &mutex->lock.cs, ms); + } else { + res = SleepConditionVariableSRW(cond, &mutex->lock.srw, ms, 0); + } + return res ? 0 : ETIMEDOUT; } int pthread_cond_timedwait(pthread_cond_t *restrict cond, |