tsan_clock.h revision 9d150bdb433ddd092073dabd87ba15aa176603a1
17ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===-- tsan_clock.h --------------------------------------------*- C++ -*-===//
27ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
37ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//                     The LLVM Compiler Infrastructure
47ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
57ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is distributed under the University of Illinois Open Source
67ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// License. See LICENSE.TXT for details.
77ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
87ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
97ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
107ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// This file is a part of ThreadSanitizer (TSan), a race detector.
117ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//
127ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany//===----------------------------------------------------------------------===//
137ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#ifndef TSAN_CLOCK_H
147ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#define TSAN_CLOCK_H
157ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
167ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_defs.h"
177ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#include "tsan_vector.h"
187ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
197ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanynamespace __tsan {
207ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
217ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// The clock that lives in sync variables (mutexes, atomics, etc).
227ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanyclass SyncClock {
237ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany public:
247ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  SyncClock();
257ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
267ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  uptr size() const {
277ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return clk_.Size();
287ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
297ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
307ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void Reset() {
317ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    clk_.Reset();
327ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
337ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
347ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany private:
357ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  Vector<u64> clk_;
367ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  friend struct ThreadClock;
377ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
387ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
397ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany// The clock that lives in threads.
407ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryanystruct ThreadClock {
417ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany public:
427ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  ThreadClock();
437ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
443d6ae1580e5f34f02d24ff0c8bb352a5026c327aKostya Serebryany  u64 get(unsigned tid) const {
45adfb65039646774f0f063b538f8fb0aec021f42bDmitry Vyukov    DCHECK_LT(tid, kMaxTidInClock);
467ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return clk_[tid];
477ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
487ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
493d6ae1580e5f34f02d24ff0c8bb352a5026c327aKostya Serebryany  void set(unsigned tid, u64 v) {
50adfb65039646774f0f063b538f8fb0aec021f42bDmitry Vyukov    DCHECK_LT(tid, kMaxTid);
51adfb65039646774f0f063b538f8fb0aec021f42bDmitry Vyukov    DCHECK_GE(v, clk_[tid]);
527ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    clk_[tid] = v;
533d6ae1580e5f34f02d24ff0c8bb352a5026c327aKostya Serebryany    if (nclk_ <= tid)
547ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      nclk_ = tid + 1;
557ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
567ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
573d6ae1580e5f34f02d24ff0c8bb352a5026c327aKostya Serebryany  void tick(unsigned tid) {
58adfb65039646774f0f063b538f8fb0aec021f42bDmitry Vyukov    DCHECK_LT(tid, kMaxTid);
597ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    clk_[tid]++;
603d6ae1580e5f34f02d24ff0c8bb352a5026c327aKostya Serebryany    if (nclk_ <= tid)
617ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany      nclk_ = tid + 1;
627ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
637ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
649d150bdb433ddd092073dabd87ba15aa176603a1Dmitry Vyukov  void Disable(unsigned tid);
658f1104cbf1af615242e14c66d1b3dd9e8437b152Dmitry Vyukov
667ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  uptr size() const {
677ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany    return nclk_;
687ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  }
697ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
707ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void acquire(const SyncClock *src);
717ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void release(SyncClock *dst) const;
727ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  void acq_rel(SyncClock *dst);
739d150bdb433ddd092073dabd87ba15aa176603a1Dmitry Vyukov  void ReleaseStore(SyncClock *dst) const;
747ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
757ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany private:
767ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany  uptr nclk_;
77069ce828e3057819ee34426496ea7080f7cc52f0Dmitry Vyukov  u64 clk_[kMaxTidInClock];
787ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany};
797ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
807ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany}  // namespace __tsan
817ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany
827ac41484ea322e0ea5774df681660269f5dc321eKostya Serebryany#endif  // TSAN_CLOCK_H
83