Robot Raconteur Core C++ Library
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1
23
24#include <boost/date_time.hpp>
26
27#ifndef ROBOTRACONTEUR_EMSCRIPTEN
28#include <boost/thread.hpp>
29#ifndef BOOST_ASIO_DISABLE_DEPRECATED_MSG
30#define BOOST_ASIO_DISABLE_DEPRECATED_MSG
31#define RR_BOOST_ASIO_DISABLE_DEPRECATED_MSG
32#endif
33#include <boost/asio/deadline_timer.hpp>
34#ifdef RR_BOOST_ASIO_DISABLE_DEPRECATED_MSG
35#undef RR_BOOST_ASIO_DISABLE_DEPRECATED_MSG
36#undef BOOST_ASIO_DISABLE_DEPRECATED_MSG
37#endif
38#endif
39
40#include <boost/system/error_code.hpp>
41
42#pragma once
43
44namespace RobotRaconteur
45{
46
54struct ROBOTRACONTEUR_CORE_API TimerEvent
55{
57 bool stopped;
59 boost::posix_time::ptime last_expected;
61 boost::posix_time::ptime last_real;
63 boost::posix_time::ptime current_expected;
65 boost::posix_time::ptime current_real;
66
67 TimerEvent();
68};
69
79class ROBOTRACONTEUR_CORE_API Timer : private boost::noncopyable
80{
81 public:
88 virtual void Start() = 0;
89
94 virtual void Stop() = 0;
95
100 virtual void TryStop() = 0;
101
107 virtual boost::posix_time::time_duration GetPeriod() = 0;
108
114 virtual void SetPeriod(const boost::posix_time::time_duration& period) = 0;
115
122 virtual bool IsRunning() = 0;
123
131 virtual void Clear() = 0;
132
133 virtual ~Timer() {}
134};
135
136class ROBOTRACONTEUR_CORE_API RobotRaconteurNode;
137
145class ROBOTRACONTEUR_CORE_API Rate : private boost::noncopyable
146{
147
148 public:
153 virtual void Sleep() = 0;
154
155 virtual ~Rate(){};
156};
157
158class ROBOTRACONTEUR_CORE_API WallRate : public Rate
159{
160 protected:
161 RR_WEAK_PTR<RobotRaconteurNode> node;
162 boost::posix_time::time_duration period;
163 boost::posix_time::ptime start_time;
164 boost::posix_time::ptime last_time;
165
166#if !defined(ROBOTRACONTEUR_EMSCRIPTEN) && !defined(ROBOTRACONTEUR_WINDOWS)
167 // TODO: at the next ABI change, use boost::asio::steady_timer. deadline_timer runs on the wall clock, so it is
168 // only used with relative waits (expires_from_now)
169 boost::asio::deadline_timer timer;
170#endif
171
172#ifdef ROBOTRACONTEUR_WINDOWS
173 boost::shared_ptr<void> timer_handle;
174#endif
175#ifdef ROBOTRACONTEUR_LINUX
176 timespec ts;
177#endif
178
179 public:
180 WallRate(double frequency, const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
181
182 RR_OVIRTUAL void Sleep() RR_OVERRIDE;
183
184 RR_OVIRTUAL ~WallRate() RR_OVERRIDE {}
185};
186
187class ROBOTRACONTEUR_CORE_API WallTimer : public Timer, public RR_ENABLE_SHARED_FROM_THIS<WallTimer>
188{
189 protected:
190 boost::posix_time::time_duration period;
191 boost::posix_time::ptime start_time;
192 boost::posix_time::ptime actual_last_time;
193 boost::posix_time::ptime last_time;
194 bool oneshot;
195
196 bool running;
197 boost::mutex running_lock;
198
199 boost::function<void(const TimerEvent&)> handler;
200
201#ifndef ROBOTRACONTEUR_EMSCRIPTEN
202 // TODO: at the next ABI change, use boost::asio::steady_timer. deadline_timer runs on the wall clock while node
203 // time is steady, so it is armed with relative waits (expires_from_now) computed in node time
204 RR_SHARED_PTR<boost::asio::deadline_timer> timer;
205#else
206 boost::initialized<long> em_timer;
207 static std::map<void*, RR_SHARED_PTR<WallTimer> > em_timers;
208#endif
209
210 RR_WEAK_PTR<RobotRaconteurNode> node;
211
212#ifndef ROBOTRACONTEUR_EMSCRIPTEN
213 void timer_handler(const boost::system::error_code& ec);
214 void timer_handler2(const boost::system::error_code& ec,
215 const RR_SHARED_PTR<boost::asio::deadline_timer>& wait_timer);
216#else
217 friend void timer_handler(void* userData);
218 void timer_handler1();
219 static void node_shutdown(RobotRaconteurNode* node);
220#endif
221
222 public:
223 WallTimer(const boost::posix_time::time_duration& period, boost::function<void(const TimerEvent&)> handler,
224 bool oneshot, const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
225
226 RR_OVIRTUAL void Start() RR_OVERRIDE;
227
228 RR_OVIRTUAL void Stop() RR_OVERRIDE;
229
230 RR_OVIRTUAL void TryStop() RR_OVERRIDE;
231
232 RR_OVIRTUAL boost::posix_time::time_duration GetPeriod() RR_OVERRIDE;
233
234 RR_OVIRTUAL void SetPeriod(const boost::posix_time::time_duration& period) RR_OVERRIDE;
235
236 RR_OVIRTUAL bool IsRunning() RR_OVERRIDE;
237
238 RR_OVIRTUAL void Clear() RR_OVERRIDE;
239
240 RR_OVIRTUAL ~WallTimer() RR_OVERRIDE {}
241};
242
243#ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
245using TimerPtr = RR_SHARED_PTR<Timer>;
247using RatePtr = RR_SHARED_PTR<Rate>;
248#endif
249
258void HighResolutionSleep(const boost::posix_time::time_duration& duration);
259
260} // namespace RobotRaconteur
boost::shared_ptr< Rate > RatePtr
Convenience alias for Rate shared_ptr.
Definition Timer.h:247
boost::shared_ptr< Timer > TimerPtr
Convenience alias for Timer shared_ptr.
Definition Timer.h:245
void HighResolutionSleep(const boost::posix_time::time_duration &duration)
Sleep using high resolution timer provided by the OS.
Rate to stabilize a loop.
Definition Timer.h:146
virtual void Sleep()=0
Sleep the calling thread until the current loop period expires.
A timer to invoke a callback.
Definition Timer.h:80
virtual void Clear()=0
Clear the timer.
virtual void Start()=0
Start the timer.
virtual bool IsRunning()=0
Check if timer is running.
virtual void Stop()=0
Stop the timer.
virtual void TryStop()=0
Stop the timer without throwing an exception if the timer is not running.
virtual boost::posix_time::time_duration GetPeriod()=0
Get the period of the timer.
virtual void SetPeriod(const boost::posix_time::time_duration &period)=0
Set the period of the timer.
boost::posix_time::ptime current_expected
The current expected invocation time.
Definition Timer.h:63
boost::posix_time::ptime current_real
The current invocation time.
Definition Timer.h:65
boost::posix_time::ptime last_expected
The last expected callback invocation time.
Definition Timer.h:59
bool stopped
true if timer has been stopped
Definition Timer.h:57
boost::posix_time::ptime last_real
The real last callback invocation time.
Definition Timer.h:61