|
UniSet
2.6.0
|
00001 /* 00002 * Copyright (c) 2015 Pavel Vainerman. 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as 00006 * published by the Free Software Foundation, version 2.1. 00007 * 00008 * This program is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Lesser General Lesser Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Lesser General Public License 00014 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 */ 00016 // -------------------------------------------------------------------------- 00020 //---------------------------------------------------------------------------- 00021 # ifndef PASSIVETIMER_H_ 00022 # define PASSIVETIMER_H_ 00023 //---------------------------------------------------------------------------- 00024 #include <signal.h> 00025 #include <condition_variable> 00026 #include <thread> 00027 #include <mutex> 00028 #include <atomic> 00029 #include <chrono> 00030 #include <limits> 00031 #include <Poco/Timespan.h> 00032 #include "Mutex.h" 00033 //---------------------------------------------------------------------------------------- 00034 namespace uniset 00035 { 00036 //---------------------------------------------------------------------------------------- 00037 typedef Poco::Timespan::TimeDiff timeout_t; 00038 //---------------------------------------------------------------------------------------- 00043 class UniSetTimer 00044 { 00045 public: 00046 virtual ~UniSetTimer() {}; 00047 00048 virtual bool checkTime() const noexcept = 0; 00049 virtual timeout_t setTiming( timeout_t msec ) noexcept = 0; 00050 virtual void reset() noexcept = 0; 00052 virtual timeout_t getCurrent() const noexcept = 0; 00053 virtual timeout_t getInterval() const noexcept = 0; 00055 timeout_t getLeft( timeout_t timeout ) const noexcept; 00057 // объявлены не чисто виртуальными т.к. 00058 // некоторые классы могут не иметь подобных 00059 // свойств. 00060 virtual bool wait(timeout_t timeMS); 00061 virtual void terminate() {} 00064 virtual void stop() noexcept; 00065 00069 static const timeout_t WaitUpTime = std::numeric_limits<timeout_t>::max(); 00070 00071 // преобразование в Poco::Timespan с учётом WaitUpTime 00072 static const Poco::Timespan millisecToPoco( const timeout_t msec ) noexcept; 00073 static const Poco::Timespan microsecToPoco( const timeout_t usec ) noexcept; 00074 00078 static const timeout_t MinQuantityTime = 10; 00079 }; 00080 //---------------------------------------------------------------------------------------- 00090 class PassiveTimer: 00091 public UniSetTimer 00092 { 00093 public: 00094 PassiveTimer() noexcept; 00095 PassiveTimer( timeout_t msec ) noexcept; 00096 virtual ~PassiveTimer() noexcept; 00097 00098 virtual bool checkTime() const noexcept override; 00099 virtual timeout_t setTiming( timeout_t msec ) noexcept override; 00100 virtual void reset() noexcept; 00102 virtual timeout_t getCurrent() const noexcept override; 00107 virtual timeout_t getInterval() const noexcept override; 00108 00109 virtual void terminate() noexcept; 00111 protected: 00112 timeout_t t_msec = { 0 }; 00114 // Т.к. НЕ ВЕСЬ КОД переведён на использование std::chrono 00115 // везде используется timeout_t (и WaitUpTime) 00116 // отделяем внутреннее (теперь уже стандартное >= c++11) 00117 // представление для работы со временем (std::chrono) 00118 // и тип (t_msec) для "пользователей" 00119 std::chrono::high_resolution_clock::time_point t_start; 00120 std::chrono::milliseconds t_inner_msec; 00122 private: 00123 }; 00124 00125 //---------------------------------------------------------------------------------------- 00135 class PassiveCondTimer: 00136 public PassiveTimer 00137 { 00138 public: 00139 00140 PassiveCondTimer() noexcept; 00141 virtual ~PassiveCondTimer() noexcept; 00142 00143 virtual bool wait(timeout_t t_msec) noexcept override; 00144 virtual void terminate() noexcept override; 00146 protected: 00147 00148 private: 00149 std::atomic_bool terminated; 00150 std::mutex m_working; 00151 std::condition_variable cv_working; 00152 }; 00153 // ------------------------------------------------------------------------- 00154 } // end of uniset namespace 00155 //---------------------------------------------------------------------------------------- 00156 # endif //PASSIVETIMER_H_
1.7.6.1