#include <timer.hh>
The Timer class can be used to have a callback fire after a given time interval. A created Timer will fire repetitively until it is destroyed.
Definition at line 32 of file timer.hh.
Public Types | |
typedef void(* | TimeoutHandler )(void *data) |
Data type of Timer callback. | |
Public Methods | |
Timer (long delay, TimeoutHandler cb, void *data) | |
Constructs a new running timer and queues it. | |
void | operator delete (void *self) |
Overloaded delete so we can leave deleted objects in queue for later reap. | |
Static Public Methods | |
void | dispatchTimers (bool wait=true) |
Dispatches all elligible timers, then optionally waits for X events. | |
bool | nearestTimeout (struct timeval &tm) |
Returns a relative timeval (to pass select) of the next timer. | |
void | initialize (void) |
Initializes internal data before use. | |
void | destroy (void) |
Deletes all waiting timers. | |
Private Types | |
typedef std::priority_queue< Timer *, std::vector< Timer * >, TimerCompare > | TimerQ |
Static Private Methods | |
void | realDelete (Timer *self) |
Really delete something (not just flag for later). | |
void | timevalAdd (timeval &a, long msec) |
Adds a millisecond delay to a timeval structure. | |
Private Attributes | |
long | _delay |
Milliseconds between timer firings. | |
TimeoutHandler | _action |
Callback for timer expiry. | |
void * | _data |
Data sent to callback. | |
bool | _del_me |
We overload the delete operator to just set this to true. | |
timeval | _last |
The time the last fire should've been at. | |
timeval | _timeout |
When this timer will next trigger. | |
Static Private Attributes | |
TimerQ | _q |
Queue of pending timers. | |
timeval | _nearest_timeout |
Time next timer will expire. | |
timeval | _now |
Time at start of current processing loop. | |
Friends | |
struct | TimerCompare |
|
Data type of Timer callback.
|
|
|
|
Constructs a new running timer and queues it.
Definition at line 103 of file timer.cc. References _q, and timevalAdd().
|
|
Deletes all waiting timers.
Definition at line 134 of file timer.cc. References _q, and realDelete(). Referenced by ob::Openbox::~Openbox().
00135 { 00136 while(!_q.empty()) { 00137 realDelete(_q.top()); 00138 _q.pop(); 00139 } 00140 } |
|
Dispatches all elligible timers, then optionally waits for X events.
Definition at line 53 of file timer.cc. References _action, _data, _del_me, _delay, _last, _nearest_timeout, _now, _q, _timeout, nearestTimeout(), realDelete(), and timevalAdd(). Referenced by ob::Openbox::eventLoop().
00054 { 00055 fd_set selset; 00056 int fd; 00057 timeval next; 00058 Timer *curr; 00059 00060 gettimeofday(&_now, NULL); 00061 _nearest_timeout = _now; 00062 _nearest_timeout.tv_sec += 10000; 00063 00064 while (!_q.empty()) { 00065 curr = _q.top(); 00066 /* since we overload the destructor to keep from removing from the middle of 00067 the priority queue, set _del_me, we have to do our real delete in here. 00068 */ 00069 if (curr->_del_me) { 00070 _q.pop(); 00071 realDelete(curr); 00072 continue; 00073 } 00074 00075 // the queue is sorted, so if this timer shouldn't fire, none are ready 00076 _nearest_timeout = curr->_timeout; 00077 if (!timercmp(&_now, &_nearest_timeout, >)) 00078 break; 00079 00080 /* we set the last fired time to delay msec after the previous firing, then 00081 re-insert. timers maintain their order and may trigger more than once if 00082 they've waited more than one delay's worth of time. 00083 */ 00084 _q.pop(); 00085 timevalAdd(curr->_last, curr->_delay); 00086 curr->_action(curr->_data); 00087 timevalAdd(curr->_timeout, curr->_delay); 00088 _q.push(curr); 00089 } 00090 00091 if (wait) { 00092 // wait for the nearest trigger, or for X to do something interesting 00093 fd = ConnectionNumber(**display); 00094 FD_ZERO(&selset); 00095 FD_SET(fd, &selset); 00096 if (nearestTimeout(next)) 00097 select(fd + 1, &selset, NULL, NULL, &next); 00098 else 00099 select(fd + 1, &selset, NULL, NULL, NULL); 00100 } 00101 } |
|
Initializes internal data before use.
Definition at line 127 of file timer.cc. References _nearest_timeout. Referenced by ob::Openbox::Openbox().
00128 { 00129 gettimeofday(&_now, NULL); 00130 _nearest_timeout.tv_sec = 100000; 00131 _nearest_timeout.tv_usec = 0; 00132 } |
|
Returns a relative timeval (to pass select) of the next timer.
Definition at line 34 of file timer.cc. References _nearest_timeout, _now, and _q. Referenced by dispatchTimers().
00035 { 00036 if (_q.empty()) 00037 return false; 00038 tm.tv_sec = _nearest_timeout.tv_sec - _now.tv_sec; 00039 tm.tv_usec = _nearest_timeout.tv_usec - _now.tv_usec; 00040 00041 while (tm.tv_usec < 0) { 00042 tm.tv_usec += 1000000; 00043 tm.tv_sec--; 00044 } 00045 tm.tv_sec += tm.tv_usec / 1000000; 00046 tm.tv_usec %= 1000000; 00047 if (tm.tv_sec < 0) 00048 tm.tv_sec = 0; 00049 00050 return true; 00051 } |
|
Overloaded delete so we can leave deleted objects in queue for later reap.
Definition at line 115 of file timer.cc. References _del_me.
|
|
Really delete something (not just flag for later).
Definition at line 122 of file timer.cc. Referenced by destroy(), and dispatchTimers().
00123 { 00124 ::delete me; 00125 } |
|
Adds a millisecond delay to a timeval structure.
Definition at line 26 of file timer.cc. Referenced by dispatchTimers(), and Timer().
00027 { 00028 a.tv_sec += msec / 1000; 00029 a.tv_usec += (msec % 1000) * 1000; 00030 a.tv_sec += a.tv_usec / 1000000; 00031 a.tv_usec %= 1000000; 00032 } |
|
|
|
Callback for timer expiry.
Definition at line 53 of file timer.hh. Referenced by dispatchTimers(). |
|
Data sent to callback.
Definition at line 55 of file timer.hh. Referenced by dispatchTimers(). |
|
We overload the delete operator to just set this to true.
Definition at line 57 of file timer.hh. Referenced by dispatchTimers(), and operator delete(). |
|
Milliseconds between timer firings.
Definition at line 51 of file timer.hh. Referenced by dispatchTimers(). |
|
The time the last fire should've been at.
Definition at line 59 of file timer.hh. Referenced by dispatchTimers(). |
|
Time next timer will expire.
Definition at line 23 of file timer.cc. Referenced by dispatchTimers(), initialize(), and nearestTimeout(). |
|
Time at start of current processing loop.
Definition at line 23 of file timer.cc. Referenced by dispatchTimers(), and nearestTimeout(). |
|
Queue of pending timers.
Definition at line 24 of file timer.cc. Referenced by destroy(), dispatchTimers(), nearestTimeout(), and Timer(). |
|
When this timer will next trigger.
Definition at line 61 of file timer.hh. Referenced by dispatchTimers(), and otk::Timer::TimerCompare::operator()(). |