Event groups
How a task waits for several events at once, all of them or any one, how one set of bits wakes every task it satisfies, and what each kernel returns to the waiter.
Simulated kernel: FreeRTOS V11.3.3 and 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.
A semaphore signals one kind of event. A task that needs several things before it can go on (a reading from the sensor and a network link, say) could wait on two semaphores one after the other, but then it holds on to the first while it waits for the second, and it cannot wait for “whichever comes first” at all. An event group answers both: it is a word of bits, each one an event, and a task can wait for all of a set of bits or for any of them.
- Setting bits (from a task, an interrupt or a timer callback) wakes every waiting task whose condition is now met. One set can wake several tasks, unlike a semaphore give, which wakes one.
- A waiter can ask for the bits it waited for to be cleared on exit, so the same events are not handled twice. Without it, the bits stay set until someone clears them.
- Bits that are set while nobody waits are remembered, but not counted: setting a bit that is already set changes nothing.
FreeRTOS calls them event groups (xEventGroupSetBits(), xEventGroupWaitBits()); Zephyr calls
them event objects, k_event (k_event_post(), k_event_wait() for any bit,
k_event_wait_all() for all of them, and the _safe variants, which clear on exit).
The scenario
E has three bits, DATA, LINK and ERR. The application names them (#define BIT_DATA ( 1 << 0 )).
Upload(urgency 3) waits for all ofDATAandLINK, clearing them as it takes them, and then works for two ticks. It does that twice.Status(urgency 2) waits for any ofLINKorERR, once, and works for one tick.Net(urgency 1) sleeps until tick 6, setsLINK, sleeps 6 more ticks and setsLINKagain.- The
Sensorinterrupt setsDATAat ticks 3 and 8.
What happens on FreeRTOS
- Tick 3: the
Sensorinterrupt setsDATA. On FreeRTOS an interrupt does not set the bits itself:xEventGroupSetBitsFromISR()hands the work to the timer service task, which is the most urgent task and runs as soon as the interrupt returns.DATAalone wakes nobody:Uploadalso needsLINK, andStatuswaits forLINKorERR. - Tick 6:
NetsetsLINK. The kernel walks the list of waiting tasks:Uploadnow has both bits,Statushas one of its two. Both are woken by the one call.Uploadasked for its bits to be cleared, but that happens only after the walk, soStatusseesLINKtoo.Uploadis the more urgent and runs first. - Tick 8:
Uploadis done with its work, and the interrupt setsDATAagain.Uploadwaits forDATAandLINKa second time;DATAis there,LINKis not.Statusfinally runs. - Status gets its bits:
xEventGroupWaitBits()returnsDATA | LINK, the whole group as it was whenStatuswas woken, althoughStatusonly waited forLINK. It is the caller’s job to test the bits it cares about. - Tick 15:
NetsetsLINKagain, andUploadtakes both bits for the second time.
What happens on Zephyr
The bits move the same way, a tick later where a sleep is involved (Net’s sleeps get
the extra tick: LINK is set at ticks 7 and 17). Two things
differ:
- The interrupt calls
k_event_post()and setsDATAitself, inside the handler. There is no helper task. - A wait returns only the events that satisfied it.
StatusgetsLINK, notDATA | LINK. The rest of the group is not its business, and in its_safeform the wait clears exactly those bits.
The cost of deferring
Why does FreeRTOS not set the bits in the interrupt? Setting bits may wake any number of tasks,
and walking a list of unknown length is not something FreeRTOS does with interrupts off. So the
interrupt only queues a request (setBits(E, DATA) in the timer queue) and the timer service task
does the walk later, with the scheduler suspended but interrupts on.
“Later” is only as soon as the timer task gets the CPU. Set Timer task urgency to 1, the lowest
task priority. At tick 8 the timer task now waits behind Upload and Status, and DATA is set
only at tick 9: the bit lights up in E a tick after the interrupt. If a task more urgent than the
timer task were busy for longer, the event would wait for it. Zephyr pays the walk inside the
interrupt instead, with interrupts masked for as long as it takes.
What to try
- Untick Yield from ISR (FreeRTOS): the timer service task is woken by the interrupt, but no
switch is requested. At tick 3 nothing else happens until the next tick, so
DATAis set at tick 4. At tick 8Uploadmakes a kernel call right after the interrupt, and resuming the scheduler in that call makes the switch that was left pending:DATAis set on time. - Swap the urgencies of
UploadandStatus: at tick 6Statusruns first, and on FreeRTOS it still returnsDATA | LINKalthoughUpload, woken by the same call, clears them. - Read the kernel side in the walkthroughs for FreeRTOS and for Zephyr.