qpcpp/qf/source/qvanilla.cpp

221 lines
8.6 KiB
C++
Raw Normal View History

2012-08-14 18:00:48 -04:00
/// \file
2014-04-13 21:35:34 -04:00
/// \brief cooperative "vanilla" kernel, definition of QP::QF_readySet_ and
/// implementation of kernel-specific functins.
2012-08-14 18:00:48 -04:00
/// \ingroup qf
2014-04-13 21:35:34 -04:00
/// \cond
///***************************************************************************
/// Product: QF/C++
2014-09-22 11:48:11 -04:00
/// Last updated for version 5.3.1
/// Last updated on 2014-09-05
2014-04-13 21:35:34 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
/// Copyright (C) Quantum Leaps, www.state-machine.com.
///
/// 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
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// Contact information:
/// Web: www.state-machine.com
/// Email: info@state-machine.com
///***************************************************************************
/// \endcond
#define QP_IMPL // this is QP implementation
#include "qf_port.h" // QF port
#include "qf_pkg.h"
#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
2014-04-21 21:48:04 -04:00
#include "qassert.h"
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("qvanilla")
2014-04-13 21:35:34 -04:00
/// \note The functions implemented in this module can have a different
/// implementation in other QF ports. The implementations included here
/// are appropriate for the "vanilla" cooperative kernel only.
// Package-scope objects *****************************************************
2012-08-14 18:00:48 -04:00
extern "C" {
#if (QF_MAX_ACTIVE <= 8)
2014-04-13 21:35:34 -04:00
QPSet8 QF_readySet_; // ready set of AOs
2012-08-14 18:00:48 -04:00
#else
2014-04-13 21:35:34 -04:00
QPSet64 QF_readySet_; // ready set of AOs
2012-08-14 18:00:48 -04:00
#endif
2014-04-13 21:35:34 -04:00
} // extern "C"
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
//****************************************************************************
/// \description
/// Initializes QF and must be called exactly once before any other QF
/// function. Typcially, QP::QF::init() is called from main() even before
/// initializing the Board Support Package (BSP).
///
/// \note QP::QF::init() clears the internal QF variables, so that the
/// framework can start correctly even if the startup code fails to clear
/// the uninitialized data (as is required by the C Standard).
///
2012-08-14 18:00:48 -04:00
void QF::init(void) {
2014-09-22 11:48:11 -04:00
extern uint_fast8_t QF_maxPool_;
QF_maxPool_ = static_cast<uint_fast8_t>(0);
2014-04-13 21:35:34 -04:00
bzero(&QF_readySet_, static_cast<uint_fast16_t>(sizeof(QF_readySet_)));
bzero(&QF::timeEvtHead_[0],
static_cast<uint_fast16_t>(sizeof(QF::timeEvtHead_)));
bzero(&QF::active_[0],
static_cast<uint_fast16_t>(
static_cast<uint_fast16_t>(QF_MAX_ACTIVE)
* static_cast<uint_fast16_t>(sizeof(QActive *))));
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
/// \description
/// This function stops the QF application. After calling this function,
/// QF attempts to gracefully stop the application. This graceful shutdown
/// might take some time to complete. The typical use of this function is
/// for terminating the QF application to return back to the operating
/// system or for handling fatal errors that require shutting down
/// (and possibly re-setting) the system.
///
/// \sa QP::QF::onCleanup()
///
2012-08-14 18:00:48 -04:00
void QF::stop(void) {
2014-04-13 21:35:34 -04:00
onCleanup(); // cleanup callback
2012-08-14 18:00:48 -04:00
// nothing else to do for the "vanilla" kernel
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
/// \description
/// QP::QF::run() is typically called from your startup code after you
/// initialize the QF and start at least one active object with
/// QP::QActive::start().
///
/// \returns QP::QF::run() typically does not return in embedded applications.
/// However, when QP runs on top of an operating system, QP::QF::run() might
/// return and in this case the return represents the error code (0 for
/// success). Typically the value returned from QP::QF::run() is subsequently
/// passed on as return from main().
///
/// \note This function is strongly platform-dependent and is not implemented
/// in the QF, but either in the QF port or in the Board Support Package (BSP)
/// for the given application. All QF ports must implement QP::QF::run().
///
2013-12-30 17:41:15 -05:00
int_t QF::run(void) {
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
// the bacground loop of the "Vanilla" kernel
for (;;) {
2012-08-14 18:00:48 -04:00
QF_INT_DISABLE();
if (QF_readySet_.notEmpty()) {
2014-04-13 21:35:34 -04:00
uint_fast8_t p = QF_readySet_.findMax();
2012-08-14 18:00:48 -04:00
QActive *a = active_[p];
QF_INT_ENABLE();
2014-04-13 21:35:34 -04:00
// perform the run-to-completion (RTS) step...
// 1. retrieve the event from the AO's event queue, which by this
// time must be non-empty and The "Vanialla" kernel asserts it.
// 2. dispatch the event to the AO's state machine.
// 3. determine if event is garbage and collect it if so
//
QEvt const *e = a->get_();
a->dispatch(e);
gc(e);
2012-08-14 18:00:48 -04:00
}
else {
2014-04-13 21:35:34 -04:00
// QF::onIdle() must be called with interrupts DISABLED because
// the determination of the idle condition (no events in the
// queues) can change at any time by an interrupt posting events
// to a queue. QF_onIdle() MUST enable interrupts internally,
// perhaps at the same time as putting the CPU into a power-saving
// mode.
//
onIdle();
2012-08-14 18:00:48 -04:00
}
}
2013-10-10 20:01:51 -04:00
2014-04-13 21:35:34 -04:00
#ifdef __GNUC__ // GNU compiler?
return static_cast<int_t>(0);
2013-10-10 20:01:51 -04:00
#endif
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
/// \description
/// Starts execution of the AO and registers the AO with the framework.
///
/// \arguments
/// \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 function should be called via the macro START().
///
/// \usage
/// The following example shows starting an AO when a per-task stack is needed
/// \include qf_start.cpp
///
void QActive::start(uint_fast8_t const prio,
QEvt const *qSto[], uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const,
2012-08-14 18:00:48 -04:00
QEvt const * const ie)
{
2014-04-13 21:35:34 -04:00
/// \pre the priority must be in range and the stack storage must not
/// be provided, because "Vanilla" kernel does not need per-AO stacks.
///
2014-09-22 11:48:11 -04:00
Q_REQUIRE_ID(400, (static_cast<uint_fast8_t>(0) < prio)
2014-04-13 21:35:34 -04:00
&& (prio <= static_cast<uint_fast8_t>(QF_MAX_ACTIVE))
2014-09-22 11:48:11 -04:00
&& (stkSto == static_cast<void *>(0)));
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
m_eQueue.init(qSto, qLen); // initialize QEQueue of this AO
m_prio = prio; // set the QF priority of this AO
QF::add_(this); // make QF aware of this AO
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
this->init(ie); // execute initial transition (virtual call)
QS_FLUSH(); // flush the trace buffer to the host
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
/// \description
/// The preferred way of calling this function is from within the active
/// object that needs to stop. In other words, an active object should stop
/// itself rather than being stopped by someone else. This policy works
/// best, because only the active object itself "knows" when it has reached
/// the appropriate state for the shutdown.
///
/// \note By the time the AO calls QP::QActive::stop(), it should have
/// unsubscribed from all events and no more events should be directly-posted
/// to it.
///
2012-08-14 18:00:48 -04:00
void QActive::stop(void) {
QF::remove_(this);
}
2014-04-13 21:35:34 -04:00
} // namespace QP
2012-08-14 18:00:48 -04:00