Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JTimekeeper.hh
Go to the documentation of this file.
1 #ifndef __JEEP__JTIMEKEEPER__
2 #define __JEEP__JTIMEKEEPER__
3 
4 #include <limits>
5 #include <sys/select.h>
6 #include <unistd.h>
7 
8 #include "JSystem/JTime.hh"
10 #include "JLang/JTimeval.hh"
11 #include "Jeep/JTimer.hh"
12 
13 
14 /**
15  * \file
16  * Scheduling of actions via fixed latency intervals.
17  * \author mdejong
18  */
19 namespace JEEP {}
20 namespace JPP { using namespace JEEP; }
21 
22 namespace JEEP {
23 
26  using JLANG::JTimeval;
27 
28 
29  /**
30  * Time keeper.
31  * This class can be used to check whether elapsed time exceeds preset time interval or
32  * to wait until preset time interval has elapsed.
33  */
34  class JTimekeeper {
35  public:
36  /**
37  * Default constructor.
38  */
40  interval (0),
41  total_delay(0),
42  counter (0)
43  {
44  reset();
45  }
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param interval_us interval time [us]
52  */
53  JTimekeeper(const long long int interval_us) :
54  interval (interval_us),
55  total_delay(0),
56  counter (0)
57  {
58  reset();
59  }
60 
61 
62  /**
63  * Get interval time.
64  *
65  * \return interval time [us]
66  */
67  long long int getInterval() const
68  {
69  return interval;
70  }
71 
72 
73  /**
74  * Get total delay time.
75  *
76  * \return delay time [us]
77  */
78  long long int getDelay() const
79  {
80  return total_delay;
81  }
82 
83 
84  /**
85  * Set interval time.
86  *
87  * \param interval_us interval time [us]
88  */
89  void setInterval(const long long int interval_us)
90  {
91  interval = interval_us;
92  }
93 
94 
95  /**
96  * Reset time.
97  *
98  * \param t0 time [us]
99  */
100  void reset(const long long int t0)
101  {
102  this->t0 = t0;
103  this->counter = 0;
104  this->total_delay = 0;
105  }
106 
107 
108  /**
109  * Reset time.
110  */
111  void reset()
112  {
113  reset(getLocalTime());
114  }
115 
116 
117  /**
118  * Check whether the number of time intervals has elapsed since
119  * the last call to the reset method.
120  * The number of intervals is equal to the number of calls this
121  * method returned true since the last call to the reset method.
122  *
123  * \return true if elapsed time exceeds interval time; else false
124  */
125  bool operator()() const
126  {
127  if (getLocalTime() >= t0 + (counter + 1) * interval) {
128 
129  ++counter;
130 
131  return true;
132 
133  } else
134 
135  return false;
136  }
137 
138 
139  /**
140  * Wait until the number of time intervals has elapsed since
141  * the last call to the reset method.
142  * The number of intervals is equal to the number of calls to
143  * the method wait since the last call to the reset method.
144  */
145  void wait() const
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  }
163 
164 
165  /**
166  * Wait until the number of time intervals has elapsed since
167  * the last call to the reset method or input data have become
168  * available on one of the file descriptors in the given mask.
169  * The number of intervals is equal to the number of calls to
170  * the method wait since the last call to the reset method.
171  * Note that the mask is overwritten following a select call.
172  *
173  * \param mask input mask
174  * \return true if interrupted; else false
175  */
176  bool wait(JFileDescriptorMask& mask) const
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  }
204 
205 
206  protected:
207  long long int interval; //!< interval time [us]
208 
209  private:
210  mutable long long int t0; //!< t0 of elapsed time [us]
211  mutable long long int total_delay; //!< total delay time [us]
212  mutable long long int counter; //!< interval counter
213  };
214 }
215 
216 #endif
System time information.
Time keeper.
Definition: JTimekeeper.hh:34
long long int total_delay
total delay time [us]
Definition: JTimekeeper.hh:211
long long int getDelay() const
Get total delay time.
Definition: JTimekeeper.hh:78
bool wait(JFileDescriptorMask &mask) const
Wait until the number of time intervals has elapsed since the last call to the reset method or input ...
Definition: JTimekeeper.hh:176
void setInterval(const long long int interval_us)
Set interval time.
Definition: JTimekeeper.hh:89
long long int t0
t0 of elapsed time [us]
Definition: JTimekeeper.hh:210
JTimekeeper()
Default constructor.
Definition: JTimekeeper.hh:39
void wait() const
Wait until the number of time intervals has elapsed since the last call to the reset method.
Definition: JTimekeeper.hh:145
JTimekeeper(const long long int interval_us)
Constructor.
Definition: JTimekeeper.hh:53
long long int interval
interval time [us]
Definition: JTimekeeper.hh:207
void reset(const long long int t0)
Reset time.
Definition: JTimekeeper.hh:100
void reset()
Reset time.
Definition: JTimekeeper.hh:111
bool operator()() const
Check whether the number of time intervals has elapsed since the last call to the reset method.
Definition: JTimekeeper.hh:125
long long int counter
interval counter
Definition: JTimekeeper.hh:212
long long int getInterval() const
Get interval time.
Definition: JTimekeeper.hh:67
Auxiliary class for method select.
bool in_avail(JTimeval timeout=JTimeval::min())
Check availability of input.
Auxiliary class for time values.
Definition: JTimeval.hh:29
General puprpose classes and methods.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static const JLocalTime getLocalTime
Function object to get local time in micro seconds.