]>
Dogcows Code - chaz/openbox/blob - otk/timer.cc
755096b84cf9c564460fc368b4a3b96604e0c7a8
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
10 #ifdef HAVE_SYS_SELECT_H
11 # include <sys/select.h>
14 # include <sys/types.h>
16 # endif // HAVE_UNISTD_H
17 #endif // HAVE_SYS_SELECT_H
21 timeval
Timer::_nearest_timeout
, Timer::_now
;
22 Timer::TimerQ
Timer::_q
;
24 void Timer::timevalAdd(timeval
&a
, long msec
)
26 a
.tv_sec
+= msec
/ 1000;
27 a
.tv_usec
+= (msec
% 1000) * 1000;
28 a
.tv_sec
+= a
.tv_usec
/ 1000000;
32 bool Timer::nearestTimeout(struct timeval
&tm
)
36 tm
.tv_sec
= _nearest_timeout
.tv_sec
- _now
.tv_sec
;
37 tm
.tv_usec
= _nearest_timeout
.tv_usec
- _now
.tv_usec
;
39 while (tm
.tv_usec
< 0) {
40 tm
.tv_usec
+= 1000000;
43 tm
.tv_sec
+= tm
.tv_usec
/ 1000000;
44 tm
.tv_usec
%= 1000000;
51 void Timer::dispatchTimers(bool wait
)
58 gettimeofday(&_now
, NULL
);
59 _nearest_timeout
= _now
;
60 _nearest_timeout
.tv_sec
+= 10000;
64 /* since we overload the destructor to keep from removing from the middle of
65 the priority queue, set _del_me, we have to do our real delete in here.
73 // the queue is sorted, so if this timer shouldn't fire, none are ready
74 _nearest_timeout
= curr
->_timeout
;
75 if (!timercmp(&_now
, &_nearest_timeout
, >))
78 /* we set the last fired time to delay msec after the previous firing, then
79 re-insert. timers maintain their order and may trigger more than once if
80 they've waited more than one delay's worth of time.
83 timevalAdd(curr
->_last
, curr
->_delay
);
84 curr
->_action(curr
->_data
);
85 timevalAdd(curr
->_timeout
, curr
->_delay
);
90 // wait for the nearest trigger, or for X to do something interesting
91 fd
= ConnectionNumber(**display
);
94 if (nearestTimeout(next
))
95 select(fd
+ 1, &selset
, NULL
, NULL
, &next
);
97 select(fd
+ 1, &selset
, NULL
, NULL
, NULL
);
101 Timer::Timer(long delay
, Timer::TimeoutHandler action
, void *data
)
109 timevalAdd(_timeout
, delay
);
113 void Timer::operator delete(void *self
)
120 void Timer::realDelete(Timer
*me
)
125 void Timer::initialize(void)
127 gettimeofday(&_now
, NULL
);
128 _nearest_timeout
.tv_sec
= 100000;
129 _nearest_timeout
.tv_usec
= 0;
132 void Timer::destroy(void)
135 realDelete(_q
.top());
This page took 0.037406 seconds and 4 git commands to generate.