Commit ceea4cb3 by Eugene Zhuk

[#4] A few minor changes.

parent 4ed4ff95
...@@ -443,15 +443,19 @@ class State::FastClock { ...@@ -443,15 +443,19 @@ class State::FastClock {
REAL_TIME, REAL_TIME,
CPU_TIME CPU_TIME
}; };
explicit FastClock(Type type) : type_(type), approx_time_(NowMicros()) { explicit FastClock(Type type)
pthread_cond_init(&bg_cond_, 0); : type_(type),
pthread_mutex_init(&bg_mutex_, 0); approx_time_(NowMicros()),
bg_done_(false) {
pthread_cond_init(&bg_cond_, nullptr);
pthread_mutex_init(&bg_mutex_, nullptr);
pthread_create(&bg_, NULL, &BGThreadWrapper, this); pthread_create(&bg_, NULL, &BGThreadWrapper, this);
} }
~FastClock() { ~FastClock() {
{ {
mutex_lock l(&bg_mutex_); mutex_lock l(&bg_mutex_);
bg_done_ = true;
pthread_cond_signal(&bg_cond_); pthread_cond_signal(&bg_cond_);
} }
pthread_join(bg_, NULL); pthread_join(bg_, NULL);
...@@ -493,6 +497,7 @@ class State::FastClock { ...@@ -493,6 +497,7 @@ class State::FastClock {
private: private:
Type type_; Type type_;
std::atomic<int64_t> approx_time_; // Last time measurement taken by bg_ 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_t bg_; // Background thread that updates last_time_ once every ms
pthread_mutex_t bg_mutex_; pthread_mutex_t bg_mutex_;
...@@ -504,30 +509,27 @@ class State::FastClock { ...@@ -504,30 +509,27 @@ class State::FastClock {
} }
void BGThread() { void BGThread() {
int done = 0;
do { 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()); 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());
{ } while (!bg_done_);
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);
} }
DISALLOW_COPY_AND_ASSIGN(FastClock) DISALLOW_COPY_AND_ASSIGN(FastClock)
......
...@@ -8,6 +8,7 @@ const int64_t kNumMillisPerSecond = 1000LL; ...@@ -8,6 +8,7 @@ const int64_t kNumMillisPerSecond = 1000LL;
const int64_t kNumMicrosPerMilli = 1000LL; const int64_t kNumMicrosPerMilli = 1000LL;
const int64_t kNumMicrosPerSecond = kNumMillisPerSecond * 1000LL; const int64_t kNumMicrosPerSecond = kNumMillisPerSecond * 1000LL;
const int64_t kNumNanosPerMicro = 1000LL; const int64_t kNumNanosPerMicro = 1000LL;
const int64_t kNumNanosPerSecond = kNumNanosPerMicro * kNumMicrosPerSecond;
void SleepForMicroseconds(int64_t microseconds); void SleepForMicroseconds(int64_t microseconds);
void SleepForMilliseconds(int milliseconds); void SleepForMilliseconds(int milliseconds);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment