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 atomic_uint8_t state_
;
46 void NOINLINE
LockSlow() {
47 for (int i
= 0;; i
++) {
51 internal_sched_yield();
52 if (atomic_load(&state_
, memory_order_relaxed
) == 0
53 && atomic_exchange(&state_
, 1, memory_order_acquire
) == 0)
59 class SpinMutex
: public StaticSpinMutex
{
66 SpinMutex(const SpinMutex
&);
67 void operator=(const SpinMutex
&);
72 explicit BlockingMutex(LinkerInitialized
);
76 uptr opaque_storage_
[10];
77 uptr owner_
; // for debugging
80 template<typename MutexType
>
81 class GenericScopedLock
{
83 explicit GenericScopedLock(MutexType
*mu
)
88 ~GenericScopedLock() {
95 GenericScopedLock(const GenericScopedLock
&);
96 void operator=(const GenericScopedLock
&);
99 template<typename MutexType
>
100 class GenericScopedReadLock
{
102 explicit GenericScopedReadLock(MutexType
*mu
)
107 ~GenericScopedReadLock() {
114 GenericScopedReadLock(const GenericScopedReadLock
&);
115 void operator=(const GenericScopedReadLock
&);
118 typedef GenericScopedLock
<StaticSpinMutex
> SpinMutexLock
;
119 typedef GenericScopedLock
<BlockingMutex
> BlockingMutexLock
;
121 } // namespace __sanitizer
123 #endif // SANITIZER_MUTEX_H