Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Timer.hxx
Go to the documentation of this file.
1
35#ifndef _EXECUTOR_TIMER_HXX_
36#define _EXECUTOR_TIMER_HXX_
37
39#include "utils/Buffer.hxx"
40#include "utils/QMember.hxx"
41#include "os/OS.hxx"
42
43class Timer;
44class ExecutorBase;
45
49{
50public:
59
61
66 long long get_next_timeout();
67
69 bool empty();
70
76 void schedule_timer(::Timer *timer);
77
84 void update_timer(::Timer *timer);
85
91 void remove_timer(::Timer *timer);
92
95 {
96 return executor_;
97 }
98
101 void notify() override;
102
104 void run() override;
105
106private:
110 void remove_locked(::Timer *timer);
111
114 void insert_locked(::Timer *timer);
115
123 std::atomic_uint_least8_t isPending_;
124
125 friend class TimerTest;
126
128};
129
133class Timer : public Executable
134{
135public:
140 : activeTimers_(timers)
141 , priority_(UINT_MAX)
142 , when_(0)
143 , period_(0)
144 , isActive_(0)
145 , isExpired_(0)
146 , isCancelled_(0)
147 , tcRequestStop_(0)
148 {
149 }
150
152 ~Timer();
153
156 void run() override;
157
159 enum
160 {
161 NONE = 0,
163 DELETE = -1,
164 };
165
169 virtual long long timeout() = 0;
170
175 long long schedule_time()
176 {
177 return when_;
178 }
179
185 void start(long long period = -1)
186 {
189 isActive_ = 1;
190 isCancelled_ = 0;
191 when_ = OSTime::get_monotonic() + period;
192 period_ = period;
194 }
195
199 void start_absolute(long long expiry_time_nsec)
200 {
203 isActive_ = 1;
204 isCancelled_ = 0;
205 when_ = expiry_time_nsec;
208 }
209
214 void restart()
215 {
217 if (isExpired_)
218 return;
220 isCancelled_ = 0;
221 if (isActive_)
222 {
224 }
225 else
226 {
227 isActive_ = 1;
229 }
230 }
231
237 void trigger()
238 {
240 if (isExpired_)
241 return;
243 isCancelled_ = 1;
244 when_ = 2; // in the past
246 }
247
250 {
251 if (isActive_) trigger();
252 }
253
260 void cancel()
261 {
262 when_ = INT64_MAX; // This will ensure the we don't get from active to
263 // expired from now on.
265 isCancelled_ = 1;
266 if (isActive_) {
268 }
269 }
270
274 {
275 return isCancelled_;
276 }
277
281 {
282 isCancelled_ = 1;
283 }
284
285protected:
288 void update_period(long long period)
289 {
290 period_ = period;
291 }
292
293private:
294 friend class ActiveTimers; // for scheduling an expiring timers
295 friend class CountingTimer; // for testing
296
300 unsigned priority_;
302 long long when_;
304 long long period_;
306 unsigned isActive_ : 1;
309 unsigned isExpired_ : 1;
312 unsigned isCancelled_ : 1;
314 unsigned tcRequestStop_ : 1;
315
317};
318
330class SyncTimeout : public ::Timer {
331public:
333 SyncTimeout(ActiveTimers *timers) : Timer(timers)
334 {
335 }
336
342
343private:
347 long long timeout() OVERRIDE {
348 n_.notify();
349 return NONE;
350 }
351
354};
355
356#endif // _EXECUTOR_TIMER_HXX_
Class that manages the list of active timers.
Definition Timer.hxx:49
ExecutorBase * executor_
Parent.
Definition Timer.hxx:117
ActiveTimers(ExecutorBase *executor)
Constructor.
Definition Timer.hxx:54
void run() override
Callback from the executor.
Definition Timer.cxx:77
void remove_timer(::Timer *timer)
Deletes an already scheduled but not yet expired timer.
Definition Timer.cxx:181
void remove_locked(::Timer *timer)
Removes a timer from the active list.
Definition Timer.cxx:159
void insert_locked(::Timer *timer)
Inserts a timer into the active list.
Definition Timer.cxx:138
std::atomic_uint_least8_t isPending_
1 if we in the executor's queue.
Definition Timer.hxx:123
void schedule_timer(::Timer *timer)
Adds a new timer to the active timer list.
Definition Timer.cxx:132
long long get_next_timeout()
Tell when the first timer will expire.
Definition Timer.cxx:84
ExecutorBase * executor()
Definition Timer.hxx:94
bool empty()
Definition Timer.cxx:124
void update_timer(::Timer *timer)
Updates the expiration time of an already scheduled timer.
Definition Timer.cxx:173
void notify() override
Notification callback from the timer.
Definition Timer.cxx:69
OSMutex lock_
Protects the timer list.
Definition Timer.hxx:119
QMember activeTimers_
List of timers that are scheduled.
Definition Timer.hxx:121
An object that can be scheduled on an executor to run.
This class implements an execution of tasks pulled off an input queue.
Definition Executor.hxx:64
This class provides a mutex API.
Definition OS.hxx:427
static long long get_monotonic()
Get the monotonic time since the system started.
Definition OS.hxx:560
Essentially a "next" pointer container.
Definition QMember.hxx:42
A Notifiable for synchronously waiting for a notification.
void notify() override
Implementation of notification receive.
void wait_for_notification()
Blocks the current thread until the notification is delivered.
Class usable by synchronous code to utilize a timeout.
Definition Timer.hxx:330
SyncNotifiable n_
Blocks the calling thread until triggered or timeout expired.
Definition Timer.hxx:353
SyncTimeout(ActiveTimers *timers)
Definition Timer.hxx:333
void wait_for_notification()
Blocks the current thread's execution until the timeout is expired or triggered.
Definition Timer.hxx:339
long long timeout() OVERRIDE
Clients of timer should override this function.
Definition Timer.hxx:347
A timer that can schedule itself to run on an executor at specified times in the future.
Definition Timer.hxx:134
~Timer()
Destructor.
Definition Timer.cxx:39
void cancel()
Dangerous, do not call.
Definition Timer.hxx:260
bool is_triggered()
Definition Timer.hxx:273
void trigger()
This will wakeup the timer prematurely, immediately.
Definition Timer.hxx:237
Timer(ActiveTimers *timers)
Constructor.
Definition Timer.hxx:139
@ RESTART
Restart the timer with existing period.
Definition Timer.hxx:162
@ DELETE
delete the timer, use with extreme caution
Definition Timer.hxx:163
@ NONE
Do not restart the timer.
Definition Timer.hxx:161
unsigned isActive_
true when the timer is in the active timers list
Definition Timer.hxx:306
void set_triggered()
Sets the timer as if it was woken up by a trigger(), even if it was never started.
Definition Timer.hxx:280
void start(long long period=-1)
Starts a timer.
Definition Timer.hxx:185
void run() override
Callback from the executor when this timer is scheduled.
Definition Timer.cxx:45
long long schedule_time()
Definition Timer.hxx:175
ActiveTimers * activeTimers_
Points to the executor's timer structure.
Definition Timer.hxx:298
unsigned tcRequestStop_
For children: 1 if a repeated timer should stop sending wakeups.
Definition Timer.hxx:314
void start_absolute(long long expiry_time_nsec)
Starts the timer with an absolute deadline.
Definition Timer.hxx:199
void restart()
Restart a timer with the existing period but from the current time.
Definition Timer.hxx:214
unsigned isCancelled_
Was the timer cancelled or did the timer expire regularly? 1 if cancelled or triggered.
Definition Timer.hxx:312
long long period_
period in nanoseconds for timer
Definition Timer.hxx:304
long long when_
when in nanoseconds timer should expire
Definition Timer.hxx:302
unsigned isExpired_
True when the timer is in the pending executables list of the Executor.
Definition Timer.hxx:309
void update_period(long long period)
Updates the period, to be used after the next expiration of the timer in order to restart it.
Definition Timer.hxx:288
virtual long long timeout()=0
Clients of timer should override this function.
void ensure_triggered()
Triggers the timer if it is not expired yet.
Definition Timer.hxx:249
unsigned priority_
what priority to schedule this timer at
Definition Timer.hxx:300
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171