blob: 2ad635ed69d429b9c49a9ad4dde222c288237f07 [file] [log] [blame]
Kostya Serebryany4ad375f2012-05-10 13:48:04 +00001//===-- 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
19namespace __tsan {
20
21// The clock that lives in sync variables (mutexes, atomics, etc).
22class 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.
40struct ThreadClock {
41 public:
42 ThreadClock();
43
Kostya Serebryany07c48052012-05-11 14:42:24 +000044 u64 get(unsigned tid) const {
Dmitry Vyukovfee5b7d2012-05-17 14:17:51 +000045 DCHECK(tid < kMaxTidInClock);
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000046 return clk_[tid];
47 }
48
Kostya Serebryany07c48052012-05-11 14:42:24 +000049 void set(unsigned tid, u64 v) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000050 DCHECK(tid < kMaxTid);
51 DCHECK(v >= clk_[tid]);
52 clk_[tid] = v;
Kostya Serebryany07c48052012-05-11 14:42:24 +000053 if (nclk_ <= tid)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000054 nclk_ = tid + 1;
55 }
56
Kostya Serebryany07c48052012-05-11 14:42:24 +000057 void tick(unsigned tid) {
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000058 DCHECK(tid < kMaxTid);
59 clk_[tid]++;
Kostya Serebryany07c48052012-05-11 14:42:24 +000060 if (nclk_ <= tid)
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000061 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 Vyukovfee5b7d2012-05-17 14:17:51 +000074 u64 clk_[kMaxTidInClock];
Kostya Serebryany4ad375f2012-05-10 13:48:04 +000075};
76
77} // namespace __tsan
78
79#endif // TSAN_CLOCK_H