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 MutexTypeDDetector, 33 34 // This must be the last. 35 MutexTypeCount 36 }; 37 38 class Mutex { 39 public: 40 explicit Mutex(MutexType type, StatType stat_type); 41 ~Mutex(); 42 43 void Lock(); 44 void Unlock(); 45 46 void ReadLock(); 47 void ReadUnlock(); 48 49 void CheckLocked(); 50 51 private: 52 atomic_uintptr_t state_; 53 #if TSAN_DEBUG 54 MutexType type_; 55 #endif 56 #if TSAN_COLLECT_STATS 57 StatType stat_type_; 58 #endif 59 60 Mutex(const Mutex&); 61 void operator = (const Mutex&); 62 }; 63 64 typedef GenericScopedLock<Mutex> Lock; 65 typedef GenericScopedReadLock<Mutex> ReadLock; 66 67 class InternalDeadlockDetector { 68 public: 69 InternalDeadlockDetector(); 70 void Lock(MutexType t); 71 void Unlock(MutexType t); 72 void CheckNoLocks(); 73 private: 74 u64 seq_; 75 u64 locked_[MutexTypeCount]; 76 }; 77 78 void InitializeMutex(); 79 80 // Checks that the current thread does not hold any runtime locks 81 // (e.g. when returning from an interceptor). 82 void CheckNoLocks(ThreadState *thr); 83 84 } // namespace __tsan 85 86 #endif // TSAN_MUTEX_H 87