You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.8 KiB

  1. #ifndef Py_LIMITED_API
  2. #ifndef Py_ATOMIC_H
  3. #define Py_ATOMIC_H
  4. /* XXX: When compilers start offering a stdatomic.h with lock-free
  5. atomic_int and atomic_address types, include that here and rewrite
  6. the atomic operations in terms of it. */
  7. #include "dynamic_annotations.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /* This is modeled after the atomics interface from C1x, according to
  12. * the draft at
  13. * http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1425.pdf.
  14. * Operations and types are named the same except with a _Py_ prefix
  15. * and have the same semantics.
  16. *
  17. * Beware, the implementations here are deep magic.
  18. */
  19. typedef enum _Py_memory_order {
  20. _Py_memory_order_relaxed,
  21. _Py_memory_order_acquire,
  22. _Py_memory_order_release,
  23. _Py_memory_order_acq_rel,
  24. _Py_memory_order_seq_cst
  25. } _Py_memory_order;
  26. typedef struct _Py_atomic_address {
  27. void *_value;
  28. } _Py_atomic_address;
  29. typedef struct _Py_atomic_int {
  30. int _value;
  31. } _Py_atomic_int;
  32. /* Only support GCC (for expression statements) and x86 (for simple
  33. * atomic semantics) for now */
  34. #if defined(__GNUC__) && (defined(__i386__) || defined(__amd64))
  35. static __inline__ void
  36. _Py_atomic_signal_fence(_Py_memory_order order)
  37. {
  38. if (order != _Py_memory_order_relaxed)
  39. __asm__ volatile("":::"memory");
  40. }
  41. static __inline__ void
  42. _Py_atomic_thread_fence(_Py_memory_order order)
  43. {
  44. if (order != _Py_memory_order_relaxed)
  45. __asm__ volatile("mfence":::"memory");
  46. }
  47. /* Tell the race checker about this operation's effects. */
  48. static __inline__ void
  49. _Py_ANNOTATE_MEMORY_ORDER(const volatile void *address, _Py_memory_order order)
  50. {
  51. (void)address; /* shut up -Wunused-parameter */
  52. switch(order) {
  53. case _Py_memory_order_release:
  54. case _Py_memory_order_acq_rel:
  55. case _Py_memory_order_seq_cst:
  56. _Py_ANNOTATE_HAPPENS_BEFORE(address);
  57. break;
  58. case _Py_memory_order_relaxed:
  59. case _Py_memory_order_acquire:
  60. break;
  61. }
  62. switch(order) {
  63. case _Py_memory_order_acquire:
  64. case _Py_memory_order_acq_rel:
  65. case _Py_memory_order_seq_cst:
  66. _Py_ANNOTATE_HAPPENS_AFTER(address);
  67. break;
  68. case _Py_memory_order_relaxed:
  69. case _Py_memory_order_release:
  70. break;
  71. }
  72. }
  73. #define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
  74. __extension__ ({ \
  75. __typeof__(ATOMIC_VAL) atomic_val = ATOMIC_VAL; \
  76. __typeof__(atomic_val->_value) new_val = NEW_VAL;\
  77. volatile __typeof__(new_val) *volatile_data = &atomic_val->_value; \
  78. _Py_memory_order order = ORDER; \
  79. _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); \
  80. \
  81. /* Perform the operation. */ \
  82. _Py_ANNOTATE_IGNORE_WRITES_BEGIN(); \
  83. switch(order) { \
  84. case _Py_memory_order_release: \
  85. _Py_atomic_signal_fence(_Py_memory_order_release); \
  86. /* fallthrough */ \
  87. case _Py_memory_order_relaxed: \
  88. *volatile_data = new_val; \
  89. break; \
  90. \
  91. case _Py_memory_order_acquire: \
  92. case _Py_memory_order_acq_rel: \
  93. case _Py_memory_order_seq_cst: \
  94. __asm__ volatile("xchg %0, %1" \
  95. : "+r"(new_val) \
  96. : "m"(atomic_val->_value) \
  97. : "memory"); \
  98. break; \
  99. } \
  100. _Py_ANNOTATE_IGNORE_WRITES_END(); \
  101. })
  102. #define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
  103. __extension__ ({ \
  104. __typeof__(ATOMIC_VAL) atomic_val = ATOMIC_VAL; \
  105. __typeof__(atomic_val->_value) result; \
  106. volatile __typeof__(result) *volatile_data = &atomic_val->_value; \
  107. _Py_memory_order order = ORDER; \
  108. _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); \
  109. \
  110. /* Perform the operation. */ \
  111. _Py_ANNOTATE_IGNORE_READS_BEGIN(); \
  112. switch(order) { \
  113. case _Py_memory_order_release: \
  114. case _Py_memory_order_acq_rel: \
  115. case _Py_memory_order_seq_cst: \
  116. /* Loads on x86 are not releases by default, so need a */ \
  117. /* thread fence. */ \
  118. _Py_atomic_thread_fence(_Py_memory_order_release); \
  119. break; \
  120. default: \
  121. /* No fence */ \
  122. break; \
  123. } \
  124. result = *volatile_data; \
  125. switch(order) { \
  126. case _Py_memory_order_acquire: \
  127. case _Py_memory_order_acq_rel: \
  128. case _Py_memory_order_seq_cst: \
  129. /* Loads on x86 are automatically acquire operations so */ \
  130. /* can get by with just a compiler fence. */ \
  131. _Py_atomic_signal_fence(_Py_memory_order_acquire); \
  132. break; \
  133. default: \
  134. /* No fence */ \
  135. break; \
  136. } \
  137. _Py_ANNOTATE_IGNORE_READS_END(); \
  138. result; \
  139. })
  140. #else /* !gcc x86 */
  141. /* Fall back to other compilers and processors by assuming that simple
  142. volatile accesses are atomic. This is false, so people should port
  143. this. */
  144. #define _Py_atomic_signal_fence(/*memory_order*/ ORDER) ((void)0)
  145. #define _Py_atomic_thread_fence(/*memory_order*/ ORDER) ((void)0)
  146. #define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
  147. ((ATOMIC_VAL)->_value = NEW_VAL)
  148. #define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
  149. ((ATOMIC_VAL)->_value)
  150. #endif /* !gcc x86 */
  151. /* Standardized shortcuts. */
  152. #define _Py_atomic_store(ATOMIC_VAL, NEW_VAL) \
  153. _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, _Py_memory_order_seq_cst)
  154. #define _Py_atomic_load(ATOMIC_VAL) \
  155. _Py_atomic_load_explicit(ATOMIC_VAL, _Py_memory_order_seq_cst)
  156. /* Python-local extensions */
  157. #define _Py_atomic_store_relaxed(ATOMIC_VAL, NEW_VAL) \
  158. _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, _Py_memory_order_relaxed)
  159. #define _Py_atomic_load_relaxed(ATOMIC_VAL) \
  160. _Py_atomic_load_explicit(ATOMIC_VAL, _Py_memory_order_relaxed)
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif /* Py_ATOMIC_H */
  165. #endif /* Py_LIMITED_API */