1// Protocol Buffers - Google's data interchange format
2// Copyright 2012 Google Inc.  All rights reserved.
3// http://code.google.com/p/protobuf/
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
31// The routines exported by this module are subtle.  If you use them, even if
32// you get the code right, it will depend on careful reasoning about atomicity
33// and memory ordering; it will be less readable, and harder to maintain.  If
34// you plan to use these routines, you should have a good reason, such as solid
35// evidence that performance would otherwise suffer, or there being no
36// alternative.  You should assume only properties explicitly guaranteed by the
37// specifications in this file.  You are almost certainly _not_ writing code
38// just for the x86; if you assume x86 semantics, x86 hardware bugs and
39// implementations on other archtectures will cause your code to break.  If you
40// do not know what you are doing, avoid these routines, and use a Mutex.
41//
42// It is incorrect to make direct assignments to/from an atomic variable.
43// You should use one of the Load or Store routines.  The NoBarrier
44// versions are provided when no barriers are needed:
45//   NoBarrier_Store()
46//   NoBarrier_Load()
47// Although there are currently no compiler enforcement, you are encouraged
48// to use these.
49
50// This header and the implementations for each platform (located in
51// atomicops_internals_*) must be kept in sync with the upstream code (V8).
52
53#ifndef GOOGLE_PROTOBUF_ATOMICOPS_H_
54#define GOOGLE_PROTOBUF_ATOMICOPS_H_
55
56// Don't include this file for people not concerned about thread safety.
57#ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
58
59#include <google/protobuf/stubs/platform_macros.h>
60
61namespace google {
62namespace protobuf {
63namespace internal {
64
65typedef int32 Atomic32;
66#ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
67// We need to be able to go between Atomic64 and AtomicWord implicitly.  This
68// means Atomic64 and AtomicWord should be the same type on 64-bit.
69#if defined(__ILP32__) || defined(GOOGLE_PROTOBUF_OS_NACL)
70// NaCl's intptr_t is not actually 64-bits on 64-bit!
71// http://code.google.com/p/nativeclient/issues/detail?id=1162
72typedef int64 Atomic64;
73#else
74typedef intptr_t Atomic64;
75#endif
76#endif
77
78// Use AtomicWord for a machine-sized pointer.  It will use the Atomic32 or
79// Atomic64 routines below, depending on your architecture.
80typedef intptr_t AtomicWord;
81
82// Atomically execute:
83//      result = *ptr;
84//      if (*ptr == old_value)
85//        *ptr = new_value;
86//      return result;
87//
88// I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
89// Always return the old value of "*ptr"
90//
91// This routine implies no memory barriers.
92Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
93                                  Atomic32 old_value,
94                                  Atomic32 new_value);
95
96// Atomically store new_value into *ptr, returning the previous value held in
97// *ptr.  This routine implies no memory barriers.
98Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
99
100// Atomically increment *ptr by "increment".  Returns the new value of
101// *ptr with the increment applied.  This routine implies no memory barriers.
102Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
103
104Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
105                                 Atomic32 increment);
106
107// These following lower-level operations are typically useful only to people
108// implementing higher-level synchronization operations like spinlocks,
109// mutexes, and condition-variables.  They combine CompareAndSwap(), a load, or
110// a store with appropriate memory-ordering instructions.  "Acquire" operations
111// ensure that no later memory access can be reordered ahead of the operation.
112// "Release" operations ensure that no previous memory access can be reordered
113// after the operation.  "Barrier" operations have both "Acquire" and "Release"
114// semantics.   A MemoryBarrier() has "Barrier" semantics, but does no memory
115// access.
116Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
117                                Atomic32 old_value,
118                                Atomic32 new_value);
119Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
120                                Atomic32 old_value,
121                                Atomic32 new_value);
122
123void MemoryBarrier();
124void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
125void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
126void Release_Store(volatile Atomic32* ptr, Atomic32 value);
127
128Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
129Atomic32 Acquire_Load(volatile const Atomic32* ptr);
130Atomic32 Release_Load(volatile const Atomic32* ptr);
131
132// 64-bit atomic operations (only available on 64-bit processors).
133#ifdef GOOGLE_PROTOBUF_ARCH_64_BIT
134Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
135                                  Atomic64 old_value,
136                                  Atomic64 new_value);
137Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
138Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
139Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
140
141Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
142                                Atomic64 old_value,
143                                Atomic64 new_value);
144Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
145                                Atomic64 old_value,
146                                Atomic64 new_value);
147void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
148void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
149void Release_Store(volatile Atomic64* ptr, Atomic64 value);
150Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
151Atomic64 Acquire_Load(volatile const Atomic64* ptr);
152Atomic64 Release_Load(volatile const Atomic64* ptr);
153#endif  // GOOGLE_PROTOBUF_ARCH_64_BIT
154
155}  // namespace internal
156}  // namespace protobuf
157}  // namespace google
158
159// Include our platform specific implementation.
160#define GOOGLE_PROTOBUF_ATOMICOPS_ERROR \
161#error "Atomic operations are not supported on your platform"
162
163// ThreadSanitizer, http://clang.llvm.org/docs/ThreadSanitizer.html.
164#if defined(THREAD_SANITIZER)
165#include <google/protobuf/stubs/atomicops_internals_tsan.h>
166// MSVC.
167#elif defined(_MSC_VER)
168#if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
169#include <google/protobuf/stubs/atomicops_internals_x86_msvc.h>
170#else
171GOOGLE_PROTOBUF_ATOMICOPS_ERROR
172#endif
173
174// Apple.
175#elif defined(GOOGLE_PROTOBUF_OS_APPLE)
176#include <google/protobuf/stubs/atomicops_internals_macosx.h>
177
178// GCC.
179#elif defined(__GNUC__)
180#if defined(GOOGLE_PROTOBUF_ARCH_IA32) || defined(GOOGLE_PROTOBUF_ARCH_X64)
181#include <google/protobuf/stubs/atomicops_internals_x86_gcc.h>
182#elif defined(GOOGLE_PROTOBUF_ARCH_ARM)
183#include <google/protobuf/stubs/atomicops_internals_arm_gcc.h>
184#elif defined(GOOGLE_PROTOBUF_ARCH_AARCH64)
185#include <google/protobuf/stubs/atomicops_internals_arm64_gcc.h>
186#elif defined(GOOGLE_PROTOBUF_ARCH_ARM_QNX)
187#include <google/protobuf/stubs/atomicops_internals_arm_qnx.h>
188#elif defined(GOOGLE_PROTOBUF_ARCH_MIPS) || defined(GOOGLE_PROTOBUF_ARCH_MIPS64)
189#include <google/protobuf/stubs/atomicops_internals_mips_gcc.h>
190#elif defined(__pnacl__)
191#include <google/protobuf/stubs/atomicops_internals_pnacl.h>
192#else
193GOOGLE_PROTOBUF_ATOMICOPS_ERROR
194#endif
195
196// Unknown.
197#else
198GOOGLE_PROTOBUF_ATOMICOPS_ERROR
199#endif
200
201// On some platforms we need additional declarations to make AtomicWord
202// compatible with our other Atomic* types.
203#if defined(GOOGLE_PROTOBUF_OS_APPLE)
204#include <google/protobuf/stubs/atomicops_internals_atomicword_compat.h>
205#endif
206
207#undef GOOGLE_PROTOBUF_ATOMICOPS_ERROR
208
209#endif  // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
210
211#endif  // GOOGLE_PROTOBUF_ATOMICOPS_H_
212