- inline const timeval &getTimeout() const { return _timeout; }
- inline const timeval &getStartTime() const { return _start; }
-
- timeval timeRemaining(const timeval &tm) const;
- bool shouldFire(const timeval &tm) const;
- timeval endpoint() const;
-
- inline void recurring(bool b) { recur = b; }
-
- void setTimeout(long t);
- void setTimeout(const timeval &t);
-
- void start(); // manager acquires timer
- void stop(); // manager releases timer
- void halt(); // halts the timer
-
- bool operator<(const OBTimer& other) const
- { return shouldFire(other.endpoint()); }
-};
+private:
+ //! Compares two timeval structs
+ struct TimerCompare {
+ //! Compares two timeval structs
+ inline bool operator()(const Timer *a, const Timer *b) const {
+ return ((&a->_timeout)->tv_sec == (&b->_timeout)->tv_sec) ?
+ ((&a->_timeout)->tv_usec > (&b->_timeout)->tv_usec) :
+ ((&a->_timeout)->tv_sec > (&b->_timeout)->tv_sec);
+ }
+ };
+ friend struct TimerCompare; // give access to _timeout for shitty compilers
+
+ typedef
+ std::priority_queue<Timer*, std::vector<Timer*>, TimerCompare> TimerQ;
+
+ //! Milliseconds between timer firings
+ long _delay;
+ //! Callback for timer expiry
+ TimeoutHandler _action;
+ //! Data sent to callback
+ void *_data;
+ //! We overload the delete operator to just set this to true
+ bool _del_me;
+ //! The time the last fire should've been at
+ struct timeval _last;
+ //! When this timer will next trigger
+ struct timeval _timeout;
+
+ //! Queue of pending timers
+ static TimerQ _q;
+ //! Time next timer will expire
+ static timeval _nearest_timeout;
+ //! Time at start of current processing loop
+ static timeval _now;
+
+ //! Really delete something (not just flag for later)
+ /*!
+ @param self Timer to be deleted.
+ */
+ static void realDelete(Timer *self);