mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH 1/2] Let the signaler do all work concerning consistency of the list
@ 2014-08-27  9:56 Jens Gustedt
  0 siblings, 0 replies; only message in thread
From: Jens Gustedt @ 2014-08-27  9:56 UTC (permalink / raw)
  To: musl

---
 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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2014-08-27  9:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-27  9:56 [PATCH 1/2] Let the signaler do all work concerning consistency of the list Jens Gustedt

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).