blob: 68b33a7f837f22ea6ea31cfafac3025ac5e1acf3 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- 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
Dmitry Vyukov6fa46f72012-06-29 16:58:33 +000016#include "sanitizer_common/sanitizer_atomic.h"
Dmitry Vyukov7a9fa7d2012-06-29 17:10:08 +000017#include "sanitizer_common/sanitizer_mutex.h"
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000018#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,
Dmitry Vyukovfd5ebcd2012-12-06 12:16:15 +000032 MutexTypeMBlock,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000033
34 // This must be the last.
Alexey Samsonov046248c2012-09-13 11:54:41 +000035 MutexTypeCount
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000036};
37
38class Mutex {
39 public:
40 explicit Mutex(MutexType type, StatType stat_type);
41 ~Mutex();
42
43 void Lock();
44 void Unlock();
45
46 void ReadLock();
47 void ReadUnlock();
48
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000049 void CheckLocked();
50
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000051 private:
52 atomic_uintptr_t state_;
53#if TSAN_DEBUG
54 MutexType type_;
55#endif
56#if TSAN_COLLECT_STATS
57 StatType stat_type_;
58#endif
59
60 Mutex(const Mutex&);
61 void operator = (const Mutex&);
62};
63
Dmitry Vyukov7a9fa7d2012-06-29 17:10:08 +000064typedef GenericScopedLock<Mutex> Lock;
65typedef GenericScopedReadLock<Mutex> ReadLock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000066
67class DeadlockDetector {
68 public:
69 DeadlockDetector();
70 void Lock(MutexType t);
71 void Unlock(MutexType t);
72 private:
73 u64 seq_;
74 u64 locked_[MutexTypeCount];
75};
76
77void InitializeMutex();
78
79} // namespace __tsan
80
81#endif // TSAN_MUTEX_H