From 17b117e73df71925d73ee026b4f54aa1867ce0a5 Mon Sep 17 00:00:00 2001 From: Jesse Gilles Date: Mon, 20 Apr 2015 16:49:52 -0500 Subject: initial commit --- src/MTS_TimerThread.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/MTS_TimerThread.cpp (limited to 'src/MTS_TimerThread.cpp') diff --git a/src/MTS_TimerThread.cpp b/src/MTS_TimerThread.cpp new file mode 100644 index 0000000..5a69c46 --- /dev/null +++ b/src/MTS_TimerThread.cpp @@ -0,0 +1,116 @@ +/* + * 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(); +} -- cgit v1.2.3