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
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(unsigned tid) const {
45    DCHECK_LT(tid, kMaxTidInClock);
46    return clk_[tid];
47  }
48
49  void set(unsigned tid, u64 v) {
50    DCHECK_LT(tid, kMaxTid);
51    DCHECK_GE(v, clk_[tid]);
52    clk_[tid] = v;
53    if (nclk_ <= tid)
54      nclk_ = tid + 1;
55  }
56
57  void tick(unsigned tid) {
58    DCHECK_LT(tid, kMaxTid);
59    clk_[tid]++;
60    if (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  void ReleaseStore(SyncClock *dst) const;
72
73 private:
74  uptr nclk_;
75  u64 clk_[kMaxTidInClock];
76};
77
78}  // namespace __tsan
79
80#endif  // TSAN_CLOCK_H
81