atomic.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef SKYNET_ATOMIC_H
  2. #define SKYNET_ATOMIC_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #ifdef __STDC_NO_ATOMICS__
  6. #define ATOM_INT volatile int
  7. #define ATOM_POINTER volatile uintptr_t
  8. #define ATOM_SIZET volatile size_t
  9. #define ATOM_ULONG volatile unsigned long
  10. #define ATOM_INIT(ptr, v) (*(ptr) = v)
  11. #define ATOM_LOAD(ptr) (*(ptr))
  12. #define ATOM_STORE(ptr, v) (*(ptr) = v)
  13. #define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
  14. #define ATOM_CAS_ULONG(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
  15. #define ATOM_CAS_SIZET(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
  16. #define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval)
  17. #define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1)
  18. #define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1)
  19. #define ATOM_FADD(ptr,n) __sync_fetch_and_add(ptr, n)
  20. #define ATOM_FSUB(ptr,n) __sync_fetch_and_sub(ptr, n)
  21. #define ATOM_FAND(ptr,n) __sync_fetch_and_and(ptr, n)
  22. #else
  23. #if defined (__cplusplus)
  24. #include <atomic>
  25. #define STD_ std::
  26. #define atomic_value_type_(p, v) decltype((p)->load())(v)
  27. #else
  28. #include <stdatomic.h>
  29. #define STD_
  30. #define atomic_value_type_(p, v) v
  31. #endif
  32. #define ATOM_INT STD_ atomic_int
  33. #define ATOM_POINTER STD_ atomic_uintptr_t
  34. #define ATOM_SIZET STD_ atomic_size_t
  35. #define ATOM_ULONG STD_ atomic_ulong
  36. #define ATOM_INIT(ref, v) STD_ atomic_init(ref, v)
  37. #define ATOM_LOAD(ptr) STD_ atomic_load(ptr)
  38. #define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v)
  39. static inline int
  40. ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) {
  41. return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
  42. }
  43. static inline int
  44. ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) {
  45. return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
  46. }
  47. static inline int
  48. ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) {
  49. return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
  50. }
  51. static inline int
  52. ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) {
  53. return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
  54. }
  55. #define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1))
  56. #define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1))
  57. #define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n))
  58. #define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n))
  59. #define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n))
  60. #endif
  61. #endif