Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JEEP::JTimekeeper Class Reference

Time keeper. More...

#include <JTimekeeper.hh>

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

Public Member Functions

 JTimekeeper ()
 Default constructor.
 
 JTimekeeper (const long long int interval_us)
 Constructor.
 
long long int getInterval () const
 Get interval time.
 
long long int getDelay () const
 Get total delay time.
 
void setInterval (const long long int interval_us)
 Set interval time.
 
void reset (const long long int t0)
 Reset time.
 
void reset ()
 Reset time.
 
bool operator() () const
 Check whether the number of time intervals has elapsed since the last call to the reset method.
 
void wait () const
 Wait until the number of time intervals has elapsed since the last call to the reset method.
 
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.
 

Protected Attributes

long long int interval
 interval time [us]
 

Private Attributes

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

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

◆ JTimekeeper() [1/2]

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 }
long long int total_delay
total delay time [us]
long long int interval
interval time [us]
void reset()
Reset time.
long long int counter
interval counter

◆ JTimekeeper() [2/2]

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 }

Member Function Documentation

◆ getInterval()

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 }

◆ getDelay()

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 }

◆ setInterval()

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 }

◆ reset() [1/2]

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 t0
t0 of elapsed time [us]

◆ reset() [2/2]

void JEEP::JTimekeeper::reset ( )
inline

Reset time.

Definition at line 111 of file JTimekeeper.hh.

112 {
113 reset(getLocalTime());
114 }

◆ operator()()

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 }

◆ wait() [1/2]

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 }

◆ wait() [2/2]

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 }

Member Data Documentation

◆ interval

long long int JEEP::JTimekeeper::interval
protected

interval time [us]

Definition at line 207 of file JTimekeeper.hh.

◆ t0

long long int JEEP::JTimekeeper::t0
mutableprivate

t0 of elapsed time [us]

Definition at line 210 of file JTimekeeper.hh.

◆ total_delay

long long int JEEP::JTimekeeper::total_delay
mutableprivate

total delay time [us]

Definition at line 211 of file JTimekeeper.hh.

◆ counter

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: