1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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)
83 _manager->removeTimer(this);
88 void OBTimer::fire(void)
95 timeval OBTimer::remainingTime(const timeval &tm) const
97 timeval ret = endTime();
99 ret.tv_sec -= tm.tv_sec;
100 ret.tv_usec -= tm.tv_usec;
102 return normalizeTimeval(ret);
106 timeval OBTimer::endTime(void) const
110 ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
111 ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
113 return normalizeTimeval(ret);
117 bool OBTimer::shouldFire(const timeval &tm) const
119 timeval end = endTime();
121 return ! ((tm.tv_sec < end.tv_sec) ||
122 (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));