qpcpp/ports/win32/qf_port.cpp

255 lines
8.6 KiB
C++
Raw Normal View History

//============================================================================
// QP/C++ Real-Time Embedded Framework (RTEF)
2022-08-11 15:36:19 -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>
//============================================================================
//! @date Last updated on: 2023-11-30
//! @version Last updated for: @ref qpcpp_7_3_1
//!
2022-08-11 15:36:19 -04:00
//! @file
//! @brief QF/C++ port to Win32 (multithreaded)
2022-08-11 15:36:19 -04:00
2019-10-27 12:26:31 -04:00
#define QP_IMPL // this is QP implementation
#include "qp_port.hpp" // QP port
#include "qp_pkg.hpp" // QP package-scope interface
#include "qsafe.h" // QP Functional Safety (FuSa) Subsystem
2019-10-27 12:26:31 -04:00
#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-13 21:35:34 -04:00
#else
2019-10-27 12:26:31 -04:00
#include "qs_dummy.hpp" // disable the QS software tracing
2014-04-13 21:35:34 -04:00
#endif // Q_SPY
2012-08-14 18:00:48 -04:00
2022-09-09 16:34:14 -04:00
#include <climits> // limits of dynamic range for integers
2019-10-27 12:26:31 -04:00
#include <conio.h> // console input/output
2015-05-14 16:05:04 -04:00
2022-08-11 15:36:19 -04:00
namespace { // unnamed local namespace
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
// Local objects =============================================================
2017-08-29 16:01:46 -04:00
static CRITICAL_SECTION l_startupCritSect;
2014-04-13 21:35:34 -04:00
static DWORD l_tickMsec = 10U; // clock tick in msec (argument for Sleep())
static int l_tickPrio = 50; // default priority of the "ticker" thread
2015-06-06 18:30:55 -04:00
static bool l_isRunning; // flag indicating when QF is running
2012-08-14 18:00:48 -04:00
2022-08-11 15:36:19 -04:00
//............................................................................
// helper function to match the signature expeced by CreateThread() Win32 API
static DWORD WINAPI ao_thread(LPVOID me) {
QP::QActive::evtLoop_(static_cast<QP::QActive *>(me));
2022-08-11 15:36:19 -04:00
return static_cast<DWORD>(0); // return success
}
} // unnamed local namespace
//============================================================================
2022-08-11 15:36:19 -04:00
namespace QP {
namespace QF {
2022-08-11 15:36:19 -04:00
static CRITICAL_SECTION l_win32CritSect;
static int_t l_critSectNest; // critical section nesting up-down counter
2022-08-11 15:36:19 -04:00
//............................................................................
void enterCriticalSection_() {
if (l_isRunning) {
EnterCriticalSection(&l_win32CritSect);
Q_ASSERT_INCRIT(100, l_critSectNest == 0); // NO nesting of crit.sect!
++l_critSectNest;
}
2012-08-14 18:00:48 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
void leaveCriticalSection_() {
if (l_isRunning) {
Q_ASSERT_INCRIT(200, l_critSectNest == 1); // crit.sect. must ballace!
if ((--l_critSectNest) == 0) {
LeaveCriticalSection(&l_win32CritSect);
}
}
2012-08-14 18:00:48 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
void init() {
InitializeCriticalSection(&l_win32CritSect);
// initialize and enter the startup critical section object to block
// any active objects started before calling QF::run()
InitializeCriticalSection(&l_startupCritSect);
EnterCriticalSection(&l_startupCritSect);
2012-08-14 18:00:48 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
int run() {
2020-03-17 21:33:58 -04:00
onStartup(); // application-specific startup callback
2012-08-14 18:00:48 -04:00
2020-08-24 16:42:48 -04:00
// produce the QS_QF_RUN trace record
QS_BEGIN_PRE_(QS_QF_RUN, 0U)
QS_END_PRE_()
2020-08-24 16:42:48 -04:00
2017-08-29 16:01:46 -04:00
// leave the startup critical section to unblock any active objects
// started before calling QF::run()
LeaveCriticalSection(&l_startupCritSect);
2015-06-06 18:30:55 -04:00
l_isRunning = true; // QF is running
2015-05-14 16:05:04 -04:00
2018-10-25 11:13:01 -04:00
// set the ticker (this thread) priority according to selection made in
2022-08-11 15:36:19 -04:00
// QF::setTickRate()
2018-10-25 11:13:01 -04:00
int threadPrio = THREAD_PRIORITY_NORMAL;
if (l_tickPrio < 33) {
threadPrio = THREAD_PRIORITY_BELOW_NORMAL;
}
else if (l_tickPrio > 66) {
threadPrio = THREAD_PRIORITY_ABOVE_NORMAL;
}
SetThreadPriority(GetCurrentThread(), threadPrio);
2012-08-14 18:00:48 -04:00
2020-07-18 17:58:58 -04:00
while (l_isRunning) {
2022-08-11 15:36:19 -04:00
Sleep(l_tickMsec); // wait for the tick interval
onClockTick(); // must call QTimeEvt::TICK_X()
2020-07-18 17:58:58 -04:00
}
2014-04-13 21:35:34 -04:00
2022-08-11 15:36:19 -04:00
onCleanup(); // cleanup callback
QS_EXIT(); // cleanup the QSPY connection
2017-08-29 16:01:46 -04:00
//DeleteCriticalSection(&l_startupCritSect);
2012-08-14 18:00:48 -04:00
//DeleteCriticalSection(&l_win32CritSect);
2020-03-17 21:33:58 -04:00
return 0; // return success
2012-08-14 18:00:48 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
void stop() {
l_isRunning = false; // terminate the main (ticker) thread
}
//............................................................................
void setTickRate(std::uint32_t ticksPerSec, int tickPrio) {
2020-03-17 21:33:58 -04:00
Q_REQUIRE_ID(600, ticksPerSec != 0U);
2012-08-14 18:00:48 -04:00
l_tickMsec = 1000UL / ticksPerSec;
2018-10-25 11:13:01 -04:00
l_tickPrio = tickPrio;
2012-08-14 18:00:48 -04:00
}
2018-10-25 11:13:01 -04:00
//............................................................................
void consoleSetup() {
2018-10-25 11:13:01 -04:00
}
//............................................................................
void consoleCleanup() {
2018-10-25 11:13:01 -04:00
}
//............................................................................
int consoleGetKey() {
2020-03-17 21:33:58 -04:00
if (_kbhit()) { // any key pressed?
return static_cast<int>(_getwch());
2018-10-25 11:13:01 -04:00
}
return 0;
}
//............................................................................
int consoleWaitForKey(void) {
2020-03-17 21:33:58 -04:00
return static_cast<int>(_getwch());
2018-10-25 11:13:01 -04:00
}
} // namespace QF
// QActive functions =========================================================
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-10-27 12:26:31 -04: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);
2019-12-31 15:56:23 -05:00
// no external AO-stack storage needed for this port
2022-08-28 22:12:27 -04:00
Q_REQUIRE_ID(800, stkSto == nullptr);
2022-08-29 14:42:32 -04:00
m_prio = static_cast<std::uint8_t>(prioSpec & 0xFFU); // QF-priority
m_pthre = 0U; // preemption-threshold (not used in QF, but in Win32)
register_(); // register this AO
2013-12-30 17:41:15 -05:00
2016-11-08 12:48:44 -05:00
// create the Win32 "event" to throttle the AO's event queue
2012-08-14 18:00:48 -04:00
m_osObject = CreateEvent(NULL, FALSE, FALSE, NULL);
2022-08-29 14:42:32 -04:00
m_eQueue.init(qSto, qLen);
2013-12-30 17:41:15 -05:00
// the top-most initial tran. (virtual)
this->init(par, m_prio);
2017-08-29 16:01:46 -04:00
QS_FLUSH(); // flush the QS trace buffer to the host
2012-08-14 18:00:48 -04:00
2016-11-08 12:48:44 -05:00
// create a Win32 thread for the AO;
// The thread is created with THREAD_PRIORITY_NORMAL
2020-03-17 21:33:58 -04:00
m_thread = CreateThread(
NULL,
(stkSize < 1024U ? 1024U : stkSize),
&ao_thread,
this,
0,
NULL);
2020-07-18 17:58:58 -04:00
Q_ENSURE_ID(830, m_thread != nullptr); // must succeed
2022-08-29 14:42:32 -04:00
// set the priority of the Win32 thread based on the
// "prio-threshold" field provided in the `prioSpec` parameter
2022-08-29 14:42:32 -04:00
int win32Prio;
switch ((prioSpec >> 8U) & 0xFFU) {
2022-08-29 14:42:32 -04:00
case 1U:
win32Prio = THREAD_PRIORITY_LOWEST;
break;
case 2U:
win32Prio = THREAD_PRIORITY_NORMAL;
break;
case 3U:
win32Prio = THREAD_PRIORITY_TIME_CRITICAL;
break;
default:
win32Prio = THREAD_PRIORITY_NORMAL;
break;
}
SetThreadPriority(m_thread, win32Prio);
2020-07-18 17:58:58 -04:00
}
//............................................................................
#ifdef QACTIVE_CAN_STOP
void QActive::stop() {
2020-07-18 17:58:58 -04:00
unsubscribeAll(); // unsubscribe this AO from all events
m_thread = nullptr; // stop the thread loop (see QF::thread_)
2012-08-14 18:00:48 -04:00
}
2020-07-18 17:58:58 -04:00
#endif
2012-08-14 18:00:48 -04:00
//............................................................................
void QActive::evtLoop_(QActive *act) {
// block this thread until the startup critical section is exited
// from QF::run()
EnterCriticalSection(&l_startupCritSect);
LeaveCriticalSection(&l_startupCritSect);
#ifdef QACTIVE_CAN_STOP
while (act->m_thread)
#else
for (;;) // for-ever
#endif
{
QEvt const *e = act->get_(); // wait for event
act->dispatch(e, act->m_prio); // dispatch to the AO's state machine
QF::gc(e); // check if the event is garbage, and collect it if so
}
#ifdef QACTIVE_CAN_STOP
act->unregister_(); // remove this object from QF
#endif
}
2014-04-13 21:35:34 -04:00
} // namespace QP
2020-03-17 21:33:58 -04:00