1 //===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_MUTEX_H
13 #define SANITIZER_MUTEX_H
15 #include "sanitizer_atomic.h"
16 #include "sanitizer_internal_defs.h"
17 #include "sanitizer_libc.h"
19 namespace __sanitizer
{
21 class StaticSpinMutex
{
24 atomic_store(&state_
, 0, memory_order_relaxed
);
34 return atomic_exchange(&state_
, 1, memory_order_acquire
) == 0;
38 atomic_store(&state_
, 0, memory_order_release
);
42 atomic_uint8_t state_
;
44 void NOINLINE
LockSlow() {
45 for (int i
= 0;; i
++) {
49 internal_sched_yield();
50 if (atomic_load(&state_
, memory_order_relaxed
) == 0
51 && atomic_exchange(&state_
, 1, memory_order_acquire
) == 0)
57 class SpinMutex
: public StaticSpinMutex
{
64 SpinMutex(const SpinMutex
&);
65 void operator=(const SpinMutex
&);
70 explicit BlockingMutex(LinkerInitialized
);
74 uptr opaque_storage_
[10];
75 uptr owner_
; // for debugging
78 template<typename MutexType
>
79 class GenericScopedLock
{
81 explicit GenericScopedLock(MutexType
*mu
)
86 ~GenericScopedLock() {
93 GenericScopedLock(const GenericScopedLock
&);
94 void operator=(const GenericScopedLock
&);
97 template<typename MutexType
>
98 class GenericScopedReadLock
{
100 explicit GenericScopedReadLock(MutexType
*mu
)
105 ~GenericScopedReadLock() {
112 GenericScopedReadLock(const GenericScopedReadLock
&);
113 void operator=(const GenericScopedReadLock
&);
116 typedef GenericScopedLock
<StaticSpinMutex
> SpinMutexLock
;
117 typedef GenericScopedLock
<BlockingMutex
> BlockingMutexLock
;
119 } // namespace __sanitizer
121 #endif // SANITIZER_MUTEX_H