2016-10-21 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_mutex.h
blob9960eee9bc8d9f98344eda14f1f1edacece9f8d8
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
14 #include "sanitizer_common/sanitizer_atomic.h"
15 #include "sanitizer_common/sanitizer_mutex.h"
16 #include "tsan_defs.h"
18 namespace __tsan {
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,
36 // This must be the last.
37 MutexTypeCount
40 class Mutex {
41 public:
42 explicit Mutex(MutexType type, StatType stat_type);
43 ~Mutex();
45 void Lock();
46 void Unlock();
48 void ReadLock();
49 void ReadUnlock();
51 void CheckLocked();
53 private:
54 atomic_uintptr_t state_;
55 #if SANITIZER_DEBUG
56 MutexType type_;
57 #endif
58 #if TSAN_COLLECT_STATS
59 StatType stat_type_;
60 #endif
62 Mutex(const Mutex&);
63 void operator = (const Mutex&);
66 typedef GenericScopedLock<Mutex> Lock;
67 typedef GenericScopedReadLock<Mutex> ReadLock;
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];
80 void InitializeMutex();
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);
86 } // namespace __tsan
88 #endif // TSAN_MUTEX_H