stdatomic.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* This file is derived from clang's stdatomic.h */
  2. /*===---- stdatomic.h - Standard header for atomic types and operations -----===
  3. *
  4. * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. * See https://llvm.org/LICENSE.txt for license information.
  6. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. *
  8. *===-----------------------------------------------------------------------===
  9. */
  10. #ifndef _STDATOMIC_H
  11. #define _STDATOMIC_H
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #define __ATOMIC_RELAXED 0
  16. #define __ATOMIC_CONSUME 1
  17. #define __ATOMIC_ACQUIRE 2
  18. #define __ATOMIC_RELEASE 3
  19. #define __ATOMIC_ACQ_REL 4
  20. #define __ATOMIC_SEQ_CST 5
  21. /* Memory ordering */
  22. typedef enum {
  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. /* Atomic typedefs */
  31. typedef _Atomic(_Bool) atomic_bool;
  32. typedef _Atomic(char) atomic_char;
  33. typedef _Atomic(signed char) atomic_schar;
  34. typedef _Atomic(unsigned char) atomic_uchar;
  35. typedef _Atomic(short) atomic_short;
  36. typedef _Atomic(unsigned short) atomic_ushort;
  37. typedef _Atomic(int) atomic_int;
  38. typedef _Atomic(unsigned int) atomic_uint;
  39. typedef _Atomic(long) atomic_long;
  40. typedef _Atomic(unsigned long) atomic_ulong;
  41. typedef _Atomic(long long) atomic_llong;
  42. typedef _Atomic(unsigned long long) atomic_ullong;
  43. typedef _Atomic(uint_least16_t) atomic_char16_t;
  44. typedef _Atomic(uint_least32_t) atomic_char32_t;
  45. typedef _Atomic(wchar_t) atomic_wchar_t;
  46. typedef _Atomic(int_least8_t) atomic_int_least8_t;
  47. typedef _Atomic(uint_least8_t) atomic_uint_least8_t;
  48. typedef _Atomic(int_least16_t) atomic_int_least16_t;
  49. typedef _Atomic(uint_least16_t) atomic_uint_least16_t;
  50. typedef _Atomic(int_least32_t) atomic_int_least32_t;
  51. typedef _Atomic(uint_least32_t) atomic_uint_least32_t;
  52. typedef _Atomic(int_least64_t) atomic_int_least64_t;
  53. typedef _Atomic(uint_least64_t) atomic_uint_least64_t;
  54. typedef _Atomic(int_fast8_t) atomic_int_fast8_t;
  55. typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t;
  56. typedef _Atomic(int_fast16_t) atomic_int_fast16_t;
  57. typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t;
  58. typedef _Atomic(int_fast32_t) atomic_int_fast32_t;
  59. typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t;
  60. typedef _Atomic(int_fast64_t) atomic_int_fast64_t;
  61. typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t;
  62. typedef _Atomic(intptr_t) atomic_intptr_t;
  63. typedef _Atomic(uintptr_t) atomic_uintptr_t;
  64. typedef _Atomic(size_t) atomic_size_t;
  65. typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t;
  66. typedef _Atomic(intmax_t) atomic_intmax_t;
  67. typedef _Atomic(uintmax_t) atomic_uintmax_t;
  68. /* Atomic flag */
  69. typedef struct {
  70. atomic_bool value;
  71. } atomic_flag;
  72. #define ATOMIC_FLAG_INIT {0}
  73. #define ATOMIC_VAR_INIT(value) (value)
  74. /* Generic routines */
  75. #define atomic_init(object, desired) \
  76. atomic_store_explicit(object, desired, __ATOMIC_RELAXED)
  77. #define __atomic_store_n(ptr, val, order) \
  78. (*(ptr) = (val), __atomic_store((ptr), &(typeof(*(ptr))){val}, (order)))
  79. #define atomic_store_explicit(object, desired, order) \
  80. ({ __typeof__ (object) ptr = (object); \
  81. __typeof__ (*ptr) tmp = (desired); \
  82. __atomic_store (ptr, &tmp, (order)); \
  83. })
  84. #define atomic_store(object, desired) \
  85. atomic_store_explicit (object, desired, __ATOMIC_SEQ_CST)
  86. #define __atomic_load_n(ptr, order) \
  87. ({ typeof(*(ptr)) __val; \
  88. __atomic_load((ptr), &__val, (order)); \
  89. __val; })
  90. #define atomic_load_explicit(object, order) \
  91. ({ __typeof__ (object) ptr = (object); \
  92. __typeof__ (*ptr) tmp; \
  93. __atomic_load (ptr, &tmp, (order)); \
  94. tmp; \
  95. })
  96. #define atomic_load(object) atomic_load_explicit (object, __ATOMIC_SEQ_CST)
  97. #define atomic_exchange_explicit(object, desired, order) \
  98. ({ __typeof__ (object) ptr = (object); \
  99. __typeof__ (*ptr) val = (desired); \
  100. __typeof__ (*ptr) tmp; \
  101. __atomic_exchange (ptr, &val, &tmp, (order)); \
  102. tmp; \
  103. })
  104. #define atomic_exchange(object, desired) \
  105. atomic_exchange_explicit (object, desired, __ATOMIC_SEQ_CST)
  106. #define __atomic_compare_exchange_n(ptr, expected, desired, weak, success, failure) \
  107. ({ typeof(*(ptr)) __desired = (desired); \
  108. __atomic_compare_exchange((ptr), (expected), &__desired, \
  109. (weak), (success), (failure)); })
  110. #define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \
  111. ({ __typeof__ (object) ptr = (object); \
  112. __typeof__ (*ptr) tmp = desired; \
  113. __atomic_compare_exchange(ptr, expected, &tmp, 0, success, failure); \
  114. })
  115. #define atomic_compare_exchange_strong(object, expected, desired) \
  116. atomic_compare_exchange_strong_explicit (object, expected, desired, \
  117. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
  118. #define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \
  119. ({ __typeof__ (object) ptr = (object); \
  120. __typeof__ (*ptr) tmp = desired; \
  121. __atomic_compare_exchange(ptr, expected, &tmp, 1, success, failure); \
  122. })
  123. #define atomic_compare_exchange_weak(object, expected, desired) \
  124. atomic_compare_exchange_weak_explicit (object, expected, desired, \
  125. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
  126. #define atomic_fetch_add(object, operand) \
  127. __atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST)
  128. #define atomic_fetch_add_explicit __atomic_fetch_add
  129. #define atomic_fetch_sub(object, operand) \
  130. __atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST)
  131. #define atomic_fetch_sub_explicit __atomic_fetch_sub
  132. #define atomic_fetch_or(object, operand) \
  133. __atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST)
  134. #define atomic_fetch_or_explicit __atomic_fetch_or
  135. #define atomic_fetch_xor(object, operand) \
  136. __atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST)
  137. #define atomic_fetch_xor_explicit __atomic_fetch_xor
  138. #define atomic_fetch_and(object, operand) \
  139. __atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST)
  140. #define atomic_fetch_and_explicit __atomic_fetch_and
  141. extern void atomic_thread_fence (memory_order);
  142. #define __atomic_thread_fence(order) atomic_thread_fence (order)
  143. extern void atomic_signal_fence (memory_order);
  144. #define __atomic_signal_fence(order) atomic_signal_fence(order)
  145. #define atomic_signal_fence(order) __atomic_signal_fence (order)
  146. extern bool __atomic_is_lock_free(size_t size, void *ptr);
  147. #define atomic_is_lock_free(OBJ) __atomic_is_lock_free (sizeof (*(OBJ)), (OBJ))
  148. extern bool atomic_flag_test_and_set(void *object);
  149. extern bool atomic_flag_test_and_set_explicit(void *object, memory_order order);
  150. extern void atomic_flag_clear(void *object);
  151. extern void atomic_flag_clear_explicit(void *object, memory_order order);
  152. #endif /* _STDATOMIC_H */