qpc/source/qf_defer.c

133 lines
4.8 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
* @brief QActive_defer() and QActive_recall() implementation.
* @ingroup qf
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-06-04 22:47:13 -04:00
* Last updated for version 5.4.2
* Last updated on 2015-06-03
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
******************************************************************************
2015-04-28 13:45:35 -04:00
* @endcond
2014-04-06 11:43:13 -04:00
*/
#define QP_IMPL /* this is QP implementation */
#include "qf_port.h" /* QF port */
2015-04-28 13:45:35 -04:00
#include "qf_pkg.h" /* QF package-scope interface */
#include "qassert.h" /* QP embedded systems-friendly assertions */
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
Q_DEFINE_THIS_MODULE("qf_defer")
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* This function is part of the event deferral support. An active object
2015-04-28 13:45:35 -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-06 11:43:13 -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-04-28 13:45:35 -04:00
* @param[in,out] me pointer (see @ref oop)
* @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-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @returns 'true' (success) when the event could be deferred and 'false'
2014-04-06 11:43:13 -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.
*
2015-04-28 13:45:35 -04:00
* @sa QActive_recall(), QEQueue
2012-08-14 18:07:04 -04:00
*/
2015-06-04 22:47:13 -04:00
bool QActive_defer(QMActive * const me, QEQueue * const eq,
2014-04-06 11:43:13 -04:00
QEvt const * const e)
2012-08-14 18:07:04 -04:00
{
2014-04-06 11:43:13 -04:00
(void)me; /* avoid compiler warning about 'me' not used */
return QEQueue_post(eq, e, (uint_fast16_t)1);
2012-08-14 18:07:04 -04:00
}
2014-04-06 11:43:13 -04:00
/****************************************************************************/
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -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-04-28 13:45:35 -04:00
* deferred event queue @p eq and posted (LIFO) to the event queue of
2014-04-06 11:43:13 -04:00
* the active object.
*
2015-04-28 13:45:35 -04:00
* @param[in,out] me pointer (see @ref oop)
* @param[in] eq pointer to a "raw" thread-safe queue to recall
* an event from.
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @returns 'true' if an event has been recalled and 'false' if not.
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @note An active object can use multiple event queues to defer events of
2014-04-06 11:43:13 -04:00
* different kinds.
*
2015-04-28 13:45:35 -04:00
* @sa QActive_recall(), QEQueue, QActive_postLIFO_()
2014-04-06 11:43:13 -04:00
*/
2015-06-04 22:47:13 -04:00
bool QActive_recall(QMActive * const me, QEQueue * const eq) {
2014-04-06 11:43:13 -04:00
QEvt const *e = QEQueue_get(eq); /* get an event from deferred queue */
bool recalled;
/* event available? */
if (e != (QEvt const *)0) {
2012-08-14 18:07:04 -04:00
QF_CRIT_STAT_
2013-09-23 14:34:35 -04:00
QACTIVE_POST_LIFO(me, e); /* post it to the front of the AO's queue */
2012-08-14 18:07:04 -04:00
QF_CRIT_ENTRY_();
2014-04-06 11:43:13 -04:00
/* is it a dynamic event? */
if (e->poolId_ != (uint8_t)0) {
2012-08-14 18:07:04 -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.
*/
2014-04-06 11:43:13 -04:00
Q_ASSERT_ID(210, e->refCtr_ > (uint8_t)1);
2012-08-14 18:07:04 -04:00
/* we need to decrement the reference counter once, to account
* for removing the event from the deferred event queue.
*/
2014-04-06 11:43:13 -04:00
QF_EVT_REF_CTR_DEC_(e); /* decrement the reference counter */
2012-08-14 18:07:04 -04:00
}
QF_CRIT_EXIT_();
2014-04-06 11:43:13 -04:00
recalled = true;
2012-08-14 18:07:04 -04:00
}
else {
2014-04-06 11:43:13 -04:00
recalled = false;
2012-08-14 18:07:04 -04:00
}
return recalled;
}