190885
From 2fa2ea233bec906b682fc82376649a1a6e18e9df Mon Sep 17 00:00:00 2001
190885
From: "H.J. Lu" <hjl.tools@gmail.com>
190885
Date: Tue, 2 Nov 2021 18:33:07 -0700
190885
Subject: [PATCH] Add LLL_MUTEX_READ_LOCK [BZ #28537]
190885
190885
CAS instruction is expensive.  From the x86 CPU's point of view, getting
190885
a cache line for writing is more expensive than reading.  See Appendix
190885
A.2 Spinlock in:
190885
190885
https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf
190885
190885
The full compare and swap will grab the cache line exclusive and cause
190885
excessive cache line bouncing.
190885
190885
Add LLL_MUTEX_READ_LOCK to do an atomic load and skip CAS in spinlock
190885
loop if compare may fail to reduce cache line bouncing on contended locks.
190885
190885
Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
190885
(cherry picked from commit d672a98a1af106bd68deb15576710cd61363f7a6)
190885
---
190885
 nptl/pthread_mutex_lock.c | 7 +++++++
190885
 1 file changed, 7 insertions(+)
190885
190885
diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
190885
index 60ada70d..eb4d8baa 100644
190885
--- a/nptl/pthread_mutex_lock.c
190885
+++ b/nptl/pthread_mutex_lock.c
190885
@@ -56,6 +56,11 @@
190885
 #define FORCE_ELISION(m, s)
190885
 #endif
190885
 
190885
+#ifndef LLL_MUTEX_READ_LOCK
190885
+# define LLL_MUTEX_READ_LOCK(mutex) \
190885
+  atomic_load_relaxed (&(mutex)->__data.__lock)
190885
+#endif
190885
+
190885
 static int __pthread_mutex_lock_full (pthread_mutex_t *mutex)
190885
      __attribute_noinline__;
190885
 
190885
@@ -136,6 +141,8 @@ __pthread_mutex_lock (pthread_mutex_t *mutex)
190885
 		  break;
190885
 		}
190885
 	      atomic_spin_nop ();
190885
+	      if (LLL_MUTEX_READ_LOCK (mutex) != 0)
190885
+		continue;
190885
 	    }
190885
 	  while (LLL_MUTEX_TRYLOCK (mutex) != 0);
190885
 
190885
-- 
190885
GitLab
190885