diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
commit | 541c8c407b7ba8dd10f85bb48bcb5900270b3f84 (patch) | |
tree | 71a6abc0f2a43bcfd33f80b3b30b878f234cbf05 /src/FbTk/Timer.hh | |
parent | 60a53113e05db443af4d520883ec3145680642a8 (diff) | |
download | fluxbox-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.zip fluxbox-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.tar.bz2 |
changed timing functions to use a monotonic increasing clock
gettimeofday() is subject to be changed on daylight-saving or to ntp-related
(think leap-seconds). even worse, it is subject to be changed BACK in time. this
is hard to fix correctly (see commit 45726d3016e and bug #3560509). it is
irrelevant for timers to know the nano-seconds since the epoch anyways.
Diffstat (limited to 'src/FbTk/Timer.hh')
-rw-r--r-- | src/FbTk/Timer.hh | 61 |
1 files changed, 18 insertions, 43 deletions
diff --git a/src/FbTk/Timer.hh b/src/FbTk/Timer.hh index 376eac0..241e327 100644 --- a/src/FbTk/Timer.hh +++ b/src/FbTk/Timer.hh | |||
@@ -27,26 +27,13 @@ | |||
27 | 27 | ||
28 | #include "RefCount.hh" | 28 | #include "RefCount.hh" |
29 | #include "Command.hh" | 29 | #include "Command.hh" |
30 | 30 | #include "FbTime.hh" | |
31 | #ifdef HAVE_CTIME | ||
32 | #include <ctime> | ||
33 | #else | ||
34 | #include <time.h> | ||
35 | #endif | ||
36 | #include <list> | ||
37 | #include <string> | ||
38 | 31 | ||
39 | #ifdef HAVE_CONFIG_H | 32 | #ifdef HAVE_CONFIG_H |
40 | #include "config.h" | 33 | #include "config.h" |
41 | #endif //HAVE_CONFIG_H | 34 | #endif //HAVE_CONFIG_H |
42 | 35 | ||
43 | #ifdef HAVE_INTTYPES_H | 36 | #include <string> |
44 | #include <inttypes.h> | ||
45 | #endif // HAVE_INTTYPES_H | ||
46 | |||
47 | #include <sys/types.h> | ||
48 | #include <sys/time.h> | ||
49 | #include <unistd.h> | ||
50 | 37 | ||
51 | namespace FbTk { | 38 | namespace FbTk { |
52 | 39 | ||
@@ -57,24 +44,21 @@ class Timer { | |||
57 | public: | 44 | public: |
58 | Timer(); | 45 | Timer(); |
59 | explicit Timer(const RefCount<Slot<void> > &handler); | 46 | explicit Timer(const RefCount<Slot<void> > &handler); |
60 | virtual ~Timer(); | 47 | ~Timer(); |
61 | 48 | ||
62 | void fireOnce(bool once) { m_once = once; } | 49 | void fireOnce(bool once) { m_once = once; } |
63 | /// set timeout | 50 | void setTimeout(uint64_t timeout); |
64 | void setTimeout(time_t val); | ||
65 | /// set timeout | ||
66 | void setTimeout(const timeval &val); | ||
67 | void setTimeout(unsigned int secs, unsigned int usecs); | ||
68 | void setCommand(const RefCount<Slot<void> > &cmd); | 51 | void setCommand(const RefCount<Slot<void> > &cmd); |
52 | |||
69 | template<typename Functor> | 53 | template<typename Functor> |
70 | void setFunctor(const Functor &functor) | 54 | void setFunctor(const Functor &functor) { |
71 | { setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); } | 55 | setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); |
72 | void setInterval(int val) { m_interval = val; } | 56 | } |
73 | /// start timing | 57 | |
58 | void setInterval(int seconds) { m_interval = seconds; } | ||
74 | void start(); | 59 | void start(); |
75 | /// stop timing | ||
76 | void stop(); | 60 | void stop(); |
77 | /// update all timers | 61 | |
78 | static void updateTimers(int file_descriptor); | 62 | static void updateTimers(int file_descriptor); |
79 | 63 | ||
80 | int isTiming() const { return m_timing; } | 64 | int isTiming() const { return m_timing; } |
@@ -82,38 +66,29 @@ public: | |||
82 | 66 | ||
83 | int doOnce() const { return m_once; } | 67 | int doOnce() const { return m_once; } |
84 | 68 | ||
85 | const timeval &getTimeout() const { return m_timeout; } | 69 | uint64_t getTimeout() const { return m_timeout; } |
86 | const timeval &getStartTime() const { return m_start; } | 70 | uint64_t getStartTime() const { return m_start; } |
87 | void makeEndTime(timeval &tm) const; | 71 | uint64_t getEndTime() const; |
88 | 72 | ||
89 | protected: | 73 | protected: |
90 | /// force a timeout | 74 | /// force a timeout |
91 | void fireTimeout(); | 75 | void fireTimeout(); |
92 | 76 | ||
93 | private: | 77 | private: |
94 | /// add a timer to the static list | ||
95 | static void addTimer(Timer *timer); | ||
96 | /// remove a timer from the static list | ||
97 | static void removeTimer(Timer *timer); | ||
98 | |||
99 | typedef std::list<Timer *> TimerList; | ||
100 | static TimerList m_timerlist; ///< list of all timers, sorted by next trigger time (start + timeout) | ||
101 | |||
102 | RefCount<Slot<void> > m_handler; ///< what to do on a timeout | 78 | RefCount<Slot<void> > m_handler; ///< what to do on a timeout |
103 | 79 | ||
104 | bool m_timing; ///< clock running? | 80 | bool m_timing; ///< clock running? |
105 | bool m_once; ///< do timeout only once? | 81 | bool m_once; ///< do timeout only once? |
106 | int m_interval; ///< Is an interval-only timer (e.g. clock) | 82 | int m_interval; ///< Is an interval-only timer (e.g. clock), in seconds |
107 | // note that intervals only take note of the seconds, not microseconds | ||
108 | 83 | ||
109 | timeval m_start; ///< start time | 84 | uint64_t m_start; ///< start time in microseconds |
110 | timeval m_timeout; ///< time length | 85 | uint64_t m_timeout; ///< time length in microseconds |
111 | }; | 86 | }; |
112 | 87 | ||
113 | /// executes a command after a specified timeout | 88 | /// executes a command after a specified timeout |
114 | class DelayedCmd: public Command<void> { | 89 | class DelayedCmd: public Command<void> { |
115 | public: | 90 | public: |
116 | DelayedCmd(const RefCount<Slot<void> > &cmd, unsigned int timeout = 200000); | 91 | DelayedCmd(const RefCount<Slot<void> > &cmd, unsigned int timeout = 200); |
117 | 92 | ||
118 | // this constructor has inverted order of parameters to avoid ambiguity with the previous | 93 | // this constructor has inverted order of parameters to avoid ambiguity with the previous |
119 | // constructor | 94 | // constructor |