Try this for a spinlock. It's similar to something I coded when I ported
Postgresql a couple of years ago. (Have we been doing this for > 2 years
already?)
31 bit:
static inline int TAS(int *lock)
{
int _res;
__asm__ __volatile(" lghi 1,1\n"
" lg 2,%0\n"
"0: slgr 0,0\n"
" cs 0,1,0(2)\n"
" jnz 0b\n"
" lgr %1,0"
: "+m" (lock), "=d" (_res)
: : "0", "1", "2", "cc");
return(_res);
}
static inline void CLR(int *lock)
{
__asm__ __volatile(" slr 0,0\n"
" l 1,%0\n"
" st 0,0(1)\n"
: "=m" (lock) : : "0", "1");
return;
}
64-bit:
static inline int TAS(int *lock)
{
int _res;
__asm__ __volatile(" lghi 1,1\n"
" lg 2,%0\n"
"0: slgr 0,0\n"
" cs 0,1,0(2)\n"
" jnz 0b\n"
" lgr %1,0"
: "+m" (lock), "=d" (_res)
: : "0", "1", "2", "cc");
return(_res);
}
static inline void CLR(int *lock)
{
__asm__ __volatile(" slgr 0,0\n"
" lg 1,%0\n"
" st 0,0(1)\n"
: "=m" (lock) : : "0", "1");
return;
}