/* * Copyright (C) 2015 by Multi-Tech Systems * * This file is part of libmts. * * libmts is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * libmts is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libmts. If not, see . * */ #include #include using namespace MTS; TimerThread::TimerThread(const std::string& name) : Thread(name, true) , m_bCurrentlyExecuting(false) , m_bExecutionStopped(false) , m_ui32IntervalMillis(1000) { init(); } TimerThread::TimerThread(const std::string& name, uint32_t intervalMillis) : Thread(name, true) , m_bCurrentlyExecuting(false) , m_bExecutionStopped(false) , m_ui32IntervalMillis(intervalMillis) { init(); } void TimerThread::init() { printConfig("TimerThread| %s - starting up", Thread::getName().c_str()); m_apStateLock.reset(new MTS::Lock); } TimerThread::~TimerThread() { stop(); m_apStateLock.reset(); printConfig("TimerThread| %s - shutting down", Thread::getName().c_str()); } void TimerThread::stopExecution() { m_apStateLock->lock(); m_bExecutionStopped = true; m_apStateLock->unlock(); printDebug("TimerThread| %s - stopping execution of task", Thread::getName().c_str()); } void TimerThread::restartExecution() { m_apStateLock->lock(); m_bExecutionStopped = false; m_apStateLock->unlock(); printDebug("TimerThread| %s - restarting execution of task", Thread::getName().c_str()); } void TimerThread::run() { uint32_t intervalMillis; bool executionStopped; while (! Thread::isCanceled()) { m_apStateLock->lock(); intervalMillis = m_ui32IntervalMillis; executionStopped = m_bExecutionStopped; m_apStateLock->unlock(); if (! executionStopped) { printTrace("TimerThread| %s - starting execute() function", Thread::getName().c_str()); m_apStateLock->lock(); m_bCurrentlyExecuting = true; m_apStateLock->unlock(); execute(); m_apStateLock->lock(); m_bCurrentlyExecuting = false; m_apStateLock->unlock(); printTrace("TimerThread| %s - finished execute() function", Thread::getName().c_str()); } Thread::sleep(intervalMillis); } } bool TimerThread::isExecuting() const { bool retval = false; m_apStateLock->lock(); retval = m_bCurrentlyExecuting; m_apStateLock->unlock(); return retval; } void TimerThread::updateIntervalMillis(uint32_t newMillis) { m_apStateLock->lock(); m_ui32IntervalMillis = newMillis; m_apStateLock->unlock(); }