atomicops.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2012 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // The routines exported by this module are subtle. If you use them, even if
  31. // you get the code right, it will depend on careful reasoning about atomicity
  32. // and memory ordering; it will be less readable, and harder to maintain. If
  33. // you plan to use these routines, you should have a good reason, such as solid
  34. // evidence that performance would otherwise suffer, or there being no
  35. // alternative. You should assume only properties explicitly guaranteed by the
  36. // specifications in this file. You are almost certainly _not_ writing code
  37. // just for the x86; if you assume x86 semantics, x86 hardware bugs and
  38. // implementations on other archtectures will cause your code to break. If you
  39. // do not know what you are doing, avoid these routines, and use a Mutex.
  40. //
  41. // It is incorrect to make direct assignments to/from an atomic variable.
  42. // You should use one of the Load or Store routines. The NoBarrier
  43. // versions are provided when no barriers are needed:
  44. // NoBarrier_Store()
  45. // NoBarrier_Load()
  46. // Although there are currently no compiler enforcement, you are encouraged
  47. // to use these.
  48. // This header and the implementations for each platform (located in
  49. // atomicops_internals_*) must be kept in sync with the upstream code (V8).
  50. #ifndef GOOGLE_PROTOBUF_ATOMICOPS_H_
  51. #define GOOGLE_PROTOBUF_ATOMICOPS_H_
  52. // Don't include this file for people not concerned about thread safety.
  53. #ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  54. #include <google/protobuf/stubs/platform_macros.h>
  55. namespace google {
  56. namespace protobuf {
  57. namespace internal {
  58. typedef int32 Atomic32;
  59. #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
  60. // We need to be able to go between Atomic64 and AtomicWord implicitly. This
  61. // means Atomic64 and AtomicWord should be the same type on 64-bit.
  62. #if defined(__ILP32__) || defined(GOOGLE_PROTOBUF_OS_NACL) || defined(GOOGLE_PROTOBUF_ARCH_SPARC)
  63. // NaCl's intptr_t is not actually 64-bits on 64-bit!
  64. // http://code.google.com/p/nativeclient/issues/detail?id=1162
  65. // sparcv9's pointer type is 32bits
  66. typedef int64 Atomic64;
  67. #else
  68. typedef intptr_t Atomic64;
  69. #endif
  70. #endif
  71. // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
  72. // Atomic64 routines below, depending on your architecture.
  73. typedef intptr_t AtomicWord;
  74. // Atomically execute:
  75. // result = *ptr;
  76. // if (*ptr == old_value)
  77. // *ptr = new_value;
  78. // return result;
  79. //
  80. // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
  81. // Always return the old value of "*ptr"
  82. //
  83. // This routine implies no memory barriers.
  84. Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  85. Atomic32 old_value,
  86. Atomic32 new_value);
  87. // Atomically store new_value into *ptr, returning the previous value held in
  88. // *ptr. This routine implies no memory barriers.
  89. Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
  90. // Atomically increment *ptr by "increment". Returns the new value of
  91. // *ptr with the increment applied. This routine implies no memory barriers.
  92. Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
  93. Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  94. Atomic32 increment);
  95. // These following lower-level operations are typically useful only to people
  96. // implementing higher-level synchronization operations like spinlocks,
  97. // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
  98. // a store with appropriate memory-ordering instructions. "Acquire" operations
  99. // ensure that no later memory access can be reordered ahead of the operation.
  100. // "Release" operations ensure that no previous memory access can be reordered
  101. // after the operation. "Barrier" operations have both "Acquire" and "Release"
  102. // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
  103. // access.
  104. Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  105. Atomic32 old_value,
  106. Atomic32 new_value);
  107. Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  108. Atomic32 old_value,
  109. Atomic32 new_value);
  110. #if defined(__MINGW32__) && defined(MemoryBarrier)
  111. #undef MemoryBarrier
  112. #endif
  113. void MemoryBarrier();
  114. void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
  115. void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
  116. void Release_Store(volatile Atomic32* ptr, Atomic32 value);
  117. Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
  118. Atomic32 Acquire_Load(volatile const Atomic32* ptr);
  119. Atomic32 Release_Load(volatile const Atomic32* ptr);
  120. // 64-bit atomic operations (only available on 64-bit processors).
  121. #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
  122. Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
  123. Atomic64 old_value,
  124. Atomic64 new_value);
  125. Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
  126. Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  127. Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  128. Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
  129. Atomic64 old_value,
  130. Atomic64 new_value);
  131. Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
  132. Atomic64 old_value,
  133. Atomic64 new_value);
  134. void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
  135. void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
  136. void Release_Store(volatile Atomic64* ptr, Atomic64 value);
  137. Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
  138. Atomic64 Acquire_Load(volatile const Atomic64* ptr);
  139. Atomic64 Release_Load(volatile const Atomic64* ptr);
  140. #endif // GOOGLE_PROTOBUF_ARCH_64_BIT
  141. } // namespace internal
  142. } // namespace protobuf
  143. } // namespace google
  144. // Include our platform specific implementation.
  145. #define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \
  146. #error "Atomic operations are not supported on your platform"
  147. // ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html.
  148. #if defined(THREAD_SANITIZER)
  149. #include <google/protobuf/stubs/atomicops_internals_tsan.h>
  150. // MSVC.
  151. #elif defined(_MSC_VER)
  152. #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
  153. #include <google/protobuf/stubs/atomicops_internals_x86_msvc.h>
  154. #else
  155. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  156. #endif
  157. // Solaris
  158. #elif defined(GOOGLE_PROTOBUF_OS_SOLARIS)
  159. #include <google/protobuf/stubs/atomicops_internals_solaris.h>
  160. // Apple.
  161. #elif defined(GOOGLE_PROTOBUF_OS_APPLE)
  162. #include <google/protobuf/stubs/atomicops_internals_macosx.h>
  163. // GCC.
  164. #elif defined(__GNUC__)
  165. #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
  166. #include <google/protobuf/stubs/atomicops_internals_x86_gcc.h>
  167. #elif defined(GOOGLE_PROTOBUF_ARCH_ARM) && defined(__linux__)
  168. #include <google/protobuf/stubs/atomicops_internals_arm_gcc.h>
  169. #elif defined(GOOGLE_PROTOBUF_ARCH_AARCH64)
  170. #include <google/protobuf/stubs/atomicops_internals_arm64_gcc.h>
  171. #elif defined(GOOGLE_PROTOBUF_ARCH_ARM_QNX)
  172. #include <google/protobuf/stubs/atomicops_internals_arm_qnx.h>
  173. #elif defined(GOOGLE_PROTOBUF_ARCH_MIPS) || defined(GOOGLE_PROTOBUF_ARCH_MIPS64)
  174. #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h>
  175. #elif defined(__native_client__)
  176. #include <google/protobuf/stubs/atomicops_internals_pnacl.h>
  177. #elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4))
  178. #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h>
  179. #elif defined(__clang__)
  180. #if __has_extension(c_atomic)
  181. #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h>
  182. #else
  183. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  184. #endif
  185. #else
  186. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  187. #endif
  188. // Unknown.
  189. #else
  190. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  191. #endif
  192. // On some platforms we need additional declarations to make AtomicWord
  193. // compatible with our other Atomic* types.
  194. #if defined(GOOGLE_PROTOBUF_OS_APPLE)
  195. #include <google/protobuf/stubs/atomicops_internals_atomicword_compat.h>
  196. #endif
  197. #undef GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  198. #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  199. #endif // GOOGLE_PROTOBUF_ATOMICOPS_H_