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
11#pragma once
12
97
98#include <stdio.h>
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#include "ztimer.h"
110
111#ifdef __cplusplus
112extern "C" {
113#endif
114
115#ifndef THREAD_FLAG_EVENT
119#define THREAD_FLAG_EVENT (0x1)
120#endif
121
125#define EVENT_QUEUE_INIT { .waiter = thread_get_active() }
126
130#define EVENT_QUEUE_INIT_DETACHED { .waiter = NULL }
131
135typedef struct event event_t;
136
140typedef void (*event_handler_t)(event_t *);
141
149
157
166static inline void event_queues_init(event_queue_t *queues,
167 size_t n_queues)
168{
169 assert(queues && n_queues);
171 for (size_t i = 0; i < n_queues; i++) {
172 memset(&queues[i], '\0', sizeof(queues[0]));
173 queues[i].waiter = me;
174 }
175}
176
184static inline void event_queue_init(event_queue_t *queue)
185{
186 event_queues_init(queue, 1);
187}
188
195static inline void event_queues_init_detached(event_queue_t *queues,
196 size_t n_queues)
197{
198 assert(queues);
199 for (size_t i = 0; i < n_queues; i++) {
200 memset(&queues[i], '\0', sizeof(queues[0]));
201 }
202}
203
209static inline void event_queue_init_detached(event_queue_t *queue)
210{
212}
213
225static inline void event_queues_claim(event_queue_t *queues, size_t n_queues)
226{
227 assert(queues);
229 for (size_t i = 0; i < n_queues; i++) {
230 assert(queues[i].waiter == NULL);
231 queues[i].waiter = me;
232 }
233}
234
245static inline void event_queue_claim(event_queue_t *queue)
246{
247 event_queues_claim(queue, 1);
248}
249
264
276
286bool event_is_queued(const event_queue_t *queue, const event_t *event);
287
300
332event_t *event_wait_multi(event_queue_t *queues, size_t n_queues);
333
351static inline event_t *event_wait(event_queue_t *queue)
352{
353 return event_wait_multi(queue, 1);
354}
355
356#if IS_USED(MODULE_XTIMER) || defined(DOXYGEN)
369event_t *event_wait_timeout(event_queue_t *queue, uint32_t timeout);
370
383event_t *event_wait_timeout64(event_queue_t *queue, uint64_t timeout);
384#endif
385
386#if IS_USED(MODULE_ZTIMER) || defined(DOXYGEN)
405 ztimer_clock_t *clock, uint32_t timeout);
406#endif
407
436static inline void event_loop_multi(event_queue_t *queues, size_t n_queues)
437{
438 while (1) {
439 event_t *event = event_wait_multi(queues, n_queues);
440 if (IS_USED(MODULE_EVENT_LOOP_DEBUG)) {
441 uint32_t now;
443
445 if (!IS_USED(MODULE_EVENT_CALLBACK) ||
447 printf("event: executing %p->%p\n",
448 (void *)event, (void *)(uintptr_t)event->handler);
449 }
450 now = ztimer_now(ZTIMER_USEC);
451
452 event->handler(event);
453
454 printf("event: %p took %" PRIu32 " µs\n",
455 (void *)event, ztimer_now(ZTIMER_USEC) - now);
457 }
458 else {
459 event->handler(event);
460 }
461 }
462}
463
486static inline void event_loop(event_queue_t *queue)
487{
488 event_loop_multi(queue, 1);
489}
490
511
512#ifdef __cplusplus
513}
514#endif
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:135
void _event_callback_handler(event_t *event)
event callback handler function (used internally)
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:106
struct _thread thread_t
forward declaration for thread_t, defined in thread.h
Definition sched.h:154
static thread_t * thread_get_active(void)
Returns a pointer to the Thread Control Block of the currently running thread.
Definition thread.h:413
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition stdio.h:60
static void event_loop_multi(event_queue_t *queues, size_t n_queues)
Simple event loop with multiple queues.
Definition event.h:436
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:351
static void event_loop(event_queue_t *queue)
Simple event loop.
Definition event.h:486
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:184
void(* event_handler_t)(event_t *)
event handler type definition
Definition event.h:140
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:209
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition event.h:245
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:225
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:195
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:166
struct event event_t
event structure forward declaration
Definition event.h:135
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.
bool ztimer_release(ztimer_clock_t *clock)
Release a clock.
struct ztimer_clock ztimer_clock_t
ztimer_clock_t forward declaration
Definition ztimer.h:290
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
bool ztimer_acquire(ztimer_clock_t *clock)
Acquire a clock.
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:683
IRQ driver interface.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition modules.h:70
Pointer Tagging Helpers.
event queue structure
Definition event.h:153
thread_t * waiter
thread owning event queue
Definition event.h:155
clist_node_t event_list
list of queued events
Definition event.h:154
event structure
Definition event.h:145
event_handler_t handler
pointer to event handler function
Definition event.h:147
clist_node_t list_node
event queue list entry
Definition event.h:146
Thread Flags API.
ztimer API