1 //===-- tsan_clock.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 "tsan_defs.h"
15 #include "tsan_dense_alloc.h"
21 u64 reused
: 64 - kClkBits
;
25 static const uptr kSize
= 512;
26 static const uptr kTableSize
= kSize
/ sizeof(u32
);
27 static const uptr kClockCount
= kSize
/ sizeof(ClockElem
);
30 u32 table
[kTableSize
];
31 ClockElem clock
[kClockCount
];
38 typedef DenseSlabAlloc
<ClockBlock
, 1<<16, 1<<10> ClockAlloc
;
39 typedef DenseSlabAllocCache ClockCache
;
41 // The clock that lives in sync variables (mutexes, atomics, etc).
51 u64
get(unsigned tid
) const {
52 return elem(tid
).epoch
;
55 void Resize(ClockCache
*c
, uptr nclk
);
56 void Reset(ClockCache
*c
);
58 void DebugDump(int(*printf
)(const char *s
, ...));
61 friend struct ThreadClock
;
62 static const uptr kDirtyTids
= 2;
64 unsigned release_store_tid_
;
65 unsigned release_store_reused_
;
66 unsigned dirty_tids_
[kDirtyTids
];
67 // tab_ contains indirect pointer to a 512b block using DenseSlabAlloc.
68 // If size_ <= 64, then tab_ points to an array with 64 ClockElem's.
69 // Otherwise, tab_ points to an array with 128 u32 elements,
70 // each pointing to the second-level 512b block with 64 ClockElem's.
75 ClockElem
&elem(unsigned tid
) const;
78 // The clock that lives in threads.
81 typedef DenseSlabAllocCache Cache
;
83 explicit ThreadClock(unsigned tid
, unsigned reused
= 0);
85 u64
get(unsigned tid
) const {
86 DCHECK_LT(tid
, kMaxTidInClock
);
87 return clk_
[tid
].epoch
;
90 void set(unsigned tid
, u64 v
);
93 DCHECK_GE(v
, clk_
[tid_
].epoch
);
105 void acquire(ClockCache
*c
, const SyncClock
*src
);
106 void release(ClockCache
*c
, SyncClock
*dst
) const;
107 void acq_rel(ClockCache
*c
, SyncClock
*dst
);
108 void ReleaseStore(ClockCache
*c
, SyncClock
*dst
) const;
111 void DebugDump(int(*printf
)(const char *s
, ...));
114 static const uptr kDirtyTids
= SyncClock::kDirtyTids
;
116 const unsigned reused_
;
119 ClockElem clk_
[kMaxTidInClock
];
121 bool IsAlreadyAcquired(const SyncClock
*src
) const;
122 void UpdateCurrentThread(SyncClock
*dst
) const;
125 } // namespace __tsan
127 #endif // TSAN_CLOCK_H