Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

otk::Timer Class Reference

#include <timer.hh>

List of all members.


Detailed Description

The Timer class implements timed callbacks.

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


Member Typedef Documentation

typedef void(* otk::Timer::TimeoutHandler)(void *data)
 

Data type of Timer callback.

Definition at line 35 of file timer.hh.

typedef std::priority_queue<Timer*, std::vector<Timer*>, TimerCompare> otk::Timer::TimerQ [private]
 

Definition at line 48 of file timer.hh.


Constructor & Destructor Documentation

otk::Timer::Timer long    delay,
TimeoutHandler    cb,
void *    data
 

Constructs a new running timer and queues it.

Parameters:
delay Time in milliseconds between firings
cb The function to be called on fire.
data Data to be passed to the callback on fire.

Definition at line 103 of file timer.cc.

References _q, and timevalAdd().

00104   : _delay(delay),
00105     _action(action),
00106     _data(data),
00107     _del_me(false),
00108     _last(_now),
00109     _timeout(_now)
00110 {
00111   timevalAdd(_timeout, delay);
00112   _q.push(this);
00113 }


Member Function Documentation

void otk::Timer::destroy void    [static]
 

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 }

void otk::Timer::dispatchTimers bool    wait = true [static]
 

Dispatches all elligible timers, then optionally waits for X events.

Parameters:
wait Whether to wait for X events after processing timers.

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 }

void otk::Timer::initialize void    [static]
 

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 }

bool otk::Timer::nearestTimeout struct timeval &    tm [static]
 

Returns a relative timeval (to pass select) of the next timer.

Parameters:
tm Changed to hold the time until next timer.
Returns:
true if there are any timers queued, and the timeout is being returned in 'tm'. false if there are no timers queued.

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 }

void otk::Timer::operator delete void *    self
 

Overloaded delete so we can leave deleted objects in queue for later reap.

Parameters:
self Pointer to current instance of Timer.

Definition at line 115 of file timer.cc.

References _del_me.

00116 {
00117   Timer *t;
00118   t = (Timer *)self;
00119   t->_del_me = true;
00120 }

void otk::Timer::realDelete Timer *    self [static, private]
 

Really delete something (not just flag for later).

Parameters:
self Timer to be deleted.

Definition at line 122 of file timer.cc.

Referenced by destroy(), and dispatchTimers().

00123 {
00124   ::delete me;
00125 }

void otk::Timer::timevalAdd timeval &    a,
long    msec
[static, private]
 

Adds a millisecond delay to a timeval structure.

Parameters:
a Amount of time to increment.
msec Number of milliseconds to increment by.

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 }


Friends And Related Function Documentation

friend struct TimerCompare [friend]
 

Definition at line 45 of file timer.hh.


Member Data Documentation

TimeoutHandler otk::Timer::_action [private]
 

Callback for timer expiry.

Definition at line 53 of file timer.hh.

Referenced by dispatchTimers().

void* otk::Timer::_data [private]
 

Data sent to callback.

Definition at line 55 of file timer.hh.

Referenced by dispatchTimers().

bool otk::Timer::_del_me [private]
 

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().

long otk::Timer::_delay [private]
 

Milliseconds between timer firings.

Definition at line 51 of file timer.hh.

Referenced by dispatchTimers().

struct timeval otk::Timer::_last [private]
 

The time the last fire should've been at.

Definition at line 59 of file timer.hh.

Referenced by dispatchTimers().

timeval otk::Timer::_nearest_timeout [static, private]
 

Time next timer will expire.

Definition at line 23 of file timer.cc.

Referenced by dispatchTimers(), initialize(), and nearestTimeout().

timeval otk::Timer::_now [static, private]
 

Time at start of current processing loop.

Definition at line 23 of file timer.cc.

Referenced by dispatchTimers(), and nearestTimeout().

Timer::TimerQ otk::Timer::_q [static, private]
 

Queue of pending timers.

Definition at line 24 of file timer.cc.

Referenced by destroy(), dispatchTimers(), nearestTimeout(), and Timer().

struct timeval otk::Timer::_timeout [private]
 

When this timer will next trigger.

Definition at line 61 of file timer.hh.

Referenced by dispatchTimers(), and otk::Timer::TimerCompare::operator()().


The documentation for this class was generated from the following files:
Generated on Tue Feb 4 23:00:29 2003 for Openbox by doxygen1.3-rc2