QP/C 8.1.2
Real-Time Event Framework
Loading...
Searching...
No Matches
qv.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 internal 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
40// protection against including this source file in a wrong project
41#ifndef QV_H_
42 #error Source file included in a project NOT based on the QV kernel
43#endif // QV_H_
44
45Q_DEFINE_THIS_MODULE("qv")
46
47//............................................................................
49
50//............................................................................
51//! @static @public @memberof QV
52void QV_schedDisable(uint8_t const ceiling) {
55
56 if (ceiling > QV_priv_.schedCeil) { // raising the scheduler ceiling?
57
59 QS_TIME_PRE(); // timestamp
60 // the previous sched ceiling & new sched ceiling
61 QS_2U8_PRE(QV_priv_.schedCeil,
62 (uint8_t)ceiling);
64
65 QV_priv_.schedCeil = (uint8_t)ceiling;
66 }
68}
69
70//............................................................................
71//! @static @public @memberof QV
72void QV_schedEnable(void) {
75
76 if (QV_priv_.schedCeil != 0U) { // actually enabling the scheduler?
77
79 QS_TIME_PRE(); // timestamp
80 // current sched ceiling (old), previous sched ceiling (new)
81 QS_2U8_PRE(QV_priv_.schedCeil, 0U);
83
84 QV_priv_.schedCeil = 0U;
85 }
87}
88
89//............................................................................
90//! @static @public @memberof QF
91void QF_init(void) {
92#ifndef Q_UNSAFE
93 QTimeEvt_init(); // initialize QTimeEvts
94#endif // Q_UNSAFE
95
96#ifdef QV_INIT
97 QV_INIT(); // port-specific initialization of the QV kernel
98#endif
99}
100
101//............................................................................
102//! @static @public @memberof QF
103void QF_stop(void) {
104 QF_onCleanup(); // application-specific cleanup callback
105 // nothing else to do for the QV kernel
106}
107
108//............................................................................
109//! @static @public @memberof QF
110int_t QF_run(void) {
112#ifdef Q_SPY
113 // produce the QS_QF_RUN trace record
114 QS_beginRec_((uint_fast8_t)QS_QF_RUN);
115 QS_endRec_();
116#endif // Q_SPY
117
118#ifdef QV_START
119 QV_START(); // port-specific startup of the QV kernel
120#endif
121
122#if (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
123 uint_fast8_t pprev = 0U; // previous prio.
124
125#ifdef QF_ON_CONTEXT_SW
126 // officially switch to the idle cotext
127 QF_onContextSw((QActive *)0, (QActive *)0);
128#endif // def QF_ON_CONTEXT_SW
129
130#endif // (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
131
133
134 QF_onStartup(); // app. callback: configure and enable interrupts
135
137 for (;;) { // QV event loop...
138 // find the maximum prio. AO ready to run
139 uint_fast8_t const p = (QPSet_notEmpty(&QV_priv_.readySet)
140 ? QPSet_findMax(&QV_priv_.readySet)
141 : 0U);
142
143 if (p > QV_priv_.schedCeil) { // is it above the sched ceiling?
144 QActive * const a = QActive_registry_[p];
145
146#if (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
147 if (p != pprev) { // changing threads?
148
149 QS_BEGIN_PRE(QS_SCHED_NEXT, p)
150 QS_TIME_PRE(); // timestamp
151 QS_2U8_PRE((uint8_t)p,
152 (uint8_t)pprev);
153 QS_END_PRE()
154
155#ifdef QF_ON_CONTEXT_SW
156 QF_onContextSw(((pprev != 0U)
157 ? QActive_registry_[pprev]
158 : (QActive *)0), a);
159#endif // QF_ON_CONTEXT_SW
160
161 pprev = p; // update previous prio.
162 }
163#endif // (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
164
166
167 QEvt const * const e = QActive_get_(a);
168
169 // dispatch event (virtual call)
170 (*a->super.vptr->dispatch)(&a->super, e, p);
171#if (QF_MAX_EPOOL > 0U)
172 QF_gc(e);
173#endif
175
176 if (a->eQueue.frontEvt == (QEvt *)0) { // empty queue?
177 QPSet_remove(&QV_priv_.readySet, p);
178 }
179 }
180 else { // no AO ready to run --> idle
181#if (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
182 if (pprev != 0U) {
184 QS_TIME_PRE(); // timestamp
185 QS_U8_PRE((uint8_t)pprev);
186 QS_END_PRE()
187
188#ifdef QF_ON_CONTEXT_SW
189 QF_onContextSw(QActive_registry_[pprev], (QActive *)0);
190#endif // QF_ON_CONTEXT_SW
191
192 pprev = 0U; // update previous prio.
193 }
194#endif // (defined QF_ON_CONTEXT_SW) || (defined Q_SPY)
195
196 // QV_onIdle() must be called with interrupts DISABLED because
197 // the determination of the idle condition can change at any time
198 // by an interrupt posting events to a queue.
199 //
200 // NOTE: QV_onIdle() MUST enable interrupts internally, ideally
201 // atomically with putting the CPU into a power-saving mode.
202 QV_onIdle();
203
204 QF_INT_DISABLE(); // disable interrupts before looping back
205 }
206 }
207}
208
209//----------------------------------------------------------------------------
210//! @public @memberof QActive
211void QActive_start(QActive * const me,
212 QPrioSpec const prioSpec,
213 QEvtPtr * const qSto,
214 uint_fast16_t const qLen,
215 void * const stkSto,
216 uint_fast16_t const stkSize,
217 void const * const par)
218{
219 Q_UNUSED_PAR(stkSto); // not needed in QV
220 Q_UNUSED_PAR(stkSize); // not needed in QV
221
224
225 // the VPTR for this AO must be valid
226 Q_REQUIRE_INCRIT(300, me->super.vptr != (struct QAsmVtable *)0);
227
228 // stack storage must NOT be provided for the AO (QV does not need it)
229 Q_REQUIRE_INCRIT(310, stkSto == (void *)0);
230
231 QF_CRIT_EXIT();
232
233 me->prio = (uint8_t)(prioSpec & 0xFFU); // QF-prio.
234 me->pthre = 0U; // not used
235 QActive_register_(me); // make QF aware of this AO
236
237 QEQueue_init(&me->eQueue, qSto, qLen);
238
239 // top-most initial tran. (virtual call)
240 (*me->super.vptr->init)(&me->super, par, me->prio);
241 QS_FLUSH(); // flush the trace buffer to the host
242}
#define Q_UNUSED_PAR(par_)
Helper macro to mark unused parameters of functions.
Definition qp.h:90
QEvt const * QEvtPtr
Pointer to const event instances passed around in QP/C Framework.
Definition qp.h:133
int int_t
Alias for assertion-ID numbers in QP assertions and return from QF_run().
Definition qp.h:87
uint16_t QPrioSpec
Priority specification for Active Objects in QP.
Definition qp.h:383
QP/C Framework in C internal (package-scope) interface.
Sample QP/C port.
#define QF_INT_DISABLE()
Port-specific interrupt disable.
Definition qp_port.h:56
#define QF_INT_ENABLE()
Port-specific interrupt enable.
Definition qp_port.h:64
#define QS_TIME_PRE()
Definition qs.h:340
@ QS_QF_RUN
QF_run() was entered.
Definition qs.h:152
@ QS_SCHED_IDLE
scheduler restored the idle task
Definition qs.h:129
@ QS_SCHED_LOCK
scheduler was locked
Definition qs.h:126
@ QS_SCHED_UNLOCK
scheduler was unlocked
Definition qs.h:127
#define QS_FLUSH()
Definition qs.h:262
QS (QP/Spy software tracing) internal (package-scope) interface.
#define QS_2U8_PRE(data1_, data2_)
Output two pre-formatted unsigned 8-bit integer data elements.
Definition qs_pkg.h:41
#define QS_U8_PRE(data_)
Output pre-formatted unsigned 8-bit integer data element.
Definition qs_pkg.h:40
#define QS_END_PRE()
Pre-formatted QS trace record end.
Definition qs_pkg.h:38
#define QS_BEGIN_PRE(rec_, qsId_)
Pre-formatted QS trace record begin.
Definition qs_pkg.h:32
Sample QS/C port.
QP Functional Safety (FuSa) Subsystem.
#define QF_CRIT_ENTRY()
Definition qsafe.h:40
#define QF_CRIT_EXIT()
Definition qsafe.h:44
#define Q_REQUIRE_INCRIT(id_, expr_)
Assertion for checking a precondition (in critical section).
Definition qsafe.h:98
#define QF_CRIT_STAT
Definition qsafe.h:36
int_t QF_run(void)
Definition qutest.c:215
void QF_init(void)
Definition qutest.c:138
void QF_stop(void)
Definition qutest.c:143
QV QV_priv_
Definition qv.c:48
Active object class (based on the QHsm implementation strategy).
Definition qp.h:447
QAsm super
Definition qp.h:448
QACTIVE_EQUEUE_TYPE eQueue
Port-dependent event-queue type (often QEQueue).
Definition qp.h:461
void QActive_register_(QActive *const me)
Register this active object to be managed by the framework.
Definition qf_qact.c:72
uint8_t prio
QF-priority [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:449
void QActive_start(QActive *const me, QPrioSpec const prioSpec, QEvtPtr *const qSto, uint_fast16_t const qLen, void *const stkSto, uint_fast16_t const stkSize, void const *const par)
Starts execution of an active object and registers the object with the framework.
Definition qv.c:211
uint8_t pthre
Preemption-threshold [1..QF_MAX_ACTIVE] of this AO.
Definition qp.h:450
struct QAsmVtable const * vptr
Virtual pointer inherited by all QAsm subclasses (see also SAS_QP_OOA).
Definition qp.h:179
Virtual table for the QAsm class.
Definition qp.h:208
void(* init)(QAsm *const me, void const *const e, uint_fast8_t const qsId)
Virtual function to take the top-most initial transition in the state machine.
Definition qp.h:209
Event class.
Definition qp.h:100
QV non-preemptive kernel.
Definition qv.h:34
void QV_schedDisable(uint8_t const ceiling)
QV selective scheduler disable.
Definition qv.c:52
void QV_schedEnable(void)
QV scheduler enable.
Definition qv.c:72
QV QV_priv_
Definition qv.h:49