From dd13412986e52671b1e19c6c7ddb81593b5d211f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sun, 15 Oct 2023 19:48:07 +0200 Subject: win32/pthread: add support for pthread_mutex_trylock --- osdep/win32/include/pthread.h | 2 ++ osdep/win32/pthread.c | 9 +++++++++ 2 files changed, 11 insertions(+) (limited to 'osdep/win32') diff --git a/osdep/win32/include/pthread.h b/osdep/win32/include/pthread.h index b955e722ae..71dc8084a4 100644 --- a/osdep/win32/include/pthread.h +++ b/osdep/win32/include/pthread.h @@ -33,6 +33,7 @@ #define pthread_mutex_destroy m_pthread_mutex_destroy #define pthread_mutex_init m_pthread_mutex_init #define pthread_mutex_lock m_pthread_mutex_lock +#define pthread_mutex_trylock m_pthread_mutex_trylock #define pthread_mutex_unlock m_pthread_mutex_unlock #define pthread_cond_timedwait m_pthread_cond_timedwait #define pthread_cond_wait m_pthread_cond_wait @@ -71,6 +72,7 @@ int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); int pthread_mutex_lock(pthread_mutex_t *mutex); +int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); #define pthread_cond_t CONDITION_VARIABLE diff --git a/osdep/win32/pthread.c b/osdep/win32/pthread.c index 9bc7f0057d..11a0c50476 100644 --- a/osdep/win32/pthread.c +++ b/osdep/win32/pthread.c @@ -66,6 +66,15 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) return 0; } +int pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + if (mutex->use_cs) { + return !TryEnterCriticalSection(&mutex->lock.cs); + } else { + return !TryAcquireSRWLockExclusive(&mutex->lock.srw); + } +} + int pthread_mutex_unlock(pthread_mutex_t *mutex) { if (mutex->use_cs) { -- cgit v1.2.3