]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
htl: Make pthread*_cond_timedwait register wref before releasing mutex
authorSamuel Thibault <samuel.thibault@ens-lyon.org>
Mon, 22 Aug 2022 20:27:24 +0000 (22:27 +0200)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Mon, 22 Aug 2022 20:27:24 +0000 (22:27 +0200)
Otherwise another thread could be rightly trying to destroy the condition,
see e.g. tst-cond20.

sysdeps/htl/pt-cond-timedwait.c
sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c

index 6f4cb41bf1458f2bb6608a56352833fe60807d0c..4352e54fff91463129031acc4773ad5ac6d00e7f 100644 (file)
@@ -142,13 +142,15 @@ __pthread_cond_timedwait_internal (pthread_cond_t *cond,
 
   __pthread_mutex_unlock (&self->cancel_lock);
 
-  /* Release MUTEX before blocking.  */
-  __pthread_mutex_unlock (mutex);
-
   /* Increase the waiter reference count.  Relaxed MO is sufficient because
-     we only need to synchronize when decrementing the reference count.  */
+     we only need to synchronize when decrementing the reference count.
+     We however need to have the mutex held to prevent concurrency with
+     a pthread_cond_destroy.  */
   atomic_fetch_add_relaxed (&cond->__wrefs, 2);
 
+  /* Release MUTEX before blocking.  */
+  __pthread_mutex_unlock (mutex);
+
   /* Block the thread.  */
   if (abstime != NULL)
     err = __pthread_timedblock (self, abstime, clock_id);
index 16f0d3b3099260b42e497f1fa312f9196c20d8ca..167792575559b545e2331a9bf2ec036cb83a3720 100644 (file)
@@ -103,6 +103,12 @@ __pthread_hurd_cond_timedwait_internal (pthread_cond_t *cond,
   __pthread_spin_unlock (&cond->__lock);
   __spin_unlock (&ss->lock);
 
+  /* Increase the waiter reference count.  Relaxed MO is sufficient because
+     we only need to synchronize when decrementing the reference count.
+     We however need to have the mutex held to prevent concurrency with
+     a pthread_cond_destroy.  */
+  atomic_fetch_add_relaxed (&cond->__wrefs, 2);
+
   if (cancel)
     {
       /* Cancelled on entry.  Just leave the mutex locked.  */
@@ -115,10 +121,6 @@ __pthread_hurd_cond_timedwait_internal (pthread_cond_t *cond,
       /* Release MUTEX before blocking.  */
       __pthread_mutex_unlock (mutex);
 
-  /* Increase the waiter reference count.  Relaxed MO is sufficient because
-     we only need to synchronize when decrementing the reference count.  */
-  atomic_fetch_add_relaxed (&cond->__wrefs, 2);
-
       /* Block the thread.  */
       if (abstime != NULL)
        err = __pthread_timedblock (self, abstime, clock_id);