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