2013-10-10 20:01:51 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
// Product: QF/C++ port to Win32
|
2014-04-13 21:35:34 -04:00
|
|
|
// Last updated for version 5.3.0
|
|
|
|
// Last updated on 2014-03-04
|
2012-08-14 18:00:48 -04:00
|
|
|
//
|
|
|
|
// Q u a n t u m L e a P s
|
|
|
|
// ---------------------------
|
|
|
|
// innovating embedded systems
|
|
|
|
//
|
2014-04-13 21:35:34 -04:00
|
|
|
// Copyright (C) Quantum Leaps, www.state-machine.com.
|
2012-08-14 18:00:48 -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
|
2013-10-16 16:46:00 -04:00
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
2012-08-14 18:00:48 -04:00
|
|
|
// (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:
|
2014-04-13 21:35:34 -04:00
|
|
|
// Web: www.state-machine.com
|
|
|
|
// Email: info@state-machine.com
|
2013-10-10 20:01:51 -04:00
|
|
|
//****************************************************************************
|
2014-04-13 21:35:34 -04:00
|
|
|
|
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qf_port.h" // QF port
|
2012-08-14 18:00:48 -04:00
|
|
|
#include "qf_pkg.h"
|
|
|
|
#include "qassert.h"
|
2014-04-13 21:35:34 -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
|
|
|
|
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
|
|
|
// Global objects ************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
CRITICAL_SECTION l_win32CritSect;
|
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
// Local objects *************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
static DWORD WINAPI thread_function(LPVOID arg);
|
2014-04-13 21:35:34 -04:00
|
|
|
static DWORD l_tickMsec = 10U; // clock tick in msec (argument for Sleep())
|
2013-12-30 17:41:15 -05:00
|
|
|
static bool l_running;
|
|
|
|
|
|
|
|
static DWORD WINAPI thread_function(LPVOID arg);
|
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);
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
void QF_enterCriticalSection(void) {
|
|
|
|
EnterCriticalSection(&l_win32CritSect);
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
void QF_leaveCriticalSection(void) {
|
|
|
|
LeaveCriticalSection(&l_win32CritSect);
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
void QF::stop(void) {
|
|
|
|
l_running = false;
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
void QF::thread_(QActive *act) {
|
2014-04-13 21:35:34 -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
|
2012-08-14 18:00:48 -04:00
|
|
|
} while (act->m_thread != NULL);
|
2014-04-13 21:35:34 -04:00
|
|
|
QF::remove_(act); // remove this object from any subscriptions
|
|
|
|
CloseHandle(act->m_osObject); // cleanup the OS event
|
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) {
|
2012-08-14 18:00:48 -04:00
|
|
|
l_running = true;
|
2014-04-13 21:35:34 -04:00
|
|
|
onStartup(); // startup callback
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
// if necessary, raise the priority of this (main) thread
|
|
|
|
// to tick more timely
|
|
|
|
//SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
|
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())
|
2012-08-14 18:00:48 -04:00
|
|
|
} while (l_running);
|
2014-04-13 21:35:34 -04:00
|
|
|
|
|
|
|
onCleanup(); // cleanup callback
|
|
|
|
QS_EXIT(); // cleanup the QSPY connection
|
2012-08-14 18:00:48 -04:00
|
|
|
//DeleteCriticalSection(&l_win32CritSect);
|
2014-04-13 21:35:34 -04:00
|
|
|
return static_cast<int_t>(0); // return success
|
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_setTickRate(uint32_t ticksPerSec) {
|
|
|
|
l_tickMsec = 1000UL / ticksPerSec;
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
|
|
|
void QActive::start(uint_fast8_t prio,
|
|
|
|
QEvt const *qSto[], uint_fast16_t qLen,
|
|
|
|
void *stkSto, uint_fast16_t stkSize,
|
2012-08-14 18:00:48 -04:00
|
|
|
QEvt const *ie)
|
|
|
|
{
|
2013-12-30 17:41:15 -05:00
|
|
|
Q_REQUIRE((qSto != static_cast<QEvt const **>(0)) /*valid queue storage */
|
2014-04-13 21:35:34 -04:00
|
|
|
&& (stkSto == (void *)0)); // Windows allocates stack internally
|
2012-08-14 18:00:48 -04:00
|
|
|
|
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
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
// create the Win32 event object to block the QEQueue
|
2012-08-14 18:00:48 -04:00
|
|
|
m_osObject = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2013-12-30 17:41:15 -05:00
|
|
|
m_eQueue.init(qSto, qLen);
|
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
this->init(ie); // execute initial transition (virtual call)
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
// stack size not provided?
|
|
|
|
if (stkSize == 0U) {
|
|
|
|
stkSize = 1024U; // NOTE: will be rounded up to the nearest page
|
2013-12-30 17:41:15 -05:00
|
|
|
}
|
2012-08-14 18:00:48 -04:00
|
|
|
m_thread = CreateThread(NULL, stkSize, &thread_function, this, 0, NULL);
|
2014-04-13 21:35:34 -04:00
|
|
|
Q_ASSERT(m_thread != NULL); // thread must be created
|
2012-08-14 18:00:48 -04:00
|
|
|
|
|
|
|
int p;
|
2014-04-13 21:35:34 -04:00
|
|
|
switch (m_prio) { // remap QF priority to Win32 priority
|
2012-08-14 18:00:48 -04:00
|
|
|
case 1:
|
|
|
|
p = THREAD_PRIORITY_IDLE;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
p = THREAD_PRIORITY_LOWEST;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
p = THREAD_PRIORITY_BELOW_NORMAL;
|
|
|
|
break;
|
|
|
|
case (QF_MAX_ACTIVE - 1):
|
|
|
|
p = THREAD_PRIORITY_ABOVE_NORMAL;
|
|
|
|
break;
|
|
|
|
case QF_MAX_ACTIVE:
|
|
|
|
p = THREAD_PRIORITY_HIGHEST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p = THREAD_PRIORITY_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SetThreadPriority(m_thread, p);
|
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
2012-08-14 18:00:48 -04:00
|
|
|
void QActive::stop(void) {
|
2014-04-13 21:35:34 -04:00
|
|
|
m_thread = NULL; // stop the QActive::run() loop
|
2012-08-14 18:00:48 -04:00
|
|
|
}
|
2014-04-13 21:35:34 -04:00
|
|
|
//****************************************************************************
|
|
|
|
static DWORD WINAPI thread_function(LPVOID me) { // for CreateThread()
|
2013-12-30 17:41:15 -05:00
|
|
|
QF::thread_(static_cast<QActive *>(me));
|
2014-04-13 21:35:34 -04:00
|
|
|
return 0; // return success
|
2013-12-30 17:41:15 -05:00
|
|
|
}
|
2012-08-14 18:00:48 -04:00
|
|
|
|
2014-04-13 21:35:34 -04:00
|
|
|
} // namespace QP
|