Reference Manual For QP/C 8.1.6
Loading...
Searching...
No Matches
qf_qact.c
Go to the documentation of this file.
1//============================================================================
2// QP/C Real-Time Event Framework (RTEF)
3//
4// Copyright (C) 2005-2026 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, version 3 or later (GPL-3.0-or-later), or,
14// alternatively, under the terms of one of the closed-source Quantum Leaps
15// commercial licenses.
16//
17// Redistributions in source code must retain this top-level comment block.
18//
19// NOTE:
20// The GPL does NOT permit distributing this code as part of proprietary
21// (closed-source) programs. For such use, please contact Quantum Leaps for
22// a commercial license, which is an alternative to the GPL designed
23// explicitly for 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.h" // QP port
31#include "qp_pkg.h" // 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.h" // QS port
35 #include "qs_pkg.h" // QS facilities for pre-defined trace records
36#else
37 #include "qs_dummy.h" // disable the QS software tracing
38#endif // Q_SPY
39
40Q_DEFINE_THIS_MODULE("qf_qact")
41
42//! @static @private @memberof QActive
43QActive * QActive_registry_[QF_MAX_ACTIVE + 1U];
44
45//! @static @private @memberof QF
46QF_Attr QF_priv_;
47
48//----------------------------------------------------------------------------
49//! @protected @memberof QActive
50void QActive_ctor(QActive * const me,
51 QStateHandler const initial)
52{
53 // NOTE: QActive indirectly inherits the abstract QAsm base class,
54 // but it will delegate the state machine behavior to the QHsm class,
55 // so the following initialization is identical as in QHsm ctor:
56 QHsm_ctor((QHsm *)(me), initial);
57
58 // NOTE: this vtable is identical as QHsm, but is provided
59 // for the QActive subclass to ensure a UNIQUE vptr to distinguish
60 // subclasses of QActive (e.g., in the debugger).
61 static struct QAsmVtable const vtable = { // QActive virtual table
62 &QHsm_init_,
63 &QHsm_dispatch_,
64 &QHsm_isIn_,
65 &QHsm_getStateHandler_
66 };
67 me->super.vptr = &vtable; // hook vptr to QActive vtable
68
69 me->prio = 0U; // QF-prio. (assigned when the AO is started)
70 me->pthre = 0U; // preemption-threshold (assigned when the AO is started)
71}
72
73//............................................................................
74//! @private @memberof QActive
75void QActive_register_(QActive * const me) {
78
79 if (me->pthre == 0U) { // preemption-threshold not defined?
80 me->pthre = me->prio; // apply the default
81 }
82
83 // AO's prio. must be in range
84 Q_REQUIRE_INCRIT(100, (0U < me->prio) && (me->prio <= QF_MAX_ACTIVE));
85
86 // the AO must NOT be registered already
88
89 // the AO's prio. must not exceed the preemption threshold
90 Q_REQUIRE_INCRIT(130, me->prio <= me->pthre);
91
92 // the AO's preemption threshold must be in range
94
95#ifndef Q_UNSAFE
96 uint8_t prev_thre = me->pthre;
97 uint8_t next_thre = me->pthre;
98
99 for (uint8_t p = me->prio - 1U; p > 0U; --p) {
100 if (QActive_registry_[p] != (QActive *)0) {
101 prev_thre = QActive_registry_[p]->pthre;
102 break;
103 }
104 }
105 for (uint8_t p = me->prio + 1U; p <= QF_MAX_ACTIVE; ++p) {
106 if (QActive_registry_[p] != (QActive *)0) {
107 next_thre = QActive_registry_[p]->pthre;
108 break;
109 }
110 }
111
112 // the preemption threshold of this AO must be between
113 // preemption threshold of the previous AO and next AO
114 Q_ASSERT_INCRIT(160,
115 (prev_thre <= me->pthre) && (me->pthre <= next_thre));
116
117#endif // Q_UNSAFE
118
119 // register the AO at the QF-prio.
120 QActive_registry_[me->prio] = me;
121
122 QF_CRIT_EXIT();
123}
124
125//............................................................................
126//! @private @memberof QActive
127void QActive_unregister_(QActive * const me) {
130
131 uint8_t const p = me->prio; // put AO's prio. in a temporary
132
133 // AO's prio. must be in range
134 Q_REQUIRE_INCRIT(210, (0U < p) && (p <= QF_MAX_ACTIVE));
135
136 // this AO must be registered at prio. p
137 Q_REQUIRE_INCRIT(230, me == QActive_registry_[p]);
138
139 me->super.state.fun = Q_STATE_CAST(0); // invalidate the state
140 QActive_registry_[p] = (QActive *)0; // free-up the prio. level
141
142 QF_CRIT_EXIT();
143}
144
145//----------------------------------------------------------------------------
146//! @static @public @memberof QF
147QPrioSpec QF_prioSpec(uint8_t const prio, uint8_t const pthre) {
148 // combine the QF prio. preemption-threshold pthre in the upper byte
149 return (QPrioSpec)((uint32_t)prio | ((uint32_t)pthre << 8U));
150}
151
152//----------------------------------------------------------------------------
153#ifndef QF_LOG2
154uint_fast8_t QF_LOG2(QPSetBits const bitmask) {
155 // look-up table for log2(0..15)
156 static uint8_t const log2LUT[16] = {
157 0U, 1U, 2U, 2U, 3U, 3U, 3U, 3U,
158 4U, 4U, 4U, 4U, 4U, 4U, 4U, 4U
159 };
160 uint_fast8_t n = 0U;
161 QPSetBits x = bitmask;
162 QPSetBits tmp; // temporary for modified bitmask parameter
163
164#if (QF_MAX_ACTIVE > 16U)
165 tmp = (x >> 16U);
166 if (tmp != 0U) { // x > 2^16?
167 n += 16U;
168 x = tmp;
169 }
170#endif
171#if (QF_MAX_ACTIVE > 8U)
172 tmp = (x >> 8U);
173 if (tmp != 0U) { // x > 2^8?
174 n += 8U;
175 x = tmp;
176 }
177#endif
178 tmp = (x >> 4U);
179 if (tmp != 0U) { // x > 2^4?
180 n += 4U;
181 x = tmp;
182 }
183 // x is guaranteed to be in the 0..15 range for the look-up
184 return (uint_fast8_t)(n + log2LUT[x]);
185}
186#endif // ndef QF_LOG2
187
188//----------------------------------------------------------------------------
189//! @public @memberof QPSet
190void QPSet_setEmpty(QPSet * const me) {
191 me->bits0 = 0U; // clear bitmask for elements 1..32
192#if (QF_MAX_ACTIVE > 32)
193 me->bits1 = 0U; // clear bitmask for elements 33..64
194#endif
195}
196//............................................................................
197//! @public @memberof QPSet
198bool QPSet_isEmpty(QPSet const * const me) {
199#if (QF_MAX_ACTIVE <= 32U)
200 return (me->bits0 == 0U); // check only bitmask for elements 1..32
201#else
202 return (me->bits0 == 0U) // bitmask for elements 1..32 empty?
203 ? (me->bits1 == 0U) // check bitmask for elements 33..64
204 : false; // the set is NOT empty
205#endif
206}
207//............................................................................
208//! @public @memberof QPSet
209bool QPSet_notEmpty(QPSet const * const me) {
210#if (QF_MAX_ACTIVE <= 32U)
211 return (me->bits0 != 0U); // check only bitmask for elements 1..32
212#else
213 return (me->bits0 != 0U) // bitmask for elements 1..32 not empty?
214 ? true // the set is NOT empty
215 : (me->bits1 != 0U); // check bitmask for elements 33..64
216#endif
217}
218//............................................................................
219//! @public @memberof QPSet
220bool QPSet_hasElement(QPSet const * const me, uint_fast8_t const n) {
221#if (QF_MAX_ACTIVE <= 32U)
222 // check the bit only in bitmask for elements 1..32
223 return ((uint_fast32_t)me->bits0 & ((uint_fast32_t)1U << (n - 1U))) != 0U;
224#else
225 return (n <= 32U) // which group of elements (1..32 or 33..64)?
226 ? (((uint_fast32_t)me->bits0 & ((uint_fast32_t)1U << (n - 1U))) != 0U)
227 : ((me->bits1 & ((QPSetBits)1U << (n - 33U))) != 0U);
228#endif
229}
230//............................................................................
231//! @public @memberof QPSet
232void QPSet_insert(QPSet * const me, uint_fast8_t const n) {
233#if (QF_MAX_ACTIVE <= 32U)
234 // set the bit only in bitmask for elements 1..32
235 me->bits0 = (QPSetBits)((uint_fast32_t)me->bits0
236 | ((uint_fast32_t)1U << (n - 1U)));
237#else
238 if (n <= 32U) { // set the bit in the bitmask for elements 1..32?
239 me->bits0 = (QPSetBits)((uint_fast32_t)me->bits0
240 | ((uint_fast32_t)1U << (n - 1U)));
241 }
242 else { // set the bit in the bitmask for elements 33..64
243 me->bits1 = (me->bits1 | ((QPSetBits)1U << (n - 33U)));
244 }
245#endif
246}
247//............................................................................
248//! @public @memberof QPSet
249void QPSet_remove(QPSet * const me, uint_fast8_t const n) {
250#if (QF_MAX_ACTIVE <= 32U)
251 // clear the bit only in bitmask for elements 1..32
252 me->bits0 = (QPSetBits)((uint_fast32_t)me->bits0
253 & ~((uint_fast32_t)1U << (n - 1U)));
254#else
255 if (n <= 32U) { // clear the bit in the bitmask for elements 1..32?
256 me->bits0 = (QPSetBits)((uint_fast32_t)me->bits0
257 & ~((uint_fast32_t)1U << (n - 1U)));
258 }
259 else { // clear the bit in the bitmask for elements 33..64
260 (me->bits1 = (me->bits1 & ~((QPSetBits)1U << (n - 33U))));
261 }
262#endif
263}
264//............................................................................
265//! @public @memberof QPSet
266uint_fast8_t QPSet_findMax(QPSet const * const me) {
267#if (QF_MAX_ACTIVE <= 32U)
268 // check only the bitmask for elements 1..32
269 return QF_LOG2(me->bits0);
270#else
271 return (me->bits1 != 0U) // bitmask for elements 33..64 not empty?
272 ? (32U + QF_LOG2(me->bits1)) // 32 + log2(bits 33..64)
273 : (QF_LOG2(me->bits0)); // log2(bits 1..32)
274#endif
275}
QPrioSpec QF_prioSpec(uint8_t const prio, uint8_t const pthre)
Create a QPrioSpec object to specify priority of an AO or a thread.
Definition qf_qact.c:147
#define Q_STATE_CAST(handler_)
Perform cast to QStateHandler.
Definition qp.h:172
uint32_t QPSetBits
Bitmask for the internal representation of QPSet elements.
Definition qp.h:397
QState(*) QStateHandler(void *const me, QEvt const *const e)
Pointer to a state-handler function \clearpage .
Definition qp.h:142
uint16_t QPrioSpec
Priority specification for Active Objects in QP.
Definition qp.h:384
#define QF_MAX_ACTIVE
Maximum # Active Objects in the system (1..64).
Definition qp_config.h:57
QP/C Framework in C internal (package-scope) interface.
Super-template of the QP/C port (port interface reference).
#define QF_CRIT_ENTRY()
Port-specific critical section entry.
Definition qp_port.h:97
#define QF_LOG2(n_)
Port-specific integer log-base-2 of a 32-bit bitmask.
Definition qp_port.h:127
#define QF_CRIT_EXIT()
Port-specific critical section exit.
Definition qp_port.h:98
#define QF_CRIT_STAT
Define the critical section status that was present before entering the critical section.
Definition qp_port.h:96
QS (QP/Spy software tracing) internal (package-scope) interface.
Super-template of the QS port.
QP Functional Safety (FuSa) Subsystem (fault detection).
#define Q_ASSERT_INCRIT(id_, expr_)
General-purpose assertion with user-specified ID number (in critical section).
Definition qsafe.h:54
#define Q_REQUIRE_INCRIT(id_, expr_)
Assertion for checking a precondition (in critical section).
Definition qsafe.h:98
Active Object class (based on the QHsm implementation strategy).
Definition qp.h:446
void QActive_unregister_(QActive *const me)
Un-register the active object from the framework.
Definition qf_qact.c:127
void QActive_ctor(QActive *const me, QStateHandler const initial)
QActive constructor (abstract base class).
Definition qf_qact.c:50
void QActive_register_(QActive *const me)
Register this active object to be managed by the framework.
Definition qf_qact.c:75
QActive * QActive_registry_[QF_MAX_ACTIVE+1U]
Static (one per-class) array of registered active objects.
Definition qp_pkg.h:38
uint8_t prio
QF-priority [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:448
uint8_t pthre
Preemption-threshold [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:449
union QAsmAttr state
Current state (pointer to the current state-handler function) \clearpage .
Definition qp.h:180
Virtual table for the QAsm class.
Definition qp.h:208
Private attributes of the QF framework.
Definition qp_pkg.h:66
Hierarchical State Machine class (QHsm-style implementation strategy).
Definition qp.h:240
Set of Active Objects of up to QF_MAX_ACTIVE elements.
Definition qp.h:406
void QPSet_remove(QPSet *const me, uint_fast8_t const n)
Remove element n from the priority-set (n = 1..QF_MAX_ACTIVE).
Definition qf_qact.c:249
bool QPSet_isEmpty(QPSet const *const me)
Find out whether the priority-set is empty.
Definition qf_qact.c:198
bool QPSet_notEmpty(QPSet const *const me)
Find out whether the priority-set is NOT empty.
Definition qf_qact.c:209
void QPSet_insert(QPSet *const me, uint_fast8_t const n)
Insert element n into the priority-set (n = 1..QF_MAX_ACTIVE).
Definition qf_qact.c:232
void QPSet_setEmpty(QPSet *const me)
Make the priority set empty \clearpage .
Definition qf_qact.c:190
bool QPSet_hasElement(QPSet const *const me, uint_fast8_t const n)
Find out whether the priority-set has element n.
Definition qf_qact.c:220
QPSetBits bits0
Bitmask for elements 1..32 \clearpage .
Definition qp.h:408
QPSetBits bits1
Bitmask for elements 33..64 \clearpage .
Definition qp.h:411
uint_fast8_t QPSet_findMax(QPSet const *const me)
Find the maximum element in the (non-empty) priority-set.
Definition qf_qact.c:266