Skip to main content
Version: 0.5.0

Deadlines

This article has examples in the following target languages:

Lingua Franca includes a notion of a deadline, which is a constraint on the relation between logical time and physical time. Specifically, a program may specify that the invocation of a reaction must occur within some physical time interval of the logical time of the message. If a reaction is invoked at logical time 12 noon, for example, and the reaction has a deadline of one hour, then the reaction is required to be invoked before the physical-time clock of the execution platform reaches 1 PM. If the deadline is violated, then the specified deadline handler is invoked instead of the reaction.

Purposes for Deadlines​

A deadline in an LF program serves two purposes. First, it can guide scheduling in that a scheduler may prioritize reactions with deadlines over those without or those with longer deadlines. For this purpose, if a reaction has a deadline, then all upstream reactions on which it depends (without logical delay) inherit its deadline. Hence, those upstream reactions will also be given higher priority.

Second, the deadline mechanism provides a fault handler, a section of code to invoke when the deadline requirement is violated. Because invocation of the fault handler depends on factors beyond the control of the LF program, an LF program with deadlines becomes nondeterministic. The behavior of the program depends on the exact timing of the execution.

There remains the question of when the fault handler should be invoked. By default, deadlines in LF are lazy, meaning that the fault handler is invoked at the logical time of the event triggering the reaction whose deadline is missed. Specifically, the possible violation of a deadline is not checked until the reaction with the deadline is ready to execute. Only then is the determination made whether to invoke the regular reaction or the fault handler.

Deadline Specification​

A deadline is specified as follows:

target C; reactor Deadline { input x:int; output d:int; // Produced if the deadline is violated. reaction(x) -> d {= printf("Normal reaction.\n"); =} deadline(10 msec) {= printf("Deadline violation detected.\n"); lf_set(d, x->value); =} }

This reactor specifies a deadline of 10 milliseconds (this can be a parameter of the reactor). If the reaction to x is triggered later in physical time than 10 msec past the timestamp of x, then the second body of code is executed instead of the first. That second body of code has access to anything the first body of code has access to, including the input x and the output d. The output can be used to notify the rest of the system that a deadline violation occurred. This reactor can be tested as follows:

target C import Deadline from "Deadline.lf" preamble {= #include "platform.h" =} main reactor { logical action a d = new Deadline() reaction(startup) -> d.x, a {= lf_set(d.x, 0); lf_schedule(a, 0); =} reaction(a) -> d.x {= lf_set(d.x, 0); lf_sleep(MSEC(20)); =} reaction(d.d) {= printf("Deadline reactor produced an output.\n"); =} }
Lingua Franca diagram: DeadlineTestCreator: FreeHEP Graphics2D Driver Producer: de.cau.cs.kieler.klighd.piccolo.freehep.SemanticSVGGraphics2D Revision Source: Date: Sunday, April 10, 2022 at 10:03:56 AM Pacific Daylight TimeDeadlineTestDeadline10 msecxd12L3

Running this program will result in the following output:

Normal reaction.
Deadline violation detected.
Deadline reactor produced an output.

The first reaction of the Deadline reactor does not violate the deadline, but the second does. Notice that the sleep in the main reactor occurs after setting the output, but because of the deterministic semantics of LF, this does not matter. The actual value of an output cannot be known until every reaction that sets that output completes its execution. Since this reaction takes at least 20 msec to complete, the deadline is assured of being violated.

Notice that the deadline is annotated in the diagram with a small clock symbol.

Notice that the deadline violation here is caused by an invocation of lf_sleep, defined in "platform.h" (see Libraries Available to Programmers). It is not generally advisable for a reaction to sleep because this can block other reactions from executing. But this is exactly what we are trying to accomplish here in order to force a deadline to be violated.

Deadline Violations During Execution​

Whether a deadline violation occurs is checked only before invoking the reaction with a deadline. What if the reaction itself runs for long enough that the deadline gets violated during the reaction execution? For this purpose, a target-language function is provided to check whether a deadline is violated during execution of a reaction with a deadline.

info

As of this writing, this function is only implemented in the C target.