]> icculus.org git repositories - mikachu/openbox.git/blob - otk/timerqueuemanager.cc
otk::Timer-ng!! thanks ManMower for this shizznit!
[mikachu/openbox.git] / otk / timerqueuemanager.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "timerqueuemanager.hh"
8 #include "display.hh"
9
10 namespace otk {
11
12 void TimerQueueManager::fire(bool wait)
13 {
14   fd_set rfds;
15   timeval now, tm, *timeout = (timeval *) 0;
16
17   const int xfd = ConnectionNumber(**display);
18   
19   FD_ZERO(&rfds);
20   FD_SET(xfd, &rfds); // break on any x events
21
22   if (wait) {
23     if (! timerList.empty()) {
24       const Timer* const timer = timerList.top();
25       
26       gettimeofday(&now, 0);
27       tm = timer->remainingTime(now);
28       
29       timeout = &tm;
30     }
31
32     select(xfd + 1, &rfds, 0, 0, timeout);
33   }
34
35   // check for timer timeout
36   gettimeofday(&now, 0);
37
38   // there is a small chance for deadlock here:
39   // *IF* the timer list keeps getting refreshed *AND* the time between
40   // timer->start() and timer->shouldFire() is within the timer's period
41   // then the timer will keep firing.  This should be VERY near impossible.
42   while (! timerList.empty()) {
43     Timer *timer = timerList.top();
44     if (! timer->shouldFire(now))
45       break;
46
47     timerList.pop();
48
49     timer->fire();
50     if (timer->recurring())
51       timer->start();
52   }
53 }
54
55
56 void TimerQueueManager::addTimer(Timer *timer)
57 {
58   assert(timer);
59   timerList.push(timer);
60 }
61
62 void TimerQueueManager::removeTimer(Timer* timer)
63 {
64   assert(timer);
65   timerList.release(timer);
66 }
67
68 }