don't check the clock so often...

This commit is contained in:
simonb 2006-04-18 15:17:11 +00:00
parent 939ffc7ef1
commit 4c011a0a12
4 changed files with 57 additions and 22 deletions

View file

@ -1,6 +1,8 @@
(Format: Year/Month/Day) (Format: Year/Month/Day)
Changes for 0.9.16: Changes for 0.9.16:
*06/04/18: *06/04/18:
* Add new timer ability - interval - to reduce clock update checks (Simon)
ClockTool.cc FbTk/Timer.hh/cc
* Fix system tray resize looping/livelock, sf.net bug #1359442 (Simon) * Fix system tray resize looping/livelock, sf.net bug #1359442 (Simon)
SystemTray.cc SystemTray.cc
* Fix iconbar updates (icon and title) * Fix iconbar updates (icon and title)

View file

@ -150,10 +150,8 @@ ClockTool::ClockTool(const FbTk::FbWindow &parent,
// setup timer to check the clock every 0.01 second // setup timer to check the clock every 0.01 second
// if nothing has changed, it wont update the graphics // if nothing has changed, it wont update the graphics
timeval delay; m_timer.setInterval(1);
delay.tv_sec = 0; // m_timer.setTimeout(delay); // don't need to set timeout on interval timer
delay.tv_usec = 100000;
m_timer.setTimeout(delay);
FbTk::RefCount<FbTk::Command> update_graphic(new FbTk::SimpleCommand<ClockTool>(*this, FbTk::RefCount<FbTk::Command> update_graphic(new FbTk::SimpleCommand<ClockTool>(*this,
&ClockTool::updateTime)); &ClockTool::updateTime));
m_timer.setCommand(update_graphic); m_timer.setCommand(update_graphic);
@ -243,7 +241,6 @@ unsigned int ClockTool::height() const {
} }
void ClockTool::updateTime() { void ClockTool::updateTime() {
// update clock // update clock
time_t the_time = time(0); time_t the_time = time(0);

View file

@ -48,14 +48,15 @@ namespace FbTk {
Timer::TimerList Timer::m_timerlist; Timer::TimerList Timer::m_timerlist;
Timer::Timer():m_timing(false), m_once(false) { Timer::Timer():m_timing(false), m_once(false), m_interval(0) {
} }
Timer::Timer(RefCount<Command> &handler): Timer::Timer(RefCount<Command> &handler):
m_handler(handler), m_handler(handler),
m_timing(false), m_timing(false),
m_once(false) { m_once(false),
m_interval(0) {
} }
@ -72,7 +73,7 @@ void Timer::setTimeout(time_t t) {
} }
void Timer::setTimeout(timeval t) { void Timer::setTimeout(const timeval &t) {
m_timeout.tv_sec = t.tv_sec; m_timeout.tv_sec = t.tv_sec;
m_timeout.tv_usec = t.tv_usec; m_timeout.tv_usec = t.tv_usec;
} }
@ -85,10 +86,10 @@ void Timer::start() {
gettimeofday(&m_start, 0); gettimeofday(&m_start, 0);
// only add Timers that actually DO something // only add Timers that actually DO something
if (! m_timing && *m_handler) { if ((! m_timing || m_interval != 0) && *m_handler) {
m_timing = true; m_timing = true;
addTimer(this); //add us to the list addTimer(this); //add us to the list
} }
} }
@ -111,17 +112,19 @@ void Timer::updateTimers(int fd) {
FD_SET(fd, &rfds); FD_SET(fd, &rfds);
if (m_timerlist.size() > 0) { if (!m_timerlist.empty()) {
gettimeofday(&now, 0); gettimeofday(&now, 0);
tm.tv_sec = tm.tv_usec = 0l; tm.tv_sec = tm.tv_usec = 0l;
Timer *timer = m_timerlist.front(); Timer *timer = m_timerlist.front();
tm.tv_sec = timer->getStartTime().tv_sec + const timeval &start = timer->getStartTime();
timer->getTimeout().tv_sec - now.tv_sec; const timeval &length = timer->getTimeout();
tm.tv_usec = timer->getStartTime().tv_usec + tm.tv_sec = start.tv_sec +
timer->getTimeout().tv_usec - now.tv_usec; length.tv_sec - now.tv_sec;
tm.tv_usec = start.tv_usec +
length.tv_usec - now.tv_usec;
while (tm.tv_usec >= 1000000) { while (tm.tv_usec >= 1000000) {
tm.tv_sec++; tm.tv_sec++;
@ -138,10 +141,17 @@ void Timer::updateTimers(int fd) {
} }
} }
if (tm.tv_sec < 0) {
tm.tv_sec = 0;
tm.tv_usec = 0;
}
timeout = &tm; timeout = &tm;
} }
select(fd + 1, &rfds, 0, 0, timeout); if (select(fd + 1, &rfds, 0, 0, timeout) != 0)
// didn't time out! x events pending
return;
TimerList::iterator it; TimerList::iterator it;
@ -167,10 +177,13 @@ void Timer::updateTimers(int fd) {
//This is to make sure we don't get an invalid iterator //This is to make sure we don't get an invalid iterator
//when we do fireTimeout //when we do fireTimeout
Timer &t = *(*it); Timer &t = *(*it);
tm.tv_sec = t.getStartTime().tv_sec + const timeval &start = t.getStartTime();
t.getTimeout().tv_sec; const timeval &length = t.getTimeout();
tm.tv_usec = t.getStartTime().tv_usec +
t.getTimeout().tv_usec; tm.tv_sec = start.tv_sec +
length.tv_sec;
tm.tv_usec = start.tv_usec +
length.tv_usec;
if (((now.tv_sec < tm.tv_sec) || if (((now.tv_sec < tm.tv_sec) ||
(now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))) (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)))
@ -179,6 +192,9 @@ void Timer::updateTimers(int fd) {
t.fireTimeout(); t.fireTimeout();
// restart the current timer so that the start time is updated // restart the current timer so that the start time is updated
if (! t.doOnce()) { if (! t.doOnce()) {
if (t.getInterval() != 0) {
it = m_timerlist.erase(it);
}
t.start(); t.start();
// Note that this mustn't be done if we're deleting the // Note that this mustn't be done if we're deleting the
@ -200,6 +216,22 @@ void Timer::updateTimers(int fd) {
void Timer::addTimer(Timer *timer) { void Timer::addTimer(Timer *timer) {
assert(timer); assert(timer);
int interval = timer->getInterval();
// interval timers have their timeout change every time they are started!
if (interval != 0) {
timeval tm;
tm.tv_sec = timer->getStartTime().tv_sec;
tm.tv_usec = timer->getStartTime().tv_usec;
// now convert to interval
tm.tv_sec = interval - (tm.tv_sec % interval) - 1;
tm.tv_usec = 1000000 - tm.tv_usec;
if (tm.tv_usec == 1000000) {
tm.tv_usec = 0;
tm.tv_sec += 1;
}
timer->setTimeout(tm);
}
TimerList::iterator it = m_timerlist.begin(); TimerList::iterator it = m_timerlist.begin();
TimerList::iterator it_end = m_timerlist.end(); TimerList::iterator it_end = m_timerlist.end();

View file

@ -64,8 +64,9 @@ public:
/// set timeout /// set timeout
void setTimeout(time_t val); void setTimeout(time_t val);
/// set timeout /// set timeout
void setTimeout(timeval val); void setTimeout(const timeval &val);
void setCommand(RefCount<Command> &cmd); void setCommand(RefCount<Command> &cmd);
void setInterval(int val) { m_interval = val; }
/// start timing /// start timing
void start(); void start();
/// stop timing /// stop timing
@ -73,7 +74,8 @@ public:
/// update all timers /// update all timers
static void updateTimers(int file_descriptor); static void updateTimers(int file_descriptor);
inline int isTiming() const { return m_timing; } inline int isTiming() const { return m_timing; }
inline int getInterval() const { return m_interval; }
inline int doOnce() const { return m_once; } inline int doOnce() const { return m_once; }
inline const timeval &getTimeout() const { return m_timeout; } inline const timeval &getTimeout() const { return m_timeout; }
@ -96,6 +98,8 @@ private:
bool m_timing; ///< clock running? bool m_timing; ///< clock running?
bool m_once; ///< do timeout only once? bool m_once; ///< do timeout only once?
int m_interval; ///< Is an interval-only timer (e.g. clock)
// note that intervals only take note of the seconds, not microseconds
timeval m_start; ///< start time timeval m_start; ///< start time
timeval m_timeout; ///< time length timeval m_timeout; ///< time length