• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35   // This must be the last.
36   MutexTypeCount
37 };
38 
39 class Mutex {
40  public:
41   explicit Mutex(MutexType type, StatType stat_type);
42   ~Mutex();
43 
44   void Lock();
45   void Unlock();
46 
47   void ReadLock();
48   void ReadUnlock();
49 
50   void CheckLocked();
51 
52  private:
53   atomic_uintptr_t state_;
54 #if TSAN_DEBUG
55   MutexType type_;
56 #endif
57 #if TSAN_COLLECT_STATS
58   StatType stat_type_;
59 #endif
60 
61   Mutex(const Mutex&);
62   void operator = (const Mutex&);
63 };
64 
65 typedef GenericScopedLock<Mutex> Lock;
66 typedef GenericScopedReadLock<Mutex> ReadLock;
67 
68 class DeadlockDetector {
69  public:
70   DeadlockDetector();
71   void Lock(MutexType t);
72   void Unlock(MutexType t);
73  private:
74   u64 seq_;
75   u64 locked_[MutexTypeCount];
76 };
77 
78 void InitializeMutex();
79 
80 }  // namespace __tsan
81 
82 #endif  // TSAN_MUTEX_H
83