atomic.h 668 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_feature(cxx_atomic)
  12. #define ATOMIC_SWAP(addr, val)\
  13. __atomic_exchange_n(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_feature(cxx_atomic)
  22. #define ATOMIC_LOAD(addr)\
  23. __atomic_load_n(addr, __ATOMIC_ACQUIRE)
  24. #else
  25. #define ATOMIC_LOAD(addr)\
  26. (__sync_synchronize(), *addr)
  27. #endif