Add mutex when reading counters_ (Fixes #1335) (#1338)

This commit is contained in:
Liqiang TAO 2022-02-07 22:03:19 +09:00 committed by GitHub
parent 1ee7bee6c5
commit bdea5051b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -136,10 +136,14 @@ class PerfCountersMeasurement final {
// concurrently running with this. This is preferring efficiency to
// maintainability, because the address of the static can be known at compile
// time.
bool IsValid() const { return counters_.IsValid(); }
bool IsValid() const {
MutexLock l(mutex_);
return counters_.IsValid();
}
BENCHMARK_ALWAYS_INLINE void Start() {
assert(IsValid());
MutexLock l(mutex_);
// Tell the compiler to not move instructions above/below where we take
// the snapshot.
ClobberMemory();
@ -150,6 +154,7 @@ class PerfCountersMeasurement final {
BENCHMARK_ALWAYS_INLINE bool Stop(
std::vector<std::pair<std::string, double>>& measurements) {
assert(IsValid());
MutexLock l(mutex_);
// Tell the compiler to not move instructions above/below where we take
// the snapshot.
ClobberMemory();