atomicops.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // Small C++ header which defines implementation specific macros used to
  29. // identify the STL implementation.
  30. // - libc++: captures __config for _LIBCPP_VERSION
  31. // - libstdc++: captures bits/c++config.h for __GLIBCXX__
  32. #include <cstddef>
  33. #include "build/build_config.h"
  34. namespace base {
  35. namespace subtle {
  36. typedef int32_t Atomic32;
  37. #ifdef ARCH_CPU_64_BITS
  38. // We need to be able to go between Atomic64 and AtomicWord implicitly. This
  39. // means Atomic64 and AtomicWord should be the same type on 64-bit.
  40. #if defined(__ILP32__) || BUILDFLAG(IS_NACL)
  41. // NaCl's intptr_t is not actually 64-bits on 64-bit!
  42. // http://code.google.com/p/nativeclient/issues/detail?id=1162
  43. typedef int64_t Atomic64;
  44. #else
  45. typedef intptr_t Atomic64;
  46. #endif
  47. #endif
  48. // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
  49. // Atomic64 routines below, depending on your architecture.
  50. typedef intptr_t AtomicWord;
  51. // Atomically execute:
  52. // result = *ptr;
  53. // if (*ptr == old_value)
  54. // *ptr = new_value;
  55. // return result;
  56. //
  57. // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
  58. // Always return the old value of "*ptr"
  59. //
  60. // This routine implies no memory barriers.
  61. Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  62. Atomic32 old_value,
  63. Atomic32 new_value);
  64. // Atomically store new_value into *ptr, returning the previous value held in
  65. // *ptr. This routine implies no memory barriers.
  66. Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
  67. // Atomically increment *ptr by "increment". Returns the new value of
  68. // *ptr with the increment applied. This routine implies no memory barriers.
  69. Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
  70. Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  71. Atomic32 increment);
  72. // These following lower-level operations are typically useful only to people
  73. // implementing higher-level synchronization operations like spinlocks,
  74. // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
  75. // a store with appropriate memory-ordering instructions. "Acquire" operations
  76. // ensure that no later memory access can be reordered ahead of the operation.
  77. // "Release" operations ensure that no previous memory access can be reordered
  78. // after the operation. "Barrier" operations have both "Acquire" and "Release"
  79. // semantics.
  80. Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  81. Atomic32 old_value,
  82. Atomic32 new_value);
  83. Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  84. Atomic32 old_value,
  85. Atomic32 new_value);
  86. void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
  87. void Release_Store(volatile Atomic32* ptr, Atomic32 value);
  88. Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
  89. Atomic32 Acquire_Load(volatile const Atomic32* ptr);
  90. // 64-bit atomic operations (only available on 64-bit processors).
  91. #ifdef ARCH_CPU_64_BITS
  92. Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
  93. Atomic64 old_value,
  94. Atomic64 new_value);
  95. Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
  96. Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  97. Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  98. Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
  99. Atomic64 old_value,
  100. Atomic64 new_value);
  101. Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
  102. Atomic64 old_value,
  103. Atomic64 new_value);
  104. void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
  105. void Release_Store(volatile Atomic64* ptr, Atomic64 value);
  106. Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
  107. Atomic64 Acquire_Load(volatile const Atomic64* ptr);
  108. #endif // ARCH_CPU_64_BITS
  109. } // namespace subtle
  110. } // namespace base
  111. #include "base/atomicops_internals_portable.h"
  112. // On some platforms we need additional declarations to make
  113. // AtomicWord compatible with our other Atomic* types.
  114. #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OPENBSD)
  115. #include "base/atomicops_internals_atomicword_compat.h"
  116. #endif
  117. #endif // BASE_ATOMICOPS_H_