tsan_clock.h revision 1.1
1//===-- tsan_clock.h --------------------------------------------*- C++ -*-===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// This file is a part of ThreadSanitizer (TSan), a race detector.
9//
10//===----------------------------------------------------------------------===//
11#ifndef TSAN_CLOCK_H
12#define TSAN_CLOCK_H
13
14#include "tsan_defs.h"
15#include "tsan_vector.h"
16
17namespace __tsan {
18
19// The clock that lives in sync variables (mutexes, atomics, etc).
20class SyncClock {
21 public:
22  SyncClock();
23
24  uptr size() const {
25    return clk_.Size();
26  }
27
28  void Reset() {
29    clk_.Reset();
30  }
31
32 private:
33  Vector<u64> clk_;
34  friend struct ThreadClock;
35};
36
37// The clock that lives in threads.
38struct ThreadClock {
39 public:
40  ThreadClock();
41
42  u64 get(unsigned tid) const {
43    DCHECK_LT(tid, kMaxTidInClock);
44    return clk_[tid];
45  }
46
47  void set(unsigned tid, u64 v) {
48    DCHECK_LT(tid, kMaxTid);
49    DCHECK_GE(v, clk_[tid]);
50    clk_[tid] = v;
51    if (nclk_ <= tid)
52      nclk_ = tid + 1;
53  }
54
55  void tick(unsigned tid) {
56    DCHECK_LT(tid, kMaxTid);
57    clk_[tid]++;
58    if (nclk_ <= tid)
59      nclk_ = tid + 1;
60  }
61
62  uptr size() const {
63    return nclk_;
64  }
65
66  void acquire(const SyncClock *src);
67  void release(SyncClock *dst) const;
68  void acq_rel(SyncClock *dst);
69  void ReleaseStore(SyncClock *dst) const;
70
71 private:
72  uptr nclk_;
73  u64 clk_[kMaxTidInClock];
74};
75
76}  // namespace __tsan
77
78#endif  // TSAN_CLOCK_H
79