fix8  version 1.4.0
Open Source C++ FIX Framework
FIX8::Timer< T > Class Template Reference

High resolution timer. More...

#include <timer.hpp>

Public Member Functions

 Timer (T &monitor, int granularity=10)
 
virtual ~Timer ()
 Dtor. More...
 
bool schedule (TimerEvent< T > what, unsigned timeToWaitMS)
 
size_t clear ()
 
void join ()
 Join timer thread. Wait till exits. More...
 
void start ()
 Start the timer thread. More...
 
void stop ()
 Stop the timer thread. More...
 
int operator() ()
 
f8_thread_cancellation_tokencancellation_token ()
 

Private Attributes

T & _monitor
 
f8_thread< Timer_thread
 
f8_spin_lock _spin_lock
 
unsigned _granularity
 
std::priority_queue< TimerEvent< T > > _event_queue
 
f8_thread_cancellation_token _cancellation_token
 

Detailed Description

template<typename T>
class FIX8::Timer< T >

High resolution timer.

Template Parameters
Tcallback context class

Definition at line 55 of file timer.hpp.

Constructor & Destructor Documentation

template<typename T>
FIX8::Timer< T >::Timer ( T &  monitor,
int  granularity = 10 
)
inlineexplicit

Ctor.

Parameters
monitorreference to callback class
granularitytimer recheck interval in ms

Definition at line 107 of file timer.hpp.

107 : _monitor(monitor), _thread(std::ref(*this)), _granularity(granularity) {}
unsigned _granularity
Definition: timer.hpp:98
f8_thread< Timer > _thread
Definition: timer.hpp:96
T & _monitor
Definition: timer.hpp:95
template<typename T>
virtual FIX8::Timer< T >::~Timer ( )
inlinevirtual

Dtor.

Definition at line 110 of file timer.hpp.

111  {
112  stop();
113  join();
114  }
void stop()
Stop the timer thread.
Definition: timer.hpp:133
void join()
Join timer thread. Wait till exits.
Definition: timer.hpp:127

Member Function Documentation

template<typename T>
f8_thread_cancellation_token& FIX8::Timer< T >::cancellation_token ( )
inline

Definition at line 139 of file timer.hpp.

139 { return _cancellation_token;}
f8_thread_cancellation_token _cancellation_token
Definition: timer.hpp:101
template<typename T >
size_t FIX8::Timer< T >::clear ( )

Empty the scheduler of any pending timer events.

Returns
number of timer events that were waiting on the queue

Definition at line 193 of file timer.hpp.

194 {
195  size_t result(0);
197 
198  while (_event_queue.size())
199  {
200  ++result;
201  _event_queue.pop(); // remove from queue
202  }
203 
204  return result;
205 }
f8_scoped_lock_impl< f8_spin_lock > f8_scoped_spin_lock
Definition: thread.hpp:452
f8_spin_lock _spin_lock
Definition: timer.hpp:97
std::priority_queue< TimerEvent< T > > _event_queue
Definition: timer.hpp:100
template<typename T>
void FIX8::Timer< T >::join ( )
inline

Join timer thread. Wait till exits.

Definition at line 127 of file timer.hpp.

Referenced by FIX8::Timer< FIX8::Session >::~Timer().

127 { _thread.join(); }
f8_thread< Timer > _thread
Definition: timer.hpp:96
template<typename T >
int FIX8::Timer< T >::operator() ( )

Timer thread entry point.

Returns
result at timer thread exit

Definition at line 144 of file timer.hpp.

References FIX8::Tickval::get_ticks(), FIX8::Tickval::get_tickval(), glout_info, FIX8::hypersleep< h_milliseconds >(), and FIX8::Tickval::million.

