xref: /aosp_15_r20/external/compiler-rt/lib/tsan/rtl/tsan_mutex.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- tsan_mutex.h --------------------------------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of ThreadSanitizer (TSan), a race detector.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot #ifndef TSAN_MUTEX_H
14*7c3d14c8STreehugger Robot #define TSAN_MUTEX_H
15*7c3d14c8STreehugger Robot 
16*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_atomic.h"
17*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_mutex.h"
18*7c3d14c8STreehugger Robot #include "tsan_defs.h"
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot namespace __tsan {
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot enum MutexType {
23*7c3d14c8STreehugger Robot   MutexTypeInvalid,
24*7c3d14c8STreehugger Robot   MutexTypeTrace,
25*7c3d14c8STreehugger Robot   MutexTypeThreads,
26*7c3d14c8STreehugger Robot   MutexTypeReport,
27*7c3d14c8STreehugger Robot   MutexTypeSyncVar,
28*7c3d14c8STreehugger Robot   MutexTypeSyncTab,
29*7c3d14c8STreehugger Robot   MutexTypeSlab,
30*7c3d14c8STreehugger Robot   MutexTypeAnnotations,
31*7c3d14c8STreehugger Robot   MutexTypeAtExit,
32*7c3d14c8STreehugger Robot   MutexTypeMBlock,
33*7c3d14c8STreehugger Robot   MutexTypeJavaMBlock,
34*7c3d14c8STreehugger Robot   MutexTypeDDetector,
35*7c3d14c8STreehugger Robot   MutexTypeFired,
36*7c3d14c8STreehugger Robot   MutexTypeRacy,
37*7c3d14c8STreehugger Robot   MutexTypeGlobalProc,
38*7c3d14c8STreehugger Robot 
39*7c3d14c8STreehugger Robot   // This must be the last.
40*7c3d14c8STreehugger Robot   MutexTypeCount
41*7c3d14c8STreehugger Robot };
42*7c3d14c8STreehugger Robot 
43*7c3d14c8STreehugger Robot class Mutex {
44*7c3d14c8STreehugger Robot  public:
45*7c3d14c8STreehugger Robot   explicit Mutex(MutexType type, StatType stat_type);
46*7c3d14c8STreehugger Robot   ~Mutex();
47*7c3d14c8STreehugger Robot 
48*7c3d14c8STreehugger Robot   void Lock();
49*7c3d14c8STreehugger Robot   void Unlock();
50*7c3d14c8STreehugger Robot 
51*7c3d14c8STreehugger Robot   void ReadLock();
52*7c3d14c8STreehugger Robot   void ReadUnlock();
53*7c3d14c8STreehugger Robot 
54*7c3d14c8STreehugger Robot   void CheckLocked();
55*7c3d14c8STreehugger Robot 
56*7c3d14c8STreehugger Robot  private:
57*7c3d14c8STreehugger Robot   atomic_uintptr_t state_;
58*7c3d14c8STreehugger Robot #if SANITIZER_DEBUG
59*7c3d14c8STreehugger Robot   MutexType type_;
60*7c3d14c8STreehugger Robot #endif
61*7c3d14c8STreehugger Robot #if TSAN_COLLECT_STATS
62*7c3d14c8STreehugger Robot   StatType stat_type_;
63*7c3d14c8STreehugger Robot #endif
64*7c3d14c8STreehugger Robot 
65*7c3d14c8STreehugger Robot   Mutex(const Mutex&);
66*7c3d14c8STreehugger Robot   void operator = (const Mutex&);
67*7c3d14c8STreehugger Robot };
68*7c3d14c8STreehugger Robot 
69*7c3d14c8STreehugger Robot typedef GenericScopedLock<Mutex> Lock;
70*7c3d14c8STreehugger Robot typedef GenericScopedReadLock<Mutex> ReadLock;
71*7c3d14c8STreehugger Robot 
72*7c3d14c8STreehugger Robot class InternalDeadlockDetector {
73*7c3d14c8STreehugger Robot  public:
74*7c3d14c8STreehugger Robot   InternalDeadlockDetector();
75*7c3d14c8STreehugger Robot   void Lock(MutexType t);
76*7c3d14c8STreehugger Robot   void Unlock(MutexType t);
77*7c3d14c8STreehugger Robot   void CheckNoLocks();
78*7c3d14c8STreehugger Robot  private:
79*7c3d14c8STreehugger Robot   u64 seq_;
80*7c3d14c8STreehugger Robot   u64 locked_[MutexTypeCount];
81*7c3d14c8STreehugger Robot };
82*7c3d14c8STreehugger Robot 
83*7c3d14c8STreehugger Robot void InitializeMutex();
84*7c3d14c8STreehugger Robot 
85*7c3d14c8STreehugger Robot // Checks that the current thread does not hold any runtime locks
86*7c3d14c8STreehugger Robot // (e.g. when returning from an interceptor).
87*7c3d14c8STreehugger Robot void CheckNoLocks(ThreadState *thr);
88*7c3d14c8STreehugger Robot 
89*7c3d14c8STreehugger Robot }  // namespace __tsan
90*7c3d14c8STreehugger Robot 
91*7c3d14c8STreehugger Robot #endif  // TSAN_MUTEX_H
92