gcc/
[official-gcc.git] / libsanitizer / tsan / tsan_clock.h
blob3deb7f5198cf9c79725d0416fdd5ba0b774273eb
1 //===-- tsan_clock.h --------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11 #ifndef TSAN_CLOCK_H
12 #define TSAN_CLOCK_H
14 #include "tsan_defs.h"
15 #include "tsan_dense_alloc.h"
17 namespace __tsan {
19 struct ClockElem {
20 u64 epoch : kClkBits;
21 u64 reused : 64 - kClkBits;
24 struct ClockBlock {
25 static const uptr kSize = 512;
26 static const uptr kTableSize = kSize / sizeof(u32);
27 static const uptr kClockCount = kSize / sizeof(ClockElem);
29 union {
30 u32 table[kTableSize];
31 ClockElem clock[kClockCount];
34 ClockBlock() {
38 typedef DenseSlabAlloc<ClockBlock, 1<<16, 1<<10> ClockAlloc;
39 typedef DenseSlabAllocCache ClockCache;
41 // The clock that lives in sync variables (mutexes, atomics, etc).
42 class SyncClock {
43 public:
44 SyncClock();
45 ~SyncClock();
47 uptr size() const {
48 return size_;
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, ...));
60 private:
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.
71 ClockBlock *tab_;
72 u32 tab_idx_;
73 u32 size_;
75 ClockElem &elem(unsigned tid) const;
78 // The clock that lives in threads.
79 struct ThreadClock {
80 public:
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);
92 void set(u64 v) {
93 DCHECK_GE(v, clk_[tid_].epoch);
94 clk_[tid_].epoch = v;
97 void tick() {
98 clk_[tid_].epoch++;
101 uptr size() const {
102 return nclk_;
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;
110 void DebugReset();
111 void DebugDump(int(*printf)(const char *s, ...));
113 private:
114 static const uptr kDirtyTids = SyncClock::kDirtyTids;
115 const unsigned tid_;
116 const unsigned reused_;
117 u64 last_acquire_;
118 uptr nclk_;
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