1 //===-- tsan_mutex.h --------------------------------------------*- C++ -*-===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // This file is a part of ThreadSanitizer (TSan), a race detector. 9 // 10 //===----------------------------------------------------------------------===// 11 #ifndef TSAN_MUTEX_H 12 #define TSAN_MUTEX_H 13 14 #include "sanitizer_common/sanitizer_atomic.h" 15 #include "sanitizer_common/sanitizer_mutex.h" 16 #include "tsan_defs.h" 17 18 namespace __tsan { 19 20 enum MutexType { 21 MutexTypeInvalid, 22 MutexTypeTrace, 23 MutexTypeThreads, 24 MutexTypeReport, 25 MutexTypeSyncVar, 26 MutexTypeSyncTab, 27 MutexTypeSlab, 28 MutexTypeAnnotations, 29 MutexTypeAtExit, 30 MutexTypeMBlock, 31 MutexTypeJavaMBlock, 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 void CheckLocked(); 49 50 private: 51 atomic_uintptr_t state_; 52 #if TSAN_DEBUG 53 MutexType type_; 54 #endif 55 #if TSAN_COLLECT_STATS 56 StatType stat_type_; 57 #endif 58 59 Mutex(const Mutex&); 60 void operator = (const Mutex&); 61 }; 62 63 typedef GenericScopedLock<Mutex> Lock; 64 typedef GenericScopedReadLock<Mutex> ReadLock; 65 66 class DeadlockDetector { 67 public: 68 DeadlockDetector(); 69 void Lock(MutexType t); 70 void Unlock(MutexType t); 71 private: 72 u64 seq_; 73 u64 locked_[MutexTypeCount]; 74 }; 75 76 void InitializeMutex(); 77 78 } // namespace __tsan 79 80 #endif // TSAN_MUTEX_H 81