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