timer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <atomic>
23 #include <boost/asio/deadline_timer.hpp>
24 #include <boost/chrono.hpp>
25 #include <mutex>
26 #include <condition_variable>
27 
28 namespace OpcUa
29 {
31 {
32 public:
33  PeriodicTimer(boost::asio::io_service & io)
34  : Timer(io)
35  , IsCanceled(true)
36  , Stopped(true)
37  {
38  }
39 
41  {
42  Cancel();
43  }
44 
45  void Start(const boost::asio::deadline_timer::duration_type & t, std::function<void()> handler)
46  {
47  std::unique_lock<std::mutex> lock(Mutex);
48 
49  if (!Stopped)
50  { return; }
51 
52  Stopped = false;
53  IsCanceled = false;
54  Timer.expires_from_now(t);
55  Timer.async_wait([this, handler, t](const boost::system::error_code & error)
56  {
57  OnTimer(error, handler, t);
58  });
59  }
60 
61  void Cancel()
62  {
63  std::unique_lock<std::mutex> lock(Mutex);
64 
65  if (Stopped)
66  { return; }
67 
68  IsCanceled = true;
69  Timer.cancel();
70  StopEvent.wait(lock, [this]()
71  {
72  return static_cast<bool>(Stopped);
73  });
74  }
75 
76 private:
77  void OnTimer(const boost::system::error_code & error, std::function<void()> handler, boost::asio::deadline_timer::duration_type t)
78  {
79  std::unique_lock<std::mutex> lock(Mutex);
80 
81  if (IsCanceled || error)
82  {
83  Stopped = true;
84  IsCanceled = true;
85  StopEvent.notify_all();
86  return;
87  }
88 
89  handler();
90 
91  Timer.expires_from_now(t);
92  Timer.async_wait([this, handler, t](const boost::system::error_code & error)
93  {
94  OnTimer(error, handler, t);
95  });
96  }
97 
98 private:
99  std::mutex Mutex;
100  std::condition_variable StopEvent;
101  boost::asio::deadline_timer Timer;
102  std::atomic<bool> IsCanceled;
103  std::atomic<bool> Stopped;
104 };
105 }
std::mutex Mutex
Definition: timer.h:99
std::condition_variable StopEvent
Definition: timer.h:100
boost::asio::deadline_timer Timer
Definition: timer.h:101
OPC UA Address space part. GNU LGPL.
void OnTimer(const boost::system::error_code &error, std::function< void()> handler, boost::asio::deadline_timer::duration_type t)
Definition: timer.h:77
std::atomic< bool > Stopped
Definition: timer.h:103
PeriodicTimer(boost::asio::io_service &io)
Definition: timer.h:33
std::atomic< bool > IsCanceled
Definition: timer.h:102
void Start(const boost::asio::deadline_timer::duration_type &t, std::function< void()> handler)
Definition: timer.h:45


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08