summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/tsan/rtl/tsan_clock.h
blob: a57a0e4f791e7b09e86a730f16fd83de3a177fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//===-- tsan_clock.h --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of ThreadSanitizer (TSan), a race detector.
//
//===----------------------------------------------------------------------===//
#ifndef TSAN_CLOCK_H
#define TSAN_CLOCK_H

#include "tsan_defs.h"
#include "tsan_vector.h"

namespace __tsan {

const u64 kClkMask = (1ULL << kClkBits) - 1;

// The clock that lives in sync variables (mutexes, atomics, etc).
class SyncClock {
 public:
  SyncClock();

  uptr size() const {
    return clk_.Size();
  }

  u64 get(unsigned tid) const {
    DCHECK_LT(tid, clk_.Size());
    return clk_[tid] & kClkMask;
  }

  void Reset();

  void DebugDump(int(*printf)(const char *s, ...));

 private:
  u64 release_seq_;
  unsigned release_store_tid_;
  static const uptr kDirtyTids = 2;
  unsigned dirty_tids_[kDirtyTids];
  mutable Vector<u64> clk_;
  friend struct ThreadClock;
};

// The clock that lives in threads.
struct ThreadClock {
 public:
  explicit ThreadClock(unsigned tid);

  u64 get(unsigned tid) const {
    DCHECK_LT(tid, kMaxTidInClock);
    DCHECK_EQ(clk_[tid], clk_[tid] & kClkMask);
    return clk_[tid];
  }

  void set(unsigned tid, u64 v);

  void set(u64 v) {
    DCHECK_GE(v, clk_[tid_]);
    clk_[tid_] = v;
  }

  void tick() {
    clk_[tid_]++;
  }

  uptr size() const {
    return nclk_;
  }

  void acquire(const SyncClock *src);
  void release(SyncClock *dst) const;
  void acq_rel(SyncClock *dst);
  void ReleaseStore(SyncClock *dst) const;

  void DebugDump(int(*printf)(const char *s, ...));

 private:
  static const uptr kDirtyTids = SyncClock::kDirtyTids;
  const unsigned tid_;
  u64 last_acquire_;
  uptr nclk_;
  u64 clk_[kMaxTidInClock];

  bool IsAlreadyAcquired(const SyncClock *src) const;
  void UpdateCurrentThread(SyncClock *dst) const;
};

}  // namespace __tsan

#endif  // TSAN_CLOCK_H
OpenPOWER on IntegriCloud