37 #ifndef FIX8_TIMER_HPP_
38 #define FIX8_TIMER_HPP_
72 explicit TimerEvent(
bool (T::*callback)(),
bool repeat=
false) :
_callback(callback), _repeat(repeat) {}
84 bool operator<(const TimerEvent<T>& right)
const {
return _t > right._t; };
86 friend class Timer<T>;
107 explicit Timer(T& monitor,
int granularity=10) : _monitor(monitor), _thread(
std::ref(*this)), _granularity(granularity) {}
148 while(!_cancellation_token)
150 bool shouldsleep(
false);
154 if (_event_queue.size())
169 const bool result((_monitor.*rop._callback)());
170 if (result && op._repeat)
173 _event_queue.push(std::move(op));
187 glout_info <<
"Terminating Timer thread (" << elapsed <<
" elapsed, " << _event_queue.size() <<
" queued).";
198 while (_event_queue.size())
223 _event_queue.push(std::move(what));
229 #if defined __x86_64__ && (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__))
234 __asm__ __volatile__(
"rdtsc" :
"=a" (low),
"=d" (high));
235 return (static_cast<Tickval::ticks>(high) << 32) +
static_cast<Tickval::ticks>(low);
243 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
251 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
264 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
275 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
285 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
287 startTime_ = rdtsc();
301 std::ostringstream ostr;
302 ostr.setf(std::ios::showpoint);
303 ostr.setf(std::ios::fixed);
304 #if defined FIX8_USE_RDTSC && defined FIX8_HAVE_RDTSC
305 ostr << std::setprecision(9) << what;
307 ostr << std::setprecision(9) << what.
AsDouble();
309 return os << ostr.str();
315 #endif // FIX8_TIMER_HPP_
const IntervalTimer & Calculate()
virtual ~IntervalTimer()
Dtor.
static const ticks million
static Tickval get_tickval()
TimerEvent(bool(T::*callback)(), bool repeat=false)
friend std::ostream & operator<<(std::ostream &os, const IntervalTimer &what)
f8_thread_cancellation_token _cancellation_token
Thread wrapper. Ctor provides T instance and specifies ptr to member to call or defaults to operator(...
f8_thread_cancellation_token & cancellation_token()
Timer(T &monitor, int granularity=10)
Thread cancellation token.
virtual int join(int timeoutInMs=0)
IntervalTimer()
Ctor. RAII.
int hypersleep< h_milliseconds >(unsigned amt)
void set(const Tickval &t)
void stop()
Stop the timer thread.
decltype(f8_time_point::min().time_since_epoch().count()) ticks
generic spin_lock wrapper
Timer event object to provide callback context with Timer.
std::priority_queue< TimerEvent< T > > _event_queue
bool schedule(TimerEvent< T > what, unsigned timeToWaitMS)
void join()
Join timer thread. Wait till exits.
void start()
Start the timer thread.
f8_thread< Timer > _thread
High resolution interval timer.