blob: 88fad57c78a05199d5c5f038cfae694e074d16e4 [file] [log] [blame]
Kostya Serebryany7ac41482012-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 Vyukovfce5bd42012-06-29 16:58:33 +000016#include "sanitizer_common/sanitizer_atomic.h"
Dmitry Vyukovc04d8b32012-06-29 17:10:08 +000017#include "sanitizer_common/sanitizer_mutex.h"
Kostya Serebryany7ac41482012-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 Vyukovad9da372012-12-06 12:16:15 +000032 MutexTypeMBlock,
Dmitry Vyukovf4e4f932012-12-21 11:30:14 +000033 MutexTypeJavaMBlock,
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034 MutexTypeDDetector,
Kostya Serebryany7ac41482012-05-10 13:48:04 +000035
36 // This must be the last.
Alexey Samsonov2135d8a2012-09-13 11:54:41 +000037 MutexTypeCount
Kostya Serebryany7ac41482012-05-10 13:48:04 +000038};
39
40class Mutex {
41 public:
42 explicit Mutex(MutexType type, StatType stat_type);
43 ~Mutex();
44
45 void Lock();
46 void Unlock();
47
48 void ReadLock();
49 void ReadUnlock();
50
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +000051 void CheckLocked();
52
Kostya Serebryany7ac41482012-05-10 13:48:04 +000053 private:
54 atomic_uintptr_t state_;
Stephen Hines86277eb2015-03-23 12:06:32 -070055#if SANITIZER_DEBUG
Kostya Serebryany7ac41482012-05-10 13:48:04 +000056 MutexType type_;
57#endif
58#if TSAN_COLLECT_STATS
59 StatType stat_type_;
60#endif
61
62 Mutex(const Mutex&);
63 void operator = (const Mutex&);
64};
65
Dmitry Vyukovc04d8b32012-06-29 17:10:08 +000066typedef GenericScopedLock<Mutex> Lock;
67typedef GenericScopedReadLock<Mutex> ReadLock;
Kostya Serebryany7ac41482012-05-10 13:48:04 +000068
Stephen Hines2d1fdb22014-05-28 23:58:16 -070069class InternalDeadlockDetector {
Kostya Serebryany7ac41482012-05-10 13:48:04 +000070 public:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070071 InternalDeadlockDetector();
Kostya Serebryany7ac41482012-05-10 13:48:04 +000072 void Lock(MutexType t);
73 void Unlock(MutexType t);
Stephen Hines6a211c52014-07-21 00:49:56 -070074 void CheckNoLocks();
Kostya Serebryany7ac41482012-05-10 13:48:04 +000075 private:
76 u64 seq_;
77 u64 locked_[MutexTypeCount];
78};
79
80void InitializeMutex();
81
Stephen Hines6a211c52014-07-21 00:49:56 -070082// Checks that the current thread does not hold any runtime locks
83// (e.g. when returning from an interceptor).
84void CheckNoLocks(ThreadState *thr);
85
Kostya Serebryany7ac41482012-05-10 13:48:04 +000086} // namespace __tsan
87
88#endif // TSAN_MUTEX_H