Kernel event dispatcher

Browse source code on GitHub

Overview

This sample demonstrates how a single dispatcher thread can wait for several different kernel objects without actively checking each object. It models a small sensor application with the following asynchronous event sources:

  • a message queue containing samples produced from a timer expiry function;

  • a semaphore representing a control request; and

  • a poll signal carrying the result of a shutdown request.

The dispatcher schedules housekeeping every 120 ms. Each call to k_poll() waits only until the next housekeeping deadline. After waking, the dispatcher runs housekeeping if it is due, even when an event woke it. It then checks every event because several can be ready at once, consumes the associated kernel objects, and resets the event states before polling again.

The sample timer produces four readings 80 ms apart. The control and shutdown requests follow. Every gap between inputs is shorter than the housekeeping interval, so the sample demonstrates housekeeping while events keep arriving.

This pattern is useful when one thread owns several uncontended objects. It can reduce the number of application threads and their stack memory, at the cost of enabling the polling infrastructure with CONFIG_POLL.

Building and Running

Build and run the sample on QEMU Emulation for ARM Cortex-M3 as follows:

west build -b qemu_cortex_m3 samples/kernel/poll
west build -t run

To build for another board, replace qemu_cortex_m3 with the board name.

Sample Output

The exact number and placement of housekeeping messages depend on scheduling. The following output was captured on qemu_cortex_m3:

Kernel event dispatcher started
sample 0: value=20
housekeeping 1
sample 1: value=21
sample 2: value=22
housekeeping 2
sample 3: value=23
control request handled
housekeeping 3
shutdown requested: result=0
dispatcher complete: samples=4 control=1 housekeeping=3

Exit QEMU by pressing CTRL+A followed by x.

See also

Async polling APIs