qpcpp/source/qk.cpp

337 lines
13 KiB
C++
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
/// @brief QK preemptive kernel core functions
/// @ingroup qk
/// @cond
2014-04-13 21:35:34 -04:00
///***************************************************************************
2016-12-01 10:31:49 -05:00
/// Last updated for version 5.8.0
/// Last updated on 2016-11-29
2014-04-13 21:35:34 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
2016-04-01 13:55:34 -04:00
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2014-04-13 21:35:34 -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
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
///
/// Contact information:
2015-09-29 11:34:38 -04:00
/// http://www.state-machine.com
/// mailto:info@state-machine.com
2014-04-13 21:35:34 -04:00
///***************************************************************************
2015-05-14 16:05:04 -04:00
/// @endcond
2014-04-13 21:35:34 -04:00
#define QP_IMPL // this is QF/QK implementation
#include "qf_port.h" // QF port
2016-12-01 10:31:49 -05:00
#include "qf_pkg.h" // QF package-scope internal interface
2015-09-29 11:34:38 -04:00
#include "qassert.h" // QP assertions
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
2015-12-31 14:56:37 -05:00
// protection against including this source file in a wrong project
#ifndef qk_h
#error "Source file included in a project NOT based on the QK kernel"
#endif // qk_h
2014-04-13 21:35:34 -04:00
// Public-scope objects ******************************************************
2012-08-14 18:00:48 -04:00
extern "C" {
2014-04-13 21:35:34 -04:00
2016-04-01 13:55:34 -04:00
Q_DEFINE_THIS_MODULE("qk")
2016-09-01 11:58:57 -04:00
QK_Attr QK_attr_; // global attributes of the QK kernel
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
} // extern "C"
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
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
/// @description
2014-04-13 21:35:34 -04:00
/// 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).
///
2015-05-14 16:05:04 -04:00
/// @note
2014-04-13 21:35:34 -04:00
/// 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) {
2016-12-01 10:31:49 -05:00
QF_maxPool_ = static_cast<uint_fast8_t>(0);
QF_subscrList_ = static_cast<QSubscrList *>(0);
QF_maxPubSignal_ = static_cast<enum_t>(0);
2013-12-30 17:41:15 -05:00
2014-04-13 21:35:34 -04:00
bzero(&QF::timeEvtHead_[0],
static_cast<uint_fast16_t>(sizeof(QF::timeEvtHead_)));
2015-05-14 16:05:04 -04:00
bzero(&active_[0], static_cast<uint_fast16_t>(sizeof(active_)));
2016-09-01 11:58:57 -04:00
bzero(&QK_attr_, static_cast<uint_fast16_t>(sizeof(QK_attr_)));
2016-09-29 19:54:50 -04:00
QK_attr_.actPrio = static_cast<uint_fast8_t>(0); // prio of QK idle loop
2016-09-01 11:58:57 -04:00
QK_attr_.lockPrio = static_cast<uint_fast8_t>(QF_MAX_ACTIVE); // locked
2014-04-13 21:35:34 -04:00
2016-09-01 11:58:57 -04:00
QK_init(); // QK initialization ("C" linkage, might be in assembly)
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
/// @description
2014-04-13 21:35:34 -04:00
/// 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.
///
2015-05-14 16:05:04 -04:00
/// @sa QP::QF::onCleanup()
2014-04-13 21:35:34 -04:00
///
2012-08-14 18:00:48 -04:00
void QF::stop(void) {
2014-04-13 21:35:34 -04:00
QF::onCleanup(); // cleanup callback
2012-08-14 18:00:48 -04:00
// nothing else to do for the QK preemptive kernel
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
//! process all events posted during initialization */
2015-09-29 11:34:38 -04:00
static void initial_events(void); // prototype
static void initial_events(void) {
2016-09-01 11:58:57 -04:00
QK_attr_.lockPrio = static_cast<uint_fast8_t>(0); // scheduler unlocked
2015-05-14 16:05:04 -04:00
// any active objects need to be scheduled before starting event loop?
2016-09-29 19:54:50 -04:00
if (QK_sched_() != static_cast<uint_fast8_t>(0)) {
QK_activate_(); // process all events produced so far
2012-08-14 18:00:48 -04:00
}
}
2014-04-13 21:35:34 -04:00
//****************************************************************************
2015-05-14 16:05:04 -04:00
// @description
2014-04-13 21:35:34 -04:00
// 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().
//
2015-05-14 16:05:04 -04:00
// @returns QP::QF::run() typically does not return in embedded applications.
2014-04-13 21:35:34 -04:00
// 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().
//
2015-05-14 16:05:04 -04:00
// @note This function is strongly platform-dependent and is not implemented
2014-04-13 21:35:34 -04:00
// 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) {
2012-08-14 18:00:48 -04:00
QF_INT_DISABLE();
2015-09-29 11:34:38 -04:00
initial_events(); // process all events posted during initialization
onStartup(); // application-specific startup callback
2012-08-14 18:00:48 -04:00
QF_INT_ENABLE();
2014-04-13 21:35:34 -04:00
// the QK idle loop...
for (;;) {
QK::onIdle(); // invoke the QK on-idle callback
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
//****************************************************************************
2015-05-14 16:05:04 -04:00
// @description
2014-04-13 21:35:34 -04:00
// Starts execution of the AO and registers the AO with the framework.
//
2015-05-14 16:05:04 -04:00
// @param[in] prio priority at which to start the active object
// @param[in] qSto pointer to the storage for the ring buffer of the
// event queue (used only with the built-in QP::QEQueue)
// @param[in] qLen length of the event queue (in events)
// @param[in] stkSto pointer to the stack storage (used only when
// per-AO stack is needed)
// @param[in] stkSize stack size (in bytes)
// @param[in] ie pointer to the optional initialization event
// (might be NULL).
2014-04-13 21:35:34 -04:00
//
2016-12-01 10:31:49 -05:00
void QActive::start(uint_fast8_t const prio,
2015-05-14 16:05:04 -04:00
QEvt const *qSto[], uint_fast16_t const qLen,
void * const stkSto, uint_fast16_t const stkSize,
QEvt const * const ie)
2012-08-14 18:00:48 -04:00
{
2014-04-13 21:35:34 -04:00
Q_REQUIRE_ID(500, (static_cast<uint_fast8_t>(0) < prio)
&& (prio <= static_cast<uint_fast8_t>(QF_MAX_ACTIVE)));
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
2015-12-31 14:56:37 -05:00
// QK kernel does not need per-thread stack
2015-09-29 11:34:38 -04:00
Q_ASSERT_ID(510, (stkSto == static_cast<void *>(0))
&& (stkSize == static_cast<uint_fast16_t>(0)));
2012-08-14 18:00:48 -04:00
2015-09-29 11:34:38 -04:00
this->init(ie); // take the top-most initial tran. (virtual)
2014-04-13 21:35:34 -04:00
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
//****************************************************************************
2015-05-14 16:05:04 -04:00
// @description
2014-04-13 21:35:34 -04:00
// 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.
//
2015-05-14 16:05:04 -04:00
// @note
2014-04-13 21:35:34 -04:00
// 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.
//
2016-12-01 10:31:49 -05:00
void QActive::stop(void) {
2014-04-13 21:35:34 -04:00
QF::remove_(this); // remove this active object from the QF
2012-08-14 18:00:48 -04:00
}
2014-04-13 21:35:34 -04:00
} // namespace QP
2015-05-14 16:05:04 -04:00
//============================================================================
extern "C" {
//****************************************************************************
/// @description
/// This function finds out the priority of the highest-priority active object
/// that (1) has events to process, and (2) has priority that is above the
/// current priority, and (3) has priority that is above the mutex ceiling,
/// if mutex is configured in the port.
///
/// @returns the 1-based priority of the the active object, or zero if
/// no eligible active object is ready to run.
///
2016-09-29 19:54:50 -04:00
/// @attention QK_sched_() must be always called with interrupts
2015-05-14 16:05:04 -04:00
/// __disabled__ and returns with interrupts __disabled__.
///
2016-09-29 19:54:50 -04:00
uint_fast8_t QK_sched_(void) {
2016-04-01 13:55:34 -04:00
// find the highest-prio AO with non-empty event queue
2016-09-01 11:58:57 -04:00
uint_fast8_t p = QK_attr_.readySet.findMax();
2015-05-14 16:05:04 -04:00
2016-09-29 19:54:50 -04:00
// is the highest-prio below the active prio?
if (p <= QK_attr_.actPrio) {
2015-09-29 11:34:38 -04:00
p = static_cast<uint_fast8_t>(0); // active object not eligible
2015-05-14 16:05:04 -04:00
}
2016-09-01 11:58:57 -04:00
else if (p <= QK_attr_.lockPrio) { // is it below the lock prio?
2016-04-01 13:55:34 -04:00
p = static_cast<uint_fast8_t>(0); // active object not eligible
}
else {
Q_ASSERT_ID(610, p <= static_cast<uint_fast8_t>(QF_MAX_ACTIVE));
2016-09-29 19:54:50 -04:00
QK_attr_.nextPrio = p; // next AO to run
2016-04-01 13:55:34 -04:00
}
2015-05-14 16:05:04 -04:00
return p;
}
//****************************************************************************
2016-09-29 19:54:50 -04:00
/// @description
/// QK_activate_() activates ready-to run AOs that are above the initial
/// active priority (QK_attr_.actPrio).
2015-05-14 16:05:04 -04:00
///
/// @note
2016-09-29 19:54:50 -04:00
/// The activator might enable interrupts internally, but always returns with
/// interrupts **disabled**.
2015-05-14 16:05:04 -04:00
///
2016-09-29 19:54:50 -04:00
void QK_activate_(void) {
uint_fast8_t pin = QK_attr_.actPrio; // save the active priority
uint_fast8_t p = QK_attr_.nextPrio; /* the next prio to run */
2016-12-01 10:31:49 -05:00
QP::QActive *a;
2015-09-29 11:34:38 -04:00
// QS tracing or thread-local storage?
2015-12-31 14:56:37 -05:00
#ifdef Q_SPY
2015-05-14 16:05:04 -04:00
uint_fast8_t pprev = pin;
2015-12-31 14:56:37 -05:00
#endif // Q_SPY
2015-09-29 11:34:38 -04:00
2016-09-29 19:54:50 -04:00
// QK_attr_.nextPrio must be non-zero upon entry to QK_activate_()
Q_REQUIRE_ID(800, p != static_cast<uint_fast8_t>(0));
QK_attr_.nextPrio = static_cast<uint_fast8_t>(0); // clear for next time
// loop until no more ready-to-run AOs of higher prio than the initial
2015-05-14 16:05:04 -04:00
do {
a = QP::QF::active_[p]; // obtain the pointer to the AO
2016-09-29 19:54:50 -04:00
QK_attr_.actPrio = p; // this becomes the active priority
2015-05-14 16:05:04 -04:00
2016-04-01 13:55:34 -04:00
QS_BEGIN_NOCRIT_(QP::QS_SCHED_NEXT, QP::QS::priv_.aoObjFilter, a)
2015-09-29 11:34:38 -04:00
QS_TIME_(); // timestamp
QS_2U8_(static_cast<uint8_t>(p), // prio of the scheduled AO
static_cast<uint8_t>(pprev)); // previous priority
QS_END_NOCRIT_()
2015-12-31 14:56:37 -05:00
#ifdef Q_SPY
2015-09-29 11:34:38 -04:00
if (p != pprev) { // changing priorities?
pprev = p; // update previous priority
}
2015-12-31 14:56:37 -05:00
#endif // Q_SPY
2015-05-14 16:05:04 -04:00
2015-09-29 11:34:38 -04:00
QF_INT_ENABLE(); // unconditionally enable interrupts
2015-05-14 16:05:04 -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 QActive_get_() asserts it.
// 2. dispatch the event to the AO's state machine.
// 3. determine if event is garbage and collect it if so
//
QP::QEvt const *e = a->get_();
a->dispatch(e);
QP::QF::gc(e);
// determine the next highest-priority AO ready to run...
QF_INT_DISABLE();
2016-12-01 10:31:49 -05:00
if (a->m_eQueue.isEmpty()) { // empty queue?
QK_attr_.readySet.remove(p);
}
2016-04-01 13:55:34 -04:00
// find new highest-prio AO ready to run...
2016-09-01 11:58:57 -04:00
p = QK_attr_.readySet.findMax();
2015-09-29 11:34:38 -04:00
2016-04-29 08:52:53 -04:00
// is the new priority below the initial preemption threshold?
2016-04-13 09:24:22 -04:00
if (p <= pin) {
p = static_cast<uint_fast8_t>(0); // active object not eligible
}
2016-09-01 11:58:57 -04:00
else if (p <= QK_attr_.lockPrio) { // is it below the lock prio?
2016-04-13 09:24:22 -04:00
p = static_cast<uint_fast8_t>(0); // active object not eligible
}
else {
Q_ASSERT_ID(710, p <= static_cast<uint_fast8_t>(QF_MAX_ACTIVE));
}
2015-05-14 16:05:04 -04:00
} while (p != static_cast<uint_fast8_t>(0));
2016-09-29 19:54:50 -04:00
QK_attr_.actPrio = pin; // restore the active priority
2015-05-14 16:05:04 -04:00
2015-12-31 14:56:37 -05:00
#ifdef Q_SPY
2015-09-29 11:34:38 -04:00
if (pin != static_cast<uint_fast8_t>(0)) { // resuming an active object?
2015-05-14 16:05:04 -04:00
a = QP::QF::active_[pin]; // the pointer to the preempted AO
2015-09-29 11:34:38 -04:00
2016-04-01 13:55:34 -04:00
QS_BEGIN_NOCRIT_(QP::QS_SCHED_RESUME, QP::QS::priv_.aoObjFilter, a)
2015-09-29 11:34:38 -04:00
QS_TIME_(); // timestamp
QS_2U8_(static_cast<uint8_t>(pin), // prio of the resumed AO
static_cast<uint8_t>(pprev)); // previous priority
QS_END_NOCRIT_()
}
else { // resuming priority==0 --> idle
2016-04-01 13:55:34 -04:00
QS_BEGIN_NOCRIT_(QP::QS_SCHED_IDLE,
2015-09-29 11:34:38 -04:00
static_cast<void *>(0), static_cast<void *>(0))
QS_TIME_(); // timestamp
QS_U8_(static_cast<uint8_t>(pprev)); // previous priority
QS_END_NOCRIT_()
}
2015-12-31 14:56:37 -05:00
#endif // Q_SPY
2015-05-14 16:05:04 -04:00
}
} // extern "C"