Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 1 | //===-- tsan_clock.h --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of ThreadSanitizer (TSan), a race detector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef TSAN_CLOCK_H |
| 14 | #define TSAN_CLOCK_H |
| 15 | |
| 16 | #include "tsan_defs.h" |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 17 | #include "tsan_dense_alloc.h" |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 18 | |
| 19 | namespace __tsan { |
| 20 | |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 21 | struct ClockElem { |
| 22 | u64 epoch : kClkBits; |
| 23 | u64 reused : 64 - kClkBits; |
| 24 | }; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 25 | |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 26 | struct ClockBlock { |
| 27 | static const uptr kSize = 512; |
| 28 | static const uptr kTableSize = kSize / sizeof(u32); |
| 29 | static const uptr kClockCount = kSize / sizeof(ClockElem); |
| 30 | |
| 31 | union { |
| 32 | u32 table[kTableSize]; |
| 33 | ClockElem clock[kClockCount]; |
| 34 | }; |
| 35 | |
| 36 | ClockBlock() { |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | typedef DenseSlabAlloc<ClockBlock, 1<<16, 1<<10> ClockAlloc; |
| 41 | typedef DenseSlabAllocCache ClockCache; |
| 42 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 43 | // The clock that lives in sync variables (mutexes, atomics, etc). |
| 44 | class SyncClock { |
| 45 | public: |
| 46 | SyncClock(); |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 47 | ~SyncClock(); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 48 | |
| 49 | uptr size() const { |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 50 | return size_; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 53 | u64 get(unsigned tid) const { |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 54 | return elem(tid).epoch; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Dmitry Vyukov | dc1caa7 | 2014-09-02 09:34:34 +0000 | [diff] [blame] | 57 | void Resize(ClockCache *c, uptr nclk); |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 58 | void Reset(ClockCache *c); |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 59 | |
| 60 | void DebugDump(int(*printf)(const char *s, ...)); |
| 61 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 62 | private: |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 63 | friend struct ThreadClock; |
| 64 | static const uptr kDirtyTids = 2; |
| 65 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 66 | unsigned release_store_tid_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 67 | unsigned release_store_reused_; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 68 | unsigned dirty_tids_[kDirtyTids]; |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 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. |
| 73 | ClockBlock *tab_; |
| 74 | u32 tab_idx_; |
| 75 | u32 size_; |
| 76 | |
| 77 | ClockElem &elem(unsigned tid) const; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // The clock that lives in threads. |
| 81 | struct ThreadClock { |
| 82 | public: |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 83 | typedef DenseSlabAllocCache Cache; |
| 84 | |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 85 | explicit ThreadClock(unsigned tid, unsigned reused = 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 86 | |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 87 | u64 get(unsigned tid) const { |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 88 | DCHECK_LT(tid, kMaxTidInClock); |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 89 | return clk_[tid].epoch; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 92 | void set(unsigned tid, u64 v); |
| 93 | |
| 94 | void set(u64 v) { |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 95 | DCHECK_GE(v, clk_[tid_].epoch); |
| 96 | clk_[tid_].epoch = v; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 99 | void tick() { |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 100 | clk_[tid_].epoch++; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | uptr size() const { |
| 104 | return nclk_; |
| 105 | } |
| 106 | |
Dmitry Vyukov | 70db9d4 | 2014-08-05 18:45:02 +0000 | [diff] [blame] | 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; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 111 | |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 112 | void DebugReset(); |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 113 | void DebugDump(int(*printf)(const char *s, ...)); |
| 114 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 115 | private: |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 116 | static const uptr kDirtyTids = SyncClock::kDirtyTids; |
| 117 | const unsigned tid_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 118 | const unsigned reused_; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 119 | u64 last_acquire_; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 120 | uptr nclk_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 121 | ClockElem clk_[kMaxTidInClock]; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 122 | |
| 123 | bool IsAlreadyAcquired(const SyncClock *src) const; |
| 124 | void UpdateCurrentThread(SyncClock *dst) const; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace __tsan |
| 128 | |
| 129 | #endif // TSAN_CLOCK_H |