[2/2] nptl: Simplifying condvar-internal mutex

Message ID 20221015195305.1322087-2-malteskarupke@fastmail.fm
State New
Headers
Series [1/2] nptl: Simplify condition variables to fix pthread_cond_signal (BZ 25847) |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Tang, Jun via Libc-alpha Oct. 15, 2022, 7:53 p.m. UTC
  From: Malte Skarupke <malteskarupke@fastmail.fm>

The condvar-internal mutex no longer shares its 32 bit futex with other
information. There is no reason to keep the complexity, so simplify.
---
 nptl/pthread_cond_common.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
  

Patch

diff --git a/nptl/pthread_cond_common.c b/nptl/pthread_cond_common.c
index b9043a218b..efc7999cfe 100644
--- a/nptl/pthread_cond_common.c
+++ b/nptl/pthread_cond_common.c
@@ -32,11 +32,11 @@ 
 static void __attribute__ ((unused))
 __condvar_acquire_lock (pthread_cond_t *cond, int private)
 {
-  unsigned int s = atomic_load_relaxed (&cond->__data.__lock);
-  while ((s & 3) == 0)
+  unsigned int *lock = &cond->__data.__lock;
+  unsigned int s = atomic_load_relaxed (lock);
+  while (s == 0)
     {
-      if (atomic_compare_exchange_weak_acquire (&cond->__data.__lock,
-	  &s, s | 1))
+      if (atomic_compare_exchange_weak_acquire (lock, &s, 1))
 	return;
       /* TODO Spinning and back-off.  */
     }
@@ -45,21 +45,19 @@  __condvar_acquire_lock (pthread_cond_t *cond, int private)
      from not acquired.  */
   while (1)
     {
-      while ((s & 3) != 2)
+      while (s != 2)
 	{
-	  if (atomic_compare_exchange_weak_acquire
-	      (&cond->__data.__lock, &s, (s & ~(unsigned int) 3) | 2))
+	  if (atomic_compare_exchange_weak_acquire (lock, &s, 2))
 	    {
-	      if ((s & 3) == 0)
+	      if (s == 0)
 		return;
 	      break;
 	    }
 	  /* TODO Back off.  */
 	}
-      futex_wait_simple (&cond->__data.__lock,
-	  (s & ~(unsigned int) 3) | 2, private);
+      futex_wait_simple (lock, 2, private);
       /* Reload so we see a recent value.  */
-      s = atomic_load_relaxed (&cond->__data.__lock);
+      s = atomic_load_relaxed (lock);
     }
 }
 
@@ -67,9 +65,7 @@  __condvar_acquire_lock (pthread_cond_t *cond, int private)
 static void __attribute__ ((unused))
 __condvar_release_lock (pthread_cond_t *cond, int private)
 {
-  if ((atomic_fetch_and_release (&cond->__data.__lock,
-				 ~(unsigned int) 3) & 3)
-      == 2)
+  if (atomic_exchange_release (&cond->__data.__lock, 0) == 2)
     futex_wake (&cond->__data.__lock, 1, private);
 }