blob: a2b489107a985ddf92229179c963870620c1b614 [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,
Dmitry Vyukov22be55e2012-12-21 11:30:14 +000033 MutexTypeJavaMBlock,
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000034
35 // This must be the last.
Alexey Samsonov046248c2012-09-13 11:54:41 +000036 MutexTypeCount
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000037};
38
39class 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
Dmitry Vyukov191f2f72012-08-30 13:02:30 +000050 void CheckLocked();
51
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000052 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
Dmitry Vyukov7a9fa7d2012-06-29 17:10:08 +000065typedef GenericScopedLock<Mutex> Lock;
66typedef GenericScopedReadLock<Mutex> ReadLock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000067
68class 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
78void InitializeMutex();
79
80} // namespace __tsan
81
82#endif // TSAN_MUTEX_H