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 | |
| 21 | // The clock that lives in sync variables (mutexes, atomics, etc). |
| 22 | class SyncClock { |
| 23 | public: |
| 24 | SyncClock(); |
| 25 | |
| 26 | uptr size() const { |
| 27 | return clk_.Size(); |
| 28 | } |
| 29 | |
| 30 | void Reset() { |
| 31 | clk_.Reset(); |
| 32 | } |
| 33 | |
| 34 | private: |
| 35 | Vector<u64> clk_; |
| 36 | friend struct ThreadClock; |
| 37 | }; |
| 38 | |
| 39 | // The clock that lives in threads. |
| 40 | struct ThreadClock { |
| 41 | public: |
| 42 | ThreadClock(); |
| 43 | |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 44 | u64 get(unsigned tid) const { |
Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame^] | 45 | DCHECK(tid < kMaxTidInClock); |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 46 | return clk_[tid]; |
| 47 | } |
| 48 | |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 49 | void set(unsigned tid, u64 v) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 50 | DCHECK(tid < kMaxTid); |
| 51 | DCHECK(v >= clk_[tid]); |
| 52 | clk_[tid] = v; |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 53 | if (nclk_ <= tid) |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 54 | nclk_ = tid + 1; |
| 55 | } |
| 56 | |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 57 | void tick(unsigned tid) { |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 58 | DCHECK(tid < kMaxTid); |
| 59 | clk_[tid]++; |
Kostya Serebryany | 07c4805 | 2012-05-11 14:42:24 +0000 | [diff] [blame] | 60 | if (nclk_ <= tid) |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 61 | nclk_ = tid + 1; |
| 62 | } |
| 63 | |
| 64 | uptr size() const { |
| 65 | return nclk_; |
| 66 | } |
| 67 | |
| 68 | void acquire(const SyncClock *src); |
| 69 | void release(SyncClock *dst) const; |
| 70 | void acq_rel(SyncClock *dst); |
| 71 | |
| 72 | private: |
| 73 | uptr nclk_; |
Dmitry Vyukov | fee5b7d | 2012-05-17 14:17:51 +0000 | [diff] [blame^] | 74 | u64 clk_[kMaxTidInClock]; |
Kostya Serebryany | 4ad375f | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | } // namespace __tsan |
| 78 | |
| 79 | #endif // TSAN_CLOCK_H |