UCommon
timers.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
28#ifndef _UCOMMON_TIMERS_H_
29#define _UCOMMON_TIMERS_H_
30
31#ifndef _UCOMMON_LINKED_H_
32#include <ucommon/linked.h>
33#endif
34
35#ifndef _MSWINDOWS_
36#include <unistd.h>
37#include <sys/time.h>
38#endif
39
40#include <time.h>
41
42namespace ucommon {
43
50class __EXPORT Timer
51{
52private:
53 friend class Conditional;
54 friend class Semaphore;
55 friend class Event;
56
57#if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS)
58 timespec timer;
59#else
60#undef POSIX_TIMERS // make sure not used if no support
61 timeval timer;
62#endif
63 bool updated;
64
65protected:
71 bool update(void);
72
77 bool is_active(void) const;
78
79public:
80 static const timeout_t inf = ((timeout_t)(-1));
81 static const time_t reset = ((time_t)(0));
82
83#ifdef _MSWINDOWS_
84 typedef unsigned __int64 tick_t;
85#else
86 typedef uint64_t tick_t;
87#endif
88
93
98 Timer(timeout_t offset);
99
104 Timer(time_t offset);
105
110 Timer(const Timer& copy);
111
116 void set(timeout_t expire);
117
122 void set(time_t expire);
123
127 void set(void);
128
132 void clear(void);
133
138 timeout_t get(void) const;
139
144 inline timeout_t operator*() const {
145 return get();
146 }
147
152 bool operator!() const;
153
158 operator bool() const;
159
164 Timer& operator=(time_t expire);
165
170 Timer& operator=(timeout_t expire);
171
176 Timer& operator+=(time_t expire);
177
182 Timer& operator+=(timeout_t expire);
183
188 Timer& operator-=(time_t expire);
189
194 Timer& operator-=(timeout_t expire);
195
201 timeout_t operator-(const Timer& timer);
202
208 bool operator==(const Timer& timer) const;
209
215 bool operator!=(const Timer& timer) const;
216
222 bool operator<(const Timer& timer) const;
223
229 bool operator<=(const Timer& timer) const;
230
236 bool operator>(const Timer& timer) const;
237
243 bool operator>=(const Timer& timer) const;
244
249 static void sync(Timer &timer);
250
255 static tick_t ticks(void);
256};
257
268class __EXPORT TimerQueue : public OrderedIndex
269{
270private:
271 __DELETE_COPY(TimerQueue);
272
273public:
282 class __EXPORT event : protected Timer, public DLinkedObject
283 {
284 private:
285 __DELETE_DEFAULTS(event);
286
287 protected:
288 friend class TimerQueue;
289
294 event(timeout_t expire);
295
301 event(TimerQueue *queue, timeout_t expire);
302
306 virtual void expired(void) = 0;
307
313 virtual timeout_t timeout(void);
314
315 public:
319 virtual ~event();
320
326 void attach(TimerQueue *queue);
327
331 void detach(void);
332
337 void arm(timeout_t timeout);
338
342 void disarm(void);
343
348 inline timeout_t get(void) const {
349 return Timer::get();
350 }
351
352 inline timeout_t operator*() const {
353 return Timer::get();
354 }
355
359 void update(void);
360
365 inline TimerQueue *list(void) const {
366 return static_cast<TimerQueue*>(Root);
367 }
368 };
369
370protected:
371 friend class event;
372
377 virtual void modify(void) = 0;
378
384 virtual void update(void) = 0;
385
386public:
391
395 virtual ~TimerQueue();
396
401 void operator+=(event &timer);
402
407 void operator-=(event &timer);
408
416 timeout_t expire();
417};
418
423
428
429} // namespace ucommon
430
431#endif
Linked objects, lists, templates, and containers.
Common namespace for all ucommon objects.
Definition access.h:47
TimerQueue::event TQEvent
A convenience type for timer queue timer events.
Definition timers.h:422
Timer timer_t
A convenience type for timers.
Definition timers.h:427
The conditional is a common base for other thread synchronizing classes.
Definition condition.h:228
A portable counting semaphore class.
Definition condition.h:656
An index container for maintaining an ordered list of objects.
Definition linked.h:177
A double linked list object.
Definition linked.h:769
Timer class to use when scheduling realtime events.
Definition timers.h:51
bool operator<(const Timer &timer) const
Compare timers if earlier timeout than another timer.
void set(timeout_t expire)
Set the timer to expire.
void set(void)
Set (update) the timer with current time.
bool operator<=(const Timer &timer) const
Compare timers if earlier than or equal to another timer.
Timer(const Timer &copy)
Construct a timer from a copy of another timer.
bool is_active(void) const
Check if timer active.
Timer(timeout_t offset)
Construct a triggered timer that expires at specified offset.
bool operator>(const Timer &timer) const
Compare timers if later timeout than another timer.
timeout_t operator-(const Timer &timer)
Compute difference between two timers.
timeout_t get(void) const
Get remaining time until the timer expires.
Timer()
Construct an untriggered timer set to the time of creation.
static tick_t ticks(void)
Get timer ticks since uuid epoch.
Timer & operator+=(time_t expire)
Adjust timer expiration.
bool operator!=(const Timer &timer) const
Compare timers if not same timeout.
void set(time_t expire)
Set the timer to expire.
bool operator==(const Timer &timer) const
Compare timers if same timeout.
timeout_t operator*() const
Get remaining time until timer expires by reference.
Definition timers.h:144
bool update(void)
Check if timer has been updated since last check.
Timer & operator=(timeout_t expire)
Set timer expiration.
bool operator!() const
Check if timer has expired.
Timer & operator-=(time_t expire)
Adjust timer expiration.
bool operator>=(const Timer &timer) const
Compare timers if later than or equal to another timer.
Timer(time_t offset)
Construct a triggered timer that expires at specified offset.
Timer & operator=(time_t expire)
Set timer expiration.
Timer & operator-=(timeout_t expire)
Adjust timer expiration.
Timer & operator+=(timeout_t expire)
Adjust timer expiration.
void clear(void)
Clear pending timer, has no value.
static void sync(Timer &timer)
Sleep current thread until the specified timer expires.
A timer queue for timer events.
Definition timers.h:269
virtual void modify(void)=0
Called in derived class when the queue is being modified.
TimerQueue()
Create an empty timer queue.
timeout_t expire()
Process timer queue and find when next event triggers.
void operator+=(event &timer)
Add a timer event to the timer queue.
virtual void update(void)=0
Called in derived class after the queue has been modified.
void operator-=(event &timer)
Remove a timer event from the timer queue.
virtual ~TimerQueue()
Destroy queue, does not remove event objects.
A timer event object that lives on a timer queue.
Definition timers.h:283
virtual ~event()
Detaches from queue when destroyed.
void arm(timeout_t timeout)
Arm event to trigger at specified timeout.
virtual void expired(void)=0
Event method to call in derived class when timer expires.
void detach(void)
Detach event from a timer queue.
void update(void)
Notify timer queue that the timer has been updated.
void disarm(void)
Disarm event.
event(timeout_t expire)
Construct a timer event object and initially arm.
void attach(TimerQueue *queue)
Attach event to a timer queue.
timeout_t get(void) const
Time remaining until expired.
Definition timers.h:348
TimerQueue * list(void) const
Get the timer queue we are attached to.
Definition timers.h:365
event(TimerQueue *queue, timeout_t expire)
Construct an armed timer event object and attach to queue.
virtual timeout_t timeout(void)
Expected next timeout for the timer.