blob: 833e1704291b1d55842e985a4d8dc7c2948d9520 [file] [log] [blame]
[email protected]f7d2fbd2012-06-18 02:47:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]611dbe02008-08-05 09:57:364
5// For atomic operations on reference counts, see atomic_refcount.h.
6// For atomic operations on sequence numbers, see atomic_sequence_num.h.
7
8// The routines exported by this module are subtle. If you use them, even if
9// you get the code right, it will depend on careful reasoning about atomicity
10// and memory ordering; it will be less readable, and harder to maintain. If
11// you plan to use these routines, you should have a good reason, such as solid
12// evidence that performance would otherwise suffer, or there being no
13// alternative. You should assume only properties explicitly guaranteed by the
14// specifications in this file. You are almost certainly _not_ writing code
15// just for the x86; if you assume x86 semantics, x86 hardware bugs and
16// implementations on other archtectures will cause your code to break. If you
17// do not know what you are doing, avoid these routines, and use a Mutex.
18//
19// It is incorrect to make direct assignments to/from an atomic variable.
20// You should use one of the Load or Store routines. The NoBarrier
21// versions are provided when no barriers are needed:
22// NoBarrier_Store()
23// NoBarrier_Load()
24// Although there are currently no compiler enforcement, you are encouraged
25// to use these.
26//
27
28#ifndef BASE_ATOMICOPS_H_
29#define BASE_ATOMICOPS_H_
30
jfb57a4e4a2014-10-14 15:06:0531#include <cassert> // Small C++ header which defines implementation specific
32 // macros used to identify the STL implementation.
[email protected]92677102014-02-08 04:59:5533#include <stdint.h>
34
jfb57a4e4a2014-10-14 15:06:0535#include "base/base_export.h"
[email protected]2edc2862011-04-04 18:04:3736#include "build/build_config.h"
[email protected]611dbe02008-08-05 09:57:3637
[email protected]a48610c2013-01-15 08:51:2838#if defined(OS_WIN) && defined(ARCH_CPU_64_BITS)
39// windows.h #defines this (only on x64). This causes problems because the
40// public API also uses MemoryBarrier at the public name for this fence. So, on
41// X64, undef it, and call its documented
42// (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684208.aspx)
43// implementation directly.
44#undef MemoryBarrier
45#endif
46
[email protected]611dbe02008-08-05 09:57:3647namespace base {
48namespace subtle {
49
[email protected]92677102014-02-08 04:59:5550typedef int32_t Atomic32;
[email protected]616f9a12009-07-27 21:17:2351#ifdef ARCH_CPU_64_BITS
52// We need to be able to go between Atomic64 and AtomicWord implicitly. This
53// means Atomic64 and AtomicWord should be the same type on 64-bit.
[email protected]c163248d2013-02-07 00:32:4954#if defined(__ILP32__) || defined(OS_NACL)
[email protected]bd254b42010-12-01 07:34:5855// NaCl's intptr_t is not actually 64-bits on 64-bit!
56// http://code.google.com/p/nativeclient/issues/detail?id=1162
57typedef int64_t Atomic64;
58#else
[email protected]616f9a12009-07-27 21:17:2359typedef intptr_t Atomic64;
[email protected]611dbe02008-08-05 09:57:3660#endif
[email protected]bd254b42010-12-01 07:34:5861#endif
[email protected]611dbe02008-08-05 09:57:3662
63// Use AtomicWord for a machine-sized pointer. It will use the Atomic32 or
64// Atomic64 routines below, depending on your architecture.
65typedef intptr_t AtomicWord;
66
67// Atomically execute:
68// result = *ptr;
69// if (*ptr == old_value)
70// *ptr = new_value;
71// return result;
72//
73// I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
74// Always return the old value of "*ptr"
75//
76// This routine implies no memory barriers.
77Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr,
78 Atomic32 old_value,
79 Atomic32 new_value);
80
81// Atomically store new_value into *ptr, returning the previous value held in
82// *ptr. This routine implies no memory barriers.
83Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
84
85// Atomically increment *ptr by "increment". Returns the new value of
86// *ptr with the increment applied. This routine implies no memory barriers.
87Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
88
89Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
90 Atomic32 increment);
91
92// These following lower-level operations are typically useful only to people
93// implementing higher-level synchronization operations like spinlocks,
94// mutexes, and condition-variables. They combine CompareAndSwap(), a load, or
95// a store with appropriate memory-ordering instructions. "Acquire" operations
96// ensure that no later memory access can be reordered ahead of the operation.
97// "Release" operations ensure that no previous memory access can be reordered
98// after the operation. "Barrier" operations have both "Acquire" and "Release"
99// semantics. A MemoryBarrier() has "Barrier" semantics, but does no memory
100// access.
101Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
102 Atomic32 old_value,
103 Atomic32 new_value);
104Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
105 Atomic32 old_value,
106 Atomic32 new_value);
107
108void MemoryBarrier();
109void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value);
110void Acquire_Store(volatile Atomic32* ptr, Atomic32 value);
111void Release_Store(volatile Atomic32* ptr, Atomic32 value);
112
113Atomic32 NoBarrier_Load(volatile const Atomic32* ptr);
114Atomic32 Acquire_Load(volatile const Atomic32* ptr);
115Atomic32 Release_Load(volatile const Atomic32* ptr);
116
117// 64-bit atomic operations (only available on 64-bit processors).
[email protected]616f9a12009-07-27 21:17:23118#ifdef ARCH_CPU_64_BITS
[email protected]611dbe02008-08-05 09:57:36119Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr,
120 Atomic64 old_value,
121 Atomic64 new_value);
122Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
123Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
124Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
125
126Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
127 Atomic64 old_value,
128 Atomic64 new_value);
129Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
130 Atomic64 old_value,
131 Atomic64 new_value);
132void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value);
133void Acquire_Store(volatile Atomic64* ptr, Atomic64 value);
134void Release_Store(volatile Atomic64* ptr, Atomic64 value);
135Atomic64 NoBarrier_Load(volatile const Atomic64* ptr);
136Atomic64 Acquire_Load(volatile const Atomic64* ptr);
137Atomic64 Release_Load(volatile const Atomic64* ptr);
[email protected]616f9a12009-07-27 21:17:23138#endif // ARCH_CPU_64_BITS
[email protected]611dbe02008-08-05 09:57:36139
[email protected]abe936a32014-04-13 17:36:27140} // namespace subtle
[email protected]611dbe02008-08-05 09:57:36141} // namespace base
142
jfb57a4e4a2014-10-14 15:06:05143// The following x86 CPU features are used in atomicops_internals_x86_gcc.h, but
144// this file is duplicated inside of Chrome: protobuf and tcmalloc rely on the
145// struct being present at link time. Some parts of Chrome can currently use the
146// portable interface whereas others still use GCC one. The include guards are
147// the same as in atomicops_internals_x86_gcc.cc.
148#if defined(__i386__) || defined(__x86_64__)
149// This struct is not part of the public API of this module; clients may not
150// use it. (However, it's exported via BASE_EXPORT because clients implicitly
151// do use it at link time by inlining these functions.)
152// Features of this x86. Values may not be correct before main() is run,
153// but are set conservatively.
154struct AtomicOps_x86CPUFeatureStruct {
155 bool has_amd_lock_mb_bug; // Processor has AMD memory-barrier bug; do lfence
156 // after acquire compare-and-swap.
157 // The following fields are unused by Chrome's base implementation but are
158 // still used by copies of the same code in other parts of the code base. This
159 // causes an ODR violation, and the other code is likely reading invalid
160 // memory.
161 // TODO(jfb) Delete these fields once the rest of the Chrome code base doesn't
162 // depend on them.
163 bool has_sse2; // Processor has SSE2.
164 bool has_cmpxchg16b; // Processor supports cmpxchg16b instruction.
165};
166BASE_EXPORT extern struct AtomicOps_x86CPUFeatureStruct
167 AtomicOps_Internalx86CPUFeatures;
[email protected]611dbe02008-08-05 09:57:36168#endif
169
jfb57a4e4a2014-10-14 15:06:05170// Try to use a portable implementation based on C++11 atomics.
171//
172// Some toolchains support C++11 language features without supporting library
173// features (recent compiler, older STL). Whitelist libstdc++ and libc++ that we
174// know will have <atomic> when compiling C++11.
175#if ((__cplusplus >= 201103L) && \
176 ((defined(__GLIBCXX__) && (__GLIBCXX__ > 20110216)) || \
177 (defined(_LIBCPP_VERSION) && (_LIBCPP_STD_VER >= 11))))
178# include "base/atomicops_internals_portable.h"
179#else // Otherwise use a platform specific implementation.
180# if defined(THREAD_SANITIZER)
181# error "Thread sanitizer must use the portable atomic operations"
182# elif (defined(OS_WIN) && defined(COMPILER_MSVC) && \
183 defined(ARCH_CPU_X86_FAMILY))
184# include "base/atomicops_internals_x86_msvc.h"
185# elif defined(OS_MACOSX)
186# include "base/atomicops_internals_mac.h"
187# elif defined(OS_NACL)
188# include "base/atomicops_internals_gcc.h"
189# elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARMEL)
190# include "base/atomicops_internals_arm_gcc.h"
191# elif defined(COMPILER_GCC) && defined(ARCH_CPU_ARM64)
192# include "base/atomicops_internals_arm64_gcc.h"
193# elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY)
194# include "base/atomicops_internals_x86_gcc.h"
195# elif (defined(COMPILER_GCC) && \
196 (defined(ARCH_CPU_MIPS_FAMILY) || defined(ARCH_CPU_MIPS64_FAMILY)))
197# include "base/atomicops_internals_mips_gcc.h"
198# else
199# error "Atomic operations are not supported on your platform"
200# endif
201#endif // Portable / non-portable includes.
202
[email protected]0c6f3b0c2011-04-15 06:15:04203// On some platforms we need additional declarations to make
204// AtomicWord compatible with our other Atomic* types.
205#if defined(OS_MACOSX) || defined(OS_OPENBSD)
206#include "base/atomicops_internals_atomicword_compat.h"
207#endif
208
[email protected]611dbe02008-08-05 09:57:36209#endif // BASE_ATOMICOPS_H_