Events in Zephyr
How k_event_wait records what a thread waits for in the thread itself, how k_event_post walks the wait queue and hands each waiter the events that satisfied it, and what the _safe variants clear.
Simulated kernel: Zephyr v4.5.0. This scenario and every other one are in the scheduler playground. The simulator and its data files are not covered by the site's CC BY-SA license; see their license notices.
This walkthrough follows the event groups lesson through Zephyr’s
kernel/events.c. Zephyr’s event object, k_event, is its event group. Set the step level to
Line to follow it.
What an event object is
K_EVENT_DEFINE(E) defines a struct k_event:
events: 32 bits, all of them the application’s.wait_q: the threads waiting, sorted by priority like every Zephyr wait queue.
There are four ways to wait: k_event_wait() for any of a set of events, k_event_wait_all() for
all of them, and the _safe variants of each, which consume the events they get (clear them as
the wait ends). Each one calls k_event_wait_internal() with its options.
Waiting
At tick 0 Upload calls k_event_wait_all_safe(&E, BIT_DATA | BIT_LINK, false, K_FOREVER).
static uint32_t k_event_wait_internal(struct k_event *event, uint32_t events,
unsigned int options, k_timeout_t timeout)
{
uint32_t rv = 0;
__ASSERT(((arch_is_in_isr() == false) ||
K_TIMEOUT_EQ(timeout, K_NO_WAIT)), "");
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_event, wait, event, events,
options, timeout);
if ((events == 0U) && !(options & K_EVENT_OPTION_RESET)) {
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_event, wait, event, events, 0);
return 0;
}
k_spinlock_key_t key = k_spin_lock(&event->lock);
if (options & K_EVENT_OPTION_RESET) {
event->events = 0;
}
if (events == 0) {
k_spin_unlock(&event->lock, key);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_event, wait, event, events, 0);
return 0;
}
unsigned int wait_condition = options & K_EVENT_WAIT_MASK;
struct k_thread *thread = k_sched_current_thread_query();
/* Test if the wait conditions have already been met. */
rv = are_wait_conditions_met(events, event->events, wait_condition);
if (rv != 0) {
/* clear the events that are matched */
if (options & K_EVENT_OPTION_CLEAR) {
event->events &= ~rv;
}
k_spin_unlock(&event->lock, key);
goto out;
}
if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
k_spin_unlock(&event->lock, key);
goto out;
}
/*
* The caller must pend to wait for the match. Save the desired
* set of events in the k_thread structure.
*/
thread->events = events;
thread->event_options = options;
SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_event, wait, event, events,
options, timeout);
if (z_pend_curr(&event->lock, key, &event->wait_q, timeout) == 0) {
/* Retrieve the set of events that woke the thread */
rv = thread->events;
}
out:
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_event, wait, event, events, rv);
return rv;
}
kernel/events.c, lines 282–350 · Zephyr v4.5.0 · Apache-2.0 license
are_wait_conditions_met()matches the events against the ones the thread wants. It returns the events that satisfy the wait, or 0: for “all”, 0 unless every one of them is there.- Nothing is set, so the thread must wait. It writes what it waits for into its own thread
object (
thread->eventsandthread->event_options), not into the event object. z_pend_curr()puts it inE.wait_qand switches away, as for any wait.
Status waits with k_event_wait(&E, BIT_LINK | BIT_ERR, false, K_FOREVER). It is less urgent, so
it queues behind Upload.
Posting
At tick 3 the Sensor interrupt calls k_event_post(&E, BIT_DATA), right in the
handler. k_event_post() and k_event_clear() both go through one function:
static uint32_t k_event_post_internal(struct k_event *event, uint32_t events,
uint32_t events_mask)
{
k_spinlock_key_t key;
struct event_walk_data data;
uint32_t previous_events;
key = k_spin_lock(&event->lock);
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_event, post, event, events,
events_mask);
previous_events = event->events & events_mask;
events = (event->events & ~events_mask) |
(events & events_mask);
/*
* Posting an event has the potential to wake multiple pended threads.
* It is desirable to wake all affected threads simultaneously. When
* z_sched_waitq_walk() allows removal of nodes from the wait queue,
* we wake (unpend and ready) each thread as part of the callback.
* Otherwise, proceed in two steps:
*
* 1. Walk the waitq and create a linked list of threads to wake.
* 2. Walk the resulting linked list and wake each of the threads.
*/
#ifdef CONFIG_WAITQ_SCALABLE
data.head = NULL;
#endif /* CONFIG_WAITQ_SCALABLE */
data.events = events;
data.clear_events = 0;
z_sched_waitq_walk(&event->wait_q, event_walk_op, EVENT_POST_WALK_OP_FN, &data);
/* stash any events not consumed */
event->events = data.events & ~data.clear_events;
z_reschedule(&event->lock, key);
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_event, post, event, events,
events_mask);
return previous_events;
}
kernel/events.c, lines 179–222 · Zephyr v4.5.0 · Apache-2.0 license
The new events are worked out under a mask: a post sets the bits it names, a clear posts nothing
under the mask of the bits it clears. Then z_sched_waitq_walk() calls event_walk_op() for
every waiting thread:
static int event_walk_op(struct k_thread *thread, void *data)
{
uint32_t match;
unsigned int wait_condition;
struct event_walk_data *event_data = data;
wait_condition = thread->event_options & K_EVENT_WAIT_MASK;
match = are_wait_conditions_met(thread->events, event_data->events,
wait_condition);
if (match != 0) {
/*
* The wait conditions have been satisfied. Set the
* received events then wake thread now if allowed,
* else add it to the list of threads to unpend.
*
* NOTE: thread event options can consume an event
*/
thread->events = match;
if (thread->event_options & K_EVENT_OPTION_CLEAR) {
event_data->clear_events |= match;
}
(void)z_try_abort_thread_timeout(thread);
#ifndef CONFIG_WAITQ_SCALABLE
/*
* Note: z_sched_wake_thread_locked() is safe
* to call here because this walk_op callback
* is invoked with the scheduler spinlock held.
*/
arch_thread_return_value_set(thread, 0);
z_sched_wake_thread_locked(thread);
#else /* !CONFIG_WAITQ_SCALABLE */
thread->next_wake_link = event_data->head;
event_data->head = thread;
#endif /* !CONFIG_WAITQ_SCALABLE */
}
return 0;
}
kernel/events.c, lines 138–177 · Zephyr v4.5.0 · Apache-2.0 license
At tick 3 DATA alone satisfies nobody. At tick 7 Net posts LINK, and both threads match:
UploadgetsDATA | LINK;Status, which wanted any ofLINKandERR, gets onlyLINK.thread->eventsis overwritten with the events that matched: that is what the thread’s call will return.Uploadconsumes its events, so they are added toclear_events, to be cleared after the walk;Statusstill sees them.- Each matched thread’s timeout is taken out of the timeout list, its return value is set to 0, and
z_sched_wake_thread_locked()takes it out of the wait queue and makes it ready.
After the walk the consumed events are cleared, and z_reschedule() switches to Upload, the
most urgent thread now ready. In an interrupt handler, z_reschedule() does not switch; the
interrupt’s exit does.
Waking up
Upload returns from z_pend_curr() with 0, reads thread->events, and returns DATA | LINK.
Status returns LINK, only what satisfied its wait. FreeRTOS returns the
whole group instead, as it was when the task was woken; with Zephyr’s form, the caller does not
have to mask the result.
A wait that times out returns 0: z_pend_curr() returns -EAGAIN, and the events are not looked
at again. If the events arrive a moment later, they stay set for the next wait (FreeRTOS looks once
more after a timeout and takes them).
Who clears what
k_event_wait() without _safe leaves the events set: Status could wait for LINK again and
return at once. The reset argument (false here) is different again: it clears all events
before the wait starts, so only events posted from then on count. In FreeRTOS terms, _safe is
xClearOnExit, and reset has no counterpart.