qpcpp/ports/win32/qf_port.cpp

213 lines
8.1 KiB
C++
Raw Normal View History

2018-10-25 11:13:01 -04:00
/// @file
/// @brief QF/C++ port to Win32 API (multi-threaded)
/// @ingroup qf
/// @cond
2015-05-14 16:05:04 -04:00
///***************************************************************************
2020-03-17 21:33:58 -04:00
/// Last updated for version 6.8.0
2020-03-23 19:58:24 -04:00
/// Last updated on 2020-03-23
2015-05-14 16:05:04 -04:00
///
2018-10-25 11:13:01 -04:00
/// Q u a n t u m L e a P s
/// ------------------------
/// Modern Embedded Software
2015-05-14 16:05:04 -04:00
///
2020-03-17 21:33:58 -04:00
/// Copyright (C) 2005-2020 Quantum Leaps. 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
2019-10-27 12:26:31 -04:00
/// along with this program. If not, see <www.gnu.org/licenses>.
2015-05-14 16:05:04 -04:00
///
/// Contact information:
2019-12-31 15:56:23 -05:00
/// <www.state-machine.com/licensing>
2019-10-27 12:26:31 -04:00
/// <info@state-machine.com>
2015-05-14 16:05:04 -04:00
///***************************************************************************
2018-10-25 11:13:01 -04:00
/// @endcond
///
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
2019-10-27 12:26:31 -04:00
#include <limits.h> // limits of dynamic range for integers
#include <conio.h> // console input/output
2015-05-14 16:05:04 -04:00
2013-10-10 20:01:51 -04:00
namespace QP {
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
2014-04-13 21:35:34 -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
2014-04-13 21:35:34 -04:00
//****************************************************************************
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
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
void QF_enterCriticalSection_(void) {
2012-08-14 18:00:48 -04:00
EnterCriticalSection(&l_win32CritSect);
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
void QF_leaveCriticalSection_(void) {
2012-08-14 18:00:48 -04:00
LeaveCriticalSection(&l_win32CritSect);
}
2014-04-13 21:35:34 -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
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2016-12-01 10:31:49 -05:00
void QF::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);
2019-02-10 21:01:38 -05:00
// event-loop
for (;;) { // for-ever
2014-04-13 21:35:34 -04:00
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
2019-02-10 21:01:38 -05:00
}
2015-05-14 16:05:04 -04:00
}
//****************************************************************************
// helper function to match the signature expeced by CreateThread() Win32 API
static DWORD WINAPI ao_thread(LPVOID me) {
2016-12-01 10:31:49 -05:00
QF::thread_(static_cast<QActive *>(me));
2015-05-14 16:05:04 -04:00
return static_cast<DWORD>(0); // return success
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -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
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
// 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
do {
2014-04-13 21:35:34 -04:00
Sleep(l_tickMsec); // wait for the tick interval
QF_onClockTick(); // clock tick callback (must call QF_TICK_X())
2015-06-06 18:30:55 -04:00
} while (l_isRunning);
2014-04-13 21:35:34 -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
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2020-03-17 21:33:58 -04:00
void QF_setTickRate(std::uint32_t ticksPerSec, int_t tickPrio) {
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
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2016-12-01 10:31:49 -05:00
void QF_setWin32Prio(QActive *act, int_t win32Prio) {
2019-02-12 12:47:58 -05:00
HANDLE win32thread = static_cast<HANDLE>(act->getThread());
// thread must be already created, see QActive::start()
Q_REQUIRE_ID(700, win32thread != static_cast<HANDLE>(0));
SetThreadPriority(win32thread, win32Prio);
2015-05-14 16:05:04 -04:00
}
2018-10-25 11:13:01 -04:00
//............................................................................
void QF_consoleSetup(void) {
}
//............................................................................
void QF_consoleCleanup(void) {
}
//............................................................................
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;
}
//............................................................................
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
}
2015-05-14 16:05:04 -04:00
//****************************************************************************
2020-03-17 21:33:58 -04:00
void QActive::start(std::uint_fast8_t const prio,
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
{
2019-12-31 15:56:23 -05:00
(void)stkSize; // unused paramteter in the Win32 port
2020-03-17 21:33:58 -04:00
Q_REQUIRE_ID(800, (0U < prio) /* priority...*/
&& (prio <= QF_MAX_ACTIVE) /*...in range */
&& (stkSto == nullptr)); // statck storage must NOT...
// ... be provided
2018-10-25 11:13:01 -04:00
m_eQueue.init(qSto, qLen);
2014-04-13 21:35:34 -04:00
m_prio = prio; // set the QF priority of this AO
QF::add_(this); // make QF aware of this AO
2013-12-30 17:41:15 -05:00
2015-05-14 16:05:04 -04:00
// save osObject as integer, in case it contains the Win32 priority
2020-03-17 21:33:58 -04:00
//int win32Prio = (m_osObject != nullptr)
2019-02-12 12:47:58 -05:00
// ? reinterpret_cast<intptr_t>(m_osObject)
// : THREAD_PRIORITY_NORMAL;
2015-05-14 16:05:04 -04: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);
2013-12-30 17:41:15 -05:00
2019-10-27 12:26:31 -04:00
this->init(par); // 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);
2019-02-12 12:47:58 -05:00
Q_ENSURE_ID(830, m_thread != static_cast<HANDLE>(0)); // must succeed
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