mirror of
https://github.com/google/benchmark.git
synced 2025-03-29 21:51:06 +08:00
Added some missing atomics in FastClock
This commit is contained in:
parent
9a25f47250
commit
d4ed240426
@ -19,6 +19,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <atomic>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -474,7 +475,6 @@ class State::FastClock {
|
|||||||
enum Type { REAL_TIME, CPU_TIME };
|
enum Type { REAL_TIME, CPU_TIME };
|
||||||
explicit FastClock(Type type)
|
explicit FastClock(Type type)
|
||||||
: type_(type), approx_time_(NowMicros()) {
|
: type_(type), approx_time_(NowMicros()) {
|
||||||
|
|
||||||
sem_init(&bg_done_, 0, 0);
|
sem_init(&bg_done_, 0, 0);
|
||||||
pthread_create(&bg_, NULL, &BGThreadWrapper, this);
|
pthread_create(&bg_, NULL, &BGThreadWrapper, this);
|
||||||
}
|
}
|
||||||
@ -488,7 +488,7 @@ class State::FastClock {
|
|||||||
// Returns true if the current time is guaranteed to be past "when_micros".
|
// Returns true if the current time is guaranteed to be past "when_micros".
|
||||||
// This method is very fast.
|
// This method is very fast.
|
||||||
inline bool HasReached(int64_t when_micros) {
|
inline bool HasReached(int64_t when_micros) {
|
||||||
return approx_time_ >= when_micros;
|
return std::atomic_load(&approx_time_) >= when_micros;
|
||||||
// NOTE: this is the same as we're dealing with an int64_t
|
// NOTE: this is the same as we're dealing with an int64_t
|
||||||
//return (base::subtle::NoBarrier_Load(&approx_time_) >= when_micros);
|
//return (base::subtle::NoBarrier_Load(&approx_time_) >= when_micros);
|
||||||
}
|
}
|
||||||
@ -511,14 +511,14 @@ class State::FastClock {
|
|||||||
// function starts running - see UseRealTime).
|
// function starts running - see UseRealTime).
|
||||||
void InitType(Type type) {
|
void InitType(Type type) {
|
||||||
type_ = type;
|
type_ = type;
|
||||||
approx_time_ = NowMicros();
|
std::atomic_store(&approx_time_, NowMicros());
|
||||||
// NOTE: This is the same barring a memory barrier
|
// NOTE: This is the same barring a memory barrier
|
||||||
// base::subtle::Release_Store(&approx_time_, NowMicros());
|
// base::subtle::Release_Store(&approx_time_, NowMicros());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type type_;
|
Type type_;
|
||||||
int64_t approx_time_; // Last time measurement taken by bg_
|
std::atomic<int64_t> approx_time_; // Last time measurement taken by bg_
|
||||||
pthread_t bg_; // Background thread that updates last_time_ once every ms
|
pthread_t bg_; // Background thread that updates last_time_ once every ms
|
||||||
|
|
||||||
sem_t bg_done_;
|
sem_t bg_done_;
|
||||||
@ -532,7 +532,7 @@ class State::FastClock {
|
|||||||
int done = 0;
|
int done = 0;
|
||||||
do {
|
do {
|
||||||
SleepForMicroseconds(1000);
|
SleepForMicroseconds(1000);
|
||||||
approx_time_ = NowMicros();
|
std::atomic_store(&approx_time_, NowMicros());
|
||||||
// NOTE: same code but no memory barrier. think on it.
|
// NOTE: same code but no memory barrier. think on it.
|
||||||
//base::subtle::Release_Store(&approx_time_, NowMicros());
|
//base::subtle::Release_Store(&approx_time_, NowMicros());
|
||||||
sem_getvalue(&bg_done_, &done);
|
sem_getvalue(&bg_done_, &done);
|
||||||
@ -577,6 +577,11 @@ struct State::SharedState {
|
|||||||
|
|
||||||
SharedState(const internal::Benchmark::Instance* b, int t)
|
SharedState(const internal::Benchmark::Instance* b, int t)
|
||||||
: instance(b), starting(0), stopping(0), threads(t) {
|
: instance(b), starting(0), stopping(0), threads(t) {
|
||||||
|
pthread_mutex_init(&mu, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
~SharedState() {
|
||||||
|
pthread_mutex_destroy(&mu);
|
||||||
}
|
}
|
||||||
DISALLOW_COPY_AND_ASSIGN(SharedState);
|
DISALLOW_COPY_AND_ASSIGN(SharedState);
|
||||||
};
|
};
|
||||||
@ -587,7 +592,7 @@ Benchmark::Benchmark(const char* name, BenchmarkFunction f)
|
|||||||
: name_(name), function_(f) {
|
: name_(name), function_(f) {
|
||||||
mutex_lock l(&benchmark_mutex);
|
mutex_lock l(&benchmark_mutex);
|
||||||
if (families == nullptr)
|
if (families == nullptr)
|
||||||
families = new std::vector<Benchmark*>;
|
families = new std::vector<Benchmark*>();
|
||||||
registration_index_ = families->size();
|
registration_index_ = families->size();
|
||||||
families->push_back(this);
|
families->push_back(this);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user