blob: c4f8194479f7689c7ae282b84193f333f5668773 [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
44 u64 get(int tid) const {
45 DCHECK(tid < kMaxTid);
46 return clk_[tid];
47 }
48
49 void set(int tid, u64 v) {
50 DCHECK(tid < kMaxTid);
51 DCHECK(v >= clk_[tid]);
52 clk_[tid] = v;
53 if ((int)nclk_ <= tid)
54 nclk_ = tid + 1;
55 }
56
57 void tick(int tid) {
58 DCHECK(tid < kMaxTid);
59 clk_[tid]++;
60 if ((int)nclk_ <= tid)
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_;
74 u64 clk_[kMaxTid];
75};
76
77} // namespace __tsan
78
79#endif // TSAN_CLOCK_H