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 "tsan_clock.h"
17 #include "tsan_defs.h"
18 #include "tsan_mutex.h"
27 // Initialized the object in "static mode",
28 // in this mode it never calls malloc/free but uses the provided buffer.
29 StackTrace(uptr
*buf
, uptr cnt
);
33 void Init(const uptr
*pcs
, uptr cnt
);
34 void ObtainCurrent(ThreadState
*thr
, uptr toppc
);
37 uptr
Get(uptr i
) const;
38 const uptr
*Begin() const;
39 void CopyFrom(const StackTrace
& other
);
46 StackTrace(const StackTrace
&);
47 void operator = (const StackTrace
&);
51 explicit SyncVar(uptr addr
, u64 uid
);
53 static const int kInvalidTid
= -1;
57 const u64 uid
; // Globally unique id.
59 SyncClock read_clock
; // Used for rw mutexes only.
60 StackTrace creation_stack
;
61 int owner_tid
; // Set only by exclusive owners.
68 SyncVar
*next
; // In SyncTab hashtable.
70 uptr
GetMemoryConsumption();
72 // 47 lsb is addr, then 14 bits is low part of uid, then 3 zero bits.
73 return GetLsb((u64
)addr
| (uid
<< 47), 61);
75 bool CheckId(u64 uid
) const {
76 CHECK_EQ(uid
, GetLsb(uid
, 14));
77 return GetLsb(this->uid
, 14) == uid
;
79 static uptr
SplitId(u64 id
, u64
*uid
) {
81 return (uptr
)GetLsb(id
, 47);
90 SyncVar
* GetOrCreateAndLock(ThreadState
*thr
, uptr pc
,
91 uptr addr
, bool write_lock
);
92 SyncVar
* GetIfExistsAndLock(uptr addr
, bool write_lock
);
94 // If the SyncVar does not exist, returns 0.
95 SyncVar
* GetAndRemove(ThreadState
*thr
, uptr pc
, uptr addr
);
97 SyncVar
* Create(ThreadState
*thr
, uptr pc
, uptr addr
);
99 uptr
GetMemoryConsumption(uptr
*nsync
);
105 char pad
[kCacheLineSize
- sizeof(Mutex
) - sizeof(SyncVar
*)]; // NOLINT
109 // FIXME: Implement something more sane.
110 static const int kPartCount
= 1009;
111 Part tab_
[kPartCount
];
112 atomic_uint64_t uid_gen_
;
114 int PartIdx(uptr addr
);
116 SyncVar
* GetAndLock(ThreadState
*thr
, uptr pc
,
117 uptr addr
, bool write_lock
, bool create
);
119 SyncTab(const SyncTab
&); // Not implemented.
120 void operator = (const SyncTab
&); // Not implemented.
123 } // namespace __tsan
125 #endif // TSAN_SYNC_H