Events Explained

Events are an important feature in ChibiOS/RT, most device drivers generate* events in order to notify the application that something happened at the I/O level.

Preamble

While event flags are not something unknown in other operating systems, their peculiar implementation in ChibiOS/RT requires a more in depth explanation. Lets start with the events-related terminology:

  • Event Source, an EventSource is a system object that can be broadcasted asynchronously in response of a system event, for example, when the CAN driver receives a packet from the CAN bus it broadcasts an event source in order to inform the registered threads that a packet has just arrived.
  • Event Listener, a system object that associates a Thread object to an event source.
  • Registration, the process of associating a Thread to an EventSource using an EventListener is called registration.
  • Broadcast, the operation performed on an event source in order to inform the registered threads that an event just occurred. Broadcasting can happened both in interrupt handlers and in threads.
  • Pending Events, event flags waiting to be served by a thread.
  • Add, the broadcast operation adds (logical or) an events mask to all the registered threads.
  • Wait, synchronous operation performed by a thread in order to wait for a specific combination of events. The API offers a variety of wait functions, please refer to the events API documentation.

Note that events are asynchronously generated, for example in an interrupt handler, but are synchronously served when a wait primitive is invoked.

Events-related data structures

The following diagram explains the relationship between an event source, its list of event listeners and the registered threads. Note that each event listener has a different bit mask to be added on its associated thread when the event source is broadcasted, this means that each thread can define its own event identifiers independently. A broadcast operation can also add more than one bit on the registered threads.
There are a variety of wait primitives, it is possible to wait for one or more event flags, and can also specify AND/OR conditions, for example, a thread can wait for any of the specified events or wait for all the specified events.
The field p_epending is the mask of the currently pending events, the field p_ewmask is the mask of the events the thread is currently interested in (AND or OR condition depending on the invoked wait API).

Use Scenarios

Events are best used when one of more of the following conditions are required:

  • Having to wait on multiple conditions, Events are the only mechanism that easily allows that.
  • Synchronous response to one or more asynchronous events.
  • Single threaded applications working in a event driver environment (but events are not limited to single threaded applications).
 
chibios/kb/events.txt · Last modified: 2011/10/03 20:54 by giovanni
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.3