timer.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  *   Copyright (C) 2013-2014 by Alexander Rykovanov                        *
00003  *   rykovanov.as@gmail.com                                                   *
00004  *                                                                            *
00005  *   This library is free software; you can redistribute it and/or modify     *
00006  *   it under the terms of the GNU Lesser General Public License as           *
00007  *   published by the Free Software Foundation; version 3 of the License.     *
00008  *                                                                            *
00009  *   This library is distributed in the hope that it will be useful,          *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00012  *   GNU Lesser General Public License for more details.                      *
00013  *                                                                            *
00014  *   You should have received a copy of the GNU Lesser General Public License *
00015  *   along with this library; if not, write to the                            *
00016  *   Free Software Foundation, Inc.,                                          *
00017  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
00018  ******************************************************************************/
00019 
00020 #pragma once
00021 
00022 #include <atomic>
00023 #include <boost/asio/deadline_timer.hpp>
00024 #include <boost/chrono.hpp>
00025 #include <mutex>
00026 #include <condition_variable>
00027 
00028 namespace OpcUa
00029 {
00030   class PeriodicTimer
00031   {
00032   public:
00033     PeriodicTimer(boost::asio::io_service& io)
00034       : Timer(io)
00035       , IsCanceled(true)
00036       , Stopped(true)
00037     {
00038     }
00039 
00040     ~PeriodicTimer()
00041     {
00042       Cancel();
00043     }
00044 
00045     void Start(const boost::asio::deadline_timer::duration_type& t, std::function<void()> handler)
00046     {
00047       std::unique_lock<std::mutex> lock(Mutex);
00048       if (!Stopped)
00049         return;
00050 
00051       Stopped = false;
00052       IsCanceled = false;
00053       Timer.expires_from_now(t);
00054       Timer.async_wait([this, handler, t](const boost::system::error_code& error){
00055         OnTimer(error, handler, t);
00056       });
00057     }
00058 
00059     void Cancel()
00060     {
00061       std::unique_lock<std::mutex> lock(Mutex);
00062       if (Stopped)
00063         return;
00064 
00065       IsCanceled = true;
00066       Timer.cancel();
00067       StopEvent.wait(lock, [this](){
00068         return static_cast<bool>(Stopped);
00069       });
00070     }
00071 
00072   private:
00073     void OnTimer(const boost::system::error_code& error, std::function<void()> handler, boost::asio::deadline_timer::duration_type t)
00074     {
00075       std::unique_lock<std::mutex> lock(Mutex);
00076       if (IsCanceled || error)
00077       {
00078         Stopped = true;
00079         IsCanceled = true;
00080         StopEvent.notify_all();
00081         return;
00082       }
00083 
00084       handler();
00085 
00086       Timer.expires_from_now(t);
00087       Timer.async_wait([this, handler, t](const boost::system::error_code& error){
00088         OnTimer(error, handler, t);
00089       });
00090     }
00091 
00092   private:
00093     std::mutex Mutex;
00094     std::condition_variable StopEvent;
00095     boost::asio::deadline_timer Timer;
00096     std::atomic<bool> IsCanceled;
00097     std::atomic<bool> Stopped;
00098   };
00099 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:57