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