QP/C  8.0.3
Real-Time Event Framework
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 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.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//............................................................................
43//! @protected @memberof QActive
44void QActive_ctor(QActive * const me,
45 QStateHandler const initial)
46{
47 // clear the whole QActive object, so that the framework can start
48 // correctly even if the startup code fails to clear the uninitialized
49 // data (as is required by the C Standard).
50 QF_bzero_(me, sizeof(*me));
51
52 // NOTE: QActive inherits the abstract QAsm class, but it calls the
53 // constructor of the QHsm subclass. This is because QActive inherits
54 // the behavior from the QHsm subclass.
55 QHsm_ctor((QHsm *)(me), initial);
56
57 // NOTE: this vtable is identical as QHsm, but is provided
58 // for the QActive subclass to provide a UNIQUE vptr to distinguish
59 // subclasses of QActive (e.g., in the debugger).
60 static struct QAsmVtable const vtable = { // QActive virtual table
61 &QHsm_init_,
62 &QHsm_dispatch_,
63 &QHsm_isIn_
64#ifdef Q_SPY
65 ,&QHsm_getStateHandler_
66#endif
67 };
68 me->super.vptr = &vtable; // hook vptr to QActive vtable
69}
70
71//............................................................................
72//! @private @memberof QActive
73void QActive_register_(QActive * const me) {
76
77 if (me->pthre == 0U) { // preemption-threshold not defined?
78 me->pthre = me->prio; // apply the default
79 }
80
81#ifndef Q_UNSAFE
83 (0U < me->prio) && (me->prio <= QF_MAX_ACTIVE)
84 && (QActive_registry_[me->prio] == (QActive *)0)
85 && (me->prio <= me->pthre));
86
87 uint8_t prev_thre = me->pthre;
88 uint8_t next_thre = me->pthre;
89
90 for (uint_fast8_t p = (uint_fast8_t)me->prio - 1U;
91 p > 0U;
92 --p)
93 {
94 if (QActive_registry_[p] != (QActive *)0) {
95 prev_thre = QActive_registry_[p]->pthre;
96 break;
97 }
98 }
99 for (uint_fast8_t p = (uint_fast8_t)me->prio + 1U;
100 p <= QF_MAX_ACTIVE;
101 ++p)
102 {
103 if (QActive_registry_[p] != (QActive *)0) {
104 next_thre = QActive_registry_[p]->pthre;
105 break;
106 }
107 }
108
109 Q_ASSERT_INCRIT(190,
110 (prev_thre <= me->pthre)
111 && (me->pthre <= next_thre));
112#endif // Q_UNSAFE
113
114 // register the AO at the QF-prio.
115 QActive_registry_[me->prio] = me;
116
117 QF_CRIT_EXIT();
118}
119
120//............................................................................
121//! @private @memberof QActive
122void QActive_unregister_(QActive * const me) {
123 uint_fast8_t const p = (uint_fast8_t)me->prio;
124
127
128 Q_REQUIRE_INCRIT(200, (0U < p) && (p <= QF_MAX_ACTIVE)
129 && (QActive_registry_[p] == me));
130 QActive_registry_[p] = (QActive *)0; // free-up the prio. level
131 me->super.state.fun = Q_STATE_CAST(0); // invalidate the state
132
133 QF_CRIT_EXIT();
134}
#define Q_STATE_CAST(handler_)
Perform cast to QStateHandler.
Definition qp.h:172
QState(* QStateHandler)(void *const me, QEvt const *const e)
Pointer to a state-handler function.
Definition qp.h:145
#define QF_MAX_ACTIVE
Maximum # Active Objects in the system (1..64)
Definition qp_config.h:123
Internal (package scope) QP/C interface.
Sample QP/C port.
QS/C dummy public interface.
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 Q_REQUIRE_INCRIT(id_, expr_)
Assertion for checking a precondition (in critical section)
Definition qsafe.h:86
#define QF_CRIT_STAT
Definition qsafe.h:35
Active object class (based on the QHsm implementation strategy)
Definition qp.h:504
QAsm super
Definition qp.h:505
void QActive_unregister_(QActive *const me)
Un-register the active object from the framework.
Definition qf_qact.c:122
void QActive_register_(QActive *const me)
Register this active object to be managed by the framework.
Definition qf_qact.c:73
QActive * QActive_registry_[QF_MAX_ACTIVE+1U]
Static (one per-class) array of registered active objects.
Definition qp.h:523
uint8_t prio
QF-priority [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:506
uint8_t pthre
Preemption-threshold [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:507
union QAsmAttr state
Current state (pointer to the current state-handler function)
Definition qp.h:180
Virtual table for the QAsm class.
Definition qp.h:221
Hierarchical State Machine class (QHsm-style state machine implementation strategy)
Definition qp.h:251
QStateHandler fun
Definition qp.h:165