2012-05-10 21:48:04 +08:00
|
|
|
//===-- 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"
|
2014-08-06 02:45:02 +08:00
|
|
|
#include "tsan_dense_alloc.h"
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
2014-04-11 23:38:03 +08:00
|
|
|
struct ClockElem {
|
|
|
|
u64 epoch : kClkBits;
|
|
|
|
u64 reused : 64 - kClkBits;
|
|
|
|
};
|
2014-03-25 02:54:20 +08:00
|
|
|
|
2014-08-06 02:45:02 +08:00
|
|
|
struct ClockBlock {
|
|
|
|
static const uptr kSize = 512;
|
|
|
|
static const uptr kTableSize = kSize / sizeof(u32);
|
|
|
|
static const uptr kClockCount = kSize / sizeof(ClockElem);
|
|
|
|
|
|
|
|
union {
|
|
|
|
u32 table[kTableSize];
|
|
|
|
ClockElem clock[kClockCount];
|
|
|
|
};
|
|
|
|
|
|
|
|
ClockBlock() {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseSlabAlloc<ClockBlock, 1<<16, 1<<10> ClockAlloc;
|
|
|
|
typedef DenseSlabAllocCache ClockCache;
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
// The clock that lives in sync variables (mutexes, atomics, etc).
|
|
|
|
class SyncClock {
|
|
|
|
public:
|
|
|
|
SyncClock();
|
2014-08-06 02:45:02 +08:00
|
|
|
~SyncClock();
|
2012-05-10 21:48:04 +08:00
|
|
|
|
|
|
|
uptr size() const {
|
2014-08-06 02:45:02 +08:00
|
|
|
return size_;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 02:54:20 +08:00
|
|
|
u64 get(unsigned tid) const {
|
2014-08-06 02:45:02 +08:00
|
|
|
return elem(tid).epoch;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-09-02 17:34:34 +08:00
|
|
|
void Resize(ClockCache *c, uptr nclk);
|
2014-08-06 02:45:02 +08:00
|
|
|
void Reset(ClockCache *c);
|
2014-03-25 02:54:20 +08:00
|
|
|
|
|
|
|
void DebugDump(int(*printf)(const char *s, ...));
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
private:
|
2014-08-06 02:45:02 +08:00
|
|
|
friend struct ThreadClock;
|
|
|
|
static const uptr kDirtyTids = 2;
|
|
|
|
|
2014-03-25 02:54:20 +08:00
|
|
|
unsigned release_store_tid_;
|
2014-04-11 23:38:03 +08:00
|
|
|
unsigned release_store_reused_;
|
2014-03-25 02:54:20 +08:00
|
|
|
unsigned dirty_tids_[kDirtyTids];
|
2014-08-06 02:45:02 +08:00
|
|
|
// tab_ contains indirect pointer to a 512b block using DenseSlabAlloc.
|
|
|
|
// If size_ <= 64, then tab_ points to an array with 64 ClockElem's.
|
|
|
|
// Otherwise, tab_ points to an array with 128 u32 elements,
|
|
|
|
// each pointing to the second-level 512b block with 64 ClockElem's.
|
|
|
|
ClockBlock *tab_;
|
|
|
|
u32 tab_idx_;
|
|
|
|
u32 size_;
|
|
|
|
|
|
|
|
ClockElem &elem(unsigned tid) const;
|
2012-05-10 21:48:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The clock that lives in threads.
|
|
|
|
struct ThreadClock {
|
|
|
|
public:
|
2014-08-06 02:45:02 +08:00
|
|
|
typedef DenseSlabAllocCache Cache;
|
|
|
|
|
2014-04-11 23:38:03 +08:00
|
|
|
explicit ThreadClock(unsigned tid, unsigned reused = 0);
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2012-05-11 22:42:24 +08:00
|
|
|
u64 get(unsigned tid) const {
|
2012-05-23 02:07:45 +08:00
|
|
|
DCHECK_LT(tid, kMaxTidInClock);
|
2014-04-11 23:38:03 +08:00
|
|
|
return clk_[tid].epoch;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 02:54:20 +08:00
|
|
|
void set(unsigned tid, u64 v);
|
|
|
|
|
|
|
|
void set(u64 v) {
|
2014-04-11 23:38:03 +08:00
|
|
|
DCHECK_GE(v, clk_[tid_].epoch);
|
|
|
|
clk_[tid_].epoch = v;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 02:54:20 +08:00
|
|
|
void tick() {
|
2014-04-11 23:38:03 +08:00
|
|
|
clk_[tid_].epoch++;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uptr size() const {
|
|
|
|
return nclk_;
|
|
|
|
}
|
|
|
|
|
2014-08-06 02:45:02 +08:00
|
|
|
void acquire(ClockCache *c, const SyncClock *src);
|
|
|
|
void release(ClockCache *c, SyncClock *dst) const;
|
|
|
|
void acq_rel(ClockCache *c, SyncClock *dst);
|
|
|
|
void ReleaseStore(ClockCache *c, SyncClock *dst) const;
|
2012-05-10 21:48:04 +08:00
|
|
|
|
2014-04-11 23:38:03 +08:00
|
|
|
void DebugReset();
|
2014-03-25 02:54:20 +08:00
|
|
|
void DebugDump(int(*printf)(const char *s, ...));
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
private:
|
2014-03-25 02:54:20 +08:00
|
|
|
static const uptr kDirtyTids = SyncClock::kDirtyTids;
|
|
|
|
const unsigned tid_;
|
2014-04-11 23:38:03 +08:00
|
|
|
const unsigned reused_;
|
2014-03-25 02:54:20 +08:00
|
|
|
u64 last_acquire_;
|
2012-05-10 21:48:04 +08:00
|
|
|
uptr nclk_;
|
2014-04-11 23:38:03 +08:00
|
|
|
ClockElem clk_[kMaxTidInClock];
|
2014-03-25 02:54:20 +08:00
|
|
|
|
|
|
|
bool IsAlreadyAcquired(const SyncClock *src) const;
|
|
|
|
void UpdateCurrentThread(SyncClock *dst) const;
|
2012-05-10 21:48:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace __tsan
|
|
|
|
|
|
|
|
#endif // TSAN_CLOCK_H
|