1 #ifndef MSP_TIME_TIMER_H_
2 #define MSP_TIME_TIMER_H_
5 #include <sigc++/sigc++.h>
6 #include <msp/core/mutex.h>
7 #include <msp/core/noncopyable.h>
8 #include <msp/core/semaphore.h>
10 #include "timestamp.h"
16 A class for executing functions in a deferred or periodical fashion. The add a
17 timer, use one of the add functions and connect a functor to the timeout signal
20 This class is thread-safe, to allow running timers in a separate thread.
22 class Timer: private NonCopyable
28 sigc::signal<bool> signal_timeout;
35 Slot(const TimeDelta &);
36 Slot(const TimeStamp &);
37 const TimeStamp &get_timeout() const { return timeout; }
47 bool operator<(const SlotProxy &) const;
50 std::vector<SlotProxy> slots;
59 /** Adds a timer that will be executed periodically as long as the timeout
60 signal hander returns true. */
61 Slot &add(const TimeDelta &);
63 /** Adds a timer that will be executed once at a specific time. The return
64 value of the timeout signal handler is ignored. */
65 Slot &add(const TimeStamp &);
67 /** Cancels a previously added timer. */
70 /** Deprecated. Use one of the other overloads. */
71 void tick(bool block);
73 /** Waits until a timer expires, then executes it. If no timers have been
74 set, blocks until one is added from another thread. */
77 /** Waits until a timer expires but at most the specified amount of time.
78 If a timer did expire before the timeout, it is executed. */
79 void tick(const TimeDelta &);
82 void do_tick(const TimeDelta &);
85 TimeStamp get_next_timeout() const;