Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 1919F83DC06 for ; Sun, 18 Dec 2011 18:42:18 -0400 (AST) Received: from [213.9.93.70] (helo=mail.piware.de) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RcPQv-0001ud-Kc for pgsql-bugs@postgresql.org; Sun, 18 Dec 2011 22:42:19 +0000 Received: from localhost.localdomain (p549B5F97.dip.t-dialin.net [84.155.95.151]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Martin Pitt (ThinkPad)", Issuer "piware CA" (verified OK)) by mail.piware.de (Postfix) with ESMTPS id 70D741517A61 for ; Sun, 18 Dec 2011 23:41:29 +0100 (CET) Received: by localhost.localdomain (Postfix, from userid 1000) id EDE1F235C6; Sun, 18 Dec 2011 23:42:03 +0100 (CET) Date: Sun, 18 Dec 2011 23:42:03 +0100 From: Martin Pitt To: PostgreSQL Bugs Subject: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb] Message-ID: <20111218224203.GA2950@piware.de> Mail-Followup-To: PostgreSQL Bugs MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="uQr8t48UFsdbeI+V" Content-Disposition: inline In-Reply-To: <4EEB1C77.1000309@enterprisedb.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Host-Lookup-Failed: Reverse DNS lookup failed for 213.9.93.70 (failed) X-Pg-Spam-Score: -1.1 (-) X-Archive-Number: 201112/142 X-Sequence-Number: 31953 --uQr8t48UFsdbeI+V Content-Type: multipart/mixed; boundary="ZPt4rx8FFjLCG7dd" Content-Disposition: inline --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hello all, Heikki Linnakangas wrote: > An even better approach would be to have a configure test for > __sync_lock_test_and_set. A quick google search suggests that Intel > C Compiler version >=3D 11.0 also supports __sync_lock_test_and_set, > for example. Right, http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html explicitly refers to intel CC as well. > It probably makes sense to use it on any platform where it's > defined. Presumably an implementation provided by the compiler is > always going to be at least as good as any magic assembler > incantations we can come up with. I agree. How about a patch like this? It uses builtin atomics if available, and falls back to the custom implementations if not. Note that a simple AC_CHECK_FUNC(__sync_lock_test_and_set) does not work, so I used a slightly more complicated AC_TRY_LINK() which also verifies that __sync_lock_release() is available. The patch is against 9.1.2, but I suppose it also applies to trunk (except perhaps the autogenerated configure part, this needs an autoheader/autoconf run). Thanks for considering, Martin --=20 Martin Pitt | http://www.piware.de Ubuntu Developer (www.ubuntu.com) | Debian Developer (www.debian.org) --ZPt4rx8FFjLCG7dd Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="01-atomic-builtins.patch" Content-Transfer-Encoding: quoted-printable Description: Use gcc/intel cc builtin atomic operations for test-and-set, i= f available (http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.ht= ml) Author: Martin Pitt Index: postgresql-9.1-9.1.2/configure.in =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- postgresql-9.1-9.1.2.orig/configure.in 2011-12-01 22:47:20.000000000 +0= 100 +++ postgresql-9.1-9.1.2/configure.in 2011-12-18 23:30:47.940475128 +0100 @@ -1522,6 +1522,18 @@ AC_SUBST(LDAP_LIBS_FE) AC_SUBST(LDAP_LIBS_BE) =20 +# gcc and intel compiler provide builtin functions for atomic test-and-set +AC_MSG_CHECKING([whether the C compiler provides atomic builtins]) +AC_TRY_LINK([], [int lock =3D 0; __sync_lock_test_and_set(&lock, 1); __syn= c_lock_release(&lock);], + [have_cc_atomics=3D"yes"], + [have_cc_atomics=3D"no"] +) +if test "$have_cc_atomics" =3D yes; then + AC_MSG_RESULT(yes) + AC_DEFINE(HAVE_CC_ATOMICS, 1, [Define to 1 if compiler provides atomic b= uiltins]) +else + AC_MSG_RESULT(no) +fi =20 # This test makes sure that run tests work at all. Sometimes a shared # library is found by the linker, but the runtime linker can't find it. Index: postgresql-9.1-9.1.2/configure =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- postgresql-9.1-9.1.2.orig/configure 2011-12-01 22:47:20.000000000 +0100 +++ postgresql-9.1-9.1.2/configure 2011-12-18 23:31:00.304475596 +0100 @@ -23602,6 +23602,69 @@ =20 =20 =20 +# gcc and intel compiler provide builtin functions for atomic test-and-set +{ $as_echo "$as_me:$LINENO: checking whether the C compiler provides atomi= c builtins" >&5 +$as_echo_n "checking whether the C compiler provides atomic builtins... " = >&6; } +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 =3D 0; __sync_lock_test_and_set(&lock, 1); __sync_lock_release(&l= ock); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try=3D"$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=3D\$ac_try;; + *) ac_try_echo=3D$ac_try;; +esac +eval ac_try_echo=3D"\"\$as_me:$LINENO: $ac_try_echo\"" +$as_echo "$ac_try_echo") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=3D$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + $as_echo "$as_me:$LINENO: \$? =3D $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && { + test "$cross_compiling" =3D yes || + $as_test_x conftest$ac_exeext + }; then + have_cc_atomics=3D"yes" +else + $as_echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + have_cc_atomics=3D"no" + +fi + +rm -rf conftest.dSYM +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext +if test "$have_cc_atomics" =3D yes; then + { $as_echo "$as_me:$LINENO: result: yes" >&5 +$as_echo "yes" >&6; } + +cat >>confdefs.h <<\_ACEOF +#define HAVE_CC_ATOMICS 1 +_ACEOF + +else + { $as_echo "$as_me:$LINENO: result: no" >&5 +$as_echo "no" >&6; } +fi =20 # This test makes sure that run tests work at all. Sometimes a shared # library is found by the linker, but the runtime linker can't find it. Index: postgresql-9.1-9.1.2/src/include/pg_config.h.in =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- postgresql-9.1-9.1.2.orig/src/include/pg_config.h.in 2011-12-01 22:47:2= 0.000000000 +0100 +++ postgresql-9.1-9.1.2/src/include/pg_config.h.in 2011-12-18 23:30:34.752= 474625 +0100 @@ -87,6 +87,9 @@ /* Define to 1 if you have the `cbrt' function. */ #undef HAVE_CBRT =20 +/* Define to 1 if compiler provides atomic builtins */ +#undef HAVE_CC_ATOMICS + /* Define to 1 if you have the `class' function. */ #undef HAVE_CLASS =20 Index: postgresql-9.1-9.1.2/src/include/storage/s_lock.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- postgresql-9.1-9.1.2.orig/src/include/storage/s_lock.h 2011-12-01 22:47= :20.000000000 +0100 +++ postgresql-9.1-9.1.2/src/include/storage/s_lock.h 2011-12-18 23:30:35.2= 96474646 +0100 @@ -77,8 +77,26 @@ =20 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */ =20 +/************************************************************************* + * Use compiler provided atomic builtins if available (in particular, gcc = and + * Intel provide them for many platforms), as we are unlikely to come up w= ith + * better ones. + */ +#ifdef HAVE_CC_ATOMICS +#define HAS_TEST_AND_SET +typedef int slock_t; + +#define TAS(lock) tas(lock) +#define S_UNLOCK(lock) __sync_lock_release(lock) + +static __inline__ int +tas(volatile slock_t *lock) +{ + return __sync_lock_test_and_set (lock, 1); +} +#endif =20 -#if defined(__GNUC__) || defined(__INTEL_COMPILER) +#if !defined(HAS_TEST_AND_SET) && (defined(__GNUC__) || defined(__INTEL_CO= MPILER)) /************************************************************************* * All the gcc inlines * Gcc consistently defines the CPU as __cpu__. --ZPt4rx8FFjLCG7dd-- --uQr8t48UFsdbeI+V Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBCAAGBQJO7mw0AAoJEPmIJawmtHuf+/EP/jIBXhAhXYK9M3jwBxZCutHm QbPQyRkLIlCBFoyngAfZR0aXKTVadPLADcMlzO9X/fK9wPQgGotwtjjZIM9Nbnzj gxAv7TELbjp9n6pW7VxSpsRjVYzqbx/WovWtynJmr1LbgnGaNPvNoaJqEFvuGJgB 7vTChkJh53yh8eharAZMAOKLN40u8LX4o1DnCwW61f7wTTILxQjcekoh60PONT2B T1bUnjb4ujXlA0fkEFL5h18c9TPknRYI0/VpeHtyjKECXqnhU7jNcCAUAOsOJcRp onnntQ7Dsb+uiqBJ1OzzZ/W0VdsWwPaqVZx1GgJirJTpr48iCnt5IPrw9yJ6GvII OJrBzzca4rXuv9VdIk5wIlVRULvSBxvA6kNEvhKmHby9jpHanXuGo628WnM3EGNV +mOEPTEJfVEcz061YwWj7vBG9uAahSWoGHy70vdGABWvxTSEgu98SqiR0ZUjM4cf D15P5f2phqdjYxf6Lo5LY7hgj8NU9e38nbmG3+1MsWoKPh7s5IEzj9Bz+5VHXI/T A7crhY4lBzipv0hZ4UBLhzTDuv4lW+Yzd3zM6bOl0Ss/94ZSqS889nhBYA2Zns+g gAKyTv9Tnf5j++GzBobe/R8kwVNmHCwadocEOM0DKwzRw2Sou0775Ph9tl+uuXlY 57FyPtZqpEHsiAzmgBqb =YFp3 -----END PGP SIGNATURE----- --uQr8t48UFsdbeI+V--