I’ve found in internet the implementation of __sync_val_compare_and_swap:
#define LOCK_PREFIX "lock ; "
struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))
static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
unsigned long new, int size)
{
unsigned long prev;
switch (size) {
case 1:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 2:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 4:
__asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
}
return old;
}
#define cmpxchg(ptr,o,n)\
((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\
(unsigned long)(n),sizeof(*(ptr))))
When I compile and use this function (cmpxchg) for i386 architecture – all good! But, when i compile under Sparc architecture, i’ve the following error:
error: impossible constraint in `asm'
What’s the problem?
On Solaris, better don’t write your own code for this (neither on SPARC nor on x86); rather, use the
atomic_cas(3C)functions for the purpose:That’ll do for Solaris.
Edit: if you absolutely have to inline this kind of thing, the SPARC (v8+, aka UltraSPARC) instruction to use is “compare and swap”, aka
CAS. It’s always atomic (sparc doesn’t know lock prefixes). It only comes in 32bit and 64bit (CASX) variants, so that the 8/16bit library functions perform 32bitCASmasking out the non-targeted word/bytes. I won’t help with reimplementing that – it’s not a good idea, use the library interfaces.Edit2: Some help with reimplementing you get by reading the sourcecode (if you cannot link with Solaris libc).