Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Deal with PTHREAD_STATIC_INITIALIZER if it behave as in glibc
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sun, 16 Oct 2022 14:53:27 +0000 (16:53 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sun, 16 Oct 2022 14:53:27 +0000 (16:53 +0200)
src/sthread/sthread_impl.cpp

index 4ef0265..efcdcdd 100644 (file)
@@ -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<sg4::Mutex*>(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<sg4::Mutex*>(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<sg4::Mutex*>(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<sg4::Mutex*>(mutex->mutex));
   return 0;
 }