qpcpp/source/qf_defer.cpp

148 lines
5.6 KiB
C++
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
/// @brief QP::QMActive::defer() and QP::QMActive::recall() definitions.
/// @ingroup qf
/// @cond
2014-04-13 21:35:34 -04:00
///***************************************************************************
2016-02-10 16:27:39 -05:00
/// Last updated for version 5.6.2
2016-04-01 13:55:34 -04:00
/// Last updated on 2016-02-11
2014-04-13 21:35:34 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
2016-02-10 16:27:39 -05:00
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:00:48 -04:00
///
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:
2016-02-10 16:27:39 -05: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 QP implementation
#include "qf_port.h" // QF port
2015-05-14 16:05:04 -04:00
#include "qf_pkg.h" // QF package-scope interface
#include "qassert.h" // QP embedded systems-friendly assertions
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
2015-05-14 16:05:04 -04:00
Q_DEFINE_THIS_MODULE("qf_defer")
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 is part of the event deferral support. An active object
2015-05-14 16:05:04 -04:00
/// uses this function to defer an event @p e to the QF-supported native
/// event queue @p eq. QF correctly accounts for another outstanding
2014-04-13 21:35:34 -04:00
/// reference to the event and will not recycle the event at the end of
/// the RTC step. Later, the active object might recall one event at a
/// time from the event queue.
///
2015-05-14 16:05:04 -04:00
/// @param[in] eq pointer to a "raw" thread-safe queue to recall
/// an event from.
/// @param[in] e pointer to the event to be deferred
2014-04-13 21:35:34 -04:00
///
2015-05-14 16:05:04 -04:00
/// @returns 'true' (success) when the event could be deferred and 'false'
2014-04-13 21:35:34 -04:00
/// (failure) if event deferral failed due to overflowing the queue.
///
/// An active object can use multiple event queues to defer events of
/// different kinds.
///
2016-04-01 13:55:34 -04:00
/// @sa QP::QMActive::recall(), QP::QEQueue, QP::QMActive::flushDeferred()
///
2015-05-14 16:05:04 -04:00
bool QMActive::defer(QEQueue * const eq, QEvt const * const e) const {
2014-04-13 21:35:34 -04:00
return eq->post(e, static_cast<uint_fast16_t>(1)); // non-asserting post
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 is part of the event deferral support. An active object
/// uses this function to recall a deferred event from a given QF
/// event queue. Recalling an event means that it is removed from the
2015-05-14 16:05:04 -04:00
/// deferred event queue @p eq and posted (LIFO) to the event queue of
2014-04-13 21:35:34 -04:00
/// the active object.
///
2015-05-14 16:05:04 -04:00
/// @param[in] eq pointer to a "raw" thread-safe queue to recall
/// an event from.
2014-04-13 21:35:34 -04:00
///
2015-05-14 16:05:04 -04:00
/// @returns 'true' if an event has been recalled and 'false' if not.
2014-04-13 21:35:34 -04:00
///
2015-05-14 16:05:04 -04:00
/// @note An active object can use multiple event queues to defer events of
2014-04-13 21:35:34 -04:00
/// different kinds.
///
2015-05-14 16:05:04 -04:00
/// @sa QP::QMActive::recall(), QP::QEQueue, QP::QMActive::postLIFO_()
2016-04-01 13:55:34 -04:00
///
2015-05-14 16:05:04 -04:00
bool QMActive::recall(QEQueue * const eq) {
2014-04-13 21:35:34 -04:00
QEvt const * const e = eq->get(); // try to get evt from deferred queue
2015-05-14 16:05:04 -04:00
bool const recalled = (e != static_cast<QEvt const *>(0));//evt available?
2012-08-14 18:00:48 -04:00
if (recalled) {
2014-04-13 21:35:34 -04:00
this->postLIFO(e); // post it to the _front_ of the AO's queue
2012-08-14 18:00:48 -04:00
QF_CRIT_STAT_
QF_CRIT_ENTRY_();
2014-04-13 21:35:34 -04:00
// is it a dynamic event?
2015-05-14 16:05:04 -04:00
if (e->poolId_ != static_cast<uint8_t>(0)) {
2012-08-14 18:00:48 -04:00
// after posting to the AO's queue the event must be referenced
// at least twice: once in the deferred event queue (eq->get()
// did NOT decrement the reference counter) and once in the
// AO's event queue.
2015-05-14 16:05:04 -04:00
Q_ASSERT_ID(210, e->refCtr_ > static_cast<uint8_t>(1));
2012-08-14 18:00:48 -04:00
// we need to decrement the reference counter once, to account
// for removing the event from the deferred event queue.
2014-04-13 21:35:34 -04:00
QF_EVT_REF_CTR_DEC_(e); // decrement the reference counter
2012-08-14 18:00:48 -04:00
}
QF_CRIT_EXIT_();
}
2014-04-13 21:35:34 -04:00
return recalled; // event not recalled
2012-08-14 18:00:48 -04:00
}
2016-02-10 16:27:39 -05:00
//****************************************************************************
/// @description
/// This function is part of the event deferral support. An active object
/// can use this function to flush a given QF event queue. The function makes
/// sure that the events are not leaked.
///
/// @param[in] eq pointer to a "raw" thread-safe queue to flush.
///
/// @returns the number of events actually flushed from the queue.
///
///
/// @sa QP::QMActive::defer(), QP::QMActive::recall(), QP::QEQueue
2016-04-01 13:55:34 -04:00
///
2016-02-10 16:27:39 -05:00
uint_fast16_t QMActive::flushDeferred(QEQueue * const eq) const {
uint_fast16_t n = static_cast<uint_fast16_t>(0);
for (QEvt const *e = eq->get();
e != static_cast<QEvt const *>(0);
e = eq->get())
{
QF::gc(e); // garbage collect
++n; // count the flushed event
}
return n;
}
2014-04-13 21:35:34 -04:00
} // namespace QP
2013-10-10 20:01:51 -04:00
2012-08-14 18:00:48 -04:00