os_eventq.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
22 #include <os/os_types.h>
23 
24 #include "event/callback.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
33 struct os_event
34 {
36  void *arg;
37 };
38 
42 struct os_eventq
43 {
45 };
46 
50 typedef void os_event_fn(struct os_event *ev);
51 
59 static inline void os_event_init(struct os_event *ev, os_event_fn * fn,
60  void *arg)
61 {
62  /*
63  * Need to clear list_node manually since init function below does not do
64  * this.
65  */
66  ev->e.super.list_node.next = NULL;
67  event_callback_init(&ev->e, (void(*)(void *))fn, ev);
68  ev->arg = arg;
69 }
70 
78 static inline bool os_event_is_queued(struct os_event *ev)
79 {
80  return (ev->e.super.list_node.next != NULL);
81 }
82 
88 static inline void *os_event_get_arg(struct os_event *ev)
89 {
90  return ev->arg;
91 }
92 
99 static inline void os_event_set_arg(struct os_event *ev, void *arg)
100 {
101  ev->arg = arg;
102 }
103 
109 static inline void os_event_run(struct os_event *ev)
110 {
111  ev->e.super.handler(&ev->e.super);
112 }
113 
119 static inline void os_eventq_init(struct os_eventq *evq)
120 {
122 }
123 
129 static inline int os_eventq_inited(struct os_eventq *evq)
130 {
131  return evq->q.waiter != NULL;
132 }
133 
141 static inline void os_eventq_deinit(struct os_eventq *evq)
142 {
143  (void) evq;
144  /* Can't deinit an eventq in RIOT */
145 }
146 
155 static inline struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo)
156 {
157  if (evq->q.waiter == NULL) {
158  event_queue_claim(&evq->q);
159  }
160 
161  if (tmo == 0) {
162  return (struct os_event *)event_get(&evq->q);
163  } else if (tmo == OS_WAIT_FOREVER) {
164  return (struct os_event *)event_wait(&evq->q);
165  } else {
166  return (struct os_event *)event_wait_timeout_ztimer(&evq->q,
167  ZTIMER_MSEC,
168  (uint32_t)tmo);
169  }
170 }
171 
177 static inline struct os_event * os_eventq_get_no_wait(struct os_eventq *evq)
178 {
179  if (evq->q.waiter == NULL) {
180  event_queue_claim(&evq->q);
181  }
182 
183  return (struct os_event *) event_get(&evq->q);
184 }
185 
192 static inline void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
193 {
194  event_post(&evq->q, &ev->e.super);
195 }
196 
203 static inline void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
204 {
205  event_cancel(&evq->q, &ev->e.super);
206 }
207 
213 static inline void os_eventq_run(struct os_eventq *evq)
214 {
215  struct os_event *ev = os_eventq_get(evq, OS_WAIT_FOREVER);
216  os_event_run(ev);
217 }
218 
226 static inline bool os_eventq_is_empty(struct os_eventq *evq)
227 {
228  return clist_count(&(evq->q.event_list)) == 0;
229 }
230 
231 #ifdef __cplusplus
232 }
233 #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:455
static event_t * event_wait(event_queue_t *queue)
Get next event from event queue, blocking.
Definition: event.h:351
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
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.
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
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:226
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition: os_eventq.h:59
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition: os_eventq.h:99
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition: os_eventq.h:129
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition: os_eventq.h:88
static void os_eventq_deinit(struct os_eventq *evq)
Deinitialize an event queue.
Definition: os_eventq.h:141
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition: os_eventq.h:203
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition: os_eventq.h:78
void os_event_fn(struct os_event *ev)
Event callback function.
Definition: os_eventq.h:50
static void os_event_run(struct os_event *ev)
Runs an event.
Definition: os_eventq.h:109
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition: os_eventq.h:119
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:177
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:155
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition: os_eventq.h:192
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition: os_eventq.h:213
mynewt-core types
uint32_t os_time_t
time type
Definition: os_types.h:47
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
Callback Event structure definition.
Definition: callback.h:48
event_t super
event_t structure that gets extended
Definition: callback.h:49
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
struct list_node * next
pointer to next list entry
Definition: list.h:40
Event wrapper.
Definition: os_eventq.h:34
void * arg
the event argument
Definition: os_eventq.h:36
event_callback_t e
the event callback
Definition: os_eventq.h:35
Event queue wrapper.
Definition: os_eventq.h:43
event_queue_t q
the event queue
Definition: os_eventq.h:44