2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2022-08-28 22:12:27 -04:00
|
|
|
// Copyright (C) 2005 Quantum Leaps, LLC <state-machine.com>.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
|
|
|
|
//
|
|
|
|
// This software is dual-licensed under the terms of the open source GNU
|
|
|
|
// General Public License version 3 (or any later version), or alternatively,
|
|
|
|
// under the terms of one of the closed source Quantum Leaps commercial
|
|
|
|
// licenses.
|
|
|
|
//
|
|
|
|
// The terms of the open source GNU General Public License version 3
|
|
|
|
// can be found at: <www.gnu.org/licenses/gpl-3.0>
|
|
|
|
//
|
|
|
|
// The terms of the closed source Quantum Leaps commercial licenses
|
|
|
|
// can be found at: <www.state-machine.com/licensing>
|
|
|
|
//
|
|
|
|
// Redistributions in source code must retain this top-level comment block.
|
|
|
|
// Plagiarizing this software to sidestep the license obligations is illegal.
|
|
|
|
//
|
|
|
|
// Contact information:
|
|
|
|
// <www.state-machine.com/licensing>
|
|
|
|
// <info@state-machine.com>
|
2022-04-19 19:23:30 -04:00
|
|
|
//============================================================================
|
2022-08-29 14:42:32 -04:00
|
|
|
//! @date Last updated on: 2022-08-29
|
2022-08-28 22:12:27 -04:00
|
|
|
//! @version Last updated for: @ref qpcpp_7_1_0
|
|
|
|
//!
|
|
|
|
//! @file
|
|
|
|
//! @brief QP/C++ port to Qt
|
2015-05-14 16:05:04 -04:00
|
|
|
|
2015-09-29 11:34:38 -04:00
|
|
|
#include <QCoreApplication>
|
2019-10-27 12:26:31 -04:00
|
|
|
#include "aothread.hpp"
|
|
|
|
#include "tickerthread.hpp"
|
2012-08-14 18:00:48 -04:00
|
|
|
//-----------------
|
2019-10-27 12:26:31 -04:00
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qf_port.hpp" // QF port
|
|
|
|
#include "qf_pkg.hpp" // QF package-scope interface
|
|
|
|
#include "qassert.h" // QP embedded systems-friendly assertions
|
|
|
|
#ifdef Q_SPY // QS software tracing enabled?
|
2020-03-17 21:33:58 -04:00
|
|
|
#include "qs_port.hpp" // QS port
|
|
|
|
#include "qs_pkg.hpp" // QS package-scope internal interface
|
2014-04-21 21:48:04 -04:00
|
|
|
#else
|
2019-10-27 12:26:31 -04:00
|
|
|
#include "qs_dummy.hpp" // disable the QS software tracing
|
2014-04-21 21:48:04 -04:00
|
|
|
#endif // Q_SPY
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2022-08-11 15:36:19 -04:00
|
|
|
namespace {
|
|
|
|
Q_DEFINE_THIS_MODULE("qf_port")
|
|
|
|
}
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2022-04-19 19:23:30 -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() {
|
2020-03-17 21:33:58 -04:00
|
|
|
Q_REQUIRE(m_act != nullptr);
|
2022-08-11 15:36:19 -04:00
|
|
|
QP::QActive::thread_(static_cast<QP::QActive *>(m_act));
|
2013-10-10 20:01:51 -04:00
|
|
|
}
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2022-04-19 19:23:30 -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();
|
|
|
|
|
2020-08-24 16:42:48 -04:00
|
|
|
// produce the QS_QF_RUN trace record
|
|
|
|
QS_CRIT_STAT_
|
2020-10-01 12:50:17 -04:00
|
|
|
QS_BEGIN_PRE_(QS_QF_RUN, 0U)
|
2020-08-24 16:42:48 -04:00
|
|
|
QS_END_PRE_()
|
|
|
|
|
2013-10-10 20:01:51 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
//............................................................................
|
2022-08-11 15:36:19 -04:00
|
|
|
void QActive::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
|
|
|
|
2019-02-10 21:01:38 -05:00
|
|
|
// event-loop
|
|
|
|
for (;;) { // for-ever
|
2014-04-21 21:48:04 -04:00
|
|
|
QEvt const *e = act->get_(); // wait for event
|
2020-10-01 12:50:17 -04:00
|
|
|
act->dispatch(e, act->m_prio); // dispatch to the AO's state machine
|
2022-08-11 15:36:19 -04:00
|
|
|
QF::gc(e); // check if the event is garbage, and collect it if so
|
2019-02-10 21:01:38 -05:00
|
|
|
}
|
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?
|
2020-03-17 21:33:58 -04:00
|
|
|
if (act->getThread() == nullptr) {
|
2015-05-14 16:05:04 -04:00
|
|
|
// 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
|
|
|
}
|
|
|
|
//............................................................................
|
2022-08-28 22:12:27 -04:00
|
|
|
void QActive::start(QPrioSpec const prioSpec,
|
2020-03-17 21:33:58 -04:00
|
|
|
QEvt const * * const qSto, std::uint_fast16_t const qLen,
|
|
|
|
void * const stkSto, std::uint_fast16_t const stkSize,
|
2019-12-31 15:56:23 -05:00
|
|
|
void const * const par)
|
2012-08-14 18:00:48 -04:00
|
|
|
{
|
2022-08-28 22:12:27 -04:00
|
|
|
Q_UNUSED_PAR(stkSto);
|
|
|
|
Q_UNUSED_PAR(stkSize);
|
|
|
|
|
2020-03-17 21:33:58 -04:00
|
|
|
Q_REQUIRE(stkSto == nullptr); // no per-task stack
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2022-08-29 14:42:32 -04:00
|
|
|
m_prio = static_cast<std::uint8_t>(prioSpec & 0xFFU); // QF-priority
|
|
|
|
m_pthre = static_cast<std::uint8_t>(prioSpec >> 8U); // preemption-thre.
|
2022-08-28 22:12:27 -04:00
|
|
|
register_(); // make QF aware of this AO
|
|
|
|
|
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
|
|
|
|
2020-10-01 12:50:17 -04:00
|
|
|
init(par, m_prio); // execute the initial transition
|
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
|
|
|
}
|
|
|
|
|
2014-04-21 21:48:04 -04:00
|
|
|
} // namespace QP
|