Hi,

I've managed to speak to someone knowledgeable at Digital in the UK
who pointed me in the direction of a very interesting include file for
Digital C/C++, namely /usr/include/alpha/builtins.h.

It contains a series of function prototypes which are then converted
into fast assembler sequences by the compiler.  In particular a number
of these seem highly suited for the task of rewriting the alpha
spinlock code avoiding IPC semaphores.

Amongst the many functions I believe the most relevant are, for the
TAS() macro in s_lock.h:

/*
**  Interlocked "test for bit set and then set".  Returns non-zero
**  if bit was already set.
*/
int __INTERLOCKED_TESTBITSS_QUAD(volatile void *__address, int __bit_position);
int __INTERLOCKED_TESTBITSS_QUAD_RETRY(volatile void *__address,
				       int __bit_position,
				       int __retry,
				       int *__status);

Note that this call does _not_ generate a memory barrier.  For the
others, i.e. S_LOCK and S_UNLOCK perhaps the following might help:

/*
**  Acquire/release binary spinlock based on low-order bit of a longword.
**  NOTE: Memory barrier generated after lock, before unlock.
**  _RETRY variant returns non-zero on success within retry attempts.
*/
void __LOCK_LONG(volatile void *__address);
int  __LOCK_LONG_RETRY(volatile void *__address, int __retry);
void __UNLOCK_LONG(volatile void *__address);

There are also counting semaphores if need be (all in the same file).
If we change s_lock from msemaphore to long then the following patch
compiles and is being tested by Adriaan Joubert as we speak.  It
probably crashes & burns but at least we can see if we get anywhere.
My personal opinion is that it might be the way to go, I haven't
looked carefully at S_LOCK etc. but will do so once Adriaan has
crashed the copy of Postgres currently being compiled.

===File ~/src/hacks/s_lock.diff=====================
--- s_lock.h.orig	Wed Jun 14 15:33:28 2000
+++ s_lock.h	Wed Jun 14 16:11:29 2000
@@ -252,10 +252,18 @@
  * Note that slock_t on the Alpha AXP is msemaphore instead of char
  * (see storage/ipc.h).
  */
-#define TAS(lock)	  (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
-#define S_UNLOCK(lock) msem_unlock((lock), 0)
-#define S_INIT_LOCK(lock)		msem_init((lock), MSEM_UNLOCKED)
-#define S_LOCK_FREE(lock)	  (!(lock)->msem_state)
+#if 0
+/* Original hack */
+# define TAS(lock)	  (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
+# define S_UNLOCK(lock) msem_unlock((lock), 0)
+# define S_INIT_LOCK(lock)		msem_init((lock), MSEM_UNLOCKED)
+# define S_LOCK_FREE(lock)	  (!(lock)->msem_state)
+#else
+/* Arrigo's hack */
+# include <alpha/builtins.h>
+# define TAS(lock)        (__INTERLOCKED_TESTBITSS_QUAD(lock,0))
+#endif
+
 
 #else /* i.e. not __osf__ */
 
============================================================

Ciao,

Arrigo

P.S.    Yes, I don't really know what I am doing but trying my best to
        learn ;-)

Wed, 14 Jun 2000 16:22:04 +0100 (BST)
Arrigo Triulzi <arrigo@albourne.com>
OSF/1/Digital UNIX/Tru64 UNIX spinlock code
Can I ask where this was left?

> Hi,
> 
> I've managed to speak to someone knowledgeable at Digital in the UK
> who pointed me in the direction of a very interesting include file for
> Digital C/C++, namely /usr/include/alpha/builtins.h.
> 
> It contains a series of function prototypes which are then converted
> into fast assembler sequences by the compiler.  In particular a number
> of these seem highly suited for the task of rewriting the alpha
> spinlock code avoiding IPC semaphores.
> 
> Amongst the many functions I believe the most relevant are, for the
> TAS() macro in s_lock.h:
> 
> /*
> **  Interlocked "test for bit set and then set".  Returns non-zero
> **  if bit was already set.
> */
> int __INTERLOCKED_TESTBITSS_QUAD(volatile void *__address, int __bit_position);
> int __INTERLOCKED_TESTBITSS_QUAD_RETRY(volatile void *__address,
> 				       int __bit_position,
> 				       int __retry,
> 				       int *__status);
> 
> Note that this call does _not_ generate a memory barrier.  For the
> others, i.e. S_LOCK and S_UNLOCK perhaps the following might help:
> 
> /*
> **  Acquire/release binary spinlock based on low-order bit of a longword.
> **  NOTE: Memory barrier generated after lock, before unlock.
> **  _RETRY variant returns non-zero on success within retry attempts.
> */
> void __LOCK_LONG(volatile void *__address);
> int  __LOCK_LONG_RETRY(volatile void *__address, int __retry);
> void __UNLOCK_LONG(volatile void *__address);
> 
> There are also counting semaphores if need be (all in the same file).
> If we change s_lock from msemaphore to long then the following patch
> compiles and is being tested by Adriaan Joubert as we speak.  It
> probably crashes & burns but at least we can see if we get anywhere.
> My personal opinion is that it might be the way to go, I haven't
> looked carefully at S_LOCK etc. but will do so once Adriaan has
> crashed the copy of Postgres currently being compiled.
> 
> ===File ~/src/hacks/s_lock.diff=====================
> --- s_lock.h.orig	Wed Jun 14 15:33:28 2000
> +++ s_lock.h	Wed Jun 14 16:11:29 2000
> @@ -252,10 +252,18 @@
>   * Note that slock_t on the Alpha AXP is msemaphore instead of char
>   * (see storage/ipc.h).
>   */
> -#define TAS(lock)	  (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
> -#define S_UNLOCK(lock) msem_unlock((lock), 0)
> -#define S_INIT_LOCK(lock)		msem_init((lock), MSEM_UNLOCKED)
> -#define S_LOCK_FREE(lock)	  (!(lock)->msem_state)
> +#if 0
> +/* Original hack */
> +# define TAS(lock)	  (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
> +# define S_UNLOCK(lock) msem_unlock((lock), 0)
> +# define S_INIT_LOCK(lock)		msem_init((lock), MSEM_UNLOCKED)
> +# define S_LOCK_FREE(lock)	  (!(lock)->msem_state)
> +#else
> +/* Arrigo's hack */
> +# include <alpha/builtins.h>
> +# define TAS(lock)        (__INTERLOCKED_TESTBITSS_QUAD(lock,0))
> +#endif
> +
>  
>  #else /* i.e. not __osf__ */
>  
> ============================================================
> 
> Ciao,
> 
> Arrigo
> 
> P.S.    Yes, I don't really know what I am doing but trying my best to
>         learn ;-)
> 


-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Mon, 9 Oct 2000 16:46:14 -0400 (EDT)
Bruce Momjian <pgman@candle.pha.pa.us>
Re: OSF/1/Digital UNIX/Tru64 UNIX spinlock code