145 {
146  unsigned elapsed(0);
147 
148  while(!_cancellation_token)
149  {
150  bool shouldsleep(false);
151  {
153 
154  if (_event_queue.size())
155  {
156  TimerEvent<T> op(_event_queue.top());
157  if (!op._t) // ignore empty timeval
158  {
159  _event_queue.pop(); // remove from queue
160  continue;
161  }
162 
163  const Tickval now(Tickval::get_tickval());
164  if (op._t <= now) // has elapsed
165  {
166  TimerEvent<T> rop(_event_queue.top()); // take a copy
167  _event_queue.pop(); // remove from queue
168  ++elapsed;
169  const bool result((_monitor.*rop._callback)());
170  if (result && op._repeat) // don't repeat if callback returned false
171  {
172  op._t = now.get_ticks() + op._intervalMS * Tickval::million;
173  _event_queue.push(std::move(op)); // push back on queue
174  }
175  }
176  else
177  shouldsleep = true;
178  }
179  else
180  shouldsleep = true;
181  } // we want the lock to go out of scope before we sleep
182 
183  if (shouldsleep)
185  }
186 
187  glout_info << "Terminating Timer thread (" << elapsed << " elapsed, " << _event_queue.size() << " queued).";
188  return 0;
189 }
f8_scoped_lock_impl< f8_spin_lock > f8_scoped_spin_lock
Definition: thread.hpp:452
static const ticks million
Definition: tickval.hpp:76
static Tickval get_tickval()
Definition: tickval.hpp:191
f8_spin_lock _spin_lock
Definition: timer.hpp:97
f8_thread_cancellation_token _cancellation_token
Definition: timer.hpp:101
int hypersleep< h_milliseconds >(unsigned amt)
Definition: hypersleep.hpp:105
#define glout_info
Definition: logger.hpp:601
std::priority_queue< TimerEvent< T > > _event_queue
Definition: timer.hpp:100
unsigned _granularity
Definition: timer.hpp:98
T & _monitor
Definition: timer.hpp:95
template<typename T>
bool FIX8::Timer< T >::schedule ( TimerEvent< T >  what,
unsigned  timeToWaitMS 
)

Schedule a timer event. Callback method in event called on timer expiry.

Parameters
whatTimeEvent to schedule
timeToWaitMSinterval to wait in ms
Returns
true on success

Definition at line 209 of file timer.hpp.

References FIX8::TimerEvent< T >::_intervalMS, FIX8::Tickval::get_tickval(), FIX8::Tickval::million, and FIX8::TimerEvent< T >::set().

210 {
211  Tickval tofire;
212 
213  if (timeToWait)
214  {
215  // Calculate time to fire
216  Tickval::get_tickval(tofire);
217  tofire += timeToWait * Tickval::million;
218  what._intervalMS = timeToWait;
219  }
220 
221  what.set(tofire);
223  _event_queue.push(std::move(what));
224 
225  return true;
226 }
f8_scoped_lock_impl< f8_spin_lock > f8_scoped_spin_lock
Definition: thread.hpp:452
static const ticks million
Definition: tickval.hpp:76
static Tickval get_tickval()
Definition: tickval.hpp:191
f8_spin_lock _spin_lock
Definition: timer.hpp:97
std::priority_queue< TimerEvent< T > > _event_queue
Definition: timer.hpp:100
template<typename T>
void FIX8::Timer< T >::start ( )
inline

Start the timer thread.

Definition at line 130 of file timer.hpp.

130 { _thread.start(); }
f8_thread< Timer > _thread
Definition: timer.hpp:96
template<typename T>
void FIX8::Timer< T >::stop ( )
inline

Stop the timer thread.

Definition at line 133 of file timer.hpp.

Referenced by FIX8::Timer< FIX8::Session >::~Timer().

133 { _thread.request_stop(); }
f8_thread< Timer > _thread
Definition: timer.hpp:96

Member Data Documentation

template<typename T>
f8_thread_cancellation_token FIX8::Timer< T >::_cancellation_token
private

Definition at line 101 of file timer.hpp.

Referenced by FIX8::Timer< FIX8::Session >::cancellation_token().

template<typename T>
std::priority_queue<TimerEvent<T> > FIX8::Timer< T >::_event_queue
private

Definition at line 100 of file timer.hpp.

template<typename T>
unsigned FIX8::Timer< T >::_granularity
private

Definition at line 98 of file timer.hpp.

template<typename T>
T& FIX8::Timer< T >::_monitor
private

Definition at line 95 of file timer.hpp.

template<typename T>
f8_spin_lock FIX8::Timer< T >::_spin_lock
private

Definition at line 97 of file timer.hpp.

template<typename T>
f8_thread<Timer> FIX8::Timer< T >::_thread
private

Definition at line 96 of file timer.hpp.


The documentation for this class was generated from the following file: