public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@ens-lyon.org>
To: libc-alpha@sourceware.org
Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>, commit-hurd@gnu.org
Subject: [hurd, commited] htl: Make pthread*_cond_timedwait register wref before releasing mutex
Date: Mon, 22 Aug 2022 22:28:48 +0200	[thread overview]
Message-ID: <20220822202848.3742543-1-samuel.thibault@ens-lyon.org> (raw)

Otherwise another thread could be rightly trying to destroy the condition,
see e.g. tst-cond20.
---
 sysdeps/htl/pt-cond-timedwait.c                | 10 ++++++----
 sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c | 10 ++++++----
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/sysdeps/htl/pt-cond-timedwait.c b/sysdeps/htl/pt-cond-timedwait.c
index 6f4cb41bf1..4352e54fff 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 16f0d3b309..1677925755 100644
--- a/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c
+++ b/sysdeps/mach/hurd/htl/pt-hurd-cond-timedwait.c
@@ -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);
-- 
2.35.1


                 reply	other threads:[~2022-08-22 20:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220822202848.3742543-1-samuel.thibault@ens-lyon.org \
    --to=samuel.thibault@ens-lyon.org \
    --cc=commit-hurd@gnu.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).