timer.h
Go to the documentation of this file.
1 #ifndef H_CANOPEN_TIMER
2 #define H_CANOPEN_TIMER
3 
4 #include <functional>
5 #include <memory>
6 
7 #include <boost/asio.hpp>
8 #include <boost/thread/thread.hpp>
9 #include <boost/asio/high_resolution_timer.hpp>
10 
12 
13 namespace canopen{
14 
15 class Timer{
16 public:
17  using TimerFunc = std::function<bool(void)>;
18  using TimerDelegate [[deprecated("use TimerFunc instead")]] = can::DelegateHelper<TimerFunc>;
19 
20  Timer():work(io), timer(io),thread(std::bind(
21  static_cast<size_t(boost::asio::io_service::*)(void)>(&boost::asio::io_service::run), &io))
22  {
23  }
24 
25  void stop(){
26  boost::mutex::scoped_lock lock(mutex);
27  timer.cancel();
28  }
29  template<typename T> void start(const TimerFunc &del, const T &dur, bool start_now = true){
30  boost::mutex::scoped_lock lock(mutex);
31  delegate = del;
32  period = boost::chrono::duration_cast<boost::chrono::high_resolution_clock::duration>(dur);
33  if(start_now){
34  timer.expires_from_now(period);
35  timer.async_wait(std::bind(&Timer::handler, this, std::placeholders::_1));
36  }
37  }
38  void restart(){
39  boost::mutex::scoped_lock lock(mutex);
40  timer.expires_from_now(period);
41  timer.async_wait(std::bind(&Timer::handler, this, std::placeholders::_1));
42  }
43  const boost::chrono::high_resolution_clock::duration & getPeriod(){
44  boost::mutex::scoped_lock lock(mutex);
45  return period;
46  }
47  ~Timer(){
48  io.stop();
49  thread.join();
50  }
51 
52 private:
53  boost::asio::io_service io;
54  boost::asio::io_service::work work;
55  boost::asio::basic_waitable_timer<boost::chrono::high_resolution_clock> timer;
56  boost::chrono::high_resolution_clock::duration period;
57  boost::mutex mutex;
58  boost::thread thread;
59 
61  void handler(const boost::system::error_code& ec){
62  if(!ec){
63  boost::mutex::scoped_lock lock(mutex);
64  if(delegate && delegate()){
65  timer.expires_at(timer.expires_at() + period);
66  timer.async_wait(std::bind(&Timer::handler, this, std::placeholders::_1));
67  }
68 
69  }
70  }
71 };
72 
73 }
74 
75 #endif
canopen::Timer::handler
void handler(const boost::system::error_code &ec)
Definition: timer.h:61
canopen::Timer::TimerFunc
std::function< bool(void)> TimerFunc
Definition: timer.h:17
run
void run(class_loader::ClassLoader *loader)
canopen::Timer::~Timer
~Timer()
Definition: timer.h:47
canopen::Timer::getPeriod
const boost::chrono::high_resolution_clock::duration & getPeriod()
Definition: timer.h:43
canopen::Timer::timer
boost::asio::basic_waitable_timer< boost::chrono::high_resolution_clock > timer
Definition: timer.h:55
delegates.h
canopen::Timer::restart
void restart()
Definition: timer.h:38
canopen::Timer::start
void start(const TimerFunc &del, const T &dur, bool start_now=true)
Definition: timer.h:29
can::DelegateHelper
canopen::Timer::period
boost::chrono::high_resolution_clock::duration period
Definition: timer.h:56
canopen::Timer::work
boost::asio::io_service::work work
Definition: timer.h:54
canopen::Timer::io
boost::asio::io_service io
Definition: timer.h:53
canopen::Timer
Definition: timer.h:15
canopen::Timer::thread
boost::thread thread
Definition: timer.h:58
canopen
Definition: bcm_sync.h:8
canopen::Timer::mutex
boost::mutex mutex
Definition: timer.h:57
canopen::Timer::delegate
TimerFunc delegate
Definition: timer.h:60
canopen::Timer::Timer
Timer()
Definition: timer.h:20
canopen::Timer::stop
void stop()
Definition: timer.h:25


canopen_master
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:26