qpcpp/ports/qt/qf_port.cpp

170 lines
6.0 KiB
C++
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
/// @brief QP/C++ port to Qt
/// @cond
///***************************************************************************
2016-12-01 10:31:49 -05:00
/// Last Updated for Version: QP 5.8.0/Qt 5.x
/// Last updated on 2016-11-19
2015-05-14 16:05:04 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
2016-05-05 12:22:15 -04:00
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2015-05-14 16:05:04 -04:00
///
/// This program is open source software: you can redistribute it and/or
/// modify it under the terms of the GNU General Public License as published
/// by the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// Alternatively, this program may be distributed and modified under the
/// terms of Quantum Leaps commercial licenses, which expressly supersede
/// the GNU General Public License and are specifically designed for
/// licensees interested in retaining the proprietary status of their code.
///
/// This program 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 General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// Contact information:
2016-05-05 12:22:15 -04:00
/// http://www.state-machine.com
/// mailtp:info@state-machine.com
2015-05-14 16:05:04 -04:00
///***************************************************************************
/// @endcond
2015-09-29 11:34:38 -04:00
#include <QCoreApplication>
#include "aothread.h"
#include "tickerthread.h"
2012-08-14 18:00:48 -04:00
//-----------------
2014-04-21 21:48:04 -04:00
#define QP_IMPL // this is QP implementation
#include "qf_port.h" // QF port
2015-05-14 16:05:04 -04:00
#include "qf_pkg.h" // QF package-scope interface
#include "qassert.h" // QP embedded systems-friendly assertions
2014-04-21 21:48:04 -04:00
#ifdef Q_SPY // QS software tracing enabled?
#include "qs_port.h" // include QS port
#else
#include "qs_dummy.h" // disable the QS software tracing
#endif // Q_SPY
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
2013-10-10 20:01:51 -04:00
//****************************************************************************
2013-12-30 17:41:15 -05:00
namespace QP {
//............................................................................
QMutex QF_qtMutex_;
//............................................................................
2013-10-10 20:01:51 -04:00
static TickerThread l_tickerThread;
2012-08-14 18:00:48 -04:00
2013-12-30 17:41:15 -05:00
//............................................................................
2015-05-14 16:05:04 -04:00
void QF_enterCriticalSection_() { QF_qtMutex_.lock(); }
void QF_leaveCriticalSection_() { QF_qtMutex_.unlock(); }
2013-12-30 17:41:15 -05:00
//............................................................................
2013-10-10 20:01:51 -04:00
AOThread::~AOThread() {
wait();
2012-08-14 18:00:48 -04:00
}
//............................................................................
2013-10-10 20:01:51 -04:00
void AOThread::run() {
Q_REQUIRE(m_act != static_cast<void *>(0));
QP::QF::thread_(static_cast<QP::QActive *>(m_act));
}
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
TickerThread::~TickerThread() {
wait();
}
//............................................................................
void TickerThread::run() {
m_isRunning = true;
do {
msleep(m_tickInterval);
QP::QF_onClockTick();
2012-08-14 18:00:48 -04:00
#ifdef Q_SPY
2013-10-10 20:01:51 -04:00
QP::QS_onEvent();
2012-08-14 18:00:48 -04:00
#endif
2013-10-10 20:01:51 -04:00
} while (m_isRunning);
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QF::init(void) {
}
//............................................................................
2014-04-21 21:48:04 -04:00
int_t QF::run(void) {
onStartup(); // invoke the startup callback
2013-10-10 20:01:51 -04:00
2015-09-29 11:34:38 -04:00
//l_tickerThread.setStackSize(1024U*4U); // 4KB of stack
2013-10-10 20:01:51 -04:00
l_tickerThread.start();
// run the Qt event loop (console or GUI)
2014-04-21 21:48:04 -04:00
return static_cast<int_t>(QCoreApplication::instance()->exec());
2013-10-10 20:01:51 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QF::thread_(QActive *act) {
2013-10-10 20:01:51 -04:00
AOThread *thread = static_cast<AOThread *>(act->m_thread);
thread->m_isRunning = true;
2014-04-21 21:48:04 -04:00
// loop until m_thread is cleared in QActive::stop()...
do {
QEvt const *e = act->get_(); // wait for event
act->dispatch(e); // dispatch to the active object's state machine
gc(e); // check if the event is garbage, and collect it if so
2013-10-10 20:01:51 -04:00
} while (thread->m_isRunning);
QF::remove_(act);
delete thread;
2013-12-30 17:41:15 -05:00
delete act->m_osObject;
2012-08-14 18:00:48 -04:00
}
//............................................................................
void QF::stop(void) {
2013-10-10 20:01:51 -04:00
l_tickerThread.m_isRunning = false;
2012-08-14 18:00:48 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QF_setQtPrio(QActive *act, int_t qtPrio) {
2015-05-14 16:05:04 -04:00
// thread not created yet?
if (act->getThread() == static_cast<QThread *>(0)) {
// store the priority for later
act->getOsObject() = reinterpret_cast<QWaitCondition *>(qtPrio);
}
else {
act->getThread()->setPriority(static_cast<QThread::Priority>(qtPrio));
}
}
//............................................................................
2013-10-10 20:01:51 -04:00
void QF_setTickRate(unsigned ticksPerSec) {
l_tickerThread.m_tickInterval = 1000U/ticksPerSec;
2012-08-14 18:00:48 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QActive::start(uint_fast8_t const prio,
2015-05-14 16:05:04 -04:00
QEvt const **qSto, uint_fast16_t qLen,
void * const stkSto, uint_fast16_t const stkSize,
QEvt const * const ie)
2012-08-14 18:00:48 -04:00
{
2014-04-21 21:48:04 -04:00
Q_REQUIRE(stkSto == static_cast<void *>(0)); // no per-task stack
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
m_thread = new AOThread(this);
m_osObject = new QWaitCondition;
m_eQueue.init(qSto, qLen);
2012-08-14 18:00:48 -04:00
m_prio = prio;
2013-10-10 20:01:51 -04:00
2014-04-21 21:48:04 -04:00
QF::add_(this); // make QF aware of this active object
init(ie); // execute the initial transition
2012-08-14 18:00:48 -04:00
2014-04-21 21:48:04 -04:00
QS_FLUSH(); // flush the trace buffer to the host
2013-10-10 20:01:51 -04:00
AOThread *thread = static_cast<AOThread *>(m_thread);
thread->setStackSize(stkSize);
thread->start();
2012-08-14 18:00:48 -04:00
}
//............................................................................
2016-12-01 10:31:49 -05:00
void QActive::stop(void) {
2013-10-10 20:01:51 -04:00
Q_REQUIRE(m_thread != 0);
static_cast<AOThread *>(m_thread)->m_isRunning = false;
2012-08-14 18:00:48 -04:00
}
2014-04-21 21:48:04 -04:00
} // namespace QP