qpc/ports/win32/qf_port.c

185 lines
6.9 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
* \file
* \ingroup qf
* \brief QF/C port to Win32.
* \cond
******************************************************************************
* Product: QF/C
* Last updated for version 5.3.0
* Last updated on 2014-03-01
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2014-04-06 11:43:13 -04:00
* Copyright (C) Quantum Leaps, www.state-machine.com.
2012-08-14 18:07: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
2012-08-14 18:07:04 -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-06 11:43:13 -04:00
* Web: www.state-machine.com
* Email: info@state-machine.com
******************************************************************************
* \endcond
*/
#define QP_IMPL /* this is QP implementation */
#include "qf_port.h" /* QF port */
2012-08-14 18:07:04 -04:00
#include "qf_pkg.h"
#include "qassert.h"
2014-04-06 11:43:13 -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:07:04 -04:00
Q_DEFINE_THIS_MODULE("qf_port")
2014-04-06 11:43:13 -04:00
/* Local objects ************************************************************/
2012-08-14 18:07:04 -04:00
static CRITICAL_SECTION l_win32CritSect;
2014-04-06 11:43:13 -04:00
static DWORD l_tickMsec = 10; /* clock tick in msec (argument for Sleep()) */
2013-12-30 17:37:40 -05:00
static int_t l_running;
2012-08-14 18:07:04 -04:00
static DWORD WINAPI thread_function(LPVOID arg);
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_init(void) {
InitializeCriticalSection(&l_win32CritSect);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_enterCriticalSection_(void) {
EnterCriticalSection(&l_win32CritSect);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_leaveCriticalSection_(void) {
LeaveCriticalSection(&l_win32CritSect);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_stop(void) {
2013-12-30 17:37:40 -05:00
l_running = (int_t)0;
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2013-12-30 17:37:40 -05:00
int_t QF_run(void) {
2014-04-06 11:43:13 -04:00
QF_onStartup(); /* startup callback */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/* if necessary, raise the priority of this (main) thread
* to tick more timely
*/
/*SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);*/
2013-12-30 17:37:40 -05:00
l_running = (int_t)1;
2012-08-14 18:07:04 -04:00
while (l_running) {
2014-04-06 11:43:13 -04:00
Sleep(l_tickMsec); /* wait for the tick interval */
QF_onClockTick(); /* clock tick callback (must call QF_TICKX()) */
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
QF_onCleanup(); /* cleanup callback */
QS_EXIT(); /* cleanup the QSPY connection */
2012-08-14 18:07:04 -04:00
//DeleteCriticalSection(&l_win32CritSect);
2014-04-06 11:43:13 -04:00
return (int_t)0; /* return success */
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QF_setTickRate(uint32_t ticksPerSec) {
l_tickMsec = 1000UL / ticksPerSec;
}
2014-04-06 11:43:13 -04:00
/*==========================================================================*/
/**
* \description
* Port-specific active Object start operation.
*
* \arguments
* \arg[in,out] \c me pointer (see \ref derivation)
* \arg[in] \c prio priority at which to start the active object
* \arg[in] \c qSto pointer to the storage for the ring buffer of the
* event queue (used only with the built-in ::QEQueue)
* \arg[in] \c qLen length of the event queue (in events)
* \arg[in] \c stkSto pointer to the stack storage (used only when
* per-AO stack is needed)
* \arg[in] \c stkSize stack size (in bytes)
* \arg[in] \c ie pointer to the optional initialization event
* (might be NULL).
*
* \note This QActive_start_() implementation is just an example and different
* QF ports might define this function differently.
*/
/****************************************************************************/
void QActive_start_(QActive * const me, uint_fast8_t prio,
QEvt const *qSto[], uint_fast16_t qLen,
void *stkSto, uint_fast16_t stkSize,
2013-12-30 17:37:40 -05:00
QEvt const *ie)
2012-08-14 18:07:04 -04:00
{
DWORD threadId;
int p;
2014-04-06 11:43:13 -04:00
Q_REQUIRE((qSto != (QEvt const **)0) /* queue storage must be provided */
&& (stkSto == (void *)0)); /* Windows allocates stack internally */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
me->prio = prio;
QF_add_(me); /* make QF aware of this active object */
2012-08-14 18:07:04 -04:00
2013-12-30 17:37:40 -05:00
QEQueue_init(&me->eQueue, qSto, qLen);
2012-08-14 18:07:04 -04:00
me->osObject = CreateEvent(NULL, FALSE, FALSE, NULL);
2014-04-06 11:43:13 -04:00
QMSM_INIT(&me->super, ie); /* execute initial transition */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/* stack size not provided? */
if (stkSize == 0U) {
stkSize = 1024U; /* NOTE: will be rounded up to the nearest page */
2013-12-30 17:37:40 -05:00
}
2012-08-14 18:07:04 -04:00
me->thread = CreateThread(NULL, stkSize,
2013-09-23 14:34:35 -04:00
&thread_function, me, 0, &threadId);
2014-04-06 11:43:13 -04:00
Q_ASSERT(me->thread != (HANDLE)0); /* thread must be created */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
switch (me->prio) { /* remap QF priority to Win32 priority */
2012-08-14 18:07:04 -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(me->thread, p);
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
void QActive_stop(QActive * const me) {
2014-04-06 11:43:13 -04:00
me->thread = (HANDLE)0; /* stop the event loop in thread_function() */
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
static DWORD WINAPI thread_function(LPVOID arg) { /* for CreateThread() */
/* QActive_stop() stops the loop */
do {
QEvt const *e = QActive_get_((QActive *)arg); /* wait for event */
QMSM_DISPATCH((QMsm *)arg, e); /* dispatch to the AO's SM */
QF_gc(e); /* check if the event is garbage, and collect it if so */
2012-08-14 18:07:04 -04:00
} while (((QActive *)arg)->thread != (HANDLE)0);
2014-04-06 11:43:13 -04:00
2012-08-14 18:07:04 -04:00
QF_remove_((QActive *)arg);/* remove this object from any subscriptions */
2014-04-06 11:43:13 -04:00
CloseHandle(((QActive *)arg)->osObject); /* cleanup the OS event */
return 0; /* return success */
2012-08-14 18:07:04 -04:00
}