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(Processor
*proc
);
51 // 48 lsb is addr, then 14 bits is low part of uid, then 2 zero bits.
52 return GetLsb((u64
)addr
| (uid
<< 48), 60);
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
, 48);
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(Processor
*proc
, uptr p
);
74 bool FreeRange(Processor
*proc
, uptr p
, uptr sz
);
75 void ResetRange(Processor
*proc
, uptr p
, uptr sz
);
76 MBlock
* GetBlock(uptr p
);
78 SyncVar
* GetOrCreateAndLock(ThreadState
*thr
, uptr pc
,
79 uptr addr
, bool write_lock
);
80 SyncVar
* GetIfExistsAndLock(uptr addr
, bool write_lock
);
82 void MoveMemory(uptr src
, uptr dst
, uptr sz
);
84 void OnProcIdle(Processor
*proc
);
87 static const u32 kFlagMask
= 3u << 30;
88 static const u32 kFlagBlock
= 1u << 30;
89 static const u32 kFlagSync
= 2u << 30;
90 typedef DenseSlabAlloc
<MBlock
, 1<<16, 1<<12> BlockAlloc
;
91 typedef DenseSlabAlloc
<SyncVar
, 1<<16, 1<<10> SyncAlloc
;
92 BlockAlloc block_alloc_
;
93 SyncAlloc sync_alloc_
;
94 atomic_uint64_t uid_gen_
;
96 SyncVar
* GetAndLock(ThreadState
*thr
, uptr pc
, uptr addr
, bool write_lock
,
100 } // namespace __tsan
102 #endif // TSAN_SYNC_H