diff options
Diffstat (limited to 'src/components/include/utils/timer_thread.h')
-rw-r--r-- | src/components/include/utils/timer_thread.h | 66 |
1 files changed, 51 insertions, 15 deletions
diff --git a/src/components/include/utils/timer_thread.h b/src/components/include/utils/timer_thread.h index eaa67effe2..4236c4e46f 100644 --- a/src/components/include/utils/timer_thread.h +++ b/src/components/include/utils/timer_thread.h @@ -134,9 +134,9 @@ class TimerThread { virtual bool isRunning(); /** - * @brief method suspends timer execution + * @brief Suspends timer execution after next loop. */ - virtual void pause(); + virtual void suspend(); /** * @brief Stop timer update timeout and start timer again @@ -187,12 +187,24 @@ class TimerThread { */ virtual void setTimeOut(const uint32_t timeout_seconds); + /** + * @brief Quits threadMain function after next loop. + */ + virtual void shouldBeStoped(); + + /** + * @brief Restarts non-loop timer after current iteration. + */ + virtual void shouldBeRestarted(); + protected: TimerThread* timer_thread_; uint32_t timeout_seconds_; sync_primitives::Lock state_lock_; sync_primitives::ConditionalVariable termination_condition_; volatile bool stop_flag_; + sync_primitives::Lock restart_flag_lock_; + volatile bool restart_flag_; int32_t calculateMillisecondsLeft(); private: @@ -261,11 +273,13 @@ template<class T> void TimerThread<T>::start(uint32_t timeout_seconds) { LOG4CXX_AUTO_TRACE(logger_); if (isRunning()) { - LOG4CXX_INFO(logger_, "TimerThread start needs stop " << name_); - stop(); + LOG4CXX_INFO(logger_, "Restart timer in thread " << name_); + delegate_->shouldBeRestarted(); + updateTimeOut(timeout_seconds); + } else { + updateTimeOut(timeout_seconds); + thread_->start(); } - updateTimeOut(timeout_seconds); - thread_->start(); } template<class T> @@ -291,10 +305,9 @@ bool TimerThread<T>::isRunning() { } template<class T> -void TimerThread<T>::pause() { - LOG4CXX_DEBUG(logger_, "Suspension of timer " << name_); - const uint32_t wait_seconds = std::numeric_limits<uint32_t>::max(); - updateTimeOut(wait_seconds); +void TimerThread<T>::suspend() { + LOG4CXX_DEBUG(logger_, "Suspend timer " << name_ << " after next loop"); + delegate_->shouldBeStoped(); } template<class T> @@ -313,7 +326,8 @@ TimerThread<T>::TimerDelegate::TimerDelegate(TimerThread* timer_thread) : timer_thread_(timer_thread), timeout_seconds_(0), state_lock_(true), - stop_flag_(false) { + stop_flag_(false), + restart_flag_(false) { DCHECK(timer_thread_); } @@ -344,11 +358,16 @@ void TimerThread<T>::TimerDelegate::threadMain() { LOG4CXX_TRACE(logger_, "Timer timeout " << wait_milliseconds_left << " ms"); timer_thread_->onTimeOut(); - return; } else { LOG4CXX_DEBUG(logger_, "Timeout reset force: " << TimerDelegate::timeout_seconds_); - return; + } + { + sync_primitives::AutoLock auto_lock(restart_flag_lock_); + if (!restart_flag_) { + return; + } + restart_flag_ = false; } } } @@ -378,8 +397,7 @@ void TimerThread<T>::TimerLooperDelegate::threadMain() { template<class T> void TimerThread<T>::TimerDelegate::exitThreadMain() { - sync_primitives::AutoLock auto_lock(state_lock_); - stop_flag_ = true; + shouldBeStoped(); termination_condition_.NotifyOne(); } @@ -390,6 +408,24 @@ void TimerThread<T>::TimerDelegate::setTimeOut(const uint32_t timeout_seconds) { } template<class T> +void TimerThread<T>::TimerDelegate::shouldBeStoped() { + { + sync_primitives::AutoLock auto_lock(state_lock_); + stop_flag_ = true; + } + { + sync_primitives::AutoLock auto_lock(restart_flag_lock_); + restart_flag_ = false; + } +} + +template<class T> +void TimerThread<T>::TimerDelegate::shouldBeRestarted() { + sync_primitives::AutoLock auto_lock(restart_flag_lock_); + restart_flag_ = true; +} + +template<class T> int32_t TimerThread<T>::TimerThread::TimerDelegate::calculateMillisecondsLeft() { time_t cur_time = time(NULL); time_t end_time = std::numeric_limits<time_t>::max(); |