util.h 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. /* util.h */
  2. #ifndef UTIL_H
  3. #define UTIL_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #if (defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ > 4 || __GNUC_MINOR__ >= 1) \
  8. && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) \
  9. || (defined(__INTEL_COMPILER) && !defined(_WIN32))
  10. /* atomic operations are defined by ICC and GCC >= 4.1, but by the later one supposedly not for ARM */
  11. /* note: ICC on ia64 platform possibly require ia64intrin.h, need testing */
  12. # define atomic_compare_and_swap(ptr, oldval, newval) __sync_val_compare_and_swap(ptr, oldval, newval)
  13. #elif defined(_MSC_VER)
  14. # include <windows.h>
  15. # define atomic_compare_and_swap(ptr, oldval, newval) InterlockedCompareExchange(ptr, newval, oldval)
  16. #elif defined(__sun)
  17. # include <atomic.h>
  18. # define atomic_compare_and_swap(ptr, oldval, newval) atomic_cas_32(ptr, oldval, newval)
  19. #else
  20. /* pray that it will work */
  21. # define atomic_compare_and_swap(ptr, oldval, newval) { if(*(ptr) == (oldval)) *(ptr) = (newval); }
  22. # define NO_ATOMIC_BUILTINS
  23. #endif
  24. #ifdef __cplusplus
  25. } /* extern "C" */
  26. #endif /* __cplusplus */
  27. #endif /* UTIL_H */