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 <string.h>
101 
102 #include "assert.h"
103 #include "clist.h"
104 #include "irq.h"
105 #include "thread.h"
106 #include "thread_flags.h"
107 #include "ptrtag.h"
108 
109 #if IS_USED(MODULE_ZTIMER)
110 #include "ztimer.h"
111 #endif
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif
116 
117 #ifndef THREAD_FLAG_EVENT
121 #define THREAD_FLAG_EVENT (0x1)
122 #endif
123 
127 #define EVENT_QUEUE_INIT { .waiter = thread_get_active() }
128 
132 #define EVENT_QUEUE_INIT_DETACHED { .waiter = NULL }
133 
137 typedef struct event event_t;
138 
142 typedef void (*event_handler_t)(event_t *);
143 
147 struct event {
150 };
151 
155 typedef struct PTRTAG {
159 
168 static inline void event_queues_init(event_queue_t *queues,
169  size_t n_queues)
170 {
171  assert(queues && n_queues);
172  thread_t *me = thread_get_active();
173  for (size_t i = 0; i < n_queues; i++) {
174  memset(&queues[i], '\0', sizeof(queues[0]));
175  queues[i].waiter = me;
176  }
177 }
178 
186 static inline void event_queue_init(event_queue_t *queue)
187 {
188  event_queues_init(queue, 1);
189 }
190 
197 static inline void event_queues_init_detached(event_queue_t *queues,
198  size_t n_queues)
199 {
200  assert(queues);
201  for (size_t i = 0; i < n_queues; i++) {
202  memset(&queues[i], '\0', sizeof(queues[0]));
203  }
204 }
205 
211 static inline void event_queue_init_detached(event_queue_t *queue)
212 {
213  event_queues_init_detached(queue, 1);
214 }
215 
227 static inline void event_queues_claim(event_queue_t *queues, size_t n_queues)
228 {
229  assert(queues);
230  thread_t *me = thread_get_active();
231  for (size_t i = 0; i < n_queues; i++) {
232  assert(queues[i].waiter == NULL);
233  queues[i].waiter = me;
234  }
235 }
236 
247 static inline void event_queue_claim(event_queue_t *queue)
248 {
249  event_queues_claim(queue, 1);
250 }
251 
264 
276 
289 
319 event_t *event_wait_multi(event_queue_t *queues, size_t n_queues);
320 
335 static inline event_t *event_wait(event_queue_t *queue)
336 {
337  return event_wait_multi(queue, 1);
338 }
339 
340 #if IS_USED(MODULE_XTIMER) || defined(DOXYGEN)
350 event_t *event_wait_timeout(event_queue_t *queue, uint32_t timeout);
351 
361 event_t *event_wait_timeout64(event_queue_t *queue, uint64_t timeout);
362 #endif
363 
364 #if IS_USED(MODULE_ZTIMER) || defined(DOXYGEN)
380  ztimer_clock_t *clock, uint32_t timeout);
381 #endif
382 
404 static inline void event_loop_multi(event_queue_t *queues, size_t n_queues)
405 {
406  event_t *event;
407 
408  while ((event = event_wait_multi(queues, n_queues))) {
409  event->handler(event);
410  }
411 }
412 
429 static inline void event_loop(event_queue_t *queue)
430 {
431  event_loop_multi(queue, 1);
432 }
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 #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:104
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:394
static event_t * event_wait(event_queue_t *queue)
Get next event from event queue, blocking.
Definition: event.h:335
static void event_loop_multi(event_queue_t *queues, size_t n_queues)
Simple event loop with multiple queues.
Definition: event.h:404
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
event_t * event_wait_timeout64(event_queue_t *queue, uint64_t timeout)
Get next event from event queue, blocking until timeout expires.
static void event_loop(event_queue_t *queue)
Simple event loop.
Definition: event.h:429
static void event_queue_init(event_queue_t *queue)
Initialize an event queue.
Definition: event.h:186
void(* event_handler_t)(event_t *)
event handler type definition
Definition: event.h:142
void event_cancel(event_queue_t *queue, event_t *event)
Cancel a queued event.
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.
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:211
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition: event.h:247
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:227
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:197
static void event_queues_init(event_queue_t *queues, size_t n_queues)
Initialize an array of event queues.
Definition: event.h:168
event_t * event_get(event_queue_t *queue)
Get next event from event queue, non-blocking.
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.
IRQ driver interface.
Pointer Tagging Helpers.
event queue structure
Definition: event.h:155
thread_t * waiter
thread owning event queue
Definition: event.h:157
clist_node_t event_list
list of queued events
Definition: event.h:156
thread_t holds thread's context data.
Definition: thread.h:169
event structure
Definition: event.h:147
event_handler_t handler
pointer to event handler function
Definition: event.h:149
clist_node_t list_node
event queue list entry
Definition: event.h:148
List node structure.
Definition: list.h:40
ztimer device structure
Definition: ztimer.h:331
Thread Flags API.
ztimer API