This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Optimize libc_lock_lock for MIPS XLP.


These two patches (libc part and ports part) optimize libc_lock_lock() macro that GLIBC uses for locking internally to take advantage of fetch_and_add instruction that is available as an extension on certain processors, e.g., MIPS-architecture XLP.

The libc_lock_lock macros implement boolean lock: 0 corresponds to unlocked state and non-zero corresponds to locked state.  It is, therefore, possible to use fetch_and_add semantics to acquire lock in libc_lock_lock.  For XLP this translates to a single LDADD instruction.  This optimization allows architectures that can perform fetch_and_add faster than compare_and_exchange, such situation is indicated by defining the new macro "lll_add_lock".

The unlocking counterpart doesn't require any change as it is already uses plain atomic_exchange operation, which, incidentally, also supported on XLP as a single instruction.

Tested on XLP with no regressions.  OK to apply once 2.16 branches off?

Thank you,

--
Maxim Kuvyrkov
Mentor Graphics


2012-06-15  Tom de Vries  <vries@codesourcery.com>
	    Maxim Kuvyrkov  <maxim@codesourcery.com>

	libc/
	* nptl/sysdeps/pthread/bits/libc-lockP.h (__libc_lock_lock): Use
	lll_add_lock when it is available.

	ports/
	* sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h (__lll_add_lock,)
	(lll_add_lock): Define.
---
 nptl/sysdeps/pthread/bits/libc-lockP.h |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/nptl/sysdeps/pthread/bits/libc-lockP.h b/nptl/sysdeps/pthread/bits/libc-lockP.h
index 0ebac91..58d8366 100644
--- a/nptl/sysdeps/pthread/bits/libc-lockP.h
+++ b/nptl/sysdeps/pthread/bits/libc-lockP.h
@@ -176,8 +176,14 @@ typedef pthread_key_t __libc_key_t;
 
 /* Lock the named lock variable.  */
 #if !defined NOT_IN_libc || defined IS_IN_libpthread
-# define __libc_lock_lock(NAME) \
+# if defined lll_add_lock
+/* lll_add_lock is faster, so use it when it's available.  */
+#  define __libc_lock_lock(NAME) \
+  ({ lll_add_lock (NAME, LLL_PRIVATE); 0; })
+# else
+#  define __libc_lock_lock(NAME) \
   ({ lll_lock (NAME, LLL_PRIVATE); 0; })
+# endif
 #else
 # define __libc_lock_lock(NAME) \
   __libc_maybe_call (__pthread_mutex_lock, (&(NAME)), 0)
-- 
1.7.4.1

---
 sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h |   23 ++++++++++++++++++++-
 1 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h b/sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h
index 88b601e..bbe9ea7 100644
--- a/sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h
+++ b/sysdeps/unix/sysv/linux/mips/nptl/lowlevellock.h
@@ -1,5 +1,4 @@
-/* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008,
-   2009 Free Software Foundation, Inc.
+/* Copyright (C) 2003-2012 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -172,6 +171,26 @@ extern int __lll_robust_lock_wait (int *futex, int private) attribute_hidden;
   }))
 #define lll_lock(futex, private) __lll_lock (&(futex), private)
 
+#if defined(_MIPS_ARCH_XLP)
+/* XLP has a dedicated exchange_and_add instruction, which is significantly
+   faster than ll/sc and doesn't require explicit syncs.
+   As atomic.h currently only supports a full-barrier atomic_exchange_and_add,
+   using a full-barrier operation instead of an acquire-barrier operation is
+   not beneficial for MIPS in general.
+   Limit this optimization to XLP for now.  */
+#define __lll_add_lock(futex, private)					      \
+  ((void) ({								      \
+    int *__futex = (futex);						      \
+    if (__builtin_expect (atomic_exchange_and_add (__futex, 1), 0))    \
+      {									      \
+	if (__builtin_constant_p (private) && (private) == LLL_PRIVATE)	      \
+	  __lll_lock_wait_private (__futex);				      \
+	else								      \
+	  __lll_lock_wait (__futex, private);				      \
+      }									      \
+  }))
+#define lll_add_lock(futex, private) __lll_add_lock (&(futex), private)
+#endif
 
 #define __lll_robust_lock(futex, id, private)				      \
   ({									      \
-- 
1.7.4.1



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]