UniSet
1.4.0
|
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 <sys/time.h> 00029 #include <cc++/socket.h> 00030 #include "Mutex.h" 00031 //---------------------------------------------------------------------------------------- 00036 class UniSetTimer 00037 { 00038 public: 00039 virtual ~UniSetTimer(){}; 00040 00041 virtual bool checkTime()=0; 00042 virtual timeout_t setTiming( timeout_t timeMS )=0; 00043 virtual void reset()=0; 00045 virtual timeout_t getCurrent()=0; 00046 virtual timeout_t getInterval()=0; 00047 timeout_t getLeft(timeout_t timeout) /*< получить время, которое остается от timeout после прошествия времени getCurrent() */ 00048 { 00049 timeout_t ct = getCurrent(); 00050 if( timeout <= ct ) 00051 return 0; 00052 return timeout - ct; 00053 } 00054 00055 // объявлены не чисто виртуальными т.к. 00056 // некоторые классы могут не иметь подобных 00057 // свойств. 00058 virtual bool wait(timeout_t timeMS){ return 0;} 00059 virtual void terminate(){} 00060 virtual void stop(){ terminate(); }; 00065 static const timeout_t WaitUpTime = TIMEOUT_INF; 00066 00068 static const timeout_t MinQuantityTime = 30; 00069 static const timeout_t MIN_QUANTITY_TIME_MS = 30; /*< устарело, не использовать! */ 00070 }; 00071 //---------------------------------------------------------------------------------------- 00081 class PassiveTimer: 00082 public UniSetTimer 00083 { 00084 public: 00085 PassiveTimer(); 00086 PassiveTimer( timeout_t timeMS ); 00089 virtual bool checkTime(); 00090 virtual timeout_t setTiming( timeout_t timeMS ); 00091 virtual void reset(); 00093 virtual timeout_t getCurrent(); 00094 virtual timeout_t getInterval() 00095 { 00096 // TODO: нужно убрать предположение, что нулевой интервал - выключенный таймер 00097 if( timeSS == WaitUpTime ) 00098 return 0; 00099 return timeSS*10; 00100 } 00101 00102 virtual void terminate(); 00104 protected: 00105 clock_t timeAct; 00106 timeout_t timeSS; 00107 clock_t timeStart; 00108 private: 00109 clock_t clock_ticks; // Количество тиков в секунду 00110 clock_t times(); // Возвращает текущее время в тиках 00111 }; 00112 00113 //---------------------------------------------------------------------------------------- 00114 class omni_mutex; 00115 class omni_condition; 00116 00126 class ThrPassiveTimer: 00127 public PassiveTimer 00128 { 00129 public: 00130 00131 ThrPassiveTimer(); 00132 ~ThrPassiveTimer(); 00133 00134 virtual bool wait(timeout_t timeMS); 00135 virtual void terminate(); 00136 protected: 00137 bool isTerminated(); 00138 void setTerminated( bool set ); 00139 00140 private: 00141 bool terminated; 00142 omni_mutex* tmutex; 00143 omni_condition* tcondx; 00144 UniSetTypes::uniset_mutex term_mutex; 00145 }; 00146 //---------------------------------------------------------------------------------------- 00147 00154 class PassiveSysTimer: 00155 public PassiveTimer 00156 { 00157 public: 00158 00159 PassiveSysTimer(); 00160 ~PassiveSysTimer(); 00161 00162 virtual bool wait(timeout_t timeMS); //throw(UniSetTypes::NotSetSignal); 00163 virtual void terminate(); 00164 00165 protected: 00166 00167 private: 00168 struct itimerval mtimer; 00169 pid_t pid; 00170 00171 // bool terminated; 00172 volatile sig_atomic_t terminated; 00173 00174 void init(); 00175 00176 static void callalrm(int signo ); 00177 static void call(int signo, siginfo_t *evp, void *ucontext); 00178 00179 }; 00180 00181 //---------------------------------------------------------------------------------------- 00182 00183 # endif //PASSIVETIMER_H_