mirror of
https://github.com/google/benchmark.git
synced 2025-04-30 06:50:27 +08:00
[#4] A few minor changes.
This commit is contained in:
parent
4ed4ff9574
commit
ceea4cb380
@ -443,15 +443,19 @@ class State::FastClock {
|
||||
REAL_TIME,
|
||||
CPU_TIME
|
||||
};
|
||||
explicit FastClock(Type type) : type_(type), approx_time_(NowMicros()) {
|
||||
pthread_cond_init(&bg_cond_, 0);
|
||||
pthread_mutex_init(&bg_mutex_, 0);
|
||||
explicit FastClock(Type type)
|
||||
: type_(type),
|
||||
approx_time_(NowMicros()),
|
||||
bg_done_(false) {
|
||||
pthread_cond_init(&bg_cond_, nullptr);
|
||||
pthread_mutex_init(&bg_mutex_, nullptr);
|
||||
pthread_create(&bg_, NULL, &BGThreadWrapper, this);
|
||||
}
|
||||
|
||||
~FastClock() {
|
||||
{
|
||||
mutex_lock l(&bg_mutex_);
|
||||
bg_done_ = true;
|
||||
pthread_cond_signal(&bg_cond_);
|
||||
}
|
||||
pthread_join(bg_, NULL);
|
||||
@ -493,6 +497,7 @@ class State::FastClock {
|
||||
private:
|
||||
Type type_;
|
||||
std::atomic<int64_t> approx_time_; // Last time measurement taken by bg_
|
||||
bool bg_done_; // This is used to signal background thread to exit
|
||||
pthread_t bg_; // Background thread that updates last_time_ once every ms
|
||||
|
||||
pthread_mutex_t bg_mutex_;
|
||||
@ -504,30 +509,27 @@ class State::FastClock {
|
||||
}
|
||||
|
||||
void BGThread() {
|
||||
int done = 0;
|
||||
do {
|
||||
SleepForMicroseconds(1000);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
|
||||
// Set timeout to 1 ms.
|
||||
uint32_t const timeout = 1000;
|
||||
struct timespec ts;
|
||||
ts.tv_sec = tv.tv_sec + (timeout / kNumMicrosPerSecond);
|
||||
ts.tv_nsec =
|
||||
(tv.tv_usec + (timeout % kNumMicrosPerSecond)) * kNumNanosPerMicro;
|
||||
ts.tv_sec += ts.tv_nsec / kNumNanosPerSecond;
|
||||
ts.tv_nsec %= kNumNanosPerSecond;
|
||||
|
||||
// NOTE: this should probably be platform specific.
|
||||
mutex_lock l(&bg_mutex_);
|
||||
pthread_cond_timedwait(&bg_cond_, &bg_mutex_, &ts);
|
||||
|
||||
std::atomic_store(&approx_time_, NowMicros());
|
||||
// NOTE: same code but no memory barrier. think on it.
|
||||
// base::subtle::Release_Store(&approx_time_, NowMicros());
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, 0);
|
||||
|
||||
// Set timeout to 100 ms.
|
||||
long const timeout = 100000;
|
||||
struct timespec ts;
|
||||
ts.tv_sec = tv.tv_sec + (timeout / 1000000);
|
||||
ts.tv_nsec = (tv.tv_usec + (timeout % 1000000)) * 1000;
|
||||
ts.tv_sec += ts.tv_nsec / 1000000000;
|
||||
ts.tv_nsec %= 1000000000;
|
||||
|
||||
// NOTE: this should probably be platform specific.
|
||||
mutex_lock l(&bg_mutex_);
|
||||
if (0 == pthread_cond_timedwait(&bg_cond_, &bg_mutex_, &ts))
|
||||
done = 1;
|
||||
}
|
||||
} while (done == 0);
|
||||
} while (!bg_done_);
|
||||
}
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FastClock)
|
||||
|
@ -8,6 +8,7 @@ const int64_t kNumMillisPerSecond = 1000LL;
|
||||
const int64_t kNumMicrosPerMilli = 1000LL;
|
||||
const int64_t kNumMicrosPerSecond = kNumMillisPerSecond * 1000LL;
|
||||
const int64_t kNumNanosPerMicro = 1000LL;
|
||||
const int64_t kNumNanosPerSecond = kNumNanosPerMicro * kNumMicrosPerSecond;
|
||||
|
||||
void SleepForMicroseconds(int64_t microseconds);
|
||||
void SleepForMilliseconds(int milliseconds);
|
||||
|
Loading…
Reference in New Issue
Block a user