Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id D617416B0CB2 for ; Mon, 19 Dec 2011 17:25:28 -0400 (AST) Received: from exprod7og120.obsmtp.com ([64.18.2.18]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rcki4-00066L-GC for pgsql-bugs@postgresql.org; Mon, 19 Dec 2011 21:25:28 +0000 Received: from mail-ee0-f42.google.com ([74.125.83.42]) (using TLSv1) by exprod7ob120.postini.com ([64.18.6.12]) with SMTP ID DSNKTu+rtju9oFnFONBjEvMgTkt/JQZz8m9/@postini.com; Mon, 19 Dec 2011 13:25:24 PST Received: by eeke51 with SMTP id e51so9524046eek.1 for ; Mon, 19 Dec 2011 13:25:08 -0800 (PST) Received: by 10.14.50.204 with SMTP id z52mr4859522eeb.92.1324329908616; Mon, 19 Dec 2011 13:25:08 -0800 (PST) Received: from [192.168.1.101] (dsl-hkibrasgw2-ff6cc300-200.dhcp.inet.fi. [88.195.108.200]) by mx.google.com with ESMTPS id t1sm49564011eeb.3.2011.12.19.13.25.07 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 19 Dec 2011 13:25:07 -0800 (PST) Message-ID: <4EEFABB2.5050100@enterprisedb.com> Date: Mon, 19 Dec 2011 23:25:06 +0200 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16 MIME-Version: 1.0 To: Tom Lane CC: Noah Misch , Robert Haas , PostgreSQL Bugs Subject: Re: Re: [PATCH] Use CC atomic builtins if available [was: Re: TAS patch for building on armel/armhf thumb] References: <4EEB1C77.1000309@enterprisedb.com> <20111218224203.GA2950@piware.de> <4EEF5397.8090200@enterprisedb.com> <20111219200341.GB9495@tornado.leadboat.com> <14852.1324325578@sss.pgh.pa.us> In-Reply-To: <14852.1324325578@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------080702080608060306060500" X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201112/164 X-Sequence-Number: 31975 This is a multi-part message in MIME format. --------------080702080608060306060500 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit On 19.12.2011 22:12, Tom Lane wrote: > Noah Misch writes: >> On Mon, Dec 19, 2011 at 05:09:11PM +0200, Heikki Linnakangas wrote: >>> That is not sufficient on platforms with a weak memory model, like Itanium. > >> Other processors may observe the lock as held after its release, but there's no >> correctness problem. > > How weak is the memory model, exactly? > > A correctness problem would ensue if out-of-order stores are possible, > ie other processors could observe the lock being freed (and then acquire > it) before seeing changes to shared variables that had been made while > holding the lock. > > I'm a little dubious that this applies to Itanium, because I don't see > any memory fence instruction in the TAS macro. If we needed one in > S_UNLOCK I'd rather expect there to be one in TAS as well. There's a pretty good manual called "Implementing Spinlocks on Itanium and PA-RISC" from HP at: http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf. It says, in section "3.2.1 Try to get a spinlock": > Note also, that the ‘xchg’ instruction always > has the ‘acquire’ semantics, so it enforces the correct memory ordering. But the current implementation seems to be safe, anyway. In section "3.2.3 Freeing a spinlock", that manual says: > Note, that a simple assignment statement into a volatile variable will not work, as we are > assuming that the +Ovolatile=__unordered compile option is being used. So +Ovolatile=__unordered would allow the kind of optimization that I thought was possible, and we would have a problem if we used it. But the default is more conservative, and a simple assignment does work. I compiled the attached test program on an HP Itanium box, using the same flags you get from PostgreSQL's configure on that box. The relevant assembler output is: xchg4 r14 = [r15], r14 // M [slocktest.c: 66/3] //file/line/col slocktest.c/67/3 ld1.acq r16 = [r11] // M [slocktest.c: 67/3] nop.i 0 // I //file/line/col slocktest.c/68/3 st1.rel [r11] = r10 ;; // M [slocktest.c: 68/3] //file/line/col slocktest.c/69/3 st4.rel [r15] = r0 // M [slocktest.c: 69/3] //file/line/col slocktest.c/70/1 The trick I missed is that the compiler attaches .rel to all the stores and .acq to all the loads through a volatile pointer. gcc seems to do the same. So we're safe. It would be interesting to test whether using +Ovolatile=__unordered would make a difference to performance. Tacking those memory fence modifiers to all the volatile loads and stores might be expensive. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------080702080608060306060500 Content-Type: text/x-csrc; name="slocktest.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="slocktest.c" #include #if defined(__GNUC__) || defined(__INTEL_COMPILER) #if defined(__ia64__) || defined(__ia64) /* Intel Itanium */ #define HAS_TEST_AND_SET typedef unsigned int slock_t; #define TAS(lock) tas(lock) /* On IA64, it's a win to use a non-locking test before the xchg proper */ #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock)) #ifndef __INTEL_COMPILER static __inline__ int tas(volatile slock_t *lock) { long int ret; __asm__ __volatile__( " xchg4 %0=%1,%2 \n" : "=r"(ret), "+m"(*lock) : "r"(1) : "memory"); return (int) ret; } #else /* __INTEL_COMPILER */ static __inline__ int tas(volatile slock_t *lock) { int ret; ret = _InterlockedExchange(lock,1); /* this is a xchg asm macro */ return ret; } #endif /* __INTEL_COMPILER */ #endif /* __ia64__ || __ia64 */ #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */ #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__) #define HAS_TEST_AND_SET typedef unsigned int slock_t; #include #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE) /* On IA64, it's a win to use a non-locking test before the xchg proper */ #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock)) #endif /* HPUX on IA64, non gcc */ slock_t lock; char shared2; int main(int argc, char **argv) { volatile char *p = &shared2; char local; TAS(&lock); local = *p; *p = 123; *((volatile slock_t *) &lock) = 0; } --------------080702080608060306060500--