|
| 1 | +# Execution control patterns |
| 2 | + |
| 3 | +```{admonition} Learning Objectives |
| 4 | +This tutorial explores four common patterns for controlling execution flow, |
| 5 | +each with different trade-offs: |
| 6 | +
|
| 7 | +1. Busy looping for minimal latency at the cost of CPU usage |
| 8 | +2. Timer-based sleep and wake-up for periodic work |
| 9 | +3. Event-driven sleep and wake-up for reactive applications |
| 10 | +4. Multiplexing to wait on multiple event sources simultaneously |
| 11 | +``` |
| 12 | + |
| 13 | +`iceoryx2` decouples control flow from data flow, giving you explicit control |
| 14 | +over when your participants wait and run. Many middleware systems |
| 15 | +automatically put threads to sleep when waiting for data and emit wake-up |
| 16 | +events when samples are sent. While convenient, this coupling can introduce |
| 17 | +unwanted latency through unnecessary context switches and wake-ups when no |
| 18 | +actual work is required. |
| 19 | + |
| 20 | +By separating these concerns, `iceoryx2` lets you choose execution patterns |
| 21 | +optimized for your specific requirements—whether minimizing latency, reducing |
| 22 | +CPU usage, or handling complex event scenarios. You can even mix patterns |
| 23 | +within a single application, using the approach that best fits each |
| 24 | +participant's needs. |
| 25 | + |
| 26 | +## Busy looping |
| 27 | + |
| 28 | +The simplest execution control pattern continuously polls for new data without |
| 29 | +ever sleeping. This approach minimizes latency by eliminating context switches |
| 30 | +and system calls, making it ideal for ultra-low-latency scenarios where CPU |
| 31 | +usage is not a constraint. |
| 32 | + |
| 33 | +In a busy loop, a participant repeatedly checks for available samples in |
| 34 | +a tight loop. While this consumes an entire CPU core, it guarantees the fastest |
| 35 | +possible response time when data arrives. |
| 36 | + |
| 37 | +```rust |
| 38 | +TODO |
| 39 | +``` |
| 40 | + |
| 41 | +Busy looping is most appropriate for time-critical paths where achieving the |
| 42 | +lowest possible latency is the priority and you have dedicated CPU cores |
| 43 | +available. |
| 44 | + |
| 45 | +## Periodic execution with timers |
| 46 | + |
| 47 | +Periodic execution uses timers to schedule work at regular intervals, allowing |
| 48 | +a participant to sleep between executions enabled your system to handle |
| 49 | +other tasks. This pattern is ideal for activities that run on a fixed schedule, |
| 50 | +such as sensor polling, status updates, heartbeat mechanisms, or other periodic |
| 51 | +data processing. |
| 52 | + |
| 53 | +With timer-based execution, a participant explicitly requests the platform to |
| 54 | +wake it after a specified duration. This approach significantly reduces CPU |
| 55 | +usage compared to busy looping. |
| 56 | + |
| 57 | +```rust |
| 58 | +TODO |
| 59 | +``` |
| 60 | + |
| 61 | +The trade-off is that wake-up timing depends on the platform's scheduler |
| 62 | +performance characteristics and the system load. |
| 63 | + |
| 64 | +## Reactive execution with events |
| 65 | + |
| 66 | +Reactive execution allows a participant to sleep until a specific event |
| 67 | +occurs, waking only when there is relevant work to perform. This pattern |
| 68 | +enables efficient event-driven systems that respond to asynchronous stimuli |
| 69 | +without wasting CPU cycles on polling or being delayed by a fixed execution |
| 70 | +schedule. |
| 71 | + |
| 72 | +`iceoryx2` implements this through the [event messaging pattern]( |
| 73 | +/fundamentals/messaging-patterns/event). Participants wait on listener ports, |
| 74 | +which put their threads to sleep until events are received from notifiers |
| 75 | +attached to the service. |
| 76 | + |
| 77 | +```rust |
| 78 | +TODO |
| 79 | +``` |
| 80 | + |
| 81 | +When an event occurs, the participant's thread is scheduled to run, allowing |
| 82 | +it to respond as soon as the platform is able to schedule it to run. This |
| 83 | +provides both CPU efficiency during idle periods and responsive behavior when |
| 84 | +events arrive at the cost of latency introduced by system calls |
| 85 | +and context switches when transitioning between sleep and wake states. |
| 86 | + |
| 87 | +## Multiplexed event handling |
| 88 | + |
| 89 | +Multiplexed event handling enables a single participant to wait on multiple |
| 90 | +event sources simultaneously, waking when any of them fires. |
| 91 | + |
| 92 | +`iceoryx2` provides a `WaitSet` that allows you to register multiple timers |
| 93 | +and listeners and block until any registered event occurs. When any event |
| 94 | +source fires, the participant wakes and can determine which source triggered |
| 95 | +it and process accordingly. |
| 96 | + |
| 97 | +```rust |
| 98 | +TODO |
| 99 | +``` |
| 100 | + |
| 101 | +Multiplexed event handling provides the efficiency of reactive execution while |
| 102 | +handling multiple event sources in a single participant. Like reactive |
| 103 | +execution, it incurs latency from system calls and context switches when |
| 104 | +transitioning between sleep and wake states, though the behavior may differ |
| 105 | +when multiple sources are registered. |
| 106 | + |
| 107 | +The additional trade-off is increased complexity in managing the `WaitSet` and |
| 108 | +determining which event source triggered the wake-up, compared to simpler |
| 109 | +patterns that wait on a single event source. |
| 110 | + |
| 111 | +## Further Reading |
| 112 | + |
| 113 | +````{grid} 1 1 2 3 |
| 114 | +:gutter: 2 |
| 115 | +
|
| 116 | +```{grid-item-card} Understand the Event messaging pattern |
| 117 | +:link: /fundamentals/messaging-patterns/event |
| 118 | +:link-type: doc |
| 119 | +:shadow: none |
| 120 | +
|
| 121 | +Understand the `Event` messaging pattern. |
| 122 | +``` |
| 123 | +
|
| 124 | +```{grid-item-card} Understand the WaitSet |
| 125 | +:link: /fundamentals/execution-control |
| 126 | +:link-type: doc |
| 127 | +:shadow: none |
| 128 | +
|
| 129 | +Understand the `WaitSet` component. |
| 130 | +``` |
| 131 | +
|
| 132 | +```{grid-item-card} Example: Event Multiplexing (Rust) |
| 133 | +:link: https://github.com/eclipse-iceoryx/iceoryx2/tree/main/examples/rust/event_multiplexing |
| 134 | +:link-type: url |
| 135 | +
|
| 136 | +See how to handle multiple `Event` services in a single user thread. |
| 137 | +``` |
| 138 | +
|
| 139 | +```` |
0 commit comments