blob: 5b22a414518537416c4642e66b39d2345f29dbd5 [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,
32
33 // This must be the last.
34 MutexTypeCount,
35};
36
37class Mutex {
38 public:
39 explicit Mutex(MutexType type, StatType stat_type);
40 ~Mutex();
41
42 void Lock();
43 void Unlock();
44
45 void ReadLock();
46 void ReadUnlock();
47
48 private:
49 atomic_uintptr_t state_;
50#if TSAN_DEBUG
51 MutexType type_;
52#endif
53#if TSAN_COLLECT_STATS
54 StatType stat_type_;
55#endif
56
57 Mutex(const Mutex&);
58 void operator = (const Mutex&);
59};
60
Dmitry Vyukov7a9fa7d2012-06-29 17:10:08 +000061typedef GenericScopedLock<Mutex> Lock;
62typedef GenericScopedReadLock<Mutex> ReadLock;
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000063
64class DeadlockDetector {
65 public:
66 DeadlockDetector();
67 void Lock(MutexType t);
68 void Unlock(MutexType t);
69 private:
70 u64 seq_;
71 u64 locked_[MutexTypeCount];
72};
73
74void InitializeMutex();
75
76} // namespace __tsan
77
78#endif // TSAN_MUTEX_H