Event Queue

Provides an Event loop. More...

Detailed Description

Provides an Event loop.

This module offers an event queue framework like libevent or libuev.

An event queue is basically a FIFO queue of events, with some functions to efficiently and safely handle adding and getting events to / from such a queue. An event queue is bound to a thread, but any thread or ISR can put events into a queue.

An event is a structure containing a pointer to an event handler. It can be extended to provide context or arguments to the handler. It can also be embedded into existing structures (see examples).

Compared to msg or mbox, this some fundamental differences:

  1. events are "sender allocated". Unlike msg_send(), event_post() never blocks or fails.
  2. events contain everything necessary to handle them, thus a thread processing the events of an event queue doesn't need to be changed in order to support new event types.
  3. events can be safely used (and actually perform best) when used within one thread, e.g., in order to create a state-machine like process flow. This is not (easily) possible using msg queues, as they might fill up.
  4. an event can only be queued in one event queue at the same time. Notifying many queues using only one event object is not possible with this imlementation.

At the core, event_wait() uses thread flags to implement waiting for events to be queued. Thus event queues can be used safely and efficiently in combination with thread flags and msg queues.


// simple event handler
static void handler(event_t *event)
printf("triggered 0x%08x\n", (unsigned)event);
static event_t event = { .handler = handler };
static event_queue_t queue;
int main(void)
[...] event_post(&queue, &event);
// example for event extended event struct
typedef struct {
event_t super;
const char *text;
} custom_event_t;
static void custom_handler(event_t *event)
custom_event_t *custom_event = (custom_event_t *)event;
printf("triggered custom event with text: \"%s\"\n", custom_event->text);
static custom_event_t custom_event = { .super.callback = custom_handler, .text = "CUSTOM EVENT" };
[...] event_post(&queue, &custom_event)


file  callback.h
 Provides a callback-with-argument event type.
file  event/timeout.h
 Provides functionality to trigger events after timeout.
file  event.h
 Event API.

Data Structures

struct  event
 event structure More...
struct  event_queue_t
 event queue structure More...


#define THREAD_FLAG_EVENT   (0x1)
 Thread flag use to notify available events in an event queue.
#define EVENT_QUEUE_INIT   { .waiter = (thread_t *)sched_active_thread }
 event_queue_t static initializer


typedef struct event event_t
 event structure forward declaration
typedef void(* event_handler_t) (event_t *)
 event handler type definition


void event_queue_init (event_queue_t *queue)
 Initialize an event queue. More...
void event_post (event_queue_t *queue, event_t *event)
 Queue an event. More...
void event_cancel (event_queue_t *queue, event_t *event)
 Cancel a queued event. More...
event_tevent_get (event_queue_t *queue)
 Get next event from event queue, non-blocking. More...
event_tevent_wait (event_queue_t *queue)
 Get next event from event queue, blocking. More...
void event_loop (event_queue_t *queue)
 Simple event loop. More...

Function Documentation

◆ event_cancel()

void event_cancel ( event_queue_t queue,
event_t event 

Cancel a queued event.

This will remove a queued event from an event queue.

Due to the underlying list implementation, this will run in O(n).
[in]queueevent queue to remove event from
[in]eventevent to remove from queue

◆ event_get()

event_t* event_get ( event_queue_t queue)

Get next event from event queue, non-blocking.

In order to handle an event retrieved using this function, call event->handler(event).

[in]queueevent queue to get event from
pointer to next event
NULL if no event available

◆ event_loop()

void event_loop ( event_queue_t queue)

Simple event loop.

This function will forever sit in a loop, waiting for events to be queued and executing their handlers.

It is pretty much defined as:

while ((event = event_wait(queue))) {
[in]queueevent queue to process

◆ event_post()

void event_post ( event_queue_t queue,
event_t event 

Queue an event.

[in]queueevent queue to queue event in
[in]eventevent to queue in event queue

◆ event_queue_init()

void event_queue_init ( event_queue_t queue)

Initialize an event queue.

This will set the calling thread as owner of queue.

[out]queueevent queue object to initialize

◆ event_wait()

event_t* event_wait ( event_queue_t queue)

Get next event from event queue, blocking.

This function will block until an event becomes available.

In order to handle an event retrieved using this function, call event->handler(event).

[in]queueevent queue to get event from
pointer to next event