atomicops.h 6.04 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7 8 9 10 11 12 13 14 15 16

// The routines exported by this module are subtle.  If you use them, even if
// you get the code right, it will depend on careful reasoning about atomicity
// and memory ordering; it will be less readable, and harder to maintain.  If
// you plan to use these routines, you should have a good reason, such as solid
// evidence that performance would otherwise suffer, or there being no
// alternative.  You should assume only properties explicitly guaranteed by the
// specifications in this file.  You are almost certainly _not_ writing code
// just for the x86; if you assume x86 semantics, x86 hardware bugs and
// implementations on other archtectures will cause your code to break.  If you
// do not know what you are doing, avoid these routines, and use a Mutex.
//
// It is incorrect to make direct assignments to/from an atomic variable.
17 18 19 20
// You should use one of the Load or Store routines.  The Relaxed  versions
// are provided when no fences are needed:
//   Relaxed_Store()
//   Relaxed_Load()
21 22 23 24
// Although there are currently no compiler enforcement, you are encouraged
// to use these.
//

25 26
#ifndef V8_BASE_ATOMICOPS_H_
#define V8_BASE_ATOMICOPS_H_
27

28
#include <stdint.h>
29 30 31 32 33 34 35 36

// Small C++ header which defines implementation specific macros used to
// identify the STL implementation.
// - libc++: captures __config for _LIBCPP_VERSION
// - libstdc++: captures bits/c++config.h for __GLIBCXX__
#include <cstddef>

#include "src/base/base-export.h"
37
#include "src/base/build_config.h"
38 39

namespace v8 {
40
namespace base {
41

42
typedef char Atomic8;
43
typedef int32_t Atomic32;
yangguo's avatar
yangguo committed
44
#if defined(V8_HOST_ARCH_64_BIT)
45 46
// We need to be able to go between Atomic64 and AtomicWord implicitly.  This
// means Atomic64 and AtomicWord should be the same type on 64-bit.
47
#if defined(__ILP32__)
48 49 50
typedef int64_t Atomic64;
#else
typedef intptr_t Atomic64;
yangguo's avatar
yangguo committed
51
#endif  // defined(__ILP32__)
52
#endif  // defined(V8_HOST_ARCH_64_BIT)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

// Use AtomicWord for a machine-sized pointer.  It will use the Atomic32 or
// Atomic64 routines below, depending on your architecture.
typedef intptr_t AtomicWord;

// Atomically execute:
//      result = *ptr;
//      if (*ptr == old_value)
//        *ptr = new_value;
//      return result;
//
// I.e., replace "*ptr" with "new_value" if "*ptr" used to be "old_value".
// Always return the old value of "*ptr"
//
// This routine implies no memory barriers.
68 69
Atomic32 Relaxed_CompareAndSwap(volatile Atomic32* ptr, Atomic32 old_value,
                                Atomic32 new_value);
70 71 72

// Atomically store new_value into *ptr, returning the previous value held in
// *ptr.  This routine implies no memory barriers.
73
Atomic32 Relaxed_AtomicExchange(volatile Atomic32* ptr, Atomic32 new_value);
74 75 76

// Atomically increment *ptr by "increment".  Returns the new value of
// *ptr with the increment applied.  This routine implies no memory barriers.
77
Atomic32 Relaxed_AtomicIncrement(volatile Atomic32* ptr, Atomic32 increment);
78 79 80 81 82 83

Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr,
                                 Atomic32 increment);

// These following lower-level operations are typically useful only to people
// implementing higher-level synchronization operations like spinlocks,
84 85 86 87 88 89 90
// mutexes, and condition-variables.  They combine CompareAndSwap(), a load,
// or a store with appropriate memory-ordering instructions.  "Acquire"
// operations ensure that no later memory access can be reordered ahead of the
// operation. "Release" operations ensure that no previous memory access can
// be reordered after the operation.  "Fence" operations have both "Acquire"
// and "Release" semantics. A SeqCst_MemoryFence() has "Fence" semantics, but
// does no memory access.
91 92 93 94 95 96 97
Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr,
                                Atomic32 old_value,
                                Atomic32 new_value);
Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr,
                                Atomic32 old_value,
                                Atomic32 new_value);

98
void SeqCst_MemoryFence();
99 100
void Relaxed_Store(volatile Atomic8* ptr, Atomic8 value);
void Relaxed_Store(volatile Atomic32* ptr, Atomic32 value);
101 102
void Release_Store(volatile Atomic32* ptr, Atomic32 value);

103 104
Atomic8 Relaxed_Load(volatile const Atomic8* ptr);
Atomic32 Relaxed_Load(volatile const Atomic32* ptr);
105 106 107 108
Atomic32 Acquire_Load(volatile const Atomic32* ptr);

// 64-bit atomic operations (only available on 64-bit processors).
#ifdef V8_HOST_ARCH_64_BIT
109 110 111 112
Atomic64 Relaxed_CompareAndSwap(volatile Atomic64* ptr, Atomic64 old_value,
                                Atomic64 new_value);
Atomic64 Relaxed_AtomicExchange(volatile Atomic64* ptr, Atomic64 new_value);
Atomic64 Relaxed_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);
113 114 115 116 117 118 119 120
Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, Atomic64 increment);

Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr,
                                Atomic64 old_value,
                                Atomic64 new_value);
Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr,
                                Atomic64 old_value,
                                Atomic64 new_value);
121
void Relaxed_Store(volatile Atomic64* ptr, Atomic64 value);
122
void Release_Store(volatile Atomic64* ptr, Atomic64 value);
123
Atomic64 Relaxed_Load(volatile const Atomic64* ptr);
124 125 126
Atomic64 Acquire_Load(volatile const Atomic64* ptr);
#endif  // V8_HOST_ARCH_64_BIT

127 128
}  // namespace base
}  // namespace v8
129

130
#if defined(V8_OS_WIN)
131
#include "src/base/atomicops_internals_std.h"
132
#else
133 134
// TODO(ulan): Switch to std version after performance regression with Wheezy
// sysroot is no longer relevant. Debian Wheezy LTS ends on 31st of May 2018.
135
#include "src/base/atomicops_internals_portable.h"
136 137
#endif

138 139
// On some platforms we need additional declarations to make
// AtomicWord compatible with our other Atomic* types.
140
#if defined(V8_OS_MACOSX) || defined(V8_OS_OPENBSD) || defined(V8_OS_AIX)
141
#include "src/base/atomicops_internals_atomicword_compat.h"
142 143
#endif

144
#endif  // V8_BASE_ATOMICOPS_H_