[Sanitizer] extend internal libc with stat/fstat/lstat functions
[blocksruntime.git] / lib / sanitizer_common / sanitizer_mutex.h
blob56438fce471cf9455a130fbe9096b530d9e13f0a
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.
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 {
24 public:
25 void Init() {
26 atomic_store(&state_, 0, memory_order_relaxed);
29 void Lock() {
30 if (TryLock())
31 return;
32 LockSlow();
35 bool TryLock() {
36 return atomic_exchange(&state_, 1, memory_order_acquire) == 0;
39 void Unlock() {
40 atomic_store(&state_, 0, memory_order_release);
43 private:
44 atomic_uint8_t state_;
46 void NOINLINE LockSlow() {
47 for (int i = 0;; i++) {
48 if (i < 10)
49 proc_yield(10);
50 else
51 internal_sched_yield();
52 if (atomic_load(&state_, memory_order_relaxed) == 0
53 && atomic_exchange(&state_, 1, memory_order_acquire) == 0)
54 return;
59 class SpinMutex : public StaticSpinMutex {
60 public:
61 SpinMutex() {
62 Init();
65 private:
66 SpinMutex(const SpinMutex&);
67 void operator=(const SpinMutex&);
70 class BlockingMutex {
71 public:
72 explicit BlockingMutex(LinkerInitialized);
73 void Lock();
74 void Unlock();
75 private:
76 uptr opaque_storage_[10];
77 uptr owner_; // for debugging
80 template<typename MutexType>
81 class GenericScopedLock {
82 public:
83 explicit GenericScopedLock(MutexType *mu)
84 : mu_(mu) {
85 mu_->Lock();
88 ~GenericScopedLock() {
89 mu_->Unlock();
92 private:
93 MutexType *mu_;
95 GenericScopedLock(const GenericScopedLock&);
96 void operator=(const GenericScopedLock&);
99 template<typename MutexType>
100 class GenericScopedReadLock {
101 public:
102 explicit GenericScopedReadLock(MutexType *mu)
103 : mu_(mu) {
104 mu_->ReadLock();
107 ~GenericScopedReadLock() {
108 mu_->ReadUnlock();
111 private:
112 MutexType *mu_;
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