atomicops.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // For atomic operations on reference counts, see atomic_refcount.h.
  5. // For atomic operations on sequence numbers, see atomic_sequence_num.h.
  6. // The routines exported by this module are subtle. If you use them, even if
  7. // you get the code right, it will depend on careful reasoning about atomicity
  8. // and memory ordering; it will be less readable, and harder to maintain. If
  9. // you plan to use these routines, you should have a good reason, such as solid
  10. // evidence that performance would otherwise suffer, or there being no
  11. // alternative. You should assume only properties explicitly guaranteed by the
  12. // specifications in this file. You are almost certainly _not_ writing code
  13. // just for the x86; if you assume x86 semantics, x86 hardware bugs and
  14. // implementations on other archtectures will cause your code to break. If you
  15. // do not know what you are doing, avoid these routines, and use a Mutex.
  16. //
  17. // It is incorrect to make direct assignments to/from an atomic variable.
  18. // You should use one of the Load or Store routines. The NoBarrier
  19. // versions are provided when no barriers are needed:
  20. // NoBarrier_Store()
  21. // NoBarrier_Load()
  22. // Although there are currently no compiler enforcement, you are encouraged
  23. // to use these.
  24. //
  25. #ifndef BASE_ATOMICOPS_H_
  26. #define BASE_ATOMICOPS_H_
  27. #include <stdint.h>
  28. #include "base/build_config.h"
  29. #include "base/macros.h"
  30. #if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
  31. // windows.h #defines this (only on x64). This causes problems because the
  32. // public API also uses MemoryBarrier at the public name for this fence. So, on
  33. // X64, undef it, and call its documented
  34. // (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx)
  35. // implementation directly.
  36. #undef MemoryBarrier
  37. #endif
  38. namespace base {
  39. namespace subtle {
  40. typedef int32_t Atomic32;
  41. #ifdef ARCH_CPU_64_BITS
  42. // We need to be able to go between Atomic64 and AtomicWord implicitly. This
  43. // means Atomic64 and AtomicWord should be the same type on 64-bit.
  44. #if defined(__ILP32__) || defined(OS_NACL)
  45. // NaCl's intptr_t is not actually 64-bits on 64-bit!
  46. // http://code.google.com/p/nativeclient/issues/detail?id=1162
  47. typedef int64_t Atomic64;
  48. #else
  49. typedef intptr_t Atomic64;
  50. #endif
  51. #endif
  52. // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
  53. // Atomic64 routines below, depending on your architecture.
  54. typedef intptr_t AtomicWord;
  55. // Atomically execute:
  56. // result = *ptr;
  57. // if (*ptr == old_value)
  58. // *ptr = new_value;
  59. // return result;
  60. //
  61. // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
  62. // Always return the old value of "*ptr"
  63. //
  64. // This routine implies no memory barriers.
  65. Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  66. Atomic32 old_value,
  67. Atomic32 new_value);
  68. // Atomically store new_value into *ptr, returning the previous value held in
  69. // *ptr. This routine implies no memory barriers.
  70. Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
  71. // Atomically increment *ptr by "increment". Returns the new value of
  72. // *ptr with the increment applied. This routine implies no memory barriers.
  73. Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
  74. Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  75. Atomic32 increment);
  76. // These following lower-level operations are typically useful only to people
  77. // implementing higher-level synchronization operations like spinlocks,
  78. // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
  79. // a store with appropriate memory-ordering instructions. "Acquire" operations
  80. // ensure that no later memory access can be reordered ahead of the operation.
  81. // "Release" operations ensure that no previous memory access can be reordered
  82. // after the operation. "Barrier" operations have both "Acquire" and "Release"
  83. // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
  84. // access.
  85. Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  86. Atomic32 old_value,
  87. Atomic32 new_value);
  88. Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  89. Atomic32 old_value,
  90. Atomic32 new_value);
  91. void MemoryBarrier();
  92. void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
  93. void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
  94. void Release_Store(volatile Atomic32* ptr, Atomic32 value);
  95. Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
  96. Atomic32 Acquire_Load(volatile const Atomic32* ptr);
  97. Atomic32 Release_Load(volatile const Atomic32* ptr);
  98. // 64-bit atomic operations (only available on 64-bit processors).
  99. #ifdef ARCH_CPU_64_BITS
  100. Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
  101. Atomic64 old_value,
  102. Atomic64 new_value);
  103. Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
  104. Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  105. Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  106. Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
  107. Atomic64 old_value,
  108. Atomic64 new_value);
  109. Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
  110. Atomic64 old_value,
  111. Atomic64 new_value);
  112. void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
  113. void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
  114. void Release_Store(volatile Atomic64* ptr, Atomic64 value);
  115. Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
  116. Atomic64 Acquire_Load(volatile const Atomic64* ptr);
  117. Atomic64 Release_Load(volatile const Atomic64* ptr);
  118. #endif // ARCH_CPU_64_BITS
  119. } // namespace subtle
  120. } // namespace base
  121. // Include our platform specific implementation.
  122. #if defined(THREAD_SANITIZER)
  123. #include "base/atomicops_internals_tsan.h"
  124. #elif defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
  125. #include "base/atomicops_internals_x86_msvc.h"
  126. #elif defined(OS_MACOSX)
  127. #include "base/atomicops_internals_mac.h"
  128. #elif defined(OS_NACL)
  129. #include "base/atomicops_internals_gcc.h"
  130. #elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARMEL)
  131. #include "base/atomicops_internals_arm_gcc.h"
  132. #elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARM64)
  133. #include "base/atomicops_internals_arm64_gcc.h"
  134. #elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY)
  135. #include "base/atomicops_internals_x86_gcc.h"
  136. #elif defined(COMPILER_GCC) && defined(ARCH_CPU_MIPS_FAMILY)
  137. #include "base/atomicops_internals_mips_gcc.h"
  138. #else
  139. #error "Atomic operations are not supported on your platform"
  140. #endif
  141. // On some platforms we need additional declarations to make
  142. // AtomicWord compatible with our other Atomic* types.
  143. #if defined(OS_MACOSX) || defined(OS_OPENBSD)
  144. #include "base/atomicops_internals_atomicword_compat.h"
  145. #endif
  146. // ========= Provide base::atomic<T> =========
  147. #if defined(BASE_CXX11_ENABLED)
  148. // gcc supports atomic thread fence since 4.8 checkout
  149. // https://gcc.gnu.org/gcc-4.7/cxx0x_status.html and
  150. // https://gcc.gnu.org/gcc-4.8/cxx0x_status.html for more details
  151. #if !defined(__GNUC__) || (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 >= 40800)
  152. #include <atomic>
  153. #else
  154. #if __GNUC__ * 10000 + __GNUC_MINOR__ * 100 >= 40500
  155. // gcc 4.5 renames cstdatomic to atomic
  156. // (https://gcc.gnu.org/gcc-4.5/changes.html)
  157. #include <atomic>
  158. #else
  159. #include <cstdatomic>
  160. #endif
  161. namespace std {
  162. BASE_FORCE_INLINE void atomic_thread_fence(memory_order v) {
  163. switch (v) {
  164. case memory_order_relaxed:
  165. break;
  166. case memory_order_consume:
  167. case memory_order_acquire:
  168. case memory_order_release:
  169. case memory_order_acq_rel:
  170. __asm__ __volatile__("" : : : "memory");
  171. break;
  172. case memory_order_seq_cst:
  173. __asm__ __volatile__("mfence" : : : "memory");
  174. break;
  175. }
  176. }
  177. BASE_FORCE_INLINE void atomic_signal_fence(memory_order v) {
  178. if (v != memory_order_relaxed) {
  179. __asm__ __volatile__("" : : : "memory");
  180. }
  181. }
  182. } // namespace std
  183. #endif // __GNUC__
  184. namespace base {
  185. using ::std::memory_order;
  186. using ::std::memory_order_relaxed;
  187. using ::std::memory_order_consume;
  188. using ::std::memory_order_acquire;
  189. using ::std::memory_order_release;
  190. using ::std::memory_order_acq_rel;
  191. using ::std::memory_order_seq_cst;
  192. using ::std::atomic_thread_fence;
  193. using ::std::atomic_signal_fence;
  194. template <typename T> class atomic : public ::std::atomic<T> {
  195. public:
  196. atomic() {}
  197. atomic(T v) : ::std::atomic<T>(v) {}
  198. atomic& operator=(T v) {
  199. this->store(v);
  200. return *this;
  201. }
  202. private:
  203. DISALLOW_COPY_AND_ASSIGN(atomic);
  204. // Make sure memory layout of std::atomic<T> and boost::atomic<T>
  205. // are same so that different compilation units seeing different
  206. // definitions(enable C++11 or not) should be compatible.
  207. BAIDU_CASSERT(sizeof(T) == sizeof(::std::atomic<T>), size_must_match);
  208. };
  209. } // namespace base
  210. #else
  211. #include <boost/atomic.hpp>
  212. namespace base {
  213. using ::boost::memory_order;
  214. using ::boost::memory_order_relaxed;
  215. using ::boost::memory_order_consume;
  216. using ::boost::memory_order_acquire;
  217. using ::boost::memory_order_release;
  218. using ::boost::memory_order_acq_rel;
  219. using ::boost::memory_order_seq_cst;
  220. using ::boost::atomic_thread_fence;
  221. using ::boost::atomic_signal_fence;
  222. template <typename T> class atomic : public ::boost::atomic<T> {
  223. public:
  224. atomic() {}
  225. atomic(T v) : ::boost::atomic<T>(v) {}
  226. atomic& operator=(T v) {
  227. this->store(v);
  228. return *this;
  229. }
  230. private:
  231. DISALLOW_COPY_AND_ASSIGN(atomic);
  232. // Make sure memory layout of std::atomic<T> and boost::atomic<T>
  233. // are same so that different compilation units seeing different
  234. // definitions(enable C++11 or not) should be compatible.
  235. BAIDU_CASSERT(sizeof(T) == sizeof(::boost::atomic<T>), size_must_match);
  236. };
  237. } // namespace base
  238. #endif
  239. // static_atomic<> is a work-around for C++03 to declare global atomics
  240. // w/o constructing-order issues. It can also used in C++11 though.
  241. // Example:
  242. // base::static_atomic<int> g_counter = BASE_STATIC_ATOMIC_INIT(0);
  243. // Notice that to make static_atomic work for C++03, it cannot be
  244. // initialized by a constructor. Following code is wrong:
  245. // base::static_atomic<int> g_counter(0); // Not compile
  246. #define BASE_STATIC_ATOMIC_INIT(val) { (val) }
  247. namespace base {
  248. template <typename T> struct static_atomic {
  249. T val;
  250. // NOTE: the memory_order parameters must be present.
  251. T load(memory_order o) { return ref().load(o); }
  252. void store(T v, memory_order o) { return ref().store(v, o); }
  253. T exchange(T v, memory_order o) { return ref().exchange(v, o); }
  254. bool compare_exchange_weak(T& e, T d, memory_order o)
  255. { return ref().compare_exchange_weak(e, d, o); }
  256. bool compare_exchange_weak(T& e, T d, memory_order so, memory_order fo)
  257. { return ref().compare_exchange_weak(e, d, so, fo); }
  258. bool compare_exchange_strong(T& e, T d, memory_order o)
  259. { return ref().compare_exchange_strong(e, d, o); }
  260. bool compare_exchange_strong(T& e, T d, memory_order so, memory_order fo)
  261. { return ref().compare_exchange_strong(e, d, so, fo); }
  262. T fetch_add(T v, memory_order o) { return ref().fetch_add(v, o); }
  263. T fetch_sub(T v, memory_order o) { return ref().fetch_sub(v, o); }
  264. T fetch_and(T v, memory_order o) { return ref().fetch_and(v, o); }
  265. T fetch_or(T v, memory_order o) { return ref().fetch_or(v, o); }
  266. T fetch_xor(T v, memory_order o) { return ref().fetch_xor(v, o); }
  267. static_atomic& operator=(T v) {
  268. store(v, memory_order_seq_cst);
  269. return *this;
  270. }
  271. private:
  272. DISALLOW_ASSIGN(static_atomic);
  273. BAIDU_CASSERT(sizeof(T) == sizeof(atomic<T>), size_must_match);
  274. atomic<T>& ref() {
  275. // Suppress strict-alias warnings.
  276. atomic<T>* p = reinterpret_cast<atomic<T>*>(&val);
  277. return *p;
  278. }
  279. };
  280. } // namespace base
  281. #endif // BASE_ATOMICOPS_H_