1 //===-- tsan_clock.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 "tsan_defs.h"
17 #include "tsan_dense_alloc.h"
23 u64 reused
: 64 - kClkBits
;
27 static const uptr kSize
= 512;
28 static const uptr kTableSize
= kSize
/ sizeof(u32
);
29 static const uptr kClockCount
= kSize
/ sizeof(ClockElem
);
32 u32 table
[kTableSize
];
33 ClockElem clock
[kClockCount
];
40 typedef DenseSlabAlloc
<ClockBlock
, 1<<16, 1<<10> ClockAlloc
;
41 typedef DenseSlabAllocCache ClockCache
;
43 // The clock that lives in sync variables (mutexes, atomics, etc).
53 u64
get(unsigned tid
) const {
54 return elem(tid
).epoch
;
57 void Resize(ClockCache
*c
, uptr nclk
);
58 void Reset(ClockCache
*c
);
60 void DebugDump(int(*printf
)(const char *s
, ...));
63 friend struct ThreadClock
;
64 static const uptr kDirtyTids
= 2;
66 unsigned release_store_tid_
;
67 unsigned release_store_reused_
;
68 unsigned dirty_tids_
[kDirtyTids
];
69 // tab_ contains indirect pointer to a 512b block using DenseSlabAlloc.
70 // If size_ <= 64, then tab_ points to an array with 64 ClockElem's.
71 // Otherwise, tab_ points to an array with 128 u32 elements,
72 // each pointing to the second-level 512b block with 64 ClockElem's.
77 ClockElem
&elem(unsigned tid
) const;
80 // The clock that lives in threads.
83 typedef DenseSlabAllocCache Cache
;
85 explicit ThreadClock(unsigned tid
, unsigned reused
= 0);
87 u64
get(unsigned tid
) const {
88 DCHECK_LT(tid
, kMaxTidInClock
);
89 return clk_
[tid
].epoch
;
92 void set(unsigned tid
, u64 v
);
95 DCHECK_GE(v
, clk_
[tid_
].epoch
);
107 void acquire(ClockCache
*c
, const SyncClock
*src
);
108 void release(ClockCache
*c
, SyncClock
*dst
) const;
109 void acq_rel(ClockCache
*c
, SyncClock
*dst
);
110 void ReleaseStore(ClockCache
*c
, SyncClock
*dst
) const;
113 void DebugDump(int(*printf
)(const char *s
, ...));
116 static const uptr kDirtyTids
= SyncClock::kDirtyTids
;
118 const unsigned reused_
;
121 ClockElem clk_
[kMaxTidInClock
];
123 bool IsAlreadyAcquired(const SyncClock
*src
) const;
124 void UpdateCurrentThread(SyncClock
*dst
) const;
127 } // namespace __tsan
129 #endif // TSAN_CLOCK_H