Loading...
Searching...
No Matches
os_eventq.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Inria
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
18
19#include <os/os_types.h>
20
21#include "event/callback.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
31{
33 void *arg;
34};
35
40{
42};
43
47typedef void os_event_fn(struct os_event *ev);
48
56static inline void os_event_init(struct os_event *ev, os_event_fn * fn,
57 void *arg)
58{
59 /*
60 * Need to clear list_node manually since init function below does not do
61 * this.
62 */
63 ev->e.super.list_node.next = NULL;
64 event_callback_init(&ev->e, (void(*)(void *))fn, ev);
65 ev->arg = arg;
66}
67
75static inline bool os_event_is_queued(struct os_event *ev)
76{
77 return (ev->e.super.list_node.next != NULL);
78}
79
85static inline void *os_event_get_arg(struct os_event *ev)
86{
87 return ev->arg;
88}
89
96static inline void os_event_set_arg(struct os_event *ev, void *arg)
97{
98 ev->arg = arg;
99}
100
106static inline void os_event_run(struct os_event *ev)
107{
108 ev->e.super.handler(&ev->e.super);
109}
110
116static inline void os_eventq_init(struct os_eventq *evq)
117{
119}
120
126static inline int os_eventq_inited(struct os_eventq *evq)
127{
128 return evq->q.waiter != NULL;
129}
130
138static inline void os_eventq_deinit(struct os_eventq *evq)
139{
140 (void) evq;
141 /* Can't deinit an eventq in RIOT */
142}
143
152static inline struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo)
153{
154 if (evq->q.waiter == NULL) {
155 event_queue_claim(&evq->q);
156 }
157
158 if (tmo == 0) {
159 return (struct os_event *)event_get(&evq->q);
160 } else if (tmo == OS_WAIT_FOREVER) {
161 return (struct os_event *)event_wait(&evq->q);
162 } else {
163 return (struct os_event *)event_wait_timeout_ztimer(&evq->q,
165 (uint32_t)tmo);
166 }
167}
168
174static inline struct os_event * os_eventq_get_no_wait(struct os_eventq *evq)
175{
176 if (evq->q.waiter == NULL) {
177 event_queue_claim(&evq->q);
178 }
179
180 return (struct os_event *) event_get(&evq->q);
181}
182
189static inline void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
190{
191 event_post(&evq->q, &ev->e.super);
192}
193
200static inline void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
201{
202 event_cancel(&evq->q, &ev->e.super);
203}
204
210static inline void os_eventq_run(struct os_eventq *evq)
211{
212 struct os_event *ev = os_eventq_get(evq, OS_WAIT_FOREVER);
213 os_event_run(ev);
214}
215
223static inline bool os_eventq_is_empty(struct os_eventq *evq)
224{
225 return clist_count(&(evq->q.event_list)) == 0;
226}
227
228#ifdef __cplusplus
229}
230#endif
Provides a callback-with-argument event type.
void event_callback_init(event_callback_t *event_callback, void(*callback)(void *), void *arg)
event callback initialization function
static size_t clist_count(clist_node_t *list)
Count the number of items in the given list.
Definition clist.h:502
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:348
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.
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:206
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition event.h:242
event_t * event_get(event_queue_t *queue)
Get next event from event queue, non-blocking.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
static bool os_eventq_is_empty(struct os_eventq *evq)
Check if queue is empty.
Definition os_eventq.h:223
static struct os_event * os_eventq_get_no_wait(struct os_eventq *evq)
Get next event from event queue, non-blocking.
Definition os_eventq.h:174
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition os_eventq.h:56
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition os_eventq.h:96
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition os_eventq.h:126
static struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo)
Get next event from event queue.
Definition os_eventq.h:152
static void os_eventq_deinit(struct os_eventq *evq)
Deinitialize an event queue.
Definition os_eventq.h:138
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition os_eventq.h:200
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition os_eventq.h:75
void os_event_fn(struct os_event *ev)
Event callback function.
Definition os_eventq.h:47
static void os_event_run(struct os_event *ev)
Runs an event.
Definition os_eventq.h:106
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition os_eventq.h:116
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition os_eventq.h:189
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition os_eventq.h:85
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition os_eventq.h:210
mynewt-core types
uint32_t os_time_t
time type
Definition os_types.h:44
thread_t * waiter
thread owning event queue
Definition event.h:152
clist_node_t event_list
list of queued events
Definition event.h:151
Callback Event structure definition.
Definition callback.h:45
event_t super
event_t structure that gets extended
Definition callback.h:46
event_handler_t handler
pointer to event handler function
Definition event.h:144
clist_node_t list_node
event queue list entry
Definition event.h:143
struct list_node * next
pointer to next list entry
Definition list.h:37
Event wrapper.
Definition os_eventq.h:31
void * arg
the event argument
Definition os_eventq.h:33
event_callback_t e
the event callback
Definition os_eventq.h:32
Event queue wrapper.
Definition os_eventq.h:40
event_queue_t q
the event queue
Definition os_eventq.h:41