Events, event flags or event bits (in this article events will be used) is part of each task i.e. each task has their own set of events. That means that when a task or an ISR sends events, they send them to a task. Events are normally used for task synchronization. Events also offers the possibility for a task to wait for several events at the same time, which is not possible with semaphores and message queues.
What is Events?
Events are part of the task, so they do not need to be created, as they are automatically created when the task is created. The events are part of the tasks TCB (task control block), see figure 1.
Events may be implemented in slightly different ways in different RTOSs, but this article shows a typical implementation. Events are treated as a bit-mapped field, normally either 16 or 32 bits, and each bit represents an event. In the TCB there are three bit-mapped fields that are used for events, see figure 2:
- Received, which represents events that have been received, but the task has not requested them.
- Requested, which represents events that have been requested by the task.
- Delivered, which represents events that have been requested and delivered.
A task or and ISR can send one or several events to another task. Only tasks can receive events, as an ISR does not have any events, as an ISR is not a task. A task that wants to receive events can decide if it wants to receive just one event or several events. If the task decides to wait for several it can decide if it wants to wait for several events with
- An AND condition, meaning that all the requested events must have been delivered
- An OR condition, meaning minimum one of the requested events must have been delivered
As events is a bit-mapped field, it means that events can not be accumulated, and therefore there is no counting mechanism for events. If an event has already been received, the task cannot see if it has been just received once or several times.
Usage of a Events
Events are used for task synchronization when you know which task you want to synchronize.
Otherwise it could be better to use a semaphore.
As events do not have a counting mechanism, the design of the application must be so robust that ”loosing” an event will not cause any conflicts. Otherwise use a counting semaphore or a message queue.
Events are very useful when a task needs to wait for several things at the same time (either with an AND or an OR condition).