stdatomic.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Copyright (C) 2013-2019 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. /* ISO C11 Standard: 7.17 Atomics <stdatomic.h>. */
  19. #ifndef _STDATOMIC_H
  20. #define _STDATOMIC_H
  21. typedef enum
  22. {
  23. memory_order_relaxed = __ATOMIC_RELAXED,
  24. memory_order_consume = __ATOMIC_CONSUME,
  25. memory_order_acquire = __ATOMIC_ACQUIRE,
  26. memory_order_release = __ATOMIC_RELEASE,
  27. memory_order_acq_rel = __ATOMIC_ACQ_REL,
  28. memory_order_seq_cst = __ATOMIC_SEQ_CST
  29. } memory_order;
  30. typedef _Atomic _Bool atomic_bool;
  31. typedef _Atomic char atomic_char;
  32. typedef _Atomic signed char atomic_schar;
  33. typedef _Atomic unsigned char atomic_uchar;
  34. typedef _Atomic short atomic_short;
  35. typedef _Atomic unsigned short atomic_ushort;
  36. typedef _Atomic int atomic_int;
  37. typedef _Atomic unsigned int atomic_uint;
  38. typedef _Atomic long atomic_long;
  39. typedef _Atomic unsigned long atomic_ulong;
  40. typedef _Atomic long long atomic_llong;
  41. typedef _Atomic unsigned long long atomic_ullong;
  42. typedef _Atomic __CHAR16_TYPE__ atomic_char16_t;
  43. typedef _Atomic __CHAR32_TYPE__ atomic_char32_t;
  44. typedef _Atomic __WCHAR_TYPE__ atomic_wchar_t;
  45. typedef _Atomic __INT_LEAST8_TYPE__ atomic_int_least8_t;
  46. typedef _Atomic __UINT_LEAST8_TYPE__ atomic_uint_least8_t;
  47. typedef _Atomic __INT_LEAST16_TYPE__ atomic_int_least16_t;
  48. typedef _Atomic __UINT_LEAST16_TYPE__ atomic_uint_least16_t;
  49. typedef _Atomic __INT_LEAST32_TYPE__ atomic_int_least32_t;
  50. typedef _Atomic __UINT_LEAST32_TYPE__ atomic_uint_least32_t;
  51. typedef _Atomic __INT_LEAST64_TYPE__ atomic_int_least64_t;
  52. typedef _Atomic __UINT_LEAST64_TYPE__ atomic_uint_least64_t;
  53. typedef _Atomic __INT_FAST8_TYPE__ atomic_int_fast8_t;
  54. typedef _Atomic __UINT_FAST8_TYPE__ atomic_uint_fast8_t;
  55. typedef _Atomic __INT_FAST16_TYPE__ atomic_int_fast16_t;
  56. typedef _Atomic __UINT_FAST16_TYPE__ atomic_uint_fast16_t;
  57. typedef _Atomic __INT_FAST32_TYPE__ atomic_int_fast32_t;
  58. typedef _Atomic __UINT_FAST32_TYPE__ atomic_uint_fast32_t;
  59. typedef _Atomic __INT_FAST64_TYPE__ atomic_int_fast64_t;
  60. typedef _Atomic __UINT_FAST64_TYPE__ atomic_uint_fast64_t;
  61. typedef _Atomic __INTPTR_TYPE__ atomic_intptr_t;
  62. typedef _Atomic __UINTPTR_TYPE__ atomic_uintptr_t;
  63. typedef _Atomic __SIZE_TYPE__ atomic_size_t;
  64. typedef _Atomic __PTRDIFF_TYPE__ atomic_ptrdiff_t;
  65. typedef _Atomic __INTMAX_TYPE__ atomic_intmax_t;
  66. typedef _Atomic __UINTMAX_TYPE__ atomic_uintmax_t;
  67. #define ATOMIC_VAR_INIT(VALUE) (VALUE)
  68. /* Initialize an atomic object pointed to by PTR with VAL. */
  69. #define atomic_init(PTR, VAL) \
  70. atomic_store_explicit (PTR, VAL, __ATOMIC_RELAXED)
  71. #define kill_dependency(Y) \
  72. __extension__ \
  73. ({ \
  74. __auto_type __kill_dependency_tmp = (Y); \
  75. __kill_dependency_tmp; \
  76. })
  77. extern void atomic_thread_fence (memory_order);
  78. #define atomic_thread_fence(MO) __atomic_thread_fence (MO)
  79. extern void atomic_signal_fence (memory_order);
  80. #define atomic_signal_fence(MO) __atomic_signal_fence (MO)
  81. #define atomic_is_lock_free(OBJ) __atomic_is_lock_free (sizeof (*(OBJ)), (OBJ))
  82. #define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
  83. #define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
  84. #define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
  85. #define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
  86. #define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
  87. #define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  88. #define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  89. #define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  90. #define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  91. #define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
  92. /* Note that these macros require __typeof__ and __auto_type to remove
  93. _Atomic qualifiers (and const qualifiers, if those are valid on
  94. macro operands).
  95. Also note that the header file uses the generic form of __atomic
  96. builtins, which requires the address to be taken of the value
  97. parameter, and then we pass that value on. This allows the macros
  98. to work for any type, and the compiler is smart enough to convert
  99. these to lock-free _N variants if possible, and throw away the
  100. temps. */
  101. #define atomic_store_explicit(PTR, VAL, MO) \
  102. __extension__ \
  103. ({ \
  104. __auto_type __atomic_store_ptr = (PTR); \
  105. __typeof__ (*__atomic_store_ptr) __atomic_store_tmp = (VAL); \
  106. __atomic_store (__atomic_store_ptr, &__atomic_store_tmp, (MO)); \
  107. })
  108. #define atomic_store(PTR, VAL) \
  109. atomic_store_explicit (PTR, VAL, __ATOMIC_SEQ_CST)
  110. #define atomic_load_explicit(PTR, MO) \
  111. __extension__ \
  112. ({ \
  113. __auto_type __atomic_load_ptr = (PTR); \
  114. __typeof__ (*__atomic_load_ptr) __atomic_load_tmp; \
  115. __atomic_load (__atomic_load_ptr, &__atomic_load_tmp, (MO)); \
  116. __atomic_load_tmp; \
  117. })
  118. #define atomic_load(PTR) atomic_load_explicit (PTR, __ATOMIC_SEQ_CST)
  119. #define atomic_exchange_explicit(PTR, VAL, MO) \
  120. __extension__ \
  121. ({ \
  122. __auto_type __atomic_exchange_ptr = (PTR); \
  123. __typeof__ (*__atomic_exchange_ptr) __atomic_exchange_val = (VAL); \
  124. __typeof__ (*__atomic_exchange_ptr) __atomic_exchange_tmp; \
  125. __atomic_exchange (__atomic_exchange_ptr, &__atomic_exchange_val, \
  126. &__atomic_exchange_tmp, (MO)); \
  127. __atomic_exchange_tmp; \
  128. })
  129. #define atomic_exchange(PTR, VAL) \
  130. atomic_exchange_explicit (PTR, VAL, __ATOMIC_SEQ_CST)
  131. #define atomic_compare_exchange_strong_explicit(PTR, VAL, DES, SUC, FAIL) \
  132. __extension__ \
  133. ({ \
  134. __auto_type __atomic_compare_exchange_ptr = (PTR); \
  135. __typeof__ (*__atomic_compare_exchange_ptr) __atomic_compare_exchange_tmp \
  136. = (DES); \
  137. __atomic_compare_exchange (__atomic_compare_exchange_ptr, (VAL), \
  138. &__atomic_compare_exchange_tmp, 0, \
  139. (SUC), (FAIL)); \
  140. })
  141. #define atomic_compare_exchange_strong(PTR, VAL, DES) \
  142. atomic_compare_exchange_strong_explicit (PTR, VAL, DES, __ATOMIC_SEQ_CST, \
  143. __ATOMIC_SEQ_CST)
  144. #define atomic_compare_exchange_weak_explicit(PTR, VAL, DES, SUC, FAIL) \
  145. __extension__ \
  146. ({ \
  147. __auto_type __atomic_compare_exchange_ptr = (PTR); \
  148. __typeof__ (*__atomic_compare_exchange_ptr) __atomic_compare_exchange_tmp \
  149. = (DES); \
  150. __atomic_compare_exchange (__atomic_compare_exchange_ptr, (VAL), \
  151. &__atomic_compare_exchange_tmp, 1, \
  152. (SUC), (FAIL)); \
  153. })
  154. #define atomic_compare_exchange_weak(PTR, VAL, DES) \
  155. atomic_compare_exchange_weak_explicit (PTR, VAL, DES, __ATOMIC_SEQ_CST, \
  156. __ATOMIC_SEQ_CST)
  157. #define atomic_fetch_add(PTR, VAL) __atomic_fetch_add ((PTR), (VAL), \
  158. __ATOMIC_SEQ_CST)
  159. #define atomic_fetch_add_explicit(PTR, VAL, MO) \
  160. __atomic_fetch_add ((PTR), (VAL), (MO))
  161. #define atomic_fetch_sub(PTR, VAL) __atomic_fetch_sub ((PTR), (VAL), \
  162. __ATOMIC_SEQ_CST)
  163. #define atomic_fetch_sub_explicit(PTR, VAL, MO) \
  164. __atomic_fetch_sub ((PTR), (VAL), (MO))
  165. #define atomic_fetch_or(PTR, VAL) __atomic_fetch_or ((PTR), (VAL), \
  166. __ATOMIC_SEQ_CST)
  167. #define atomic_fetch_or_explicit(PTR, VAL, MO) \
  168. __atomic_fetch_or ((PTR), (VAL), (MO))
  169. #define atomic_fetch_xor(PTR, VAL) __atomic_fetch_xor ((PTR), (VAL), \
  170. __ATOMIC_SEQ_CST)
  171. #define atomic_fetch_xor_explicit(PTR, VAL, MO) \
  172. __atomic_fetch_xor ((PTR), (VAL), (MO))
  173. #define atomic_fetch_and(PTR, VAL) __atomic_fetch_and ((PTR), (VAL), \
  174. __ATOMIC_SEQ_CST)
  175. #define atomic_fetch_and_explicit(PTR, VAL, MO) \
  176. __atomic_fetch_and ((PTR), (VAL), (MO))
  177. typedef _Atomic struct
  178. {
  179. #if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL == 1
  180. _Bool __val;
  181. #else
  182. unsigned char __val;
  183. #endif
  184. } atomic_flag;
  185. #define ATOMIC_FLAG_INIT { 0 }
  186. extern _Bool atomic_flag_test_and_set (volatile atomic_flag *);
  187. #define atomic_flag_test_and_set(PTR) \
  188. __atomic_test_and_set ((PTR), __ATOMIC_SEQ_CST)
  189. extern _Bool atomic_flag_test_and_set_explicit (volatile atomic_flag *,
  190. memory_order);
  191. #define atomic_flag_test_and_set_explicit(PTR, MO) \
  192. __atomic_test_and_set ((PTR), (MO))
  193. extern void atomic_flag_clear (volatile atomic_flag *);
  194. #define atomic_flag_clear(PTR) __atomic_clear ((PTR), __ATOMIC_SEQ_CST)
  195. extern void atomic_flag_clear_explicit (volatile atomic_flag *, memory_order);
  196. #define atomic_flag_clear_explicit(PTR, MO) __atomic_clear ((PTR), (MO))
  197. #endif /* _STDATOMIC_H */