]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_mutex.h
Update ACPICA to 20181003.
[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
96  private:
97   // Solaris mutex_t has a member that requires 64-bit alignment.
98   ALIGNED(8) uptr opaque_storage_[10];
99   uptr owner_;  // for debugging
100 };
101
102 // Reader-writer spin mutex.
103 class RWMutex {
104  public:
105   RWMutex() {
106     atomic_store(&state_, kUnlocked, memory_order_relaxed);
107   }
108
109   ~RWMutex() {
110     CHECK_EQ(atomic_load(&state_, memory_order_relaxed), kUnlocked);
111   }
112
113   void Lock() {
114     u32 cmp = kUnlocked;
115     if (atomic_compare_exchange_strong(&state_, &cmp, kWriteLock,
116                                        memory_order_acquire))
117       return;
118     LockSlow();
119   }
120
121   void Unlock() {
122     u32 prev = atomic_fetch_sub(&state_, kWriteLock, memory_order_release);
123     DCHECK_NE(prev & kWriteLock, 0);
124     (void)prev;
125   }
126
127   void ReadLock() {
128     u32 prev = atomic_fetch_add(&state_, kReadLock, memory_order_acquire);
129     if ((prev & kWriteLock) == 0)
130       return;
131     ReadLockSlow();
132   }
133
134   void ReadUnlock() {
135     u32 prev = atomic_fetch_sub(&state_, kReadLock, memory_order_release);
136     DCHECK_EQ(prev & kWriteLock, 0);
137     DCHECK_GT(prev & ~kWriteLock, 0);
138     (void)prev;
139   }
140
141   void CheckLocked() {
142     CHECK_NE(atomic_load(&state_, memory_order_relaxed), kUnlocked);
143   }
144
145  private:
146   atomic_uint32_t state_;
147
148   enum {
149     kUnlocked = 0,
150     kWriteLock = 1,
151     kReadLock = 2
152   };
153
154   void NOINLINE LockSlow() {
155     for (int i = 0;; i++) {
156       if (i < 10)
157         proc_yield(10);
158       else
159         internal_sched_yield();
160       u32 cmp = atomic_load(&state_, memory_order_relaxed);
161       if (cmp == kUnlocked &&
162           atomic_compare_exchange_weak(&state_, &cmp, kWriteLock,
163                                        memory_order_acquire))
164           return;
165     }
166   }
167
168   void NOINLINE ReadLockSlow() {
169     for (int i = 0;; i++) {
170       if (i < 10)
171         proc_yield(10);
172       else
173         internal_sched_yield();
174       u32 prev = atomic_load(&state_, memory_order_acquire);
175       if ((prev & kWriteLock) == 0)
176         return;
177     }
178   }
179
180   RWMutex(const RWMutex&);
181   void operator = (const RWMutex&);
182 };
183
184 template<typename MutexType>
185 class GenericScopedLock {
186  public:
187   explicit GenericScopedLock(MutexType *mu)
188       : mu_(mu) {
189     mu_->Lock();
190   }
191
192   ~GenericScopedLock() {
193     mu_->Unlock();
194   }
195
196  private:
197   MutexType *mu_;
198
199   GenericScopedLock(const GenericScopedLock&);
200   void operator=(const GenericScopedLock&);
201 };
202
203 template<typename MutexType>
204 class GenericScopedReadLock {
205  public:
206   explicit GenericScopedReadLock(MutexType *mu)
207       : mu_(mu) {
208     mu_->ReadLock();
209   }
210
211   ~GenericScopedReadLock() {
212     mu_->ReadUnlock();
213   }
214
215  private:
216   MutexType *mu_;
217
218   GenericScopedReadLock(const GenericScopedReadLock&);
219   void operator=(const GenericScopedReadLock&);
220 };
221
222 typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock;
223 typedef GenericScopedLock<BlockingMutex> BlockingMutexLock;
224 typedef GenericScopedLock<RWMutex> RWMutexLock;
225 typedef GenericScopedReadLock<RWMutex> RWMutexReadLock;
226
227 }  // namespace __sanitizer
228
229 #endif  // SANITIZER_MUTEX_H