UniSet  2.6.0
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  //----------------------------------------------------------------------------------------
90  class PassiveTimer:
91  public UniSetTimer
92  {
93  public:
94  PassiveTimer() noexcept;
95  PassiveTimer( timeout_t msec ) noexcept;
96  virtual ~PassiveTimer() noexcept;
97 
98  virtual bool checkTime() const noexcept override;
99  virtual timeout_t setTiming( timeout_t msec ) noexcept override;
100  virtual void reset() noexcept;
102  virtual timeout_t getCurrent() const noexcept override;
107  virtual timeout_t getInterval() const noexcept override;
108 
109  virtual void terminate() noexcept;
111  protected:
112  timeout_t t_msec = { 0 };
114  // Т.к. НЕ ВЕСЬ КОД переведён на использование std::chrono
115  // везде используется timeout_t (и WaitUpTime)
116  // отделяем внутреннее (теперь уже стандартное >= c++11)
117  // представление для работы со временем (std::chrono)
118  // и тип (t_msec) для "пользователей"
119  std::chrono::high_resolution_clock::time_point t_start;
120  std::chrono::milliseconds t_inner_msec;
122  private:
123  };
124 
125  //----------------------------------------------------------------------------------------
136  public PassiveTimer
137  {
138  public:
139 
140  PassiveCondTimer() noexcept;
141  virtual ~PassiveCondTimer() noexcept;
142 
143  virtual bool wait(timeout_t t_msec) noexcept override;
144  virtual void terminate() noexcept override;
146  protected:
147 
148  private:
149  std::atomic_bool terminated;
150  std::mutex m_working;
151  std::condition_variable cv_working;
152  };
153  // -------------------------------------------------------------------------
154 } // end of uniset namespace
155 //----------------------------------------------------------------------------------------
156 # endif //PASSIVETIMER_H_
Пассивный таймер
Definition: PassiveTimer.h:90
static const timeout_t MinQuantityTime
Definition: PassiveTimer.h:78
std::chrono::milliseconds t_inner_msec
Definition: PassiveTimer.h:120
Definition: CallbackTimer.h:29
Пассивный таймер с режимом засыпания (ожидания)
Definition: PassiveTimer.h:135
virtual timeout_t getInterval() const noexcept=0
STL namespace.
virtual void stop() noexcept
Definition: PassiveTimer.cc:110
virtual void terminate()
Definition: PassiveTimer.h:61
timeout_t getLeft(timeout_t timeout) const noexcept
Definition: PassiveTimer.cc:95
virtual bool wait(timeout_t timeMS)
Definition: PassiveTimer.cc:105
virtual bool checkTime() const noexcept=0
Базовый интерфейс пасивных таймеров
Definition: PassiveTimer.h:43
virtual timeout_t setTiming(timeout_t msec) noexcept=0
virtual timeout_t getCurrent() const noexcept=0
std::chrono::high_resolution_clock::time_point t_start
Definition: PassiveTimer.h:119
static const timeout_t WaitUpTime
Definition: PassiveTimer.h:69
virtual void reset() noexcept=0