atomicops.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/common.h>
  55. #include <google/protobuf/stubs/platform_macros.h>
  56. namespace google {
  57. namespace protobuf {
  58. namespace internal {
  59. #if defined(GOOGLE_PROTOBUF_ARCH_POWER)
  60. #if defined(_LP64) || defined(__LP64__)
  61. typedef int32 Atomic32;
  62. typedef intptr_t Atomic64;
  63. #else
  64. typedef intptr_t Atomic32;
  65. typedef int64 Atomic64;
  66. #endif
  67. #else
  68. typedef int32 Atomic32;
  69. #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
  70. // We need to be able to go between Atomic64 and AtomicWord implicitly. This
  71. // means Atomic64 and AtomicWord should be the same type on 64-bit.
  72. #if defined(__ILP32__) || defined(GOOGLE_PROTOBUF_OS_NACL) || defined(GOOGLE_PROTOBUF_ARCH_SPARC)
  73. // NaCl's intptr_t is not actually 64-bits on 64-bit!
  74. // http://code.google.com/p/nativeclient/issues/detail?id=1162
  75. // sparcv9's pointer type is 32bits
  76. typedef int64 Atomic64;
  77. #else
  78. typedef intptr_t Atomic64;
  79. #endif
  80. #endif
  81. #endif
  82. // Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
  83. // Atomic64 routines below, depending on your architecture.
  84. typedef intptr_t AtomicWord;
  85. // Atomically execute:
  86. // result = *ptr;
  87. // if (*ptr == old_value)
  88. // *ptr = new_value;
  89. // return result;
  90. //
  91. // I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
  92. // Always return the old value of "*ptr"
  93. //
  94. // This routine implies no memory barriers.
  95. Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
  96. Atomic32 old_value,
  97. Atomic32 new_value);
  98. // Atomically store new_value into *ptr, returning the previous value held in
  99. // *ptr. This routine implies no memory barriers.
  100. Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
  101. // Atomically increment *ptr by "increment". Returns the new value of
  102. // *ptr with the increment applied. This routine implies no memory barriers.
  103. Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
  104. Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
  105. Atomic32 increment);
  106. // These following lower-level operations are typically useful only to people
  107. // implementing higher-level synchronization operations like spinlocks,
  108. // mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
  109. // a store with appropriate memory-ordering instructions. "Acquire" operations
  110. // ensure that no later memory access can be reordered ahead of the operation.
  111. // "Release" operations ensure that no previous memory access can be reordered
  112. // after the operation. "Barrier" operations have both "Acquire" and "Release"
  113. // semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
  114. // access.
  115. Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
  116. Atomic32 old_value,
  117. Atomic32 new_value);
  118. Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
  119. Atomic32 old_value,
  120. Atomic32 new_value);
  121. #if defined(__MINGW32__) && defined(MemoryBarrier)
  122. #undef MemoryBarrier
  123. #endif
  124. void MemoryBarrier();
  125. void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
  126. void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
  127. void Release_Store(volatile Atomic32* ptr, Atomic32 value);
  128. Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
  129. Atomic32 Acquire_Load(volatile const Atomic32* ptr);
  130. Atomic32 Release_Load(volatile const Atomic32* ptr);
  131. // 64-bit atomic operations (only available on 64-bit processors).
  132. #ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
  133. Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
  134. Atomic64 old_value,
  135. Atomic64 new_value);
  136. Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
  137. Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  138. Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
  139. Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
  140. Atomic64 old_value,
  141. Atomic64 new_value);
  142. Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
  143. Atomic64 old_value,
  144. Atomic64 new_value);
  145. void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
  146. void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
  147. void Release_Store(volatile Atomic64* ptr, Atomic64 value);
  148. Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
  149. Atomic64 Acquire_Load(volatile const Atomic64* ptr);
  150. Atomic64 Release_Load(volatile const Atomic64* ptr);
  151. #endif // GOOGLE_PROTOBUF_ARCH_64_BIT
  152. } // namespace internal
  153. } // namespace protobuf
  154. } // namespace google
  155. // Include our platform specific implementation.
  156. #define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \
  157. #error "Atomic operations are not supported on your platform"
  158. // ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html.
  159. #if defined(THREAD_SANITIZER)
  160. #include <google/protobuf/stubs/atomicops_internals_tsan.h>
  161. // MSVC.
  162. #elif defined(_MSC_VER)
  163. #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
  164. #include <google/protobuf/stubs/atomicops_internals_x86_msvc.h>
  165. #else
  166. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  167. #endif
  168. // Solaris
  169. #elif defined(GOOGLE_PROTOBUF_OS_SOLARIS)
  170. #include <google/protobuf/stubs/atomicops_internals_solaris.h>
  171. // AIX
  172. #elif defined(GOOGLE_PROTOBUF_OS_AIX)
  173. #include <google/protobuf/stubs/atomicops_internals_aix.h>
  174. // Apple.
  175. #elif defined(GOOGLE_PROTOBUF_OS_APPLE)
  176. #include <google/protobuf/stubs/atomicops_internals_macosx.h>
  177. // GCC.
  178. #elif defined(__GNUC__)
  179. #if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
  180. #include <google/protobuf/stubs/atomicops_internals_x86_gcc.h>
  181. #elif defined(GOOGLE_PROTOBUF_ARCH_ARM) && defined(__linux__)
  182. #include <google/protobuf/stubs/atomicops_internals_arm_gcc.h>
  183. #elif defined(GOOGLE_PROTOBUF_ARCH_AARCH64)
  184. #include <google/protobuf/stubs/atomicops_internals_arm64_gcc.h>
  185. #elif defined(GOOGLE_PROTOBUF_ARCH_ARM_QNX)
  186. #include <google/protobuf/stubs/atomicops_internals_arm_qnx.h>
  187. #elif defined(GOOGLE_PROTOBUF_ARCH_MIPS) || defined(GOOGLE_PROTOBUF_ARCH_MIPS64)
  188. #include <google/protobuf/stubs/atomicops_internals_mips_gcc.h>
  189. #elif defined(__native_client__)
  190. #include <google/protobuf/stubs/atomicops_internals_pnacl.h>
  191. #elif (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4))
  192. #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h>
  193. #elif defined(__clang__)
  194. #if __has_extension(c_atomic)
  195. #include <google/protobuf/stubs/atomicops_internals_generic_gcc.h>
  196. #else
  197. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  198. #endif
  199. #else
  200. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  201. #endif
  202. // Unknown.
  203. #else
  204. GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  205. #endif
  206. // On some platforms we need additional declarations to make AtomicWord
  207. // compatible with our other Atomic* types.
  208. #if defined(GOOGLE_PROTOBUF_OS_APPLE)
  209. #include <google/protobuf/stubs/atomicops_internals_atomicword_compat.h>
  210. #endif
  211. #undef GOOGLE_PROTOBUF_ATOMICOPS_ERROR
  212. #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  213. #endif // GOOGLE_PROTOBUF_ATOMICOPS_H_