Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 Inria
3 * 2017 Kaspar Schleiser <kaspar@schleiser.de>
4 * 2018-2019 Freie Universität Berlin
5 *
6 * This file is subject to the terms and conditions of the GNU Lesser
7 * General Public License v2.1. See the file LICENSE in the top level
8 * directory for more details.
9 */
10
96#ifndef EVENT_H
97#define EVENT_H
98
99#include <stdint.h>
100#include <stdbool.h>
101#include <string.h>
102
103#include "assert.h"
104#include "clist.h"
105#include "irq.h"
106#include "thread.h"
107#include "thread_flags.h"
108#include "ptrtag.h"
109
110#if IS_USED(MODULE_ZTIMER)
111#include "ztimer.h"
112#endif
113
114#ifdef __cplusplus
115extern "C" {
116#endif
117
118#ifndef THREAD_FLAG_EVENT
122#define THREAD_FLAG_EVENT (0x1)
123#endif
124
128#define EVENT_QUEUE_INIT { .waiter = thread_get_active() }
129
133#define EVENT_QUEUE_INIT_DETACHED { .waiter = NULL }
134
138typedef struct event event_t;
139
143typedef void (*event_handler_t)(event_t *);
144
152
160
169static inline void event_queues_init(event_queue_t *queues,
170 size_t n_queues)
171{
172 assert(queues && n_queues);
174 for (size_t i = 0; i < n_queues; i++) {
175 memset(&queues[i], '\0', sizeof(queues[0]));
176 queues[i].waiter = me;
177 }
178}
179
187static inline void event_queue_init(event_queue_t *queue)
188{
189 event_queues_init(queue, 1);
190}
191
198static inline void event_queues_init_detached(event_queue_t *queues,
199 size_t n_queues)
200{
201 assert(queues);
202 for (size_t i = 0; i < n_queues; i++) {
203 memset(&queues[i], '\0', sizeof(queues[0]));
204 }
205}
206
212static inline void event_queue_init_detached(event_queue_t *queue)
213{
215}
216
228static inline void event_queues_claim(event_queue_t *queues, size_t n_queues)
229{
230 assert(queues);
232 for (size_t i = 0; i < n_queues; i++) {
233 assert(queues[i].waiter == NULL);
234 queues[i].waiter = me;
235 }
236}
237
248static inline void event_queue_claim(event_queue_t *queue)
249{
250 event_queues_claim(queue, 1);
251}
252
267
279
289bool event_is_queued(const event_queue_t *queue, const event_t *event);
290
303
335event_t *event_wait_multi(event_queue_t *queues, size_t n_queues);
336
354static inline event_t *event_wait(event_queue_t *queue)
355{
356 return event_wait_multi(queue, 1);
357}
358
359#if IS_USED(MODULE_XTIMER) || defined(DOXYGEN)
372event_t *event_wait_timeout(event_queue_t *queue, uint32_t timeout);
373
386event_t *event_wait_timeout64(event_queue_t *queue, uint64_t timeout);
387#endif
388
389#if IS_USED(MODULE_ZTIMER) || defined(DOXYGEN)
408 ztimer_clock_t *clock, uint32_t timeout);
409#endif
410
435static inline void event_loop_multi(event_queue_t *queues, size_t n_queues)
436{
437 event_t *event;
438
439 while ((event = event_wait_multi(queues, n_queues))) {
440 event->handler(event);
441 }
442}
443
463static inline void event_loop(event_queue_t *queue)
464{
465 event_loop_multi(queue, 1);
466}
467
488
489#ifdef __cplusplus
490}
491#endif
492#endif /* EVENT_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
Circular linked list.
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:407
static void event_loop_multi(event_queue_t *queues, size_t n_queues)
Simple event loop with multiple queues.
Definition event.h:435
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
static event_t * event_wait(event_queue_t *queue)
Get next event from event queue, blocking.
Definition event.h:354
static void event_loop(event_queue_t *queue)
Simple event loop.
Definition event.h:463
event_t * event_wait_timeout_ztimer(event_queue_t *queue, ztimer_clock_t *clock, uint32_t timeout)
Get next event from event queue, blocking until timeout expires.
static void event_queue_init(event_queue_t *queue)
Initialize an event queue.
Definition event.h:187
void(* event_handler_t)(event_t *)
event handler type definition
Definition event.h:143
void event_cancel(event_queue_t *queue, event_t *event)
Cancel a queued event.
struct PTRTAG event_queue_t
event queue structure
static void event_queue_init_detached(event_queue_t *queue)
Initialize an event queue not binding it to a thread.
Definition event.h:212
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition event.h:248
static void event_queues_claim(event_queue_t *queues, size_t n_queues)
Bind an array of event queues to the calling thread.
Definition event.h:228
void event_sync(event_queue_t *queue)
Synchronize with the last event on the queue.
static void event_queues_init_detached(event_queue_t *queues, size_t n_queues)
Initialize an array of event queues not binding it to a thread.
Definition event.h:198
event_t * event_wait_multi(event_queue_t *queues, size_t n_queues)
Get next event from the given event queues, blocking.
event_t * event_wait_timeout(event_queue_t *queue, uint32_t timeout)
Get next event from event queue, blocking until timeout expires.
static void event_queues_init(event_queue_t *queues, size_t n_queues)
Initialize an array of event queues.
Definition event.h:169
event_t * event_get(event_queue_t *queue)
Get next event from event queue, non-blocking.
bool event_is_queued(const event_queue_t *queue, const event_t *event)
Check if an event is already queued.
event_t * event_wait_timeout64(event_queue_t *queue, uint64_t timeout)
Get next event from event queue, blocking until timeout expires.
IRQ driver interface.
Pointer Tagging Helpers.
event queue structure
Definition event.h:156
thread_t * waiter
thread owning event queue
Definition event.h:158
clist_node_t event_list
list of queued events
Definition event.h:157
thread_t holds thread's context data.
Definition thread.h:168
event structure
Definition event.h:148
event_handler_t handler
pointer to event handler function
Definition event.h:150
clist_node_t list_node
event queue list entry
Definition event.h:149
List node structure.
Definition list.h:40
ztimer device structure
Definition ztimer.h:369
Thread Flags API.
ztimer API