Jpp  16.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Protected Attributes | Private Attributes | List of all members
JEEP::JTimekeeper Class Reference

Time keeper. More...

#include <JTimekeeper.hh>

Inheritance diagram for JEEP::JTimekeeper:
JLOGGER::JMessageScheduler

Public Member Functions

 JTimekeeper ()
 Default constructor. More...
 
 JTimekeeper (const long long int interval_us)
 Constructor. More...
 
long long int getInterval () const
 Get interval time. More...
 
long long int getDelay () const
 Get total delay time. More...
 
void setInterval (const long long int interval_us)
 Set interval time. More...
 
void reset (const long long int t0)
 Reset time. More...
 
void reset ()
 Reset time. More...
 
bool operator() () const
 Check whether the number of time intervals has elapsed since the last call to the reset method. More...
 
void wait () const
 Wait until the number of time intervals has elapsed since the last call to the reset method. More...
 
bool wait (JFileDescriptorMask &mask) const
 Wait until the number of time intervals has elapsed since the last call to the reset method or input data have become available on one of the file descriptors in the given mask. More...
 

Protected Attributes

long long int interval
 interval time [us] More...
 

Private Attributes

long long int t0
 t0 of elapsed time [us] More...
 
long long int total_delay
 total delay time [us] More...
 
long long int counter
 interval counter More...
 

Detailed Description

Time keeper.

This class can be used to check whether elapsed time exceeds preset time interval or to wait until preset time interval has elapsed.

Definition at line 34 of file JTimekeeper.hh.

Constructor & Destructor Documentation

JEEP::JTimekeeper::JTimekeeper ( )
inline

Default constructor.

Definition at line 39 of file JTimekeeper.hh.

39  :
40  interval (0),
41  total_delay(0),
42  counter (0)
43  {
44  reset();
45  }
void reset()
Reset time.
Definition: JTimekeeper.hh:111
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int counter
interval counter
Definition: JTimekeeper.hh:212
JEEP::JTimekeeper::JTimekeeper ( const long long int  interval_us)
inline

Constructor.

Parameters
interval_usinterval time [us]

Definition at line 53 of file JTimekeeper.hh.

53  :
54  interval (interval_us),
55  total_delay(0),
56  counter (0)
57  {
58  reset();
59  }
void reset()
Reset time.
Definition: JTimekeeper.hh:111
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int counter
interval counter
Definition: JTimekeeper.hh:212

Member Function Documentation

long long int JEEP::JTimekeeper::getInterval ( ) const
inline

Get interval time.

Returns
interval time [us]

Definition at line 67 of file JTimekeeper.hh.

68  {
69  return interval;
70  }
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int JEEP::JTimekeeper::getDelay ( ) const
inline

Get total delay time.

Returns
delay time [us]

Definition at line 78 of file JTimekeeper.hh.

79  {
80  return total_delay;
81  }
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
void JEEP::JTimekeeper::setInterval ( const long long int  interval_us)
inline

Set interval time.

Parameters
interval_usinterval time [us]

Definition at line 89 of file JTimekeeper.hh.

90  {
91  interval = interval_us;
92  }
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
void JEEP::JTimekeeper::reset ( const long long int  t0)
inline

Reset time.

Parameters
t0time [us]

Definition at line 100 of file JTimekeeper.hh.

101  {
102  this->t0 = t0;
103  this->counter = 0;
104  this->total_delay = 0;
105  }
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int t0
t0 of elapsed time [us]
Definition: JTimekeeper.hh:210
long long int counter
interval counter
Definition: JTimekeeper.hh:212
void JEEP::JTimekeeper::reset ( )
inline

Reset time.

Definition at line 111 of file JTimekeeper.hh.

112  {
113  reset(getLocalTime());
114  }
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
void reset()
Reset time.
Definition: JTimekeeper.hh:111
bool JEEP::JTimekeeper::operator() ( ) const
inline

Check whether the number of time intervals has elapsed since the last call to the reset method.

The number of intervals is equal to the number of calls this method returned true since the last call to the reset method.

Returns
true if elapsed time exceeds interval time; else false

Definition at line 125 of file JTimekeeper.hh.

126  {
127  if (getLocalTime() >= t0 + (counter + 1) * interval) {
128 
129  ++counter;
130 
131  return true;
132 
133  } else
134 
135  return false;
136  }
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int t0
t0 of elapsed time [us]
Definition: JTimekeeper.hh:210
long long int counter
interval counter
Definition: JTimekeeper.hh:212
void JEEP::JTimekeeper::wait ( ) const
inline

Wait until the number of time intervals has elapsed since the last call to the reset method.

The number of intervals is equal to the number of calls to the method wait since the last call to the reset method.

Definition at line 145 of file JTimekeeper.hh.

146  {
147  static const useconds_t MAXIMAL_SLEEP_TIME_US = std::numeric_limits<useconds_t>::max();
148 
149  long long int delay = t0 + (counter + 1) * interval - getLocalTime(); // [us]
150 
151  while (delay > MAXIMAL_SLEEP_TIME_US && usleep(MAXIMAL_SLEEP_TIME_US) == 0) {
152 
153  total_delay += MAXIMAL_SLEEP_TIME_US;
154  delay -= MAXIMAL_SLEEP_TIME_US;
155  }
156 
157  if (delay > 0 && usleep((useconds_t) delay) == 0) {
158  total_delay += delay;
159  }
160 
161  ++counter;
162  }
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int t0
t0 of elapsed time [us]
Definition: JTimekeeper.hh:210
long long int counter
interval counter
Definition: JTimekeeper.hh:212
bool JEEP::JTimekeeper::wait ( JFileDescriptorMask mask) const
inline

Wait until the number of time intervals has elapsed since the last call to the reset method or input data have become available on one of the file descriptors in the given mask.

The number of intervals is equal to the number of calls to the method wait since the last call to the reset method. Note that the mask is overwritten following a select call.

Parameters
maskinput mask
Returns
true if interrupted; else false

Definition at line 176 of file JTimekeeper.hh.

177  {
178  long long int t1 = getLocalTime();
179  long long int delay = t0 + (counter + 1) * interval - t1; // [us]
180 
181  // ensure one select call for each call to this method.
182 
183  if (delay < 0) {
184  delay = 0;
185  }
186 
187  JTimeval timeout((int) (delay/1000000), (int) (delay%1000000));
188 
189  if (mask.in_avail(timeout)) {
190 
191  total_delay += getLocalTime() - t1;
192 
193  return true;
194 
195  } else {
196 
197  ++counter;
198 
199  total_delay += delay;
200 
201  return false;
202  }
203  }
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int t0
t0 of elapsed time [us]
Definition: JTimekeeper.hh:210
long long int counter
interval counter
Definition: JTimekeeper.hh:212

Member Data Documentation

long long int JEEP::JTimekeeper::interval
protected

interval time [us]

Definition at line 207 of file JTimekeeper.hh.

long long int JEEP::JTimekeeper::t0
mutableprivate

t0 of elapsed time [us]

Definition at line 210 of file JTimekeeper.hh.

long long int JEEP::JTimekeeper::total_delay
mutableprivate

total delay time [us]

Definition at line 211 of file JTimekeeper.hh.

long long int JEEP::JTimekeeper::counter
mutableprivate

interval counter

Definition at line 212 of file JTimekeeper.hh.


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