|
UniSet
2.2.1
|
00001 /* This file is part of the UniSet project 00002 * Copyright (c) 2002 Free Software Foundation, Inc. 00003 * Copyright (c) 2002 Pavel Vainerman 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 // -------------------------------------------------------------------------- 00023 //---------------------------------------------------------------------------- 00024 # ifndef PASSIVETIMER_H_ 00025 # define PASSIVETIMER_H_ 00026 //---------------------------------------------------------------------------- 00027 #include <signal.h> 00028 #include <cc++/socket.h> 00029 #include <condition_variable> 00030 #include <thread> 00031 #include <mutex> 00032 #include <atomic> 00033 #include <chrono> 00034 #include "Mutex.h" 00035 //---------------------------------------------------------------------------------------- 00040 class UniSetTimer 00041 { 00042 public: 00043 virtual ~UniSetTimer() {}; 00044 00045 virtual bool checkTime() const = 0; 00046 virtual timeout_t setTiming( timeout_t msec ) = 0; 00047 virtual void reset() = 0; 00049 virtual timeout_t getCurrent() const = 0; 00050 virtual timeout_t getInterval() const = 0; 00051 timeout_t getLeft(timeout_t timeout) 00052 { 00053 timeout_t ct = getCurrent(); 00054 00055 if( timeout <= ct ) 00056 return 0; 00057 00058 return timeout - ct; 00059 } 00060 00061 // объявлены не чисто виртуальными т.к. 00062 // некоторые классы могут не иметь подобных 00063 // свойств. 00064 virtual bool wait(timeout_t timeMS) 00065 { 00066 return 0; 00067 } 00068 virtual void terminate() {} 00071 virtual void stop() 00072 { 00073 terminate(); 00074 }; 00075 00079 static const timeout_t WaitUpTime = TIMEOUT_INF; 00080 00082 static const timeout_t MinQuantityTime = 10; 00083 }; 00084 //---------------------------------------------------------------------------------------- 00094 class PassiveTimer: 00095 public UniSetTimer 00096 { 00097 public: 00098 PassiveTimer(); 00099 PassiveTimer( timeout_t msec ); 00100 virtual ~PassiveTimer(); 00101 00102 virtual bool checkTime() const; 00103 virtual timeout_t setTiming( timeout_t msec ); 00104 virtual void reset(); 00106 virtual timeout_t getCurrent() const override; 00107 virtual timeout_t getInterval() const override 00108 { 00109 return (t_msec != UniSetTimer::WaitUpTime ? t_msec : 0); 00110 } 00111 00112 virtual void terminate(); 00114 protected: 00115 timeout_t t_msec = { 0 }; 00117 // Т.к. НЕ ВЕСЬ КОД переведён на использование std::chrono 00118 // везде используется timeout_t (и WaitUpTime) 00119 // отделяем внутреннее (теперь уже стандартное >= c++11) 00120 // представление для работы со временем (std::chrono) 00121 // и тип (t_msec) для "пользователей" 00122 std::chrono::high_resolution_clock::time_point t_start; 00123 std::chrono::milliseconds t_inner_msec; 00125 private: 00126 }; 00127 00128 //---------------------------------------------------------------------------------------- 00138 class PassiveCondTimer: 00139 public PassiveTimer 00140 { 00141 public: 00142 00143 PassiveCondTimer(); 00144 virtual ~PassiveCondTimer(); 00145 00146 virtual bool wait(timeout_t t_msec); 00147 virtual void terminate(); 00149 protected: 00150 00151 private: 00152 std::atomic_bool terminated; 00153 std::mutex m_working; 00154 std::condition_variable cv_working; 00155 }; 00156 //---------------------------------------------------------------------------------------- 00157 00164 class PassiveSigTimer: 00165 public PassiveTimer 00166 { 00167 public: 00168 00169 PassiveSigTimer(); 00170 virtual ~PassiveSigTimer(); 00171 00172 virtual bool wait(timeout_t t_msec); //throw(UniSetTypes::NotSetSignal); 00173 virtual void terminate(); 00174 00175 protected: 00176 00177 private: 00178 struct itimerval mtimer = { {0, 0}, {0, 0} }; 00179 pid_t pid = { 0 }; 00180 00181 // bool terminated; 00182 volatile sig_atomic_t terminated = { 0 }; 00183 00184 void init(); 00185 00186 static void callalrm(int signo ); 00187 static void call(int signo, siginfo_t* evp, void* ucontext); 00188 00189 }; 00190 00191 //---------------------------------------------------------------------------------------- 00192 # endif //PASSIVETIMER_H_
1.7.6.1