mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Jens Gustedt <Jens.Gustedt@inria.fr>
To: musl@lists.openwall.com
Subject: [PATCH 1/2] Let the signaler do all work concerning consistency of the list
Date: Wed, 27 Aug 2014 11:56:22 +0200	[thread overview]
Message-ID: <1409133300.4476.29.camel@eris.loria.fr> (raw)

---
 src/thread/pthread_cond_timedwait.c |   31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c
index 2d192b0..52b1026 100644
--- a/src/thread/pthread_cond_timedwait.c
+++ b/src/thread/pthread_cond_timedwait.c
@@ -84,18 +84,26 @@ static void unwait(void *arg)
 		 * via the futex notify below. */
 
 		pthread_cond_t *c = node->cond;
+		int * ref;
+
 		lock(&c->_c_lock);
-		
-		if (c->_c_head == node) c->_c_head = node->next;
-		else if (node->prev) node->prev->next = node->next;
-		if (c->_c_tail == node) c->_c_tail = node->prev;
-		else if (node->next) node->next->prev = node->prev;
-		
+		/* Our membership to the list may have changed while waiting for the lock. */
+		/* Ensure that the notify field is only read while we hold the lock. */
+		ref = node->notify;
+
+		/* If there has been no race with a signaler, splice us out of the list. */
+		/* Otherwise, the signaler has already taken care of it. */
+		if (!ref) {
+			if (c->_c_head == node) c->_c_head = node->next;
+			else if (node->prev) node->prev->next = node->next;
+			if (c->_c_tail == node) c->_c_tail = node->prev;
+			else if (node->next) node->next->prev = node->prev;
+		}
 		unlock(&c->_c_lock);
 
-		if (node->notify) {
-			if (a_fetch_add(node->notify, -1)==1)
-				__wake(node->notify, 1, 1);
+		if (ref) {
+			if (a_fetch_add(ref, -1)==1)
+				__wake(ref, 1, 1);
 		}
 	} else {
 		/* Lock barrier first to control wake order. */
@@ -172,6 +180,11 @@ int __private_cond_signal(pthread_cond_t *c, int n)
 		if (a_cas(&p->state, WAITING, SIGNALED) != WAITING) {
 			ref++;
 			p->notify = &ref;
+			/* Let the signaler do all work concerning consistency of the list. */
+			if (c->_c_head == p) c->_c_head = p->next;
+			else if (p->prev) p->prev->next = p->next;
+			if (c->_c_tail == p) c->_c_tail = p->prev;
+			else if (p->next) p->next->prev = p->prev;
 		} else {
 			n--;
 			if (!first) first=p;
-- 
1.7.10.4



                 reply	other threads:[~2014-08-27  9:56 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=1409133300.4476.29.camel@eris.loria.fr \
    --to=jens.gustedt@inria.fr \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).