qpcpp/ports/win32/qf_port.cpp

234 lines
8.2 KiB
C++
Raw Normal View History

//============================================================================
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>
//============================================================================
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
//!
2022-08-11 15:36:19 -04:00
//! @file
//! @brief QF/C++ port to Win32 API (multi-threaded)
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-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")
2022-08-11 15:36:19 -04:00
// Local objects .............................................................
2015-05-14 16:05:04 -04:00
static CRITICAL_SECTION l_win32CritSect;
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())
2018-10-25 11:13:01 -04:00
static int_t 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::thread_(static_cast<QP::QActive *>(me));
return static_cast<DWORD>(0); // return success
}
} // unnamed local namespace
//============================================================================
2022-08-11 15:36:19 -04:00
namespace QP {
//............................................................................
2012-08-14 18:00:48 -04:00
void QF::init(void) {
InitializeCriticalSection(&l_win32CritSect);
2016-02-10 16:27:39 -05:00
2017-08-29 16:01:46 -04:00
// 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
//............................................................................
void QF::enterCriticalSection_(void) {
2012-08-14 18:00:48 -04:00
EnterCriticalSection(&l_win32CritSect);
}
2022-08-11 15:36:19 -04:00
//............................................................................
void QF::leaveCriticalSection_(void) {
2012-08-14 18:00:48 -04:00
LeaveCriticalSection(&l_win32CritSect);
}
2022-08-11 15:36:19 -04:00
//............................................................................
2012-08-14 18:00:48 -04:00
void QF::stop(void) {
2018-10-25 11:13:01 -04:00
l_isRunning = false; // terminate the main (ticker) thread
2012-08-14 18:00:48 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
void QActive::thread_(QActive *act) {
2017-08-29 16:01:46 -04:00
// block this thread until the startup critical section is exited
// from QF::run()
EnterCriticalSection(&l_startupCritSect);
LeaveCriticalSection(&l_startupCritSect);
2020-07-18 17:58:58 -04:00
#ifdef QF_ACTIVE_STOP
while (act->m_thread)
#else
for (;;) // for-ever
#endif
{
2014-04-13 21:35:34 -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
}
2020-07-18 17:58:58 -04:00
#ifdef QF_ACTIVE_STOP
2022-08-11 15:36:19 -04:00
act->unregister_(); // remove this object from QF
2020-07-18 17:58:58 -04:00
#endif
2015-05-14 16:05:04 -04:00
}
2022-08-11 15:36:19 -04:00
//............................................................................
2013-12-30 17:41:15 -05:00
int_t QF::run(void) {
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
2020-10-01 12:50:17 -04:00
QS_BEGIN_NOCRIT_PRE_(QS_QF_RUN, 0U)
2020-08-24 16:42:48 -04:00
QS_END_NOCRIT_PRE_()
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()
2015-05-14 16:05:04 -04:00
//
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(); // clock tick callback (must call QF::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 QF::setTickRate(std::uint32_t ticksPerSec, int_t 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
//............................................................................
2022-08-11 15:36:19 -04:00
void QF::consoleSetup(void) {
2018-10-25 11:13:01 -04:00
}
//............................................................................
2022-08-11 15:36:19 -04:00
void QF::consoleCleanup(void) {
2018-10-25 11:13:01 -04:00
}
//............................................................................
2022-08-11 15:36:19 -04:00
int QF::consoleGetKey(void) {
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;
}
//............................................................................
2022-08-11 15:36:19 -04:00
int QF::consoleWaitForKey(void) {
2020-03-17 21:33:58 -04:00
return static_cast<int>(_getwch());
2018-10-25 11:13:01 -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-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
2022-08-28 22:12:27 -04:00
// no need for external stack storage in this port
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 = static_cast<std::uint8_t>(prioSpec >> 8U); // preemption-thre.
2022-08-11 15:36:19 -04:00
register_(); // make QF aware of 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
2020-10-01 12:50:17 -04:00
this->init(par, m_prio); // execute initial transition (virtual call)
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 m_pthre
int win32Prio;
switch (m_pthre) {
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 QF_ACTIVE_STOP
void QActive::stop(void) {
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
2014-04-13 21:35:34 -04:00
} // namespace QP
2020-03-17 21:33:58 -04:00