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" |
Dmitry Vyukov | 7a9fa7d | 2012-06-29 17:10:08 +0000 | [diff] [blame^] | 17 | #include "sanitizer_common/sanitizer_mutex.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 18 | #include "tsan_defs.h" |
| 19 | |
| 20 | namespace __tsan { |
| 21 | |
| 22 | enum MutexType { |
| 23 | MutexTypeInvalid, |
| 24 | MutexTypeTrace, |
| 25 | MutexTypeThreads, |
| 26 | MutexTypeReport, |
| 27 | MutexTypeSyncVar, |
| 28 | MutexTypeSyncTab, |
| 29 | MutexTypeSlab, |
| 30 | MutexTypeAnnotations, |
| 31 | MutexTypeAtExit, |
| 32 | |
| 33 | // This must be the last. |
| 34 | MutexTypeCount, |
| 35 | }; |
| 36 | |
| 37 | class Mutex { |
| 38 | public: |
| 39 | explicit Mutex(MutexType type, StatType stat_type); |
| 40 | ~Mutex(); |
| 41 | |
| 42 | void Lock(); |
| 43 | void Unlock(); |
| 44 | |
| 45 | void ReadLock(); |
| 46 | void ReadUnlock(); |
| 47 | |
| 48 | private: |
| 49 | atomic_uintptr_t state_; |
| 50 | #if TSAN_DEBUG |
| 51 | MutexType type_; |
| 52 | #endif |
| 53 | #if TSAN_COLLECT_STATS |
| 54 | StatType stat_type_; |
| 55 | #endif |
| 56 | |
| 57 | Mutex(const Mutex&); |
| 58 | void operator = (const Mutex&); |
| 59 | }; |
| 60 | |
Dmitry Vyukov | 7a9fa7d | 2012-06-29 17:10:08 +0000 | [diff] [blame^] | 61 | typedef GenericScopedLock<Mutex> Lock; |
| 62 | typedef GenericScopedReadLock<Mutex> ReadLock; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 63 | |
| 64 | class DeadlockDetector { |
| 65 | public: |
| 66 | DeadlockDetector(); |
| 67 | void Lock(MutexType t); |
| 68 | void Unlock(MutexType t); |
| 69 | private: |
| 70 | u64 seq_; |
| 71 | u64 locked_[MutexTypeCount]; |
| 72 | }; |
| 73 | |
| 74 | void InitializeMutex(); |
| 75 | |
| 76 | } // namespace __tsan |
| 77 | |
| 78 | #endif // TSAN_MUTEX_H |