]>
Dogcows Code - chaz/openbox/blob - src/timer.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
7 #include "otk/display.hh"
13 BTimer::BTimer(OBTimerQueueManager
*m
, TimeoutHandler
*h
) {
17 recur
= timing
= False
;
21 BTimer::~BTimer(void) {
26 void BTimer::setTimeout(long t
) {
27 _timeout
.tv_sec
= t
/ 1000;
28 _timeout
.tv_usec
= t
% 1000;
29 _timeout
.tv_usec
*= 1000;
33 void BTimer::setTimeout(const timeval
&t
) {
34 _timeout
.tv_sec
= t
.tv_sec
;
35 _timeout
.tv_usec
= t
.tv_usec
;
39 void BTimer::start(void) {
40 gettimeofday(&_start
, 0);
44 manager
->addTimer(this);
49 void BTimer::stop(void) {
52 manager
->removeTimer(this);
56 void BTimer::halt(void) {
61 void BTimer::fireTimeout(void) {
67 timeval
BTimer::timeRemaining(const timeval
&tm
) const {
68 timeval ret
= endpoint();
70 ret
.tv_sec
-= tm
.tv_sec
;
71 ret
.tv_usec
-= tm
.tv_usec
;
73 return normalizeTimeval(ret
);
77 timeval
BTimer::endpoint(void) const {
80 ret
.tv_sec
= _start
.tv_sec
+ _timeout
.tv_sec
;
81 ret
.tv_usec
= _start
.tv_usec
+ _timeout
.tv_usec
;
83 return normalizeTimeval(ret
);
87 bool BTimer::shouldFire(const timeval
&tm
) const {
88 timeval end
= endpoint();
90 return ! ((tm
.tv_sec
< end
.tv_sec
) ||
91 (tm
.tv_sec
== end
.tv_sec
&& tm
.tv_usec
< end
.tv_usec
));
95 void OBTimerQueueManager::go()
98 timeval now
, tm
, *timeout
= (timeval
*) 0;
100 const int xfd
= ConnectionNumber(otk::OBDisplay::display
);
103 FD_SET(xfd
, &rfds
); // break on any x events
105 if (! timerList
.empty()) {
106 const BTimer
* const timer
= timerList
.top();
108 gettimeofday(&now
, 0);
109 tm
= timer
->timeRemaining(now
);
114 select(xfd
+ 1, &rfds
, 0, 0, timeout
);
116 // check for timer timeout
117 gettimeofday(&now
, 0);
119 // there is a small chance for deadlock here:
120 // *IF* the timer list keeps getting refreshed *AND* the time between
121 // timer->start() and timer->shouldFire() is within the timer's period
122 // then the timer will keep firing. This should be VERY near impossible.
123 while (! timerList
.empty()) {
124 BTimer
*timer
= timerList
.top();
125 if (! timer
->shouldFire(now
))
130 timer
->fireTimeout();
132 if (timer
->isRecurring())
138 void OBTimerQueueManager::addTimer(BTimer
*timer
)
141 timerList
.push(timer
);
144 void OBTimerQueueManager::removeTimer(BTimer
* timer
)
147 timerList
.release(timer
);
This page took 0.0428 seconds and 5 git commands to generate.