From 1fbb36afadc7cb35c5f80bc3ee3bb817aeab303e Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sun, 16 Oct 2022 16:53:27 +0200 Subject: [PATCH] Deal with PTHREAD_STATIC_INITIALIZER if it behave as in glibc --- src/sthread/sthread_impl.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index 4ef0265325..efcdcdda83 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -109,22 +109,38 @@ int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t int sthread_mutex_lock(sthread_mutex_t* mutex) { + /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */ + if (mutex->mutex == nullptr) + sthread_mutex_init(mutex, nullptr); + static_cast(mutex->mutex)->lock(); return 0; } int sthread_mutex_trylock(sthread_mutex_t* mutex) { + /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */ + if (mutex->mutex == nullptr) + sthread_mutex_init(mutex, nullptr); + return static_cast(mutex->mutex)->try_lock(); } int sthread_mutex_unlock(sthread_mutex_t* mutex) { + /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */ + if (mutex->mutex == nullptr) + sthread_mutex_init(mutex, nullptr); + static_cast(mutex->mutex)->unlock(); return 0; } int sthread_mutex_destroy(sthread_mutex_t* mutex) { + /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */ + if (mutex->mutex == nullptr) + sthread_mutex_init(mutex, nullptr); + intrusive_ptr_release(static_cast(mutex->mutex)); return 0; } -- 2.20.1