24#include <boost/date_time.hpp>
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
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
40#include <boost/system/error_code.hpp>
44namespace RobotRaconteur
54struct ROBOTRACONTEUR_CORE_API TimerEvent
79class ROBOTRACONTEUR_CORE_API
Timer :
private boost::noncopyable
107 virtual boost::posix_time::time_duration
GetPeriod() = 0;
114 virtual void SetPeriod(
const boost::posix_time::time_duration& period) = 0;
133class ROBOTRACONTEUR_CORE_API RobotRaconteurNode;
142class ROBOTRACONTEUR_CORE_API
Rate :
private boost::noncopyable
155class ROBOTRACONTEUR_CORE_API WallRate :
public Rate
158 RR_WEAK_PTR<RobotRaconteurNode> node;
159 boost::posix_time::time_duration period;
160 boost::posix_time::ptime start_time;
161 boost::posix_time::ptime last_time;
163#if !defined(ROBOTRACONTEUR_EMSCRIPTEN) && !defined(ROBOTRACONTEUR_WINDOWS)
164 boost::asio::deadline_timer timer;
167#ifdef ROBOTRACONTEUR_WINDOWS
168 boost::shared_ptr<void> timer_handle;
170#ifdef ROBOTRACONTEUR_LINUX
175 WallRate(
double frequency,
const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
177 RR_OVIRTUAL
void Sleep() RR_OVERRIDE;
179 RR_OVIRTUAL ~WallRate() RR_OVERRIDE {}
182class ROBOTRACONTEUR_CORE_API WallTimer :
public Timer,
public RR_ENABLE_SHARED_FROM_THIS<WallTimer>
185 boost::posix_time::time_duration period;
186 boost::posix_time::ptime start_time;
187 boost::posix_time::ptime actual_last_time;
188 boost::posix_time::ptime last_time;
192 boost::mutex running_lock;
194 boost::function<void(
const TimerEvent&)> handler;
196#ifndef ROBOTRACONTEUR_EMSCRIPTEN
197 RR_SHARED_PTR<boost::asio::deadline_timer> timer;
199 boost::initialized<long> em_timer;
200 static std::map<void*, RR_SHARED_PTR<WallTimer> > em_timers;
203 RR_WEAK_PTR<RobotRaconteurNode> node;
205#ifndef ROBOTRACONTEUR_EMSCRIPTEN
206 void timer_handler(
const boost::system::error_code& ec);
208 friend void timer_handler(
void* userData);
209 void timer_handler1();
210 static void node_shutdown(RobotRaconteurNode* node);
214 WallTimer(
const boost::posix_time::time_duration& period, boost::function<
void(
const TimerEvent&)> handler,
215 bool oneshot,
const RR_SHARED_PTR<RobotRaconteurNode>& node = RR_SHARED_PTR<RobotRaconteurNode>());
217 RR_OVIRTUAL
void Start() RR_OVERRIDE;
219 RR_OVIRTUAL
void Stop() RR_OVERRIDE;
221 RR_OVIRTUAL
void TryStop() RR_OVERRIDE;
223 RR_OVIRTUAL boost::posix_time::time_duration GetPeriod() RR_OVERRIDE;
225 RR_OVIRTUAL
void SetPeriod(const boost::posix_time::time_duration& period) RR_OVERRIDE;
227 RR_OVIRTUAL
bool IsRunning() RR_OVERRIDE;
229 RR_OVIRTUAL
void Clear() RR_OVERRIDE;
231 RR_OVIRTUAL ~WallTimer() RR_OVERRIDE {}
234#ifndef ROBOTRACONTEUR_NO_CXX11_TEMPLATE_ALIASES
boost::shared_ptr< Rate > RatePtr
Convenience alias for Rate shared_ptr.
Definition Timer.h:238
boost::shared_ptr< Timer > TimerPtr
Convenience alias for Timer shared_ptr.
Definition Timer.h:236
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:143
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