Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file is a part of ThreadSanitizer/AddressSanitizer runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 13 | |
| 14 | #ifndef SANITIZER_MUTEX_H |
| 15 | #define SANITIZER_MUTEX_H |
| 16 | |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 17 | #include "sanitizer_atomic.h" |
Dmitry Vyukov | 8d99318 | 2012-07-02 07:09:21 +0000 | [diff] [blame] | 18 | #include "sanitizer_internal_defs.h" |
| 19 | #include "sanitizer_libc.h" |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 20 | |
| 21 | namespace __sanitizer { |
| 22 | |
Dmitry Vyukov | 1b37017 | 2012-08-30 10:02:48 +0000 | [diff] [blame] | 23 | class StaticSpinMutex { |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 24 | public: |
Dmitry Vyukov | 1b37017 | 2012-08-30 10:02:48 +0000 | [diff] [blame] | 25 | void Init() { |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 26 | atomic_store(&state_, 0, memory_order_relaxed); |
| 27 | } |
| 28 | |
| 29 | void Lock() { |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 30 | if (TryLock()) |
Dmitry Vyukov | 8d99318 | 2012-07-02 07:09:21 +0000 | [diff] [blame] | 31 | return; |
| 32 | LockSlow(); |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 35 | bool TryLock() { |
| 36 | return atomic_exchange(&state_, 1, memory_order_acquire) == 0; |
| 37 | } |
| 38 | |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 39 | void Unlock() { |
| 40 | atomic_store(&state_, 0, memory_order_release); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | atomic_uint8_t state_; |
| 45 | |
Dmitry Vyukov | 8d99318 | 2012-07-02 07:09:21 +0000 | [diff] [blame] | 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; |
| 55 | } |
| 56 | } |
Dmitry Vyukov | 1b37017 | 2012-08-30 10:02:48 +0000 | [diff] [blame] | 57 | }; |
Dmitry Vyukov | 8d99318 | 2012-07-02 07:09:21 +0000 | [diff] [blame] | 58 | |
Dmitry Vyukov | 1b37017 | 2012-08-30 10:02:48 +0000 | [diff] [blame] | 59 | class SpinMutex : public StaticSpinMutex { |
| 60 | public: |
| 61 | SpinMutex() { |
| 62 | Init(); |
| 63 | } |
| 64 | |
| 65 | private: |
Dmitry Vyukov | a3eca81 | 2012-06-29 17:32:18 +0000 | [diff] [blame] | 66 | SpinMutex(const SpinMutex&); |
| 67 | void operator=(const SpinMutex&); |
| 68 | }; |
| 69 | |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 70 | class BlockingMutex { |
| 71 | public: |
| 72 | explicit BlockingMutex(LinkerInitialized); |
Alexey Samsonov | 93af594 | 2013-03-14 13:30:56 +0000 | [diff] [blame] | 73 | BlockingMutex(); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 74 | void Lock(); |
| 75 | void Unlock(); |
Alexey Samsonov | ce70097 | 2013-03-11 15:45:20 +0000 | [diff] [blame] | 76 | void CheckLocked(); |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 77 | private: |
| 78 | uptr opaque_storage_[10]; |
Dmitry Vyukov | 4852601 | 2013-01-14 08:01:58 +0000 | [diff] [blame] | 79 | uptr owner_; // for debugging |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 82 | template<typename MutexType> |
| 83 | class GenericScopedLock { |
| 84 | public: |
| 85 | explicit GenericScopedLock(MutexType *mu) |
| 86 | : mu_(mu) { |
| 87 | mu_->Lock(); |
| 88 | } |
| 89 | |
| 90 | ~GenericScopedLock() { |
| 91 | mu_->Unlock(); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | MutexType *mu_; |
| 96 | |
| 97 | GenericScopedLock(const GenericScopedLock&); |
| 98 | void operator=(const GenericScopedLock&); |
| 99 | }; |
| 100 | |
| 101 | template<typename MutexType> |
| 102 | class GenericScopedReadLock { |
| 103 | public: |
| 104 | explicit GenericScopedReadLock(MutexType *mu) |
| 105 | : mu_(mu) { |
| 106 | mu_->ReadLock(); |
| 107 | } |
| 108 | |
| 109 | ~GenericScopedReadLock() { |
| 110 | mu_->ReadUnlock(); |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | MutexType *mu_; |
| 115 | |
| 116 | GenericScopedReadLock(const GenericScopedReadLock&); |
| 117 | void operator=(const GenericScopedReadLock&); |
| 118 | }; |
| 119 | |
Dmitry Vyukov | 1b37017 | 2012-08-30 10:02:48 +0000 | [diff] [blame] | 120 | typedef GenericScopedLock<StaticSpinMutex> SpinMutexLock; |
Dmitry Vyukov | f4f51f2 | 2013-01-14 07:51:39 +0000 | [diff] [blame] | 121 | typedef GenericScopedLock<BlockingMutex> BlockingMutexLock; |
Dmitry Vyukov | e088e16 | 2012-07-02 06:54:24 +0000 | [diff] [blame] | 122 | |
Dmitry Vyukov | c04d8b3 | 2012-06-29 17:10:08 +0000 | [diff] [blame] | 123 | } // namespace __sanitizer |
| 124 | |
| 125 | #endif // SANITIZER_MUTEX_H |