Atomic.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. #include "c-api/il2cpp-config-platforms.h"
  3. #include <stdint.h>
  4. #include "utils/NonCopyable.h"
  5. #include "Baselib.h"
  6. #include "C/Baselib_Atomic_TypeSafe.h"
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. class Atomic : public il2cpp::utils::NonCopyable
  12. {
  13. public:
  14. // All 32bit atomics must be performed on 4-byte aligned addresses. All 64bit atomics must be
  15. // performed on 8-byte aligned addresses.
  16. // Add and Add64 return the *result* of the addition, not the old value! (i.e. they work like
  17. // InterlockedAdd and __sync_add_and_fetch).
  18. static inline void FullMemoryBarrier()
  19. {
  20. Baselib_atomic_thread_fence_seq_cst();
  21. }
  22. static inline int32_t Add(int32_t* location1, int32_t value)
  23. {
  24. int32_t result = Baselib_atomic_fetch_add_32_seq_cst(location1, value) + value;
  25. #if IL2CPP_TARGET_ARM64
  26. Baselib_atomic_thread_fence_seq_cst();
  27. #endif
  28. return result;
  29. }
  30. static inline uint32_t Add(uint32_t* location1, uint32_t value)
  31. {
  32. return (uint32_t)Add((int32_t*)location1, (int32_t)value);
  33. }
  34. #if IL2CPP_ENABLE_INTERLOCKED_64_REQUIRED_ALIGNMENT
  35. static inline int64_t Add64(int64_t* location1, int64_t value)
  36. {
  37. int64_t result = Baselib_atomic_fetch_add_64_seq_cst(location1, value) + value;
  38. #if IL2CPP_TARGET_ARM64
  39. Baselib_atomic_thread_fence_seq_cst();
  40. #endif
  41. return result;
  42. }
  43. #endif
  44. template<typename T>
  45. static inline T* CompareExchangePointer(T** dest, T* newValue, T* oldValue)
  46. {
  47. Baselib_atomic_compare_exchange_strong_ptr_seq_cst_seq_cst((intptr_t*)dest, (intptr_t*)&oldValue, (intptr_t)newValue);
  48. #if IL2CPP_TARGET_ARM64
  49. Baselib_atomic_thread_fence_seq_cst();
  50. #endif
  51. return static_cast<T*>(oldValue);
  52. }
  53. template<typename T>
  54. static inline T* ExchangePointer(T** dest, const T* newValue)
  55. {
  56. T* result = (T*)Baselib_atomic_exchange_ptr_seq_cst((intptr_t*)dest, (intptr_t)newValue);
  57. #if IL2CPP_TARGET_ARM64
  58. Baselib_atomic_thread_fence_seq_cst();
  59. #endif
  60. return result;
  61. }
  62. static inline int64_t Read64(int64_t* addr)
  63. {
  64. int64_t result = Baselib_atomic_fetch_add_64_seq_cst(addr, 0);
  65. #if IL2CPP_TARGET_ARM64
  66. Baselib_atomic_thread_fence_seq_cst();
  67. #endif
  68. return result;
  69. }
  70. static inline uint64_t Read64(uint64_t* addr)
  71. {
  72. return (uint64_t)Read64((int64_t*)addr);
  73. }
  74. static inline int32_t LoadRelaxed(const int32_t* addr)
  75. {
  76. return Baselib_atomic_load_32_relaxed(addr);
  77. }
  78. template<typename T>
  79. static inline T* LoadPointerRelaxed(const T* const * addr)
  80. {
  81. return (T*)Baselib_atomic_load_ptr_relaxed((const intptr_t*)addr);
  82. }
  83. template<typename T>
  84. static inline T* ReadPointer(T** pointer)
  85. {
  86. return (T*)Baselib_atomic_load_ptr_relaxed((intptr_t*)pointer);
  87. }
  88. template<typename T>
  89. static inline void PublishPointer(T** pointer, T* value)
  90. {
  91. Baselib_atomic_thread_fence_release();
  92. Baselib_atomic_store_ptr_relaxed((intptr_t*)pointer, (intptr_t)value);
  93. }
  94. static inline int32_t Increment(int32_t* value)
  95. {
  96. int32_t result = Baselib_atomic_fetch_add_32_seq_cst(value, 1) + 1;
  97. #if IL2CPP_TARGET_ARM64
  98. Baselib_atomic_thread_fence_seq_cst();
  99. #endif
  100. return result;
  101. }
  102. static inline uint32_t Increment(uint32_t* value)
  103. {
  104. return (uint32_t)Increment((int32_t*)value);
  105. }
  106. #if IL2CPP_ENABLE_INTERLOCKED_64_REQUIRED_ALIGNMENT
  107. static inline int64_t Increment64(int64_t* value)
  108. {
  109. int64_t result = Baselib_atomic_fetch_add_64_seq_cst(value, 1) + 1;
  110. #if IL2CPP_TARGET_ARM64
  111. Baselib_atomic_thread_fence_seq_cst();
  112. #endif
  113. return result;
  114. }
  115. static inline uint64_t Increment64(uint64_t* value)
  116. {
  117. return (uint64_t)Increment64((int64_t*)value);
  118. }
  119. #endif
  120. static inline int32_t Decrement(int32_t* value)
  121. {
  122. int32_t result = Baselib_atomic_fetch_add_32_seq_cst(value, -1) - 1;
  123. #if IL2CPP_TARGET_ARM64
  124. Baselib_atomic_thread_fence_seq_cst();
  125. #endif
  126. return result;
  127. }
  128. static inline uint32_t Decrement(uint32_t* value)
  129. {
  130. return (uint32_t)Decrement((int32_t*)value);
  131. }
  132. #if IL2CPP_ENABLE_INTERLOCKED_64_REQUIRED_ALIGNMENT
  133. static inline int64_t Decrement64(int64_t* value)
  134. {
  135. int64_t result = Baselib_atomic_fetch_add_64_seq_cst(value, -1) - 1;
  136. #if IL2CPP_TARGET_ARM64
  137. Baselib_atomic_thread_fence_seq_cst();
  138. #endif
  139. return result;
  140. }
  141. static inline uint64_t Decrement64(uint64_t* value)
  142. {
  143. return (uint64_t)Decrement64((int64_t*)value);
  144. }
  145. #endif
  146. static inline int32_t CompareExchange(int32_t* dest, int32_t exchange, int32_t comparand)
  147. {
  148. Baselib_atomic_compare_exchange_strong_32_seq_cst_seq_cst(dest, &comparand, exchange);
  149. #if IL2CPP_TARGET_ARM64
  150. Baselib_atomic_thread_fence_seq_cst();
  151. #endif
  152. return comparand;
  153. }
  154. static inline uint32_t CompareExchange(uint32_t* value, uint32_t newValue, uint32_t oldValue)
  155. {
  156. return (uint32_t)CompareExchange((int32_t*)value, newValue, oldValue);
  157. }
  158. static inline int64_t CompareExchange64(int64_t* dest, int64_t exchange, int64_t comparand)
  159. {
  160. Baselib_atomic_compare_exchange_strong_64_seq_cst_seq_cst(dest, &comparand, exchange);
  161. #if IL2CPP_TARGET_ARM64
  162. Baselib_atomic_thread_fence_seq_cst();
  163. #endif
  164. return comparand;
  165. }
  166. static inline uint64_t CompareExchange64(uint64_t* value, uint64_t newValue, uint64_t oldValue)
  167. {
  168. return (uint64_t)CompareExchange64((int64_t*)value, newValue, oldValue);
  169. }
  170. static inline bool Exchange(bool* dest, bool exchange)
  171. {
  172. return (bool)Baselib_atomic_exchange_8_seq_cst((int8_t*)dest, exchange);
  173. }
  174. static inline int32_t Exchange(int32_t* dest, int32_t exchange)
  175. {
  176. int32_t result = Baselib_atomic_exchange_32_seq_cst(dest, exchange);
  177. #if IL2CPP_TARGET_ARM64
  178. Baselib_atomic_thread_fence_seq_cst();
  179. #endif
  180. return result;
  181. }
  182. static inline uint32_t Exchange(uint32_t* value, uint32_t newValue)
  183. {
  184. return (uint32_t)Exchange((int32_t*)value, newValue);
  185. }
  186. #if IL2CPP_ENABLE_INTERLOCKED_64_REQUIRED_ALIGNMENT
  187. static inline int64_t Exchange64(int64_t* dest, int64_t exchange)
  188. {
  189. int64_t result = Baselib_atomic_exchange_64_seq_cst(dest, exchange);
  190. #if IL2CPP_TARGET_ARM64
  191. Baselib_atomic_thread_fence_seq_cst();
  192. #endif
  193. return result;
  194. }
  195. static inline uint64_t Exchange64(uint64_t* value, uint64_t newValue)
  196. {
  197. return (uint64_t)Exchange64((int64_t*)value, newValue);
  198. }
  199. #endif
  200. static inline intptr_t ReadPtrVal(intptr_t* addr)
  201. {
  202. intptr_t result = Baselib_atomic_fetch_add_ptr_seq_cst(addr, 0);
  203. #if IL2CPP_TARGET_ARM64
  204. Baselib_atomic_thread_fence_seq_cst();
  205. #endif
  206. return result;
  207. }
  208. };
  209. }
  210. }