1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
12 timeval Timer::_nearest_timeout, Timer::_now;
13 Timer::TimerQ Timer::_q;
15 void Timer::timevalAdd(timeval &a, long msec)
17 a.tv_sec += msec / 1000;
18 a.tv_usec += (msec % 1000) * 1000;
19 a.tv_sec += a.tv_usec / 1000000;
23 bool Timer::nearestTimeout(struct timeval &tm)
27 tm.tv_sec = _nearest_timeout.tv_sec - _now.tv_sec;
28 tm.tv_usec = _nearest_timeout.tv_usec - _now.tv_usec;
30 while (tm.tv_usec < 0) {
31 tm.tv_usec += 1000000;
34 tm.tv_sec += tm.tv_usec / 1000000;
35 tm.tv_usec %= 1000000;
42 void Timer::dispatchTimers(bool wait)
49 gettimeofday(&_now, NULL);
50 _nearest_timeout = _now;
51 _nearest_timeout.tv_sec += 10000;
55 /* since we overload the destructor to keep from removing from the middle of
56 the priority queue, set _del_me, we have to do our real delete in here.
64 // the queue is sorted, so if this timer shouldn't fire, none are ready
65 _nearest_timeout = curr->_timeout;
66 if (!timercmp(&_now, &_nearest_timeout, >))
69 /* we set the last fired time to delay msec after the previous firing, then
70 re-insert. timers maintain their order and may trigger more than once if
71 they've waited more than one delay's worth of time.
74 timevalAdd(curr->_last, curr->_delay);
75 curr->_action(curr->_data);
76 timevalAdd(curr->_timeout, curr->_delay);
81 // wait for the nearest trigger, or for X to do something interesting
82 fd = ConnectionNumber(**display);
85 if (nearestTimeout(next))
86 select(fd + 1, &selset, NULL, NULL, &next);
88 select(fd + 1, &selset, NULL, NULL, NULL);
92 Timer::Timer(long delay, Timer::TimeoutHandler action, void *data)
100 timevalAdd(_timeout, delay);
104 void Timer::operator delete(void *self)
111 void Timer::realDelete(Timer *me)
116 void Timer::initialize(void)
118 gettimeofday(&_now, NULL);
119 _nearest_timeout.tv_sec = 100000;
120 _nearest_timeout.tv_usec = 0;
123 void Timer::destroy(void)
126 realDelete(_q.top());