1//===-- tsan_mutex.h --------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of ThreadSanitizer (TSan), a race detector.
10//
11//===----------------------------------------------------------------------===//
12#ifndef TSAN_MUTEX_H
13#define TSAN_MUTEX_H
14
15#include "sanitizer_common/sanitizer_atomic.h"
16#include "sanitizer_common/sanitizer_mutex.h"
17#include "tsan_defs.h"
18
19namespace __tsan {
20
21enum MutexType {
22  MutexTypeInvalid,
23  MutexTypeTrace,
24  MutexTypeThreads,
25  MutexTypeReport,
26  MutexTypeSyncVar,
27  MutexTypeSyncTab,
28  MutexTypeSlab,
29  MutexTypeAnnotations,
30  MutexTypeAtExit,
31  MutexTypeMBlock,
32  MutexTypeJavaMBlock,
33  MutexTypeDDetector,
34  MutexTypeFired,
35  MutexTypeRacy,
36  MutexTypeGlobalProc,
37
38  // This must be the last.
39  MutexTypeCount
40};
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
53  void CheckLocked();
54
55 private:
56  atomic_uintptr_t state_;
57#if SANITIZER_DEBUG
58  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
68typedef GenericScopedLock<Mutex> Lock;
69typedef GenericScopedReadLock<Mutex> ReadLock;
70
71class InternalDeadlockDetector {
72 public:
73  InternalDeadlockDetector();
74  void Lock(MutexType t);
75  void Unlock(MutexType t);
76  void CheckNoLocks();
77 private:
78  u64 seq_;
79  u64 locked_[MutexTypeCount];
80};
81
82void InitializeMutex();
83
84// Checks that the current thread does not hold any runtime locks
85// (e.g. when returning from an interceptor).
86void CheckNoLocks(ThreadState *thr);
87
88}  // namespace __tsan
89
90#endif  // TSAN_MUTEX_H
91