From: GNU Libc Maintainers Date: Sat, 30 Sep 2023 08:31:05 +0000 (+0100) Subject: git-cond-destroy X-Git-Tag: archive/raspbian/2.36-9+rpi1+deb12u3^2~76 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=efb87f01282ad69801d7f1092f909bfba2e3b2f9;p=glibc.git git-cond-destroy commit 4565083abc972bffe810e4151f8f3cb48531e526 Author: Samuel Thibault Date: Mon Aug 22 22:27:24 2022 +0200 htl: Make pthread*_cond_timedwait register wref before releasing mutex Otherwise another thread could be rightly trying to destroy the condition, see e.g. tst-cond20. Gbp-Pq: Topic hurd-i386 Gbp-Pq: Name git-cond-destroy.diff --- diff --git a/sysdeps/htl/pt-cond-timedwait.c b/sysdeps/htl/pt-cond-timedwait.c index 6f4cb41bf..4352e54ff 100644 --- a/sysdeps/htl/pt-cond-timedwait.c +++ b/sysdeps/htl/pt-cond-timedwait.c @@ -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); diff --git a/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c b/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c index d81f0a2d8..cff85ef69 100644 --- a/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c +++ b/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c @@ -99,6 +99,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. */ @@ -111,10 +117,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);