Reference Manual For QP/C 8.1.6
Loading...
Searching...
No Matches
Interaction Viewpoint

Structure ViewpointTime Viewpoint

Design Viewpoint


[SDS_QP_INT]

Design viewpoint: Interaction.

Purpose

The Interaction Design Viewpoint describes the interaction mechanisms provided by the QP/C Framework component. The specific interactions between application-defined Active Objects are outside the scope of this SDS and are defined by the system integrator.

Design Concerns

  • behavior of the system under various scenarios of use
  • interactions in terms of events exchanged among Active Objects
  • required sequences of operations
  • control flow
  • concurrency and parallelism
  • resource management

Design Views: Behavior


[SDS_QP_START]

Design view: QP/C Framework startup sequence.

Model Kinds

The QP/C Framework startup sequence is illustrated with two kinds of models:

Figure SDS-START: QP/C Framework startup sequence.


[0] int main(int argc, char *argv[]) {
[1] QF_init(); // initialize the QF Active Object framework
[2] BSP_init(((argc > 1) ? argv[1] : (void*)0); // initialize the BSP
[3] return QF_run(); // run the application
}
[4] void BSP_init(void const * const arg) {
. . .
[5] // initialize QS software tracing...
if (!QS_INIT(arg)) {
}
. . .
[6] // initialize publish-subscribe...
static QSubscrList subscrSto[MAX_PUB_SIG];
QActive_psInit(subscrSto, Q_DIM(subscrSto));
[7] // initialize event pools...
static QF_MPOOL_EL(SmallEvt) smlPoolSto[10];
QF_poolInit(smlPoolSto, sizeof(smlPoolSto), sizeof(smlPoolSto[0]));
static QF_MPOOL_EL(MediumEvt) medPoolSto[5];
QF_poolInit(medPoolSto, sizeof(medPoolSto), sizeof(medPoolSto[0]));
. . .
[8] // instantiate and start Active Objects...
ActiveA_ctor();
static QEvt const *aoA_QueueSto[5];
QActive_start(AO_activeA,
2U, // QP priority of the AO
aoA_QueueSto, // event queue storage
Q_DIM(aoA_QueueSto), // queue length [events]
(void *)0, 0U, // no stack storage
(void *)0); // no initialization param
ActiveB_ctor();
static QEvt const *aoB_QueueSto[3];
QActive_start(AO_activeB,
5U, // QP priority of the AO
aoB_QueueSto, // event queue storage
Q_DIM(aoB_QueueSto), // queue length [events]
(void *)0, 0U, // no stack storage
(void *)0); // no initialization param
. . .
}
#define QF_MPOOL_EL(evType_)
Memory pool element to allocate correctly aligned storage for QMPool class.
Definition qmpool.h:36
#define Q_DIM(array_)
Helper macro to calculate static dimension of a 1-dim array.
Definition qp.h:86
#define QS_INIT(arg_)
Initialize the QS software tracing system.
Definition qs.h:271
#define Q_ERROR()
Assertion for a wrong path through the code (with ID provided in LINE).
Definition qsafe.h:94
Event class.
Definition qp.h:95
Subscriber List (for publish-subscribe).
Definition qp.h:437
Listing SDS-START: QP/C Application startup sequence.


Description

The following descriptions pertain to both Figure SDS-START and Listing SDS-START because the steps are labeled consistently in these two model kinds.

[0] The QP/C Application startup sequence occurs in the main() function.

[1] The Active Object framework layer gets initialized, which also performs initialization of the real-time kernel.

[2] The Board Support Package (BSP) initialization sets up the hardware, software tracing (if used), etc.

[3] The QP/C Application transfers control to QF Active Object Framework by calling QF_run(). QF_run() begins by calling the QF_onStartup() callback to configure and enable interrupts (Figure SDS-START [6A]), as the system is only now ready to receive them. After this, QF Framework starts execution of Active Objects (Figure SDS-START [6B]), which typically involves transferring control to the underlying real-time kernel.

Note
In deeply embedded applications QF_run() does not return. In case QP runs on top of a General-Purpose OS (e.g., POSIX), QF_run() might return to the OS. The behavior of QF_run() depends on the QP Port (see QP OSAL).

[4] The Board Support Package (BSP) initialzation

[5] QS target-resident software component is initialized

[6] If this QP/C Application uses publish-subscribe, it is initialized with a call to QActive_psInit().

Note
The QP/C Application must provide the memory for the subscriber-lists (subscrSto), which is allocated statically in this case.

[7] The QP/C Application initializes all event pools that it is using by repeated calls to QF_poolInit().

Note
The QP/C Application must provide the memory for each of the initialized event pools (e.g., medPoolSto), which is allocated statically in this case.

[8] The QP/C Application instantiates (by calling the constructor) and starts all Active Objects by calling QActive_start(). Each Active Object is assigned unique priorities, and provided with event-queue buffers and the stacks (if required). Also, this step involves executing the top-most initial transition in the Active Object (Figure SDS-START [5B]). Additionally, this step triggers the top-most initial transition in the Active Object's state machine, which might involve some interaction with the BSP to initialize the hardware controlled by this Active Object (Figure SDS-START [5C]).

Note
The QP/C Application must provide the event-queue buffer (e.g., aoA_QueueSto) and the stack (if required by the underlying kernel) for each started Active Object.

Backward Traceability

  • [SAS_QP_RES_00] Architectural view: General QP/C Framework component memory allocation policy.
  • [SAS_QP_RES_01] Architectural view: Active Object memory allocation policy and responsibilities.
  • [SAS_QP_RES_10] Architectural view: Event management policy in QP/C Framework.

Forward Traceability

  • [SSM_QP_ASR_405] QP/C Application shall start all Active Objects before transferring control to the QP/C Framework.
  • [SSM_QP_ASR_501] QP/C Application must initialize the event pools correctly and adequately.
  • [SSM_QP_ASR_502] QP/C Application must initialize the publish-subscribe correctly and adequately.
  • [TUN_QP_qk] Unit test for qk: Preemptive QK kernel implementation
  • [TUN_QP_qv] Unit test for qv: Non-preemptive QV kernel implementation
  • [TIN_QP_ao] Integration test suite for Active Objects and direct event posting.
  • [QActive::QActive_psInit()] Initializes the publish-subscribe facility for the application.
  • [QF::QF_init()] QF initialization
  • [QF::QF_run()] Transfers control to QF to run the application
  • [QF::QF_onStartup()] Startup callback
  • [QF::QF_poolInit()] Event pool initialization for dynamic allocation of events

Event Exchange View


[SDS_QP_POST]

Design view: QP/C event posting sequence.

Model Kinds

Event posting to Active Objects is illustrated with UML sequence diagrams shown in Figure SDS-POST1 and Figure SDS-POST2 (dynamic views). The views are explained in the labeled descriptions following the sequence diagrams. The chosen scenarios pertain to mutable events dynamically allocated from event pools. Also, the shown order of processing corresponds to a preemptive scheduler.

Scenario: Event posting from ISR to Active Object

Figure SDS-POST1: Posting event from ISR to an Active Object.


[1] Event posting begins with incrementing the reference counter of the event (for mutable events dynamically allocated from event pools), which happens within a critical section.

[2] The event is posted to the event queue of the recipient Active Object. The default behavior of the queue is to assert internally that the queue does not overflow and can accept the event (this is part of the event delivery guarantee).

[2A] Assuming that the recipient Active Object didn't have events before, adding an event to its queue makes the Active Object ready to run. However, the Active Object is not assigned the CPU just yet because the ISR has a higher priority and continues to completion. (NOTE: the same behavior would occur if the event were posted from an Active Object of a higher priority than the recipient.)

[3] Only after the ISR completes, the received event is dispatched to the internal state machine of the recipient Active Object. The virtual dispatch() function runs-to-completion (RTC) in the thread context of the recipient Active Object.

[4] After the RTC step, the event is garbage-collected, which decrements the reference counter (for a mutable dynamic event). The event is recycled back to the original event pool only when the reference count drops to zero.

Scenario: Event posting from low-priority to high-priority Active Object

Figure SDS-POST2: Posting event from a lower-priority Active Object to a higher-priority Active Object (preemptive scheduler).


[1] As before in Figure SDS-POST1, event posting begins with incrementing the reference counter of the event (for mutable events dynamically allocated from event pools), which happens within a critical section.

[2] As before, the event is posted to the event queue of the recipient Active Object.

[2A] However, assuming that the system executes under a preemptive, priority-based scheduler, the recipient Active Object immediately preempts the sender Active Object. This happens because the recipient has a higher priority than the sender, and a preemptive scheduler must always give control to the highest-priority Active Object ready to run.

[3] The recipient Active Object dispatches the event to its internal state machine.

[4] The recipient Active Object calls the garbage-collector to decrement the reference count and recycles the event back to the original event pool.

[5] The CPU is assigned back to the preempted sender Active Object. The sender completes its RTC step.

Note
The sender Active Object should NOT access the event after posing because it might be recycled by that time, as illustrated in this scenario. Please see also the dynamic event life-cycle in @ Figure SRS-EVT-LIFE.

Backward Traceability

  • [SAS_QP_RES_10] Architectural view: Event management policy in QP/C Framework.
  • [SAS_QP_IFC_01B] Architectural view: QP/C Framework component API.

Forward Traceability

  • [TUN_QP_qf_actq8] Unit test for qf_actq: QActive native event queue implementation based on QEQueue (8-bit queue counters).
  • [TUN_QP_qf_actq16] Unit test for qf_actq: QActive native event queue implementation based on QEQueue (16-bit queue counters).
  • [TIN_QP_ao] Integration test suite for Active Objects and direct event posting.
  • [QActive::QActive_post_()] Posts an event e directly to the event queue of the active object using the First-In-First-Out (FIFO) policy
  • [QActive::QActive_postLIFO_()] Posts an event e directly to the event queue of the active object using the Last-In-First-Out (LIFO) policy

[SDS_QP_QUEUE]

Design view: Active Object event queue (native QEQueue).

Description

SafeQP realizes the event-queue operations of the OSAL API ([SDS_QP_OSAL_EQ]) only with the native event queue QEQueue and the Framework module qf_actq.c , for all ports of the built-in kernels and for the QUTest ports. In this design:

  • the complete posting operation — the check of the event, the check of the free entries against the requested margin, the insertion, and the signaling of the Active Object (QACTIVE_EQUEUE_SIGNAL_()) — executes in one critical section, so the margin check and the insertion are atomic: a posting with a margin cannot fail after the check, and a posting without a margin ([SRS_QP_REQ_310]) asserts if the queue has no free entry (assertions 130, 230);
  • the number of free entries is kept in Duplicate Inverse Storage and verified at every posting and retrieval (SSRS_QP_SREQ_211), as are the front event and the tail index;
  • the minimum number of free entries ever observed (low watermark, nMin) is updated at every posting and is available through QActive_getQueueMin() and the QS-RX queue query (SSRS_QP_SREQ_212);
  • the queue holds only pointers to events (zero-copy, [SRS_QP_REQ_380]), and the front event is kept outside the ring buffer, so a queue of length N holds N+1 events;
  • the queue never blocks: with the built-in kernels, an empty queue means that the Active Object is not in the ready set (QACTIVE_EQUEUE_WAIT_() is empty).

Backward Traceability

  • [SDS_QP_OSAL_EQ] Design view: Active Object event queue as an OSAL element.
  • [SRS_QP_REQ_310] QP/C Framework shall provide event delivery guarantee for the direct event posting mechanism.
  • [SRS_QP_REQ_380] QP/C Framework shall support automatic recycling of mutable events according to the "zero-copy" memory management policy.
  • [SSRS_QP_SREQ_211] QP/C software component shall apply self-monitoring SSFs based on "Duplicate Inverse Storage" and "Duplicate Storage" to critical variables.
  • [SSRS_QP_SREQ_212] QP/C software component shall apply self-monitoring SSFs based on "low watermark" to event queues and memory pools.

Forward Traceability


[SDS_QP_PUB]

Design view: QP/C event publishing sequence.

Model Kinds

Event publishing to Active Objects is illustrated with UML sequence diagrams shown in Figure SDS-PUB1 and Figure SDS-PUB2 (dynamic view). The view is explained in the labeled descriptions following the sequence diagram. The chosen scenarios pertain to mutable events dynamically allocated from event pools. Also, the shown order of processing corresponds to a preemptive scheduler.

Scenario: Event publishing with scheduler locking

Figure SDS-PUB1: Publishing event from a medium-priority Active Object (preemptive scheduler).


[1] Event publishing begins with incrementing the reference counter of the event (for mutable events dynamically allocated from event pools), which happens within a critical section.

[2] Next, the publish operation determines the highest-priority subscriber and selectively locks the scheduler up to that priority. (NOTE: modern real-time kernels support selective scheduler locking. Older kernels support only indiscriminate, global scheduler locking.)

[3] Event is posted to the highest-priority subscriber Active Object, which increments the reference counter of the event (per [SDS_QP_POST]).

[3A] Even if the priority of that subscriber is higher than the sender, preemption does not happen because of the scheduler lock.

[4] Event is posted to the medium-priority subscriber Active Objects, which increments the reference counter of the event (per [SDS_QP_POST]).

[5] Event is posted to the low-priority subscriber Active Objects, which increments the reference counter of the event (per [SDS_QP_POST]).

[4A-5A] The events are only posted, but the receiving Active Objects don't run yet.

[6] The publish operation unlocks the scheduler to the previous level before publishing.

[7] The highest-priority recipient Active Object immediately preempts the lower-priority publisher and dispatches the published event to its state machine.

[8] During the processing, the highest-priority Active Object posts another event e1 to ActiveObjB.

[8A] The posted event e1 is only enqueued, but is not processed.

[9] The highest-priority Active Object continues and garbage-collects the published event. This only decrements its reference counter, but does not recycle the published event.

[10] The event publisher garbage-collects the original event. This decrements the reference counter incremented in step [1].

Note
The garbage-collect step [10] prevents event leak in case there are no subscribers to that event.

[11] ActiveObjB dispatches the published event to its state machine.

[12] The event publisher garbage-collects published events. This decrements the reference counter, but does not recycle the event yet.

[13] ActiveObjB dispatches the posted event e1 to its state machine.

Remarks
ActiveObjB processes events in the expected order (based on cause and effect): published event e followed by posted event e1 (caused by the published event e).

[14] ActiveObjB garbage-collects the posted event e1, which decrements its reference counter and recycles the event.

[15] The lowest-priority ActiveObjA dispatches the published event to its state machine.

[16] The lowest-priority ActiveObjA garbage-collects the original event, which decrements its reference counter. This time, the counter drops to zero, so the event is finally recycled to its original event pool.

Scenario: Event publishing WITHOUT scheduler locking

Figure SDS-PUB2: Publishing event WITHOUT scheduler locking (preemptive scheduler).


[1] As before, event publishing begins with incrementing the reference counter of the event (for mutable events dynamically allocated from event pools), which happens within a critical section.

[2] The publish operation determines the highest-priority subscriber and posts the event to that Active Object. This increments the reference counter of the event (per [SDS_QP_POST]).

[3] However, this time the scheduler is NOT locked, so the highest-priority Active Object immediately preempts the lower-priority publisher.

[4] During the processing, the highest-priority Active Object posts another event e1 to ActiveObjB.

[4A] The posted event e1 is only enqueued, but is not processed.

[5] The highest-priority Active Object continues and garbage-collects the published event. This only decrements its reference counter, but does not recycle the published event because its reference counter has been incremented in step [1]

[6] The preemptive scheduler resumes the preempted publisher, which posts the original event to ActiveObjB.

Note
At this point, the medium-priority ActiveObjB has two events, which are enqueued in the following order: e1 followed by e. This is an unexpected order because event e1 is caused by event e, yet it precedes event e. This unexpected re-ordering of events is the result of NOT locking the scheduler.

[6A] The posted event is only enqueued, but is not processed.

[7] The preemptive scheduler posts the original event to ActiveObjC.

[7A] The posted event is only enqueued, but is not processed.

[8] The event publisher garbage-collects the original event. This decrements the reference counter incremented in step [1].

Note
The garbage-collect step [8] prevents event leak in case there are no subscribers to that event.

[9] The medium-priority ActiveObjB dispatches the event e1 to its internal state machine.

[10] The medium-priority ActiveObjB garbage-collects event e1, which causes recycling of that event.

[11] The medium-priority ActiveObjB dispatches the event e to its internal state machine.

[12] The medium-priority ActiveObjB garbage-collects published event e, which decrements its reference counter but does not recycle the event yet.

[13] The lowest-priority ActiveObjA dispatches the published event e to its internal state machine.

[14] The lowest-priority ActiveObjA garbage-collects the published event e, which is no longer referenced, so it is recycled.

Remarks
Even without scheduler locking, the event publishing performs event multicasting without event leaks. However, without scheduler locking, event publishing might end up with an unexpected re-ordering of events, as illustrated for the medium-priority ActiveObjB in this scenario.

Backward Traceability

  • [SAS_QP_RES_10] Architectural view: Event management policy in QP/C Framework.
  • [SAS_QP_IFC_01B] Architectural view: QP/C Framework component API.

Forward Traceability

  • [TUN_QP_qf_ps] Unit test for qf_ps: QActive publish-subscribe event delivery
  • [TUN_QP_qk] Unit test for qk: Preemptive QK kernel implementation
  • [TUN_QP_qv] Unit test for qv: Non-preemptive QV kernel implementation
  • [TIN_QP_ps] Integration test suite for publish-subscribe event delivery.
  • [QActive::QActive_publish_()] Publish event to all subscribers of a given signal e->sig
  • [QSubscrList] Subscriber List (for publish-subscribe)

[SDS_QP_DEFER]

Design view: QP/C event deferral sequence.

Model Kinds

Event deferral in Active Objects is illustrated with UML sequence diagram shown in Figure SDS-DEFER. The view is explained in the labeled descriptions following the sequence diagrams.

Scenario: Event deferral

The scenario shows an Active Object (AO) that receives an event it cannot handle in its current state, defers it, and later recalls it for processing. The participants are: the event producer (another AO or an ISR), the AO's own event queue, the deferred-event queue owned by the AO, and the Framework's event recycling (garbage collection).

Figure SDS-DEFER: Event deferral in an Active Object.


[1] The Active Object allocates a deferred-event queue (QEQueue) with a fixed capacity and initializes it, typically in the AO constructor. The capacity determines the maximum number of events the AO can hold in deferral at any given time.

[2] An event producer posts a (possibly mutable) event to the AO's event queue. The event's reference counter is incremented by the posting operation.

[3] The AO retrieves the event from its event queue and dispatches it to its state machine, starting a new RTC step. However, the current AO state is not ready to handle the event (e.g., the AO is busy in a transaction), so the state machine calls defer() to store the event in the deferred-event queue.

[4] defer() posts the event to the deferred-event queue (FIFO). For a mutable event, this posting increments the event's reference counter, which registers the deferred-event queue as an additional holder of the event (an immutable event is not reference-counted). If the deferred-event queue is full, the event is not stored and defer() returns 'false', so the application can detect and handle the overflow.

[5] The RTC step ends and the Framework attempts to recycle the event (garbage collection), which decrements the event's reference counter. Because of the reference added in step [4], the counter does not drop to zero, so the deferred event is not recycled and remains valid.

[6] Later, in a subsequent RTC step, the AO reaches a state in which it is ready to handle the deferred event(s) (e.g., the transaction is complete), and calls recall().

[7] recall() gets the oldest event from the deferred-event queue. If the deferred-event queue is empty, recall() returns 'false' and no further action is taken.

[8] recall() posts the recalled event to the AO's own event queue using the LIFO (Last-In-First-Out) policy, which inserts the event at the front of the queue. Consequently, the recalled event is processed next, ahead of any other events already waiting in the queue. For a mutable event, this posting increments the event's reference counter, and recall() then decrements it once to release the reference held by the deferred-event queue (added in step [4]). Thereafter, the AO's event queue is the holder of the event.

[9] The next RTC step in the Active Object is guaranteed to process the recalled event.

[10] The recalled event is recycled by the end of the RTC step as any other event for the Active Object.

Note
Recalling multiple deferred events with consecutive LIFO postings in the same RTC step would reverse their original order. Therefore, the AO typically recalls one deferred event per RTC step, and recalls the next one only after the previously recalled event has been processed.

Backward Traceability

  • [SRS_QP_REQ_080] Active Object abstraction shall provide support for event deferral.
  • [SAS_QP_IFC_01B] Architectural view: QP/C Framework component API.

Forward Traceability

  • [TUN_QP_qf_defer] Unit test for qf_defer: event deferral for QActive class.
  • [TIN_QP_defer] Integration test suite for event deferral.
  • [QActive::QActive_defer()] Defer an event to a given separate event queue
  • [QActive::QActive_recall()] Recall a deferred event from a given event queue

Design View: Mutable Event Life Cycle

The zero-copy event management is designed to be intuitive and transparent to the application-level code. However, for the zero-copy event management abstraction to behave exactly like the actual event copying, QP/C Application needs to obey specific event ownership rules similar to the rules of working with objects allocated with the C++ operator new and summarized in the life cycle diagram of a mutable event (see Figure SDS-EVT-LIFE). In exchange, QP/C Framework can safely and deterministically deliver your mutable events with hard real-time performance, which does not complicate the RMS/RMA method and is superior to the copying of entire events approach.


[SDS_QP_MELC]

Design view: Mutable event ownership rules.

Model Kind

The Mutable event life cycle is illustrated with a UML state diagram.

Figure SDS-EVT-LIFE: Mutable Event Life Cycle and Ownership Rules


Figure SDS-EVT-LIFE illustrates the mutable event life cycle and possible transfers of ownership rights to the event:

[0] All mutable events are initially owned by QP/C Framework.

[1] An event producer might gain ownership of a new event only by allocating it. At this point, the producer gains the ownership rights with the permissions to write to the event. Indeed, the purpose of this stage in the mutable event's life cycle is to initialize the event and fill it with data. The event producer might keep the event as long as it needs. For example, the producer (e.g., ISR) might fill the event with data over many invocations. Eventually, however, the producer must transfer the ownership back to the framework.

[2a,2b,2c] Typically the producer posts [2a] or publishes [2b] the event. As a special case, the producer might decide that the event is not good, in which case the producer must explicitly recycle [2c] the event. After any of these three operations, the producer immediately loses ownership of the event and can no longer access it. In particular, it is illegal to post, publish, or recycle the event again.

[3] The recipient Active Object gains ownership of the current event upon the start of the RTC step. This time, the Active Object gains the read-only permissions to the current event.

[4a,4b] During the RTC step, the recipient Active Object is allowed to re-post [4a] or re-publish [4b] the current event any number of times without losing ownership of the event.

[5a] Before the end of the RTC, the Active Object may defer the current event. Event deferral extends the read-only ownership rights beyond the current RTC step.

[5b] Eventually, however, the deferred event must be recalled, which self-posts the event into the Active Object's event queue (using the LIFO policy). Recalling ends the ownership of the original deferred event. Thus recalled event later gets dispatched, so the Active Object gains again read-only ownership, as described in step [3].

[6a] Before the end of the RTC, the Active Object may create an explicit reference to the current event. Event referencing also extends the read-only ownership rights beyond the current RTC step.

[6b] Eventually, however, the event reference must be deleted, which recycles the event and ends the ownership of the original referenced event.

[7] The end of the RTC step terminates the ownership of the current event. The Active Object cannot use the event in any way past the RTC step. In particular, if any data from that event is needed in the future, the QP/C Application must copy that data (typically into some attributes inside the Active Object).

Backward Traceability

  • [SAS_QP_RES_04] Architectural view: Event pool allocation policy and responsibilities.
  • [SAS_QP_RES_10] Architectural view: Event management policy in QP/C Framework.

Forward Traceability

  • [SSM_QP_ASR_415] Application-level state machines must never change the current event.
  • [SSM_QP_ASR_506] QP/C Application shall use mutable events strictly in accordance with the QP/C mutable event lifecycle rules.
  • [SSM_QP_AOU_500] QP/C Application should initialize mutable events at the point of dynamic allocation (RAII).
  • [TUN_QP_qf_dyn] Unit test for qf_dyn: QF dynamic (mutable) event management.
  • [TUN_QP_qf_dyn_raii] Unit test for qf_dyn with QEVT_PAR_INIT: initialization of mutable events at allocation (RAII).
  • [TIN_QP_evt] Integration test suite for events and event memory management.
  • [TIN_QP_defer] Integration test suite for event deferral.

Structure ViewpointTime Viewpoint