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