1 //===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_MUTEX_H
15 #define SANITIZER_MUTEX_H
17 #include "sanitizer_atomic.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_libc.h"
21 namespace __sanitizer {
23 class StaticSpinMutex {
26 atomic_store(&state_, 0, memory_order_relaxed);
36 return atomic_exchange(&state_, 1, memory_order_acquire) == 0;
40 atomic_store(&state_, 0, memory_order_release);
44 CHECK_EQ(atomic_load(&state_, memory_order_relaxed), 1);
48 atomic_uint8_t state_;
50 void NOINLINE LockSlow() {
51 for (int i = 0;; i++) {
55 internal_sched_yield();
56 if (atomic_load(&state_, memory_order_relaxed) == 0
57 && atomic_exchange(&state_, 1, memory_order_acquire) == 0)
63 class SpinMutex : public StaticSpinMutex {
70 SpinMutex(const SpinMutex&);
71 void operator=(const SpinMutex&);
77 // Windows does not currently support LinkerInitialized
78 explicit BlockingMutex(LinkerInitialized);
80 explicit constexpr BlockingMutex(LinkerInitialized)
81 : opaque_storage_ {0, }, owner_(0) {}
87 // This function does not guarantee an explicit check that the calling thread
88 // is the thread which owns the mutex. This behavior, while more strictly
89 // correct, causes problems in cases like StopTheWorld, where a parent thread
90 // owns the mutex but a child checks that it is locked. Rather than
91 // maintaining complex state to work around those situations, the check only
92 // checks that the mutex is owned, and assumes callers to be generally
96 uptr opaque_storage_[10];
97 uptr owner_; // for debugging
100 // Reader-writer spin mutex.
104 atomic_store(&state_, kUnlocked, memory_order_relaxed);
108 CHECK_EQ(atomic_load(&state_, memory_order_relaxed), kUnlocked);
113 if (atomic_compare_exchange_strong(&state_, &cmp, kWriteLock,
114 memory_order_acquire))
120 u32 prev = atomic_fetch_sub(&state_, kWriteLock, memory_order_release);
121 DCHECK_NE(prev & kWriteLock, 0);
126 u32 prev = atomic_fetch_add(&state_, kReadLock, memory_order_acquire);
127 if ((prev & kWriteLock) == 0)
133 u32 prev = atomic_fetch_sub(&state_, kReadLock, memory_order_release);
134 DCHECK_EQ(prev & kWriteLock, 0);
135 DCHECK_GT(prev & ~kWriteLock, 0);
140 CHECK_NE(atomic_load(&state_, memory_order_relaxed), kUnlocked);
144 atomic_uint32_t state_;
152 void NOINLINE LockSlow() {
153 for (int i = 0;; i++) {
157 internal_sched_yield();
158 u32 cmp = atomic_load(&state_, memory_order_relaxed);
159 if (cmp == kUnlocked &&
160 atomic_compare_exchange_weak(&state_, &cmp, kWriteLock,
161 memory_order_acquire))
166 void NOINLINE ReadLockSlow() {
167 for (int i = 0;; i++) {
171 internal_sched_yield();
172 u32 prev = atomic_load(&state_, memory_order_acquire);
173 if ((prev & kWriteLock) == 0)
178 RWMutex(const RWMutex&);
179 void operator = (const RWMutex&);
182 template<typename MutexType>
183 class GenericScopedLock {
185 explicit GenericScopedLock(MutexType *mu)
190 ~GenericScopedLock() {
197 GenericScopedLock(const GenericScopedLock&);
198 void operator=(const GenericScopedLock&);
201 template<typename MutexType>
202 class GenericScopedReadLock {
204 explicit GenericScopedReadLock(MutexType *mu)
209 ~GenericScopedReadLock() {
216 GenericScopedReadLock(const GenericScopedReadLock&);
217 void operator=(const GenericScopedReadLock&);
220 typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
221 typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
222 typedef GenericScopedLock<RWMutex> RWMutexLock;
223 typedef GenericScopedReadLock<RWMutex> RWMutexReadLock;
225 } // namespace __sanitizer
227 #endif // SANITIZER_MUTEX_H