Question

GCC is lacking Clang's builtin __sync_swap(). I have some code that requires it, and I'm trying to figure out the way to mimic this in GCC. The Clang docs allude to this not being as simple as a single __sync_* or __atomic_* operation.

How could __sync_swap() be mimmiced in GCC?

Was it helpful?

Solution

It so appears that __sync_swap is simply an old fashioned name for what can be achieved with more "up to date" built-ins. Lets consider a case in point (atomic macros as implemented by freebsd: http://code.metager.de/source/xref/freebsd/sys/sys/stdatomic.h):

#if defined(__CLANG_ATOMICS)
....
#define atomic_exchange_explicit(object, desired, order) \
    __c11_atomic_exchange(object, desired, order)
....
#elif defined(__GNUC_ATOMICS)
....
#define atomic_exchange_explicit(object, desired, order) \
    __atomic_exchange_n(&(object)->__val, desired, order)
....
#else
....
#if __has_builtin(__sync_swap)
/* Clang provides a full-barrier atomic exchange - use it if available. */
#define atomic_exchange_explicit(object, desired, order) \
    ((void)(order), __sync_swap(&(object)->__val, desired))
....

It's fairly clear from the example that freebsd devs consider the newer clang's __c11_atomic_exchange, gcc's __atomic_exchange_n and older __sync_swap (it at all available) to have identical semantics. The later is only used as last resort option, in case newer built-ins are not available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top