- Code: Select all
//Adapted from Crafty's "lock.h"
typedef volatile unsigned long SpinLock[1];
typedef volatile unsigned long* SpinLock_P;
#define InitSpinLock(s) ((s)[0] = 0)
#define ReleaseSL(s) ((s)[0] = 0)
#if defined(_MSC_VER)
#pragma intrinsic (_InterlockedExchange)
static __forceinline void LockSL(SpinLock_P s)
{
long v;
for(;;)
{
v = _InterlockedExchange(s, 1);
if(!v) return;
while(*s);
}
}
#define TryLockSL(s) (!_InterlockedExchange(s, 1))
#endif
How would I modify the GCC Assembly code to do a TryLock?
- Code: Select all
static void __inline__ LockX86(volatile int *lock)
{
int dummy;
asm __volatile__("1: movl $1, %0" "\n\t"
" xchgl (%1), %0" "\n\t" " testl %0, %0" "\n\t"
" jz 3f" "\n\t" "2: pause" "\n\t"
" movl (%1), %0" "\n\t" " testl %0, %0" "\n\t"
" jnz 2b" "\n\t" " jmp 1b" "\n\t" "3:"
"\n\t":"=&q"(dummy)
:"q"(lock)
:"cc");
}
Also a quick question, the releasing code can be written as *lock=0 right?
- Code: Select all
static void __inline__ UnlockX86(volatile int *lock)
{
int dummy;
asm __volatile__("movl $0, (%1)":"=&q"(dummy)
:"q"(lock));
}
Also GCC assembly routines for _InterlockedIncrement and _InterlockedDecrement operating on 32-bit signed integers would be appreciated.
GCC doesn't seem to have intrinsics as far as google searching goes, is this correct?