atomic.h 782 B

12345678910111213141516171819202122232425262728
  1. #ifndef __has_builtin
  2. #define __has_builtin(x) 0
  3. #endif
  4. #ifndef __has_feature
  5. #define __has_feature(x) 0
  6. #endif
  7. /**
  8. * Swap macro that enforces a happens-before relationship with a corresponding
  9. * ATOMIC_LOAD.
  10. */
  11. #if __has_builtin(__c11_atomic_exchange)
  12. #define ATOMIC_SWAP(addr, val)\
  13. __c11_atomic_exchange(reinterpret_cast<_Atomic(__typeof__(val))*>(addr), val, __ATOMIC_ACQ_REL)
  14. #elif __has_builtin(__sync_swap)
  15. #define ATOMIC_SWAP(addr, val)\
  16. __sync_swap(addr, val)
  17. #else
  18. #define ATOMIC_SWAP(addr, val)\
  19. __sync_lock_test_and_set(addr, val)
  20. #endif
  21. #if __has_builtin(__c11_atomic_load)
  22. #define ATOMIC_LOAD(addr)\
  23. __c11_atomic_load(reinterpret_cast<_Atomic(__typeof__(*addr))*>(addr), __ATOMIC_ACQUIRE)
  24. #else
  25. #define ATOMIC_LOAD(addr)\
  26. (__sync_synchronize(), *addr)
  27. #endif