stdatomic.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*===---- stdatomic.h - Standard header for atomic types and operations -----===
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. *
  21. *===-----------------------------------------------------------------------===
  22. */
  23. #ifndef __CLANG_STDATOMIC_H
  24. #define __CLANG_STDATOMIC_H
  25. /* If we're hosted, fall back to the system's stdatomic.h. FreeBSD, for
  26. * example, already has a Clang-compatible stdatomic.h header.
  27. */
  28. #if __STDC_HOSTED__ && __has_include_next(<stdatomic.h>)
  29. # include_next <stdatomic.h>
  30. #else
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* 7.17.1 Introduction */
  37. #define ATOMIC_BOOL_LOCK_FREE __CLANG_ATOMIC_BOOL_LOCK_FREE
  38. #define ATOMIC_CHAR_LOCK_FREE __CLANG_ATOMIC_CHAR_LOCK_FREE
  39. #define ATOMIC_CHAR16_T_LOCK_FREE __CLANG_ATOMIC_CHAR16_T_LOCK_FREE
  40. #define ATOMIC_CHAR32_T_LOCK_FREE __CLANG_ATOMIC_CHAR32_T_LOCK_FREE
  41. #define ATOMIC_WCHAR_T_LOCK_FREE __CLANG_ATOMIC_WCHAR_T_LOCK_FREE
  42. #define ATOMIC_SHORT_LOCK_FREE __CLANG_ATOMIC_SHORT_LOCK_FREE
  43. #define ATOMIC_INT_LOCK_FREE __CLANG_ATOMIC_INT_LOCK_FREE
  44. #define ATOMIC_LONG_LOCK_FREE __CLANG_ATOMIC_LONG_LOCK_FREE
  45. #define ATOMIC_LLONG_LOCK_FREE __CLANG_ATOMIC_LLONG_LOCK_FREE
  46. #define ATOMIC_POINTER_LOCK_FREE __CLANG_ATOMIC_POINTER_LOCK_FREE
  47. /* 7.17.2 Initialization */
  48. #define ATOMIC_VAR_INIT(value) (value)
  49. #define atomic_init __c11_atomic_init
  50. /* 7.17.3 Order and consistency */
  51. typedef enum memory_order {
  52. memory_order_relaxed = __ATOMIC_RELAXED,
  53. memory_order_consume = __ATOMIC_CONSUME,
  54. memory_order_acquire = __ATOMIC_ACQUIRE,
  55. memory_order_release = __ATOMIC_RELEASE,
  56. memory_order_acq_rel = __ATOMIC_ACQ_REL,
  57. memory_order_seq_cst = __ATOMIC_SEQ_CST
  58. } memory_order;
  59. #define kill_dependency(y) (y)
  60. /* 7.17.4 Fences */
  61. /* These should be provided by the libc implementation. */
  62. void atomic_thread_fence(memory_order);
  63. void atomic_signal_fence(memory_order);
  64. #define atomic_thread_fence(order) __c11_atomic_thread_fence(order)
  65. #define atomic_signal_fence(order) __c11_atomic_signal_fence(order)
  66. /* 7.17.5 Lock-free property */
  67. #define atomic_is_lock_free(obj) __c11_atomic_is_lock_free(sizeof(*(obj)))
  68. /* 7.17.6 Atomic integer types */
  69. #ifdef __cplusplus
  70. typedef _Atomic(bool) atomic_bool;
  71. #else
  72. typedef _Atomic(_Bool) atomic_bool;
  73. #endif
  74. typedef _Atomic(char) atomic_char;
  75. typedef _Atomic(signed char) atomic_schar;
  76. typedef _Atomic(unsigned char) atomic_uchar;
  77. typedef _Atomic(short) atomic_short;
  78. typedef _Atomic(unsigned short) atomic_ushort;
  79. typedef _Atomic(int) atomic_int;
  80. typedef _Atomic(unsigned int) atomic_uint;
  81. typedef _Atomic(long) atomic_long;
  82. typedef _Atomic(unsigned long) atomic_ulong;
  83. typedef _Atomic(long long) atomic_llong;
  84. typedef _Atomic(unsigned long long) atomic_ullong;
  85. typedef _Atomic(uint_least16_t) atomic_char16_t;
  86. typedef _Atomic(uint_least32_t) atomic_char32_t;
  87. typedef _Atomic(wchar_t) atomic_wchar_t;
  88. typedef _Atomic(int_least8_t) atomic_int_least8_t;
  89. typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
  90. typedef _Atomic(int_least16_t) atomic_int_least16_t;
  91. typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
  92. typedef _Atomic(int_least32_t) atomic_int_least32_t;
  93. typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
  94. typedef _Atomic(int_least64_t) atomic_int_least64_t;
  95. typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
  96. typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
  97. typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
  98. typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
  99. typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
  100. typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
  101. typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
  102. typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
  103. typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
  104. typedef _Atomic(intptr_t) atomic_intptr_t;
  105. typedef _Atomic(uintptr_t) atomic_uintptr_t;
  106. typedef _Atomic(size_t) atomic_size_t;
  107. typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
  108. typedef _Atomic(intmax_t) atomic_intmax_t;
  109. typedef _Atomic(uintmax_t) atomic_uintmax_t;
  110. /* 7.17.7 Operations on atomic types */
  111. #define atomic_store(object, desired) __c11_atomic_store(object, desired, __ATOMIC_SEQ_CST)
  112. #define atomic_store_explicit __c11_atomic_store
  113. #define atomic_load(object) __c11_atomic_load(object, __ATOMIC_SEQ_CST)
  114. #define atomic_load_explicit __c11_atomic_load
  115. #define atomic_exchange(object, desired) __c11_atomic_exchange(object, desired, __ATOMIC_SEQ_CST)
  116. #define atomic_exchange_explicit __c11_atomic_exchange
  117. #define atomic_compare_exchange_strong(object, expected, desired) __c11_atomic_compare_exchange_strong(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
  118. #define atomic_compare_exchange_strong_explicit __c11_atomic_compare_exchange_strong
  119. #define atomic_compare_exchange_weak(object, expected, desired) __c11_atomic_compare_exchange_weak(object, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
  120. #define atomic_compare_exchange_weak_explicit __c11_atomic_compare_exchange_weak
  121. #define atomic_fetch_add(object, operand) __c11_atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST)
  122. #define atomic_fetch_add_explicit __c11_atomic_fetch_add
  123. #define atomic_fetch_sub(object, operand) __c11_atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST)
  124. #define atomic_fetch_sub_explicit __c11_atomic_fetch_sub
  125. #define atomic_fetch_or(object, operand) __c11_atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST)
  126. #define atomic_fetch_or_explicit __c11_atomic_fetch_or
  127. #define atomic_fetch_xor(object, operand) __c11_atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST)
  128. #define atomic_fetch_xor_explicit __c11_atomic_fetch_xor
  129. #define atomic_fetch_and(object, operand) __c11_atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST)
  130. #define atomic_fetch_and_explicit __c11_atomic_fetch_and
  131. /* 7.17.8 Atomic flag type and operations */
  132. typedef struct atomic_flag { atomic_bool _Value; } atomic_flag;
  133. #define ATOMIC_FLAG_INIT { 0 }
  134. /* These should be provided by the libc implementation. */
  135. #ifdef __cplusplus
  136. bool atomic_flag_test_and_set(volatile atomic_flag *);
  137. bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
  138. #else
  139. _Bool atomic_flag_test_and_set(volatile atomic_flag *);
  140. _Bool atomic_flag_test_and_set_explicit(volatile atomic_flag *, memory_order);
  141. #endif
  142. void atomic_flag_clear(volatile atomic_flag *);
  143. void atomic_flag_clear_explicit(volatile atomic_flag *, memory_order);
  144. #define atomic_flag_test_and_set(object) __c11_atomic_exchange(&(object)->_Value, 1, __ATOMIC_SEQ_CST)
  145. #define atomic_flag_test_and_set_explicit(object, order) __c11_atomic_exchange(&(object)->_Value, 1, order)
  146. #define atomic_flag_clear(object) __c11_atomic_store(&(object)->_Value, 0, __ATOMIC_SEQ_CST)
  147. #define atomic_flag_clear_explicit(object, order) __c11_atomic_store(&(object)->_Value, 0, order)
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* __STDC_HOSTED__ */
  152. #endif /* __CLANG_STDATOMIC_H */