LWG 3035. std::allocator's constructors should be constexpr
[official-gcc.git] / libsanitizer / tsan / tsan_sync.h
blob195279fc1f24fd5780dd40ce1e100b0130944fb4
1 //===-- tsan_sync.h ---------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11 #ifndef TSAN_SYNC_H
12 #define TSAN_SYNC_H
14 #include "sanitizer_common/sanitizer_atomic.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
17 #include "tsan_defs.h"
18 #include "tsan_clock.h"
19 #include "tsan_mutex.h"
20 #include "tsan_dense_alloc.h"
22 namespace __tsan {
24 // These need to match __tsan_mutex_* flags defined in tsan_interface.h.
25 // See documentation there as well.
26 enum MutexFlags {
27 MutexFlagLinkerInit = 1 << 0, // __tsan_mutex_linker_init
28 MutexFlagWriteReentrant = 1 << 1, // __tsan_mutex_write_reentrant
29 MutexFlagReadReentrant = 1 << 2, // __tsan_mutex_read_reentrant
30 MutexFlagReadLock = 1 << 3, // __tsan_mutex_read_lock
31 MutexFlagTryLock = 1 << 4, // __tsan_mutex_try_lock
32 MutexFlagTryLockFailed = 1 << 5, // __tsan_mutex_try_lock_failed
33 MutexFlagRecursiveLock = 1 << 6, // __tsan_mutex_recursive_lock
34 MutexFlagRecursiveUnlock = 1 << 7, // __tsan_mutex_recursive_unlock
36 // The following flags are runtime private.
37 // Mutex API misuse was detected, so don't report any more.
38 MutexFlagBroken = 1 << 30,
39 // We did not intercept pre lock event, so handle it on post lock.
40 MutexFlagDoPreLockOnPostLock = 1 << 29,
41 // Must list all mutex creation flags.
42 MutexCreationFlagMask = MutexFlagLinkerInit |
43 MutexFlagWriteReentrant |
44 MutexFlagReadReentrant,
47 struct SyncVar {
48 SyncVar();
50 static const int kInvalidTid = -1;
52 uptr addr; // overwritten by DenseSlabAlloc freelist
53 Mutex mtx;
54 u64 uid; // Globally unique id.
55 u32 creation_stack_id;
56 int owner_tid; // Set only by exclusive owners.
57 u64 last_lock;
58 int recursion;
59 atomic_uint32_t flags;
60 u32 next; // in MetaMap
61 DDMutex dd;
62 SyncClock read_clock; // Used for rw mutexes only.
63 // The clock is placed last, so that it is situated on a different cache line
64 // with the mtx. This reduces contention for hot sync objects.
65 SyncClock clock;
67 void Init(ThreadState *thr, uptr pc, uptr addr, u64 uid);
68 void Reset(Processor *proc);
70 u64 GetId() const {
71 // 48 lsb is addr, then 14 bits is low part of uid, then 2 zero bits.
72 return GetLsb((u64)addr | (uid << 48), 60);
74 bool CheckId(u64 uid) const {
75 CHECK_EQ(uid, GetLsb(uid, 14));
76 return GetLsb(this->uid, 14) == uid;
78 static uptr SplitId(u64 id, u64 *uid) {
79 *uid = id >> 48;
80 return (uptr)GetLsb(id, 48);
83 bool IsFlagSet(u32 f) const {
84 return atomic_load_relaxed(&flags) & f;
87 void SetFlags(u32 f) {
88 atomic_store_relaxed(&flags, atomic_load_relaxed(&flags) | f);
91 void UpdateFlags(u32 flagz) {
92 // Filter out operation flags.
93 if (!(flagz & MutexCreationFlagMask))
94 return;
95 u32 current = atomic_load_relaxed(&flags);
96 if (current & MutexCreationFlagMask)
97 return;
98 // Note: this can be called from MutexPostReadLock which holds only read
99 // lock on the SyncVar.
100 atomic_store_relaxed(&flags, current | (flagz & MutexCreationFlagMask));
104 /* MetaMap allows to map arbitrary user pointers onto various descriptors.
105 Currently it maps pointers to heap block descriptors and sync var descs.
106 It uses 1/2 direct shadow, see tsan_platform.h.
108 class MetaMap {
109 public:
110 MetaMap();
112 void AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz);
113 uptr FreeBlock(Processor *proc, uptr p);
114 bool FreeRange(Processor *proc, uptr p, uptr sz);
115 void ResetRange(Processor *proc, uptr p, uptr sz);
116 MBlock* GetBlock(uptr p);
118 SyncVar* GetOrCreateAndLock(ThreadState *thr, uptr pc,
119 uptr addr, bool write_lock);
120 SyncVar* GetIfExistsAndLock(uptr addr, bool write_lock);
122 void MoveMemory(uptr src, uptr dst, uptr sz);
124 void OnProcIdle(Processor *proc);
126 private:
127 static const u32 kFlagMask = 3u << 30;
128 static const u32 kFlagBlock = 1u << 30;
129 static const u32 kFlagSync = 2u << 30;
130 typedef DenseSlabAlloc<MBlock, 1<<16, 1<<12> BlockAlloc;
131 typedef DenseSlabAlloc<SyncVar, 1<<16, 1<<10> SyncAlloc;
132 BlockAlloc block_alloc_;
133 SyncAlloc sync_alloc_;
134 atomic_uint64_t uid_gen_;
136 SyncVar* GetAndLock(ThreadState *thr, uptr pc, uptr addr, bool write_lock,
137 bool create);
140 } // namespace __tsan
142 #endif // TSAN_SYNC_H