summaryrefslogtreecommitdiff
path: root/otk/timerqueuemanager.cc
diff options
context:
space:
mode:
Diffstat (limited to 'otk/timerqueuemanager.cc')
-rw-r--r--otk/timerqueuemanager.cc67
1 files changed, 67 insertions, 0 deletions
diff --git a/otk/timerqueuemanager.cc b/otk/timerqueuemanager.cc
new file mode 100644
index 00000000..0d7d4983
--- /dev/null
+++ b/otk/timerqueuemanager.cc
@@ -0,0 +1,67 @@
+// -*- mode: C++; indent-tabs-mode: nil; -*-
+
+#ifdef HAVE_CONFIG_H
+# include "../config.h"
+#endif // HAVE_CONFIG_H
+
+#include "timerqueuemanager.hh"
+#include "display.hh"
+
+namespace otk {
+
+void OBTimerQueueManager::fire()
+{
+ fd_set rfds;
+ timeval now, tm, *timeout = (timeval *) 0;
+
+ const int xfd = ConnectionNumber(otk::OBDisplay::display);
+
+ FD_ZERO(&rfds);
+ FD_SET(xfd, &rfds); // break on any x events
+
+ if (! timerList.empty()) {
+ const OBTimer* const timer = timerList.top();
+
+ gettimeofday(&now, 0);
+ tm = timer->timeRemaining(now);
+
+ timeout = &tm;
+ }
+
+ select(xfd + 1, &rfds, 0, 0, timeout);
+
+ // check for timer timeout
+ gettimeofday(&now, 0);
+
+ // there is a small chance for deadlock here:
+ // *IF* the timer list keeps getting refreshed *AND* the time between
+ // timer->start() and timer->shouldFire() is within the timer's period
+ // then the timer will keep firing. This should be VERY near impossible.
+ while (! timerList.empty()) {
+ OBTimer *timer = timerList.top();
+ if (! timer->shouldFire(now))
+ break;
+
+ timerList.pop();
+
+ timer->fireTimeout();
+ timer->halt();
+ if (timer->isRecurring())
+ timer->start();
+ }
+}
+
+
+void OBTimerQueueManager::addTimer(OBTimer *timer)
+{
+ assert(timer);
+ timerList.push(timer);
+}
+
+void OBTimerQueueManager::removeTimer(OBTimer* timer)
+{
+ assert(timer);
+ timerList.release(timer);
+}
+
+}