2002-11-17 09:41:58 +00:00
|
|
|
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
|
2002-11-04 03:59:09 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "../config.h"
|
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
|
|
|
#include "timerqueuemanager.hh"
|
|
|
|
#include "display.hh"
|
|
|
|
|
|
|
|
namespace otk {
|
|
|
|
|
2003-01-11 19:17:13 +00:00
|
|
|
void TimerQueueManager::fire(bool wait)
|
2002-11-04 03:59:09 +00:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
timeval now, tm, *timeout = (timeval *) 0;
|
|
|
|
|
2003-01-11 19:17:13 +00:00
|
|
|
const int xfd = ConnectionNumber(Display::display);
|
2002-11-04 03:59:09 +00:00
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(xfd, &rfds); // break on any x events
|
|
|
|
|
2003-01-07 19:24:38 +00:00
|
|
|
if (wait) {
|
|
|
|
if (! timerList.empty()) {
|
2003-01-11 19:17:13 +00:00
|
|
|
const Timer* const timer = timerList.top();
|
2003-01-07 19:24:38 +00:00
|
|
|
|
|
|
|
gettimeofday(&now, 0);
|
|
|
|
tm = timer->remainingTime(now);
|
|
|
|
|
|
|
|
timeout = &tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
select(xfd + 1, &rfds, 0, 0, timeout);
|
2002-11-04 03:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
2003-01-11 19:17:13 +00:00
|
|
|
Timer *timer = timerList.top();
|
2002-11-04 03:59:09 +00:00
|
|
|
if (! timer->shouldFire(now))
|
|
|
|
break;
|
|
|
|
|
|
|
|
timerList.pop();
|
|
|
|
|
2002-11-04 06:06:13 +00:00
|
|
|
timer->fire();
|
|
|
|
if (timer->recurring())
|
2002-11-04 03:59:09 +00:00
|
|
|
timer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-11 19:17:13 +00:00
|
|
|
void TimerQueueManager::addTimer(Timer *timer)
|
2002-11-04 03:59:09 +00:00
|
|
|
{
|
|
|
|
assert(timer);
|
|
|
|
timerList.push(timer);
|
|
|
|
}
|
|
|
|
|
2003-01-11 19:17:13 +00:00
|
|
|
void TimerQueueManager::removeTimer(Timer* timer)
|
2002-11-04 03:59:09 +00:00
|
|
|
{
|
|
|
|
assert(timer);
|
|
|
|
timerList.release(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|