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" |
| 17 | #include "tsan_vector.h" |
| 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 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 26 | // The clock that lives in sync variables (mutexes, atomics, etc). |
| 27 | class SyncClock { |
| 28 | public: |
| 29 | SyncClock(); |
| 30 | |
| 31 | uptr size() const { |
| 32 | return clk_.Size(); |
| 33 | } |
| 34 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 35 | u64 get(unsigned tid) const { |
| 36 | DCHECK_LT(tid, clk_.Size()); |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 37 | return clk_[tid].epoch; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 40 | void Reset(); |
Dmitry Vyukov | bde4c9c | 2014-05-29 13:50:54 +0000 | [diff] [blame] | 41 | void Zero(); |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 42 | |
| 43 | void DebugDump(int(*printf)(const char *s, ...)); |
| 44 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 45 | private: |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 46 | unsigned release_store_tid_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 47 | unsigned release_store_reused_; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 48 | static const uptr kDirtyTids = 2; |
| 49 | unsigned dirty_tids_[kDirtyTids]; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 50 | mutable Vector<ClockElem> clk_; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 51 | friend struct ThreadClock; |
| 52 | }; |
| 53 | |
| 54 | // The clock that lives in threads. |
| 55 | struct ThreadClock { |
| 56 | public: |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 57 | explicit ThreadClock(unsigned tid, unsigned reused = 0); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 59 | u64 get(unsigned tid) const { |
Dmitry Vyukov | 302cebb | 2012-05-22 18:07:45 +0000 | [diff] [blame] | 60 | DCHECK_LT(tid, kMaxTidInClock); |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 61 | return clk_[tid].epoch; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 64 | void set(unsigned tid, u64 v); |
| 65 | |
| 66 | void set(u64 v) { |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 67 | DCHECK_GE(v, clk_[tid_].epoch); |
| 68 | clk_[tid_].epoch = v; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 71 | void tick() { |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 72 | clk_[tid_].epoch++; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | uptr size() const { |
| 76 | return nclk_; |
| 77 | } |
| 78 | |
| 79 | void acquire(const SyncClock *src); |
| 80 | void release(SyncClock *dst) const; |
| 81 | void acq_rel(SyncClock *dst); |
Dmitry Vyukov | 904d3f9 | 2012-07-28 15:27:41 +0000 | [diff] [blame] | 82 | void ReleaseStore(SyncClock *dst) const; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 83 | |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 84 | void DebugReset(); |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 85 | void DebugDump(int(*printf)(const char *s, ...)); |
| 86 | |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 87 | private: |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 88 | static const uptr kDirtyTids = SyncClock::kDirtyTids; |
| 89 | const unsigned tid_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 90 | const unsigned reused_; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 91 | u64 last_acquire_; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 92 | uptr nclk_; |
Dmitry Vyukov | b5eb8f0 | 2014-04-11 15:38:03 +0000 | [diff] [blame] | 93 | ClockElem clk_[kMaxTidInClock]; |
Dmitry Vyukov | d23118c | 2014-03-24 18:54:20 +0000 | [diff] [blame] | 94 | |
| 95 | bool IsAlreadyAcquired(const SyncClock *src) const; |
| 96 | void UpdateCurrentThread(SyncClock *dst) const; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace __tsan |
| 100 | |
| 101 | #endif // TSAN_CLOCK_H |