Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_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 (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef TSAN_MUTEX_H |
| 14 | #define TSAN_MUTEX_H |
| 15 | |
Dmitry Vyukov | 6fa46f7 | 2012-06-29 16:58:33 +0000 | [diff] [blame^] | 16 | #include "sanitizer_common/sanitizer_atomic.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | #include "tsan_defs.h" |
| 18 | |
| 19 | namespace __tsan { |
| 20 | |
| 21 | enum MutexType { |
| 22 | MutexTypeInvalid, |
| 23 | MutexTypeTrace, |
| 24 | MutexTypeThreads, |
| 25 | MutexTypeReport, |
| 26 | MutexTypeSyncVar, |
| 27 | MutexTypeSyncTab, |
| 28 | MutexTypeSlab, |
| 29 | MutexTypeAnnotations, |
| 30 | MutexTypeAtExit, |
| 31 | |
| 32 | // This must be the last. |
| 33 | MutexTypeCount, |
| 34 | }; |
| 35 | |
| 36 | class Mutex { |
| 37 | public: |
| 38 | explicit Mutex(MutexType type, StatType stat_type); |
| 39 | ~Mutex(); |
| 40 | |
| 41 | void Lock(); |
| 42 | void Unlock(); |
| 43 | |
| 44 | void ReadLock(); |
| 45 | void ReadUnlock(); |
| 46 | |
| 47 | private: |
| 48 | atomic_uintptr_t state_; |
| 49 | #if TSAN_DEBUG |
| 50 | MutexType type_; |
| 51 | #endif |
| 52 | #if TSAN_COLLECT_STATS |
| 53 | StatType stat_type_; |
| 54 | #endif |
| 55 | |
| 56 | Mutex(const Mutex&); |
| 57 | void operator = (const Mutex&); |
| 58 | }; |
| 59 | |
| 60 | class Lock { |
| 61 | public: |
| 62 | explicit Lock(Mutex *m); |
| 63 | ~Lock(); |
| 64 | |
| 65 | private: |
| 66 | Mutex *m_; |
| 67 | |
| 68 | Lock(const Lock&); |
| 69 | void operator = (const Lock&); |
| 70 | }; |
| 71 | |
| 72 | class ReadLock { |
| 73 | public: |
| 74 | explicit ReadLock(Mutex *m); |
| 75 | ~ReadLock(); |
| 76 | |
| 77 | private: |
| 78 | Mutex *m_; |
| 79 | |
| 80 | ReadLock(const ReadLock&); |
| 81 | void operator = (const ReadLock&); |
| 82 | }; |
| 83 | |
| 84 | class DeadlockDetector { |
| 85 | public: |
| 86 | DeadlockDetector(); |
| 87 | void Lock(MutexType t); |
| 88 | void Unlock(MutexType t); |
| 89 | private: |
| 90 | u64 seq_; |
| 91 | u64 locked_[MutexTypeCount]; |
| 92 | }; |
| 93 | |
| 94 | void InitializeMutex(); |
| 95 | |
| 96 | } // namespace __tsan |
| 97 | |
| 98 | #endif // TSAN_MUTEX_H |