]> granicus.if.org Git - postgresql/commitdiff
Use __sync_lock_test_and_set() for spinlocks on ARM, if available.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 7 Jan 2012 20:39:11 +0000 (15:39 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 7 Jan 2012 20:39:11 +0000 (15:39 -0500)
Historically we've used the SWPB instruction for TAS() on ARM, but this
is deprecated and not available on ARMv6 and later.  Instead, make use
of a GCC builtin if available.  We'll still fall back to SWPB if not,
so as not to break existing ports using older GCC versions.

Eventually we might want to try using __sync_lock_test_and_set() on some
other architectures too, but for now that seems to present only risk and
not reward.

Back-patch to all supported versions, since people might want to use any
of them on more recent ARM chips.

Martin Pitt

configure
configure.in
src/include/pg_config.h.in
src/include/storage/s_lock.h

index 644329a5e2887b5f88215e19c8443c837fea51b9..d5ed340e978be149a1f47ecfa91b20069bde41b6 100755 (executable)
--- a/configure
+++ b/configure
 done
 
 
+{ echo "$as_me:$LINENO: checking for builtin locking functions" >&5
+echo $ECHO_N "checking for builtin locking functions... $ECHO_C" >&6; }
+if test "${pgac_cv_gcc_int_atomics+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+
+int
+main ()
+{
+int lock = 0;
+   __sync_lock_test_and_set(&lock, 1);
+   __sync_lock_release(&lock);
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+        test -z "$ac_c_werror_flag" ||
+        test ! -s conftest.err
+       } && test -s conftest$ac_exeext &&
+       $as_test_x conftest$ac_exeext; then
+  pgac_cv_gcc_int_atomics="yes"
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+       pgac_cv_gcc_int_atomics="no"
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
+      conftest$ac_exeext conftest.$ac_ext
+fi
+{ echo "$as_me:$LINENO: result: $pgac_cv_gcc_int_atomics" >&5
+echo "${ECHO_T}$pgac_cv_gcc_int_atomics" >&6; }
+if test x"$pgac_cv_gcc_int_atomics" = x"yes"; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_GCC_INT_ATOMICS 1
+_ACEOF
+
+fi
+
 
 #
 # Pthreads
index e9df8f4f0d689d9cec04fb22c52c473c5b4d1ed5..73d248b71a9887ae996bf3c005d89d79276a5c74 100644 (file)
@@ -1384,6 +1384,17 @@ AC_CHECK_FUNCS(atexit, [],
                [AC_CHECK_FUNCS(on_exit, [],
                [AC_MSG_ERROR([neither atexit() nor on_exit() found])])])
 
+AC_CACHE_CHECK([for builtin locking functions], pgac_cv_gcc_int_atomics,
+[AC_TRY_LINK([],
+  [int lock = 0;
+   __sync_lock_test_and_set(&lock, 1);
+   __sync_lock_release(&lock);],
+  [pgac_cv_gcc_int_atomics="yes"],
+  [pgac_cv_gcc_int_atomics="no"])])
+if test x"$pgac_cv_gcc_int_atomics" = x"yes"; then
+  AC_DEFINE(HAVE_GCC_INT_ATOMICS, 1, [Define to 1 if you have __sync_lock_test_and_set(int *) and friends.])
+fi
+
 
 #
 # Pthreads
index 4fd7b4818eda63276d4d838caecd04887929a381..042451e690901d19dfc026160ae1af762f566458 100644 (file)
 /* Define to 1 if your compiler understands __FUNCTION__. */
 #undef HAVE_FUNCNAME__FUNCTION
 
+/* Define to 1 if you have __sync_lock_test_and_set(int *) and friends. */
+#undef HAVE_GCC_INT_ATOMICS
+
 /* Define to 1 if you have the `getaddrinfo' function. */
 #undef HAVE_GETADDRINFO
 
index 534543eed31447d2fcf2b4ab37aeac4ff7d2798e..97ccdbebf6e3d62b68361d479c3b118b8d57ab4c 100644 (file)
@@ -252,13 +252,33 @@ tas(volatile slock_t *lock)
 #endif  /* __ia64__ || __ia64 */
 
 
+/*
+ * On ARM, we use __sync_lock_test_and_set(int *, int) if available, and if
+ * not fall back on the SWPB instruction.  SWPB does not work on ARMv6 or
+ * later, so the compiler builtin is preferred if available.  Note also that
+ * the int-width variant of the builtin works on more chips than other widths.
+ */
 #if defined(__arm__) || defined(__arm)
 #define HAS_TEST_AND_SET
 
-typedef unsigned char slock_t;
-
 #define TAS(lock) tas(lock)
 
+#ifdef HAVE_GCC_INT_ATOMICS
+
+typedef int slock_t;
+
+static __inline__ int
+tas(volatile slock_t *lock)
+{
+       return __sync_lock_test_and_set(lock, 1);
+}
+
+#define S_UNLOCK(lock) __sync_lock_release(lock)
+
+#else /* !HAVE_GCC_INT_ATOMICS */
+
+typedef unsigned char slock_t;
+
 static __inline__ int
 tas(volatile slock_t *lock)
 {
@@ -272,6 +292,7 @@ tas(volatile slock_t *lock)
        return (int) _res;
 }
 
+#endif  /* HAVE_GCC_INT_ATOMICS */
 #endif  /* __arm__ */