| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2006, Google Inc. |
| 3 |
* All rights reserved. |
| 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 |
* --- |
| 32 |
* Author: Sanjay Ghemawat |
| 33 |
*/ |
| 34 |
|
| 35 |
// SpinLock is async signal safe. |
| 36 |
// If used within a signal handler, all lock holders |
| 37 |
// should block the signal even outside the signal handler. |
| 38 |
|
| 39 |
#ifndef BASE_SPINLOCK_H_ |
| 40 |
#define BASE_SPINLOCK_H_ |
| 41 |
|
| 42 |
#include <config.h> |
| 43 |
|
| 44 |
#include <atomic> |
| 45 |
#include <type_traits> |
| 46 |
|
| 47 |
#include "base/basictypes.h" |
| 48 |
#include "base/dynamic_annotations.h" |
| 49 |
#include "base/thread_annotations.h" |
| 50 |
|
| 51 |
class LOCKABLE SpinLock { |
| 52 |
public: |
| 53 |
SpinLock() : lockword_(kSpinLockFree) { } |
| 54 |
|
| 55 |
// Special constructor for use with static SpinLock objects. E.g., |
| 56 |
// |
| 57 |
// static SpinLock lock(base::LINKER_INITIALIZED); |
| 58 |
// |
| 59 |
// When intialized using this constructor, we depend on the fact |
| 60 |
// that the linker has already initialized the memory appropriately. |
| 61 |
// A SpinLock constructed like this can be freely used from global |
| 62 |
// initializers without worrying about the order in which global |
| 63 |
// initializers run. |
| 64 |
explicit SpinLock(base::LinkerInitialized /*x*/) { |
| 65 |
// Does nothing; lockword_ is already initialized |
| 66 |
} |
| 67 |
|
| 68 |
// Acquire this SpinLock. |
| 69 |
void Lock() EXCLUSIVE_LOCK_FUNCTION() { |
| 70 |
int old = kSpinLockFree; |
| 71 |
if (!lockword_.compare_exchange_weak(old, kSpinLockHeld, std::memory_order_acquire)) { |
| 72 |
SlowLock(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Try to acquire this SpinLock without blocking and return true if the |
| 77 |
// acquisition was successful. If the lock was not acquired, false is |
| 78 |
// returned. If this SpinLock is free at the time of the call, TryLock |
| 79 |
// will return true with high probability. |
| 80 |
bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 81 |
int old = kSpinLockFree; |
| 82 |
return lockword_.compare_exchange_weak(old, kSpinLockHeld); |
| 83 |
} |
| 84 |
|
| 85 |
// Release this SpinLock, which must be held by the calling thread. |
| 86 |
void Unlock() UNLOCK_FUNCTION() { |
| 87 |
int prev_value = lockword_.exchange(kSpinLockFree, std::memory_order_release); |
| 88 |
if (prev_value != kSpinLockHeld) { |
| 89 |
// Speed the wakeup of any waiter. |
| 90 |
SlowUnlock(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Determine if the lock is held. When the lock is held by the invoking |
| 95 |
// thread, true will always be returned. Intended to be used as |
| 96 |
// CHECK(lock.IsHeld()). |
| 97 |
bool IsHeld() const { |
| 98 |
return lockword_.load(std::memory_order_relaxed) != kSpinLockFree; |
| 99 |
} |
| 100 |
|
| 101 |
static const base::LinkerInitialized LINKER_INITIALIZED; // backwards compat |
| 102 |
private: |
| 103 |
enum { kSpinLockFree = 0 }; |
| 104 |
enum { kSpinLockHeld = 1 }; |
| 105 |
enum { kSpinLockSleeper = 2 }; |
| 106 |
|
| 107 |
std::atomic<int> lockword_; |
| 108 |
|
| 109 |
void SlowLock(); |
| 110 |
void SlowUnlock(); |
| 111 |
int SpinLoop(); |
| 112 |
|
| 113 |
DISALLOW_COPY_AND_ASSIGN(SpinLock); |
| 114 |
}; |
| 115 |
|
| 116 |
// Corresponding locker object that arranges to acquire a spinlock for |
| 117 |
// the duration of a C++ scope. |
| 118 |
class SCOPED_LOCKABLE SpinLockHolder { |
| 119 |
private: |
| 120 |
SpinLock* lock_; |
| 121 |
public: |
| 122 |
explicit SpinLockHolder(SpinLock* l) EXCLUSIVE_LOCK_FUNCTION(l) |
| 123 |
: lock_(l) { |
| 124 |
l->Lock(); |
| 125 |
} |
| 126 |
SpinLockHolder(const SpinLockHolder&) = delete; |
| 127 |
~SpinLockHolder() UNLOCK_FUNCTION() { |
| 128 |
lock_->Unlock(); |
| 129 |
} |
| 130 |
}; |
| 131 |
// Catch bug where variable name is omitted, e.g. SpinLockHolder (&lock); |
| 132 |
#define SpinLockHolder(x) COMPILE_ASSERT(0, spin_lock_decl_missing_var_name) |
| 133 |
|
| 134 |
namespace tcmalloc { |
| 135 |
|
| 136 |
class TrivialOnce { |
| 137 |
public: |
| 138 |
template <typename Body> |
| 139 |
bool RunOnce(Body body) { |
| 140 |
auto done_atomic = reinterpret_cast<std::atomic<int>*>(&done_flag_); |
| 141 |
if (done_atomic->load(std::memory_order_acquire) == 1) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
SpinLockHolder h(reinterpret_cast<SpinLock*>(&lock_storage_)); |
| 146 |
|
| 147 |
if (done_atomic->load(std::memory_order_relaxed) == 1) { |
| 148 |
// barrier provided by lock |
| 149 |
return false; |
| 150 |
} |
| 151 |
body(); |
| 152 |
done_atomic->store(1, std::memory_order_release); |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
private: |
| 157 |
int done_flag_; |
| 158 |
alignas(alignof(SpinLock)) char lock_storage_[sizeof(SpinLock)]; |
| 159 |
}; |
| 160 |
|
| 161 |
static_assert(std::is_trivial<TrivialOnce>::value == true, ""); |
| 162 |
|
| 163 |
} // namespace tcmalloc |
| 164 |
|
| 165 |
|
| 166 |
#endif // BASE_SPINLOCK_H_ |