]> git.tdb.fi Git - libs/core.git/blob - source/time/timer.h
91acda9d49281373ade15beb162db9f7789c6f99
[libs/core.git] / source / time / timer.h
1 #ifndef MSP_TIME_TIMER_H_
2 #define MSP_TIME_TIMER_H_
3
4 #include <vector>
5 #include <sigc++/sigc++.h>
6 #include <msp/core/mutex.h>
7 #include <msp/core/noncopyable.h>
8 #include <msp/core/semaphore.h>
9 #include "timedelta.h"
10 #include "timestamp.h"
11
12 namespace Msp {
13 namespace Time {
14
15 /**
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
18 of the returned slot.
19
20 This class is thread-safe, to allow running timers in a separate thread.
21 */
22 class Timer: private NonCopyable
23 {
24 public:
25         class Slot
26         {
27         public:
28                 sigc::signal<bool> signal_timeout;
29         
30         private:
31                 TimeDelta interval;
32                 TimeStamp timeout;
33         
34         public:
35                 Slot(const TimeDelta &);
36                 Slot(const TimeStamp &);
37                 const TimeStamp &get_timeout() const { return timeout; }
38                 bool increment();
39         };
40
41 private:
42         struct SlotProxy
43         {
44                 Slot *slot = 0;
45
46                 SlotProxy(Slot *);
47                 bool operator<(const SlotProxy &) const;
48         };
49
50         std::vector<SlotProxy> slots;
51         Semaphore sem;
52         Mutex mutex;
53         bool blocking = false;
54
55 public:
56         Timer();
57         ~Timer();
58
59         /** Adds a timer that will be executed periodically as long as the timeout
60         signal hander returns true. */
61         Slot &add(const TimeDelta &);
62
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 &);
66
67         /** Cancels a previously added timer. */
68         void cancel(Slot &);
69
70         /** Waits until a timer expires, then executes it.  If no timers have been
71         set, blocks until one is added from another thread. */
72         void tick();
73
74         /** Waits until a timer expires but at most the specified amount of time.
75         If a timer did expire before the timeout, it is executed. */
76         void tick(const TimeDelta &);
77
78 private:
79         void do_tick(const TimeDelta &);
80
81 public:
82         TimeStamp get_next_timeout() const;
83 };
84
85 } // namespace Time
86 } // namespace Msp
87
88 #endif