blob: 27f55385c959aba46d22618ab02a4bf3decd4e6f [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,
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080035 MutexTypeFired,
36 MutexTypeRacy,
Kostya Serebryany7ac41482012-05-10 13:48:04 +000037
38 // This must be the last.
Alexey Samsonov2135d8a2012-09-13 11:54:41 +000039 MutexTypeCount
Kostya Serebryany7ac41482012-05-10 13:48:04 +000040};
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
Dmitry Vyukovff35f1d2012-08-30 13:02:30 +000053 void CheckLocked();
54
Kostya Serebryany7ac41482012-05-10 13:48:04 +000055 private:
56 atomic_uintptr_t state_;
Stephen Hines86277eb2015-03-23 12:06:32 -070057#if SANITIZER_DEBUG
Kostya Serebryany7ac41482012-05-10 13:48:04 +000058 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
Dmitry Vyukovc04d8b32012-06-29 17:10:08 +000068typedef GenericScopedLock<Mutex> Lock;
69typedef GenericScopedReadLock<Mutex> ReadLock;
Kostya Serebryany7ac41482012-05-10 13:48:04 +000070
Stephen Hines2d1fdb22014-05-28 23:58:16 -070071class InternalDeadlockDetector {
Kostya Serebryany7ac41482012-05-10 13:48:04 +000072 public:
Stephen Hines2d1fdb22014-05-28 23:58:16 -070073 InternalDeadlockDetector();
Kostya Serebryany7ac41482012-05-10 13:48:04 +000074 void Lock(MutexType t);
75 void Unlock(MutexType t);
Stephen Hines6a211c52014-07-21 00:49:56 -070076 void CheckNoLocks();
Kostya Serebryany7ac41482012-05-10 13:48:04 +000077 private:
78 u64 seq_;
79 u64 locked_[MutexTypeCount];
80};
81
82void InitializeMutex();
83
Stephen Hines6a211c52014-07-21 00:49:56 -070084// Checks that the current thread does not hold any runtime locks
85// (e.g. when returning from an interceptor).
86void CheckNoLocks(ThreadState *thr);
87
Kostya Serebryany7ac41482012-05-10 13:48:04 +000088} // namespace __tsan
89
90#endif // TSAN_MUTEX_H