stdatomic.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (C) 2013-2015 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. #define atomic_init(PTR, VAL) \
  69. do \
  70. { \
  71. *(PTR) = (VAL); \
  72. } \
  73. while (0)
  74. #define kill_dependency(Y) \
  75. __extension__ \
  76. ({ \
  77. __auto_type __kill_dependency_tmp = (Y); \
  78. __kill_dependency_tmp; \
  79. })
  80. #define atomic_thread_fence(MO) __atomic_thread_fence (MO)
  81. #define atomic_signal_fence(MO) __atomic_signal_fence (MO)
  82. #define atomic_is_lock_free(OBJ) __atomic_is_lock_free (sizeof (*(OBJ)), (OBJ))
  83. #define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE
  84. #define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE
  85. #define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE
  86. #define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE
  87. #define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE
  88. #define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE
  89. #define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE
  90. #define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE
  91. #define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE
  92. #define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE
  93. /* Note that these macros require __typeof__ and __auto_type to remove
  94. _Atomic qualifiers (and const qualifiers, if those are valid on
  95. macro operands).
  96. Also note that the header file uses the generic form of __atomic
  97. builtins, which requires the address to be taken of the value
  98. parameter, and then we pass that value on. This allows the macros
  99. to work for any type, and the compiler is smart enough to convert
  100. these to lock-free _N variants if possible, and throw away the
  101. temps. */
  102. #define atomic_store_explicit(PTR, VAL, MO) \
  103. __extension__ \
  104. ({ \
  105. __auto_type __atomic_store_ptr = (PTR); \
  106. __typeof__ (*__atomic_store_ptr) __atomic_store_tmp = (VAL); \
  107. __atomic_store (__atomic_store_ptr, &__atomic_store_tmp, (MO)); \
  108. })
  109. #define atomic_store(PTR, VAL) \
  110. atomic_store_explicit (PTR, VAL, __ATOMIC_SEQ_CST)
  111. #define atomic_load_explicit(PTR, MO) \
  112. __extension__ \
  113. ({ \
  114. __auto_type __atomic_load_ptr = (PTR); \
  115. __typeof__ (*__atomic_load_ptr) __atomic_load_tmp; \
  116. __atomic_load (__atomic_load_ptr, &__atomic_load_tmp, (MO)); \
  117. __atomic_load_tmp; \
  118. })
  119. #define atomic_load(PTR) atomic_load_explicit (PTR, __ATOMIC_SEQ_CST)
  120. #define atomic_exchange_explicit(PTR, VAL, MO) \
  121. __extension__ \
  122. ({ \
  123. __auto_type __atomic_exchange_ptr = (PTR); \
  124. __typeof__ (*__atomic_exchange_ptr) __atomic_exchange_val = (VAL); \
  125. __typeof__ (*__atomic_exchange_ptr) __atomic_exchange_tmp; \
  126. __atomic_exchange (__atomic_exchange_ptr, &__atomic_exchange_val, \
  127. &__atomic_exchange_tmp, (MO)); \
  128. __atomic_exchange_tmp; \
  129. })
  130. #define atomic_exchange(PTR, VAL) \
  131. atomic_exchange_explicit (PTR, VAL, __ATOMIC_SEQ_CST)
  132. #define atomic_compare_exchange_strong_explicit(PTR, VAL, DES, SUC, FAIL) \
  133. __extension__ \
  134. ({ \
  135. __auto_type __atomic_compare_exchange_ptr = (PTR); \
  136. __typeof__ (*__atomic_compare_exchange_ptr) __atomic_compare_exchange_tmp \
  137. = (DES); \
  138. __atomic_compare_exchange (__atomic_compare_exchange_ptr, (VAL), \
  139. &__atomic_compare_exchange_tmp, 0, \
  140. (SUC), (FAIL)); \
  141. })
  142. #define atomic_compare_exchange_strong(PTR, VAL, DES) \
  143. atomic_compare_exchange_strong_explicit (PTR, VAL, DES, __ATOMIC_SEQ_CST, \
  144. __ATOMIC_SEQ_CST)
  145. #define atomic_compare_exchange_weak_explicit(PTR, VAL, DES, SUC, FAIL) \
  146. __extension__ \
  147. ({ \
  148. __auto_type __atomic_compare_exchange_ptr = (PTR); \
  149. __typeof__ (*__atomic_compare_exchange_ptr) __atomic_compare_exchange_tmp \
  150. = (DES); \
  151. __atomic_compare_exchange (__atomic_compare_exchange_ptr, (VAL), \
  152. &__atomic_compare_exchange_tmp, 1, \
  153. (SUC), (FAIL)); \
  154. })
  155. #define atomic_compare_exchange_weak(PTR, VAL, DES) \
  156. atomic_compare_exchange_weak_explicit (PTR, VAL, DES, __ATOMIC_SEQ_CST, \
  157. __ATOMIC_SEQ_CST)
  158. #define atomic_fetch_add(PTR, VAL) __atomic_fetch_add ((PTR), (VAL), \
  159. __ATOMIC_SEQ_CST)
  160. #define atomic_fetch_add_explicit(PTR, VAL, MO) \
  161. __atomic_fetch_add ((PTR), (VAL), (MO))
  162. #define atomic_fetch_sub(PTR, VAL) __atomic_fetch_sub ((PTR), (VAL), \
  163. __ATOMIC_SEQ_CST)
  164. #define atomic_fetch_sub_explicit(PTR, VAL, MO) \
  165. __atomic_fetch_sub ((PTR), (VAL), (MO))
  166. #define atomic_fetch_or(PTR, VAL) __atomic_fetch_or ((PTR), (VAL), \
  167. __ATOMIC_SEQ_CST)
  168. #define atomic_fetch_or_explicit(PTR, VAL, MO) \
  169. __atomic_fetch_or ((PTR), (VAL), (MO))
  170. #define atomic_fetch_xor(PTR, VAL) __atomic_fetch_xor ((PTR), (VAL), \
  171. __ATOMIC_SEQ_CST)
  172. #define atomic_fetch_xor_explicit(PTR, VAL, MO) \
  173. __atomic_fetch_xor ((PTR), (VAL), (MO))
  174. #define atomic_fetch_and(PTR, VAL) __atomic_fetch_and ((PTR), (VAL), \
  175. __ATOMIC_SEQ_CST)
  176. #define atomic_fetch_and_explicit(PTR, VAL, MO) \
  177. __atomic_fetch_and ((PTR), (VAL), (MO))
  178. typedef _Atomic struct
  179. {
  180. #if __GCC_ATOMIC_TEST_AND_SET_TRUEVAL == 1
  181. _Bool __val;
  182. #else
  183. unsigned char __val;
  184. #endif
  185. } atomic_flag;
  186. #define ATOMIC_FLAG_INIT { 0 }
  187. #define atomic_flag_test_and_set(PTR) \
  188. __atomic_test_and_set ((PTR), __ATOMIC_SEQ_CST)
  189. #define atomic_flag_test_and_set_explicit(PTR, MO) \
  190. __atomic_test_and_set ((PTR), (MO))
  191. #define atomic_flag_clear(PTR) __atomic_clear ((PTR), __ATOMIC_SEQ_CST)
  192. #define atomic_flag_clear_explicit(PTR, MO) __atomic_clear ((PTR), (MO))
  193. #endif /* _STDATOMIC_H */