QP/C++  8.0.3
Real-Time Event Framework
Loading...
Searching...
No Matches
qf_defer.cpp
Go to the documentation of this file.
1//============================================================================
2// QP/C++ Real-Time Event Framework (RTEF)
3//
4// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
5//
6// Q u a n t u m L e a P s
7// ------------------------
8// Modern Embedded Software
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
11//
12// This software is dual-licensed under the terms of the open-source GNU
13// General Public License (GPL) or under the terms of one of the closed-
14// source Quantum Leaps commercial licenses.
15//
16// Redistributions in source code must retain this top-level comment block.
17// Plagiarizing this software to sidestep the license obligations is illegal.
18//
19// NOTE:
20// The GPL does NOT permit the incorporation of this code into proprietary
21// programs. Please contact Quantum Leaps for commercial licensing options,
22// which expressly supersede the GPL and are designed explicitly for
23// closed-source distribution.
24//
25// Quantum Leaps contact information:
26// <www.state-machine.com/licensing>
27// <info@state-machine.com>
28//============================================================================
29#define QP_IMPL // this is QP implementation
30#include "qp_port.hpp" // QP port
31#include "qp_pkg.hpp" // QP package-scope interface
32#include "qsafe.h" // QP Functional Safety (FuSa) Subsystem
33#ifdef Q_SPY // QS software tracing enabled?
34 #include "qs_port.hpp" // QS port
35 #include "qs_pkg.hpp" // QS facilities for pre-defined trace records
36#else
37 #include "qs_dummy.hpp" // disable the QS software tracing
38#endif // Q_SPY
39
40// unnamed namespace for local definitions with internal linkage
41namespace {
42Q_DEFINE_THIS_MODULE("qf_defer")
43} // unnamed namespace
44
45namespace QP {
46
47//............................................................................
49 QEQueue * const eq,
50 QEvt const * const e) const noexcept
51{
52 bool const status = eq->post(e, 0U, m_prio);
53
56 QS_BEGIN_PRE(QS_QF_ACTIVE_DEFER, m_prio)
57 QS_TIME_PRE(); // time stamp
58 QS_OBJ_PRE(this); // this active object
59 QS_OBJ_PRE(eq); // the deferred queue
60 QS_SIG_PRE(e->sig); // the signal of the event
61 QS_2U8_PRE(e->poolNum_, e->refCtr_);
64
65 return status;
66}
67
68//............................................................................
69bool QActive::recall(QEQueue * const eq) noexcept {
70 QEvt const * const e = eq->get(m_prio); // get evt from deferred queue
72
73 bool recalled;
74 if (e != nullptr) { // event available?
75 postLIFO(e); // post it to the _front_ of the AO's queue
76
78
79 if (e->poolNum_ != 0U) { // is it a mutable event?
80
81 // after posting to the AO's queue the event must be referenced
82 // at least twice: once in the deferred event queue (eq->get()
83 // did NOT decrement the reference counter) and once in the
84 // AO's event queue.
85 Q_ASSERT_INCRIT(205, e->refCtr_ >= 2U);
86
87 // we need to decrement the reference counter once, to account
88 // for removing the event from the deferred event queue.
89 QEvt_refCtr_dec_(e); // decrement the reference counter
90 }
91
92 QS_BEGIN_PRE(QS_QF_ACTIVE_RECALL, m_prio)
93 QS_TIME_PRE(); // time stamp
94 QS_OBJ_PRE(this); // this active object
95 QS_OBJ_PRE(eq); // the deferred queue
96 QS_SIG_PRE(e->sig); // the signal of the event
99
100 QF_CRIT_EXIT();
101
102 recalled = true;
103 }
104 else {
106
107 QS_BEGIN_PRE(QS_QF_ACTIVE_RECALL_ATTEMPT, m_prio)
108 QS_TIME_PRE(); // time stamp
109 QS_OBJ_PRE(this); // this active object
110 QS_OBJ_PRE(eq); // the deferred queue
111 QS_END_PRE()
112
113 QS_CRIT_EXIT();
114
115 recalled = false;
116 }
117 return recalled;
118}
119
120//............................................................................
121std::uint_fast16_t QActive::flushDeferred(
122 QEQueue * const eq,
123 std::uint_fast16_t const num) const noexcept
124{
125 std::uint_fast16_t n = 0U;
126 while (n < num) {
127 QEvt const * const e = eq->get(m_prio);
128 if (e != nullptr) {
129 ++n; // count one more flushed event
130#if (QF_MAX_EPOOL > 0U)
131 QF::gc(e); // garbage collect
132#endif
133 }
134 else {
135 break;
136 }
137 }
138
139 return n;
140}
141
142} // namespace QP
bool defer(QEQueue *const eq, QEvt const *const e) const noexcept
Definition qf_defer.cpp:48
void postLIFO(QEvt const *const e) noexcept
Definition qf_actq.cpp:133
bool recall(QEQueue *const eq) noexcept
Definition qf_defer.cpp:69
std::uint_fast16_t flushDeferred(QEQueue *const eq, std::uint_fast16_t const num=0xFFFFU) const noexcept
Definition qf_defer.cpp:121
std::uint8_t m_prio
Definition qp.hpp:565
Native QP event queue.
Definition qequeue.hpp:53
Event class.
Definition qp.hpp:115
QSignal sig
Signal of the event (see Event Signal)
Definition qp.hpp:117
std::uint8_t poolNum_
Event pool number of this event.
Definition qp.hpp:118
std::uint8_t volatile refCtr_
Event reference counter.
Definition qp.hpp:119
void gc(QEvt const *const e) noexcept
Recycle a mutable (mutable) event.
Definition qf_dyn.cpp:188
QP/C++ framework.
Definition qequeue.hpp:36
void QEvt_refCtr_dec_(QEvt const *const e) noexcept
Decrement the refCtr of a const event (requires casting const away)
Definition qp_pkg.hpp:72
Internal (package scope) QP/C++ interface.
Sample QP/C++ port.
QS/C++ dummy public interface.
#define QS_OBJ_PRE(obj_)
Definition qs_dummy.hpp:157
#define QS_CRIT_STAT
Definition qs_dummy.hpp:164
#define QS_SIG_PRE(sig_)
Definition qs_dummy.hpp:155
#define QS_TIME_PRE()
Definition qs_dummy.hpp:154
#define QS_2U8_PRE(data1_, data2_)
Definition qs_dummy.hpp:151
#define QS_CRIT_EXIT()
Definition qs_dummy.hpp:166
#define QS_END_PRE()
Definition qs_dummy.hpp:149
#define QS_CRIT_ENTRY()
Definition qs_dummy.hpp:165
#define QS_BEGIN_PRE(rec_, qsId_)
Definition qs_dummy.hpp:148
Sample QS/C++ port.
QP Functional Safety (FuSa) Subsystem.
#define QF_CRIT_ENTRY()
Definition qsafe.h:39
#define Q_ASSERT_INCRIT(id_, expr_)
General-purpose assertion with user-specified ID number (in critical section)
Definition qsafe.h:49
#define QF_CRIT_EXIT()
Definition qsafe.h:43
#define QF_CRIT_STAT
Definition qsafe.h:35