Click
Loading...
Searching...
No Matches
timer.hh
Go to the documentation of this file.
1// -*- c-basic-offset: 4; related-file-name: "../../lib/timer.cc" -*-
2#ifndef CLICK_TIMER_HH
3#define CLICK_TIMER_HH
4#include <click/sync.hh>
5#include <click/glue.hh>
6#include <click/element.hh>
7#include <click/timestamp.hh>
8CLICK_DECLS
9class RouterThread;
10
11typedef void (*TimerCallback)(Timer *timer, void *user_data);
12typedef TimerCallback TimerHook CLICK_DEPRECATED;
13
14class Timer { public:
15
22 Timer();
23
24 struct do_nothing_t {
25 };
26
32 Timer(const do_nothing_t &unused);
33
38 Timer(TimerCallback f, void *user_data);
39
44
47 Timer(Task *task);
48
52 Timer(const Timer &x);
53
55 inline ~Timer() {
56 if (scheduled())
57 unschedule();
58 }
59
60
62 inline void assign() {
63 _hook.callback = do_nothing_hook;
64 _thunk = (void *) 1;
65 }
66
68 inline void assign(const do_nothing_t &unused) {
69 (void) unused;
70 assign();
71 }
72
76 inline void assign(TimerCallback f, void *user_data) {
77 _hook.callback = f;
78 _thunk = user_data;
79 }
80
85 _hook.callback = element_hook;
86 _thunk = element;
87 }
88
91 void assign(Task *task) {
92 _hook.callback = task_hook;
93 _thunk = task;
94 }
95
96
98 inline bool initialized() const {
99 return _owner != 0;
100 }
101
103 inline bool scheduled() const {
104 return _schedpos1 != 0;
105 }
106
107
116 inline const Timestamp &expiry_steady() const {
117 return _expiry_s;
118 }
119
131 inline Timestamp expiry() const {
132 if (_expiry_s)
133 return _expiry_s + Timestamp::recent() - Timestamp::recent_steady();
134 else
135 return _expiry_s;
136 }
137
139 inline Router *router() const {
140 return _owner->router();
141 }
142
144 inline Element *element() const {
145 return _owner;
146 }
147
149 inline RouterThread *thread() const {
150 return _thread;
151 }
152
154 int home_thread_id() const;
155
158 void move_thread(int tid);
159
174 void initialize(Element *owner, bool quiet = false);
175
184 void initialize(Router *router);
185
186
194 void schedule_at_steady(const Timestamp &when_steady);
195
200 void reschedule_at_steady(const Timestamp &when_steady);
201
213 inline void schedule_at(const Timestamp &when);
214
219 inline void reschedule_at(const Timestamp &when);
220
227
236 void schedule_after(const Timestamp &delta);
237
242 inline void schedule_after_sec(uint32_t delta_sec) {
243 schedule_after(Timestamp(delta_sec, 0));
244 }
245
250 inline void schedule_after_msec(uint32_t delta_msec) {
252 }
253
262 inline void reschedule_after(const Timestamp &delta) {
263 schedule_at_steady(_expiry_s + delta);
264 }
265
271 inline void reschedule_after_sec(uint32_t delta_sec) {
272 schedule_at_steady(Timestamp(_expiry_s.sec() + delta_sec, _expiry_s.subsec()));
273 }
274
280 inline void reschedule_after_msec(uint32_t delta_msec) {
281 schedule_at_steady(_expiry_s + Timestamp::make_msec(delta_msec));
282 }
283
284
288 void unschedule();
289
291 inline void clear() {
292 unschedule();
293 _expiry_s = Timestamp();
294 }
295
296
307 static inline Timestamp adjustment() {
308#if TIMESTAMP_WARPABLE
309 if (Timestamp::warp_jumping())
310 return Timestamp();
311#endif
312 return Timestamp::make_usec(500);
313 }
314
315
320 inline void schedule_after_s(uint32_t delta_sec) CLICK_DEPRECATED;
321
326 inline void schedule_after_ms(uint32_t delta_sec) CLICK_DEPRECATED;
327
332 inline void reschedule_after_s(uint32_t delta_sec) CLICK_DEPRECATED;
333
338 inline void reschedule_after_ms(uint32_t delta_sec) CLICK_DEPRECATED;
339
340 enum { behind_sec = 1 };
341
342 private:
343
344 int _schedpos1;
345 Timestamp _expiry_s;
346 union {
347 TimerCallback callback;
348 } _hook;
349 void *_thunk;
350 Element *_owner;
351 RouterThread *_thread;
352
353 Timer &operator=(const Timer &x);
354
355 static void do_nothing_hook(Timer *t, void *user_data);
356 static void element_hook(Timer *t, void *user_data);
357 static void task_hook(Timer *t, void *user_data);
358
359 friend class TimerSet;
360
361};
362
363inline void
368
369inline void
371{
372 schedule_at(when);
373}
374
375inline void
376Timer::schedule_after_s(uint32_t delta_sec)
377{
378 schedule_after_sec(delta_sec);
379}
380
381inline void
382Timer::schedule_after_ms(uint32_t delta_msec)
383{
384 schedule_after_msec(delta_msec);
385}
386
387inline void
388Timer::reschedule_after_s(uint32_t delta_sec)
389{
390 reschedule_after_sec(delta_sec);
391}
392
393inline void
394Timer::reschedule_after_ms(uint32_t delta_msec)
395{
396 reschedule_after_msec(delta_msec);
397}
398
399CLICK_ENDDECLS
400#endif
Base class for Click elements.
Definition element.hh:42
Router * router() const
Return the element's router.
Definition element.hh:515
A router configuration.
Definition router.hh:28
A set of Tasks scheduled on the same CPU.
Definition routerthread.hh:32
Represents a frequently-scheduled computational task.
Definition task.hh:49
Triggers execution at a given time.
Definition timer.hh:14
void schedule_after(const Timestamp &delta)
Schedule the timer to fire delta time in the future.
Definition timer.cc:310
~Timer()
Destroy a Timer, unscheduling it first if necessary.
Definition timer.hh:55
void reschedule_at(const Timestamp &when)
Schedule the timer to fire at when.
Definition timer.hh:370
Element * element() const
Return the Timer's owning element.
Definition timer.hh:144
void schedule_at(const Timestamp &when)
Schedule the timer to fire at when.
Definition timer.hh:364
void assign()
Change the Timer to do nothing when fired.
Definition timer.hh:62
bool scheduled() const
Return true iff the Timer is currently scheduled.
Definition timer.hh:103
int home_thread_id() const
Return the Timer's associated home thread ID.
Definition timer.cc:258
void reschedule_after_ms(uint32_t delta_sec)
Schedule the timer to fire delta_msec milliseconds after its previous expiry time (deprecated).
Definition timer.hh:394
void schedule_at_steady(const Timestamp &when_steady)
Schedule the timer to fire at when_steady.
Definition timer.cc:273
void move_thread(int tid)
Change the home thread. Will only apply at next schedule.
Definition timer.cc:267
void reschedule_at_steady(const Timestamp &when_steady)
Schedule the timer to fire at when_steady.
RouterThread * thread() const
Return the Timer's associated RouterThread.
Definition timer.hh:149
static Timestamp adjustment()
Return an adjustment interval useful for precise timers.
Definition timer.hh:307
Timer()
Construct a Timer that does nothing when fired.
Definition timer.cc:195
void assign(Element *element)
Change the Timer to call element ->run_timer(this) when fired.
Definition timer.hh:84
void schedule_after_msec(uint32_t delta_msec)
Schedule the timer to fire after delta_msec milliseconds.
Definition timer.hh:250
void clear()
Unschedule the timer and reset its expiration time.
Definition timer.hh:291
void reschedule_after_sec(uint32_t delta_sec)
Schedule the timer to fire delta_sec seconds after its previous expiry.
Definition timer.hh:271
void schedule_after_ms(uint32_t delta_sec)
Schedule the timer to fire after delta_msec milliseconds (deprecated).
Definition timer.hh:382
bool initialized() const
Return true iff the Timer has been initialized.
Definition timer.hh:98
void reschedule_after_s(uint32_t delta_sec)
Schedule the timer to fire delta_sec seconds after its previous expiry time (deprecated).
Definition timer.hh:388
void reschedule_after(const Timestamp &delta)
Schedule the timer to fire delta time after its previous expiry.
Definition timer.hh:262
void assign(TimerCallback f, void *user_data)
Change the Timer to call f(this, user_data) when fired.
Definition timer.hh:76
const Timestamp & expiry_steady() const
Return the Timer's steady-clock expiration time.
Definition timer.hh:116
void schedule_now()
Shedule the timer to fire immediately.
Definition timer.hh:224
void schedule_after_s(uint32_t delta_sec)
Schedule the timer to fire after delta_sec seconds (deprecated).
Definition timer.hh:376
void assign(Task *task)
Change the Timer to schedule task when fired.
Definition timer.hh:91
void assign(const do_nothing_t &unused)
Change the Timer to do nothing when fired.
Definition timer.hh:68
void initialize(Element *owner, bool quiet=false)
Initialize the timer.
Definition timer.cc:238
void reschedule_after_msec(uint32_t delta_msec)
Schedule the timer to fire delta_msec milliseconds after its previous expiry.
Definition timer.hh:280
Timestamp expiry() const
Return the Timer's system-clock expiration time.
Definition timer.hh:131
void schedule_after_sec(uint32_t delta_sec)
Schedule the timer to fire after delta_sec seconds.
Definition timer.hh:242
Router * router() const
Return the Timer's associated Router.
Definition timer.hh:139
void unschedule()
Unschedule the timer.
Definition timer.cc:316
Represents a moment or interval in time.
Definition timestamp.hh:125
static Timestamp recent_steady()
Return a recent steady-clock time.
Definition timestamp.hh:1000
static Timestamp make_usec(seconds_type sec, uint32_t usec)
Return a timestamp representing sec seconds plus usec microseconds.
Definition timestamp.hh:349
static Timestamp make_msec(seconds_type sec, uint32_t msec)
Return a timestamp representing sec seconds plus msec milliseconds.
Definition timestamp.hh:332
seconds_type sec() const
Return this timestamp's seconds component.
Definition timestamp.hh:1075
uint32_t subsec() const
Return this timestamp's subseconds component.
Definition timestamp.hh:1089
static Timestamp recent()
Return a recent system time.
Definition timestamp.hh:972
Click's Element class.
Classes for synchronizing among multiple CPUs, particularly in the Linux kernel.
The Timestamp class represents a moment or interval in time.