1 //===-- tsan_sync.h ---------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
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"
27 static const int kInvalidTid
= -1;
29 uptr addr
; // overwritten by DenseSlabAlloc freelist
31 u64 uid
; // Globally unique id.
32 u32 creation_stack_id
;
33 int owner_tid
; // Set only by exclusive owners.
40 u32 next
; // in MetaMap
42 SyncClock read_clock
; // Used for rw mutexes only.
43 // The clock is placed last, so that it is situated on a different cache line
44 // with the mtx. This reduces contention for hot sync objects.
47 void Init(ThreadState
*thr
, uptr pc
, uptr addr
, u64 uid
);
48 void Reset(ThreadState
*thr
);
51 // 47 lsb is addr, then 14 bits is low part of uid, then 3 zero bits.
52 return GetLsb((u64
)addr
| (uid
<< 47), 61);
54 bool CheckId(u64 uid
) const {
55 CHECK_EQ(uid
, GetLsb(uid
, 14));
56 return GetLsb(this->uid
, 14) == uid
;
58 static uptr
SplitId(u64 id
, u64
*uid
) {
60 return (uptr
)GetLsb(id
, 47);
64 /* MetaMap allows to map arbitrary user pointers onto various descriptors.
65 Currently it maps pointers to heap block descriptors and sync var descs.
66 It uses 1/2 direct shadow, see tsan_platform.h.
72 void AllocBlock(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
);
73 uptr
FreeBlock(ThreadState
*thr
, uptr pc
, uptr p
);
74 void FreeRange(ThreadState
*thr
, uptr pc
, uptr p
, uptr sz
);
75 MBlock
* GetBlock(uptr p
);
77 SyncVar
* GetOrCreateAndLock(ThreadState
*thr
, uptr pc
,
78 uptr addr
, bool write_lock
);
79 SyncVar
* GetIfExistsAndLock(uptr addr
);
81 void MoveMemory(uptr src
, uptr dst
, uptr sz
);
83 void OnThreadIdle(ThreadState
*thr
);
86 static const u32 kFlagMask
= 3 << 30;
87 static const u32 kFlagBlock
= 1 << 30;
88 static const u32 kFlagSync
= 2 << 30;
89 typedef DenseSlabAlloc
<MBlock
, 1<<16, 1<<12> BlockAlloc
;
90 typedef DenseSlabAlloc
<SyncVar
, 1<<16, 1<<10> SyncAlloc
;
91 BlockAlloc block_alloc_
;
92 SyncAlloc sync_alloc_
;
93 atomic_uint64_t uid_gen_
;
95 SyncVar
* GetAndLock(ThreadState
*thr
, uptr pc
, uptr addr
, bool write_lock
,
101 #endif // TSAN_SYNC_H