1 // -*- mode: C++; indent-tabs-mode: nil; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
8 #include "timerqueuemanager.hh"
12 static timeval normalizeTimeval(const timeval &tm)
16 while (ret.tv_usec < 0) {
19 ret.tv_usec += 1000000;
25 if (ret.tv_usec >= 1000000) {
26 ret.tv_sec += ret.tv_usec / 1000000;
27 ret.tv_usec %= 1000000;
30 if (ret.tv_sec < 0) ret.tv_sec = 0;
36 OBTimer::OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d)
42 recur = timing = false;
46 OBTimer::~OBTimer(void)
52 void OBTimer::setTimeout(long t)
54 _timeout.tv_sec = t / 1000;
55 _timeout.tv_usec = t % 1000;
56 _timeout.tv_usec *= 1000;
60 void OBTimer::setTimeout(const timeval &t)
62 _timeout.tv_sec = t.tv_sec;
63 _timeout.tv_usec = t.tv_usec;
67 void OBTimer::start(void)
69 gettimeofday(&_start, 0);
73 manager->addTimer(this);
78 void OBTimer::stop(void)
82 manager->removeTimer(this);
86 void OBTimer::halt(void)
92 void OBTimer::fireTimeout(void)
99 timeval OBTimer::timeRemaining(const timeval &tm) const
101 timeval ret = endpoint();
103 ret.tv_sec -= tm.tv_sec;
104 ret.tv_usec -= tm.tv_usec;
106 return normalizeTimeval(ret);
110 timeval OBTimer::endpoint(void) const
114 ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
115 ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
117 return normalizeTimeval(ret);
121 bool OBTimer::shouldFire(const timeval &tm) const
123 timeval end = endpoint();
125 return ! ((tm.tv_sec < end.tv_sec) ||
126 (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));