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
);
28 if (atomic_exchange(&state_
, 1, memory_order_acquire
) == 0)
34 atomic_store(&state_
, 0, memory_order_release
);
38 atomic_uint8_t state_
;
40 void NOINLINE
LockSlow() {
41 for (int i
= 0;; i
++) {
45 internal_sched_yield();
46 if (atomic_load(&state_
, memory_order_relaxed
) == 0
47 && atomic_exchange(&state_
, 1, memory_order_acquire
) == 0)
53 class SpinMutex
: public StaticSpinMutex
{
60 SpinMutex(const SpinMutex
&);
61 void operator=(const SpinMutex
&);
64 template<typename MutexType
>
65 class GenericScopedLock
{
67 explicit GenericScopedLock(MutexType
*mu
)
72 ~GenericScopedLock() {
79 GenericScopedLock(const GenericScopedLock
&);
80 void operator=(const GenericScopedLock
&);
83 template<typename MutexType
>
84 class GenericScopedReadLock
{
86 explicit GenericScopedReadLock(MutexType
*mu
)
91 ~GenericScopedReadLock() {
98 GenericScopedReadLock(const GenericScopedReadLock
&);
99 void operator=(const GenericScopedReadLock
&);
102 typedef GenericScopedLock
<StaticSpinMutex
> SpinMutexLock
;
104 } // namespace __sanitizer
106 #endif // SANITIZER_MUTEX_H