1 //===-- tsan_sync.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 //===----------------------------------------------------------------------===//
16 #include "sanitizer_common/sanitizer_atomic.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_deadlock_detector_interface.h"
19 #include "tsan_defs.h"
20 #include "tsan_clock.h"
21 #include "tsan_mutex.h"
22 #include "tsan_dense_alloc.h"
29 static const int kInvalidTid = -1;
31 uptr addr; // overwritten by DenseSlabAlloc freelist
33 u64 uid; // Globally unique id.
34 u32 creation_stack_id;
35 int owner_tid; // Set only by exclusive owners.
42 u32 next; // in MetaMap
44 SyncClock read_clock; // Used for rw mutexes only.
45 // The clock is placed last, so that it is situated on a different cache line
46 // with the mtx. This reduces contention for hot sync objects.
49 void Init(ThreadState *thr, uptr pc, uptr addr, u64 uid);
50 void Reset(Processor *proc);
53 // 48 lsb is addr, then 14 bits is low part of uid, then 2 zero bits.
54 return GetLsb((u64)addr | (uid << 48), 60);
56 bool CheckId(u64 uid) const {
57 CHECK_EQ(uid, GetLsb(uid, 14));
58 return GetLsb(this->uid, 14) == uid;
60 static uptr SplitId(u64 id, u64 *uid) {
62 return (uptr)GetLsb(id, 48);
66 /* MetaMap allows to map arbitrary user pointers onto various descriptors.
67 Currently it maps pointers to heap block descriptors and sync var descs.
68 It uses 1/2 direct shadow, see tsan_platform.h.
74 void AllocBlock(ThreadState *thr, uptr pc, uptr p, uptr sz);
75 uptr FreeBlock(Processor *proc, uptr p);
76 bool FreeRange(Processor *proc, uptr p, uptr sz);
77 void ResetRange(Processor *proc, uptr p, uptr sz);
78 MBlock* GetBlock(uptr p);
80 SyncVar* GetOrCreateAndLock(ThreadState *thr, uptr pc,
81 uptr addr, bool write_lock);
82 SyncVar* GetIfExistsAndLock(uptr addr, bool write_lock);
84 void MoveMemory(uptr src, uptr dst, uptr sz);
86 void OnProcIdle(Processor *proc);
89 static const u32 kFlagMask = 3u << 30;
90 static const u32 kFlagBlock = 1u << 30;
91 static const u32 kFlagSync = 2u << 30;
92 typedef DenseSlabAlloc<MBlock, 1<<16, 1<<12> BlockAlloc;
93 typedef DenseSlabAlloc<SyncVar, 1<<16, 1<<10> SyncAlloc;
94 BlockAlloc block_alloc_;
95 SyncAlloc sync_alloc_;
96 atomic_uint64_t uid_gen_;
98 SyncVar* GetAndLock(ThreadState *thr, uptr pc, uptr addr, bool write_lock,
102 } // namespace __tsan
104 #endif // TSAN_SYNC_H