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