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&);
76 explicit constexpr BlockingMutex(LinkerInitialized)
77 : opaque_storage_ {0, }, owner_ {0} {}
82 // This function does not guarantee an explicit check that the calling thread
83 // is the thread which owns the mutex. This behavior, while more strictly
84 // correct, causes problems in cases like StopTheWorld, where a parent thread
85 // owns the mutex but a child checks that it is locked. Rather than
86 // maintaining complex state to work around those situations, the check only
87 // checks that the mutex is owned, and assumes callers to be generally
92 // Solaris mutex_t has a member that requires 64-bit alignment.
93 ALIGNED(8) uptr opaque_storage_[10];
94 uptr owner_; // for debugging
97 // Reader-writer spin mutex.
101 atomic_store(&state_, kUnlocked, memory_order_relaxed);
105 CHECK_EQ(atomic_load(&state_, memory_order_relaxed), kUnlocked);
110 if (atomic_compare_exchange_strong(&state_, &cmp, kWriteLock,
111 memory_order_acquire))
117 u32 prev = atomic_fetch_sub(&state_, kWriteLock, memory_order_release);
118 DCHECK_NE(prev & kWriteLock, 0);
123 u32 prev = atomic_fetch_add(&state_, kReadLock, memory_order_acquire);
124 if ((prev & kWriteLock) == 0)
130 u32 prev = atomic_fetch_sub(&state_, kReadLock, memory_order_release);
131 DCHECK_EQ(prev & kWriteLock, 0);
132 DCHECK_GT(prev & ~kWriteLock, 0);
137 CHECK_NE(atomic_load(&state_, memory_order_relaxed), kUnlocked);
141 atomic_uint32_t state_;
149 void NOINLINE LockSlow() {
150 for (int i = 0;; i++) {
154 internal_sched_yield();
155 u32 cmp = atomic_load(&state_, memory_order_relaxed);
156 if (cmp == kUnlocked &&
157 atomic_compare_exchange_weak(&state_, &cmp, kWriteLock,
158 memory_order_acquire))
163 void NOINLINE ReadLockSlow() {
164 for (int i = 0;; i++) {
168 internal_sched_yield();
169 u32 prev = atomic_load(&state_, memory_order_acquire);
170 if ((prev & kWriteLock) == 0)
175 RWMutex(const RWMutex&);
176 void operator = (const RWMutex&);
179 template<typename MutexType>
180 class GenericScopedLock {
182 explicit GenericScopedLock(MutexType *mu)
187 ~GenericScopedLock() {
194 GenericScopedLock(const GenericScopedLock&);
195 void operator=(const GenericScopedLock&);
198 template<typename MutexType>
199 class GenericScopedReadLock {
201 explicit GenericScopedReadLock(MutexType *mu)
206 ~GenericScopedReadLock() {
213 GenericScopedReadLock(const GenericScopedReadLock&);
214 void operator=(const GenericScopedReadLock&);
217 typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
218 typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
219 typedef GenericScopedLock<RWMutex> RWMutexLock;
220 typedef GenericScopedReadLock<RWMutex> RWMutexReadLock;
222 } // namespace __sanitizer
224 #endif // SANITIZER_MUTEX_H