UniSet  2.24.2
PassiveTimer.h
1 /*
2  * Copyright (c) 2015 Pavel Vainerman.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation, version 2.1.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Lesser Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 // --------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 # ifndef PASSIVETIMER_H_
22 # define PASSIVETIMER_H_
23 //----------------------------------------------------------------------------
24 #include <signal.h>
25 #include <condition_variable>
26 #include <thread>
27 #include <mutex>
28 #include <atomic>
29 #include <chrono>
30 #include <limits>
31 #include <Poco/Timespan.h>
32 #include "Mutex.h"
33 //----------------------------------------------------------------------------------------
34 namespace uniset
35 {
36  //----------------------------------------------------------------------------------------
37  typedef Poco::Timespan::TimeDiff timeout_t;
38  //----------------------------------------------------------------------------------------
44  {
45  public:
46  virtual ~UniSetTimer() {}
47 
48  virtual bool checkTime() const noexcept = 0;
49  virtual timeout_t setTiming( timeout_t msec ) noexcept = 0;
50  virtual void reset() noexcept = 0;
52  virtual timeout_t getCurrent() const noexcept = 0;
53  virtual timeout_t getInterval() const noexcept = 0;
55  timeout_t getLeft( timeout_t timeout ) const noexcept;
57  // объявлены не чисто виртуальными т.к.
58  // некоторые классы могут не иметь подобных
59  // свойств.
60  virtual bool wait(timeout_t timeMS);
61  virtual void terminate() {}
64  virtual void stop() noexcept;
65 
69  static const timeout_t WaitUpTime = std::numeric_limits<timeout_t>::max();
70 
71  // преобразование в Poco::Timespan с учётом WaitUpTime
72  static const Poco::Timespan millisecToPoco( const timeout_t msec ) noexcept;
73  static const Poco::Timespan microsecToPoco( const timeout_t usec ) noexcept;
74 
78  static const timeout_t MinQuantityTime = 10;
79  };
80  //----------------------------------------------------------------------------------------
92  class PassiveTimer:
93  public UniSetTimer
94  {
95  public:
96  PassiveTimer() noexcept;
97  PassiveTimer( timeout_t msec ) noexcept;
98  virtual ~PassiveTimer() noexcept;
99 
100  virtual bool checkTime() const noexcept override;
101  virtual timeout_t setTiming( timeout_t msec ) noexcept override;
102  virtual void reset() noexcept override;
104  virtual timeout_t getCurrent() const noexcept override;
109  virtual timeout_t getInterval() const noexcept override;
110 
111  virtual void terminate() noexcept override;
113  protected:
114  timeout_t t_msec = { 0 };
116  // Т.к. НЕ ВЕСЬ КОД переведён на использование std::chrono
117  // везде используется timeout_t (и WaitUpTime)
118  // отделяем внутреннее (теперь уже стандартное >= c++11)
119  // представление для работы со временем (std::chrono)
120  // и тип (t_msec) для "пользователей"
121  std::chrono::steady_clock::time_point t_start;
122  std::chrono::milliseconds t_inner_msec;
124  private:
125  };
126 
127  //----------------------------------------------------------------------------------------
138  public PassiveTimer
139  {
140  public:
141 
142  PassiveCondTimer() noexcept;
143  virtual ~PassiveCondTimer() noexcept;
144 
145  virtual bool wait(timeout_t t_msec) noexcept override;
146  virtual void terminate() noexcept override;
148  protected:
149 
150  private:
151  std::atomic_bool terminated;
152  std::mutex m_working;
153  std::condition_variable cv_working;
154  };
155  // -------------------------------------------------------------------------
156 } // end of uniset namespace
157 //----------------------------------------------------------------------------------------
158 # endif //PASSIVETIMER_H_
Пассивный таймер с режимом засыпания (ожидания)
Definition: PassiveTimer.h:139
Пассивный таймер
Definition: PassiveTimer.h:94
std::chrono::milliseconds t_inner_msec
Definition: PassiveTimer.h:122
std::chrono::steady_clock::time_point t_start
Definition: PassiveTimer.h:121
Базовый интерфейс пассивных таймеров
Definition: PassiveTimer.h:44
virtual timeout_t getCurrent() const noexcept=0
timeout_t getLeft(timeout_t timeout) const noexcept
Definition: PassiveTimer.cc:95
virtual timeout_t getInterval() const noexcept=0
static const timeout_t MinQuantityTime
Definition: PassiveTimer.h:78
virtual void reset() noexcept=0
virtual void stop() noexcept
Definition: PassiveTimer.cc:110
virtual void terminate()
Definition: PassiveTimer.h:61
virtual bool checkTime() const noexcept=0
virtual bool wait(timeout_t timeMS)
Definition: PassiveTimer.cc:105
static const timeout_t WaitUpTime
Definition: PassiveTimer.h:69
virtual timeout_t setTiming(timeout_t msec) noexcept=0
Definition: CommonEventLoop.h:15