[libdispatch-dev] lib dispatch worker threads may loop depending on compiler optimization

Paolo Bonzini bonzini at gnu.org
Thu Sep 8 22:09:17 PDT 2011


On Thu, Sep 8, 2011 at 18:16, Daniel A. Steffen <dsteffen at apple.com> wrote:
> The Lion libdispatch source already uses clang's __sync_swap() for dispatch_atomic_xchg() when available, c.f. src/shims/atomic.h
>
> #if __has_builtin(__sync_swap)
> #define dispatch_atomic_xchg(p, n) \
>                ((typeof(*(p)))__sync_swap((p), (n)))
> #else
> #define dispatch_atomic_xchg(p, n) \
>                ((typeof(*(p)))__sync_lock_test_and_set((p), (n)))
> #endif
>
> unless GCC 4.5.1 has something similar, switching dispatch_atomic_xchg() to an
> inline asm volatile("xchg") on intel when builiding with that compiler is the cleanest
> workaround IMO

I doubt it, __sync_lock_test_and_set is a full barrier on x86.
Compiler-wise it is always a full optimization barrier, the actual
semantics depend on the processor.

Paolo


More information about the libdispatch-dev mailing list