]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h
Merge llvm trunk r321017 to contrib/llvm.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_mutex.h
1 //===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef SANITIZER_MUTEX_H
15 #define SANITIZER_MUTEX_H
16
17 #include "sanitizer_atomic.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_libc.h"
20
21 namespace __sanitizer {
22
23 class StaticSpinMutex {
24  public:
25   void Init() {
26     atomic_store(&state_, 0, memory_order_relaxed);
27   }
28
29   void Lock() {
30     if (TryLock())
31       return;
32     LockSlow();
33   }
34
35   bool TryLock() {
36     return atomic_exchange(&state_, 1, memory_order_acquire) == 0;
37   }
38
39   void Unlock() {
40     atomic_store(&state_, 0, memory_order_release);
41   }
42
43   void CheckLocked() {
44     CHECK_EQ(atomic_load(&state_, memory_order_relaxed), 1);
45   }
46
47  private:
48   atomic_uint8_t state_;
49
50   void NOINLINE LockSlow() {
51     for (int i = 0;; i++) {
52       if (i < 10)
53         proc_yield(10);
54       else
55         internal_sched_yield();
56       if (atomic_load(&state_, memory_order_relaxed) == 0
57           && atomic_exchange(&state_, 1, memory_order_acquire) == 0)
58         return;
59     }
60   }
61 };
62
63 class SpinMutex : public StaticSpinMutex {
64  public:
65   SpinMutex() {
66     Init();
67   }
68
69  private:
70   SpinMutex(const SpinMutex&);
71   void operator=(const SpinMutex&);
72 };
73
74 class BlockingMutex {
75  public:
76 #if SANITIZER_WINDOWS
77   // Windows does not currently support LinkerInitialized
78   explicit BlockingMutex(LinkerInitialized);
79 #else
80   explicit constexpr BlockingMutex(LinkerInitialized)
81       : opaque_storage_ {0, }, owner_(0) {}
82 #endif
83   BlockingMutex();
84   void Lock();
85   void Unlock();
86
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
93   // well-behaved.
94   void CheckLocked();
95  private:
96   uptr opaque_storage_[10];
97   uptr owner_;  // for debugging
98 };
99
100 // Reader-writer spin mutex.
101 class RWMutex {
102  public:
103   RWMutex() {
104     atomic_store(&state_, kUnlocked, memory_order_relaxed);
105   }
106
107   ~RWMutex() {
108     CHECK_EQ(atomic_load(&state_, memory_order_relaxed), kUnlocked);
109   }
110
111   void Lock() {
112     u32 cmp = kUnlocked;
113     if (atomic_compare_exchange_strong(&state_, &cmp, kWriteLock,
114                                        memory_order_acquire))
115       return;
116     LockSlow();
117   }
118
119   void Unlock() {
120     u32 prev = atomic_fetch_sub(&state_, kWriteLock, memory_order_release);
121     DCHECK_NE(prev & kWriteLock, 0);
122     (void)prev;
123   }
124
125   void ReadLock() {
126     u32 prev = atomic_fetch_add(&state_, kReadLock, memory_order_acquire);
127     if ((prev & kWriteLock) == 0)
128       return;
129     ReadLockSlow();
130   }
131
132   void ReadUnlock() {
133     u32 prev = atomic_fetch_sub(&state_, kReadLock, memory_order_release);
134     DCHECK_EQ(prev & kWriteLock, 0);
135     DCHECK_GT(prev & ~kWriteLock, 0);
136     (void)prev;
137   }
138
139   void CheckLocked() {
140     CHECK_NE(atomic_load(&state_, memory_order_relaxed), kUnlocked);
141   }
142
143  private:
144   atomic_uint32_t state_;
145
146   enum {
147     kUnlocked = 0,
148     kWriteLock = 1,
149     kReadLock = 2
150   };
151
152   void NOINLINE LockSlow() {
153     for (int i = 0;; i++) {
154       if (i < 10)
155         proc_yield(10);
156       else
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))
162           return;
163     }
164   }
165
166   void NOINLINE ReadLockSlow() {
167     for (int i = 0;; i++) {
168       if (i < 10)
169         proc_yield(10);
170       else
171         internal_sched_yield();
172       u32 prev = atomic_load(&state_, memory_order_acquire);
173       if ((prev & kWriteLock) == 0)
174         return;
175     }
176   }
177
178   RWMutex(const RWMutex&);
179   void operator = (const RWMutex&);
180 };
181
182 template<typename MutexType>
183 class GenericScopedLock {
184  public:
185   explicit GenericScopedLock(MutexType *mu)
186       : mu_(mu) {
187     mu_->Lock();
188   }
189
190   ~GenericScopedLock() {
191     mu_->Unlock();
192   }
193
194  private:
195   MutexType *mu_;
196
197   GenericScopedLock(const GenericScopedLock&);
198   void operator=(const GenericScopedLock&);
199 };
200
201 template<typename MutexType>
202 class GenericScopedReadLock {
203  public:
204   explicit GenericScopedReadLock(MutexType *mu)
205       : mu_(mu) {
206     mu_->ReadLock();
207   }
208
209   ~GenericScopedReadLock() {
210     mu_->ReadUnlock();
211   }
212
213  private:
214   MutexType *mu_;
215
216   GenericScopedReadLock(const GenericScopedReadLock&);
217   void operator=(const GenericScopedReadLock&);
218 };
219
220 typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
221 typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
222 typedef GenericScopedLock<RWMutex> RWMutexLock;
223 typedef GenericScopedReadLock<RWMutex> RWMutexReadLock;
224
225 }  // namespace __sanitizer
226
227 #endif  // SANITIZER_MUTEX_H