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 
20 #ifndef OS_OS_EVENTQ_H
21 #define OS_OS_EVENTQ_H
22 
23 #include <os/os_types.h>
24 
25 #include "event/callback.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 struct os_event
35 {
37  void *arg;
38 };
39 
43 struct os_eventq
44 {
46 };
47 
51 typedef void os_event_fn(struct os_event *ev);
52 
60 static inline void os_event_init(struct os_event *ev, os_event_fn * fn,
61  void *arg)
62 {
63  /*
64  * Need to clear list_node manually since init function below does not do
65  * this.
66  */
67  ev->e.super.list_node.next = NULL;
68  event_callback_init(&ev->e, (void(*)(void *))fn, ev);
69  ev->arg = arg;
70 }
71 
79 static inline bool os_event_is_queued(struct os_event *ev)
80 {
81  return (ev->e.super.list_node.next != NULL);
82 }
83 
89 static inline void *os_event_get_arg(struct os_event *ev)
90 {
91  return ev->arg;
92 }
93 
100 static inline void os_event_set_arg(struct os_event *ev, void *arg)
101 {
102  ev->arg = arg;
103 }
104 
110 static inline void os_event_run(struct os_event *ev)
111 {
112  ev->e.super.handler(&ev->e.super);
113 }
114 
120 static inline void os_eventq_init(struct os_eventq *evq)
121 {
123 }
124 
130 static inline int os_eventq_inited(struct os_eventq *evq)
131 {
132  return evq->q.waiter != NULL;
133 }
134 
142 static inline void os_eventq_deinit(struct os_eventq *evq)
143 {
144  (void) evq;
145  /* Can't deinit an eventq in RIOT */
146 }
147 
156 static inline struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo)
157 {
158  if (evq->q.waiter == NULL) {
159  event_queue_claim(&evq->q);
160  }
161 
162  if (tmo == 0) {
163  return (struct os_event *)event_get(&evq->q);
164  } else if (tmo == OS_WAIT_FOREVER) {
165  return (struct os_event *)event_wait(&evq->q);
166  } else {
167  return (struct os_event *)event_wait_timeout_ztimer(&evq->q,
168  ZTIMER_MSEC,
169  (uint32_t)tmo);
170  }
171 }
172 
178 static inline struct os_event * os_eventq_get_no_wait(struct os_eventq *evq)
179 {
180  if (evq->q.waiter == NULL) {
181  event_queue_claim(&evq->q);
182  }
183 
184  return (struct os_event *) event_get(&evq->q);
185 }
186 
193 static inline void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
194 {
195  event_post(&evq->q, &ev->e.super);
196 }
197 
204 static inline void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
205 {
206  event_cancel(&evq->q, &ev->e.super);
207 }
208 
214 static inline void os_eventq_run(struct os_eventq *evq)
215 {
216  struct os_event *ev = os_eventq_get(evq, OS_WAIT_FOREVER);
217  os_event_run(ev);
218 }
219 
227 static inline bool os_eventq_is_empty(struct os_eventq *evq)
228 {
229  return clist_count(&(evq->q.event_list)) == 0;
230 }
231 
232 #ifdef __cplusplus
233 }
234 #endif
235 
236 #endif /* OS_OS_EVENTQ_H */
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:456
static event_t * event_wait(event_queue_t *queue)
Get next event from event queue, blocking.
Definition: event.h:354
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:212
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition: event.h:248
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:227
static void os_event_init(struct os_event *ev, os_event_fn *fn, void *arg)
Init a event.
Definition: os_eventq.h:60
static void os_event_set_arg(struct os_event *ev, void *arg)
Set the event argument.
Definition: os_eventq.h:100
static int os_eventq_inited(struct os_eventq *evq)
Check whether the event queue is initialized.
Definition: os_eventq.h:130
static void * os_event_get_arg(struct os_event *ev)
Returns event argument.
Definition: os_eventq.h:89
static void os_eventq_deinit(struct os_eventq *evq)
Deinitialize an event queue.
Definition: os_eventq.h:142
static void os_eventq_remove(struct os_eventq *evq, struct os_event *ev)
Remove an event from the queue.
Definition: os_eventq.h:204
static bool os_event_is_queued(struct os_event *ev)
Check if event is in queue.
Definition: os_eventq.h:79
void os_event_fn(struct os_event *ev)
Event callback function.
Definition: os_eventq.h:51
static void os_event_run(struct os_event *ev)
Runs an event.
Definition: os_eventq.h:110
static void os_eventq_init(struct os_eventq *evq)
Initialize the event queue.
Definition: os_eventq.h:120
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:178
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:156
static void os_eventq_put(struct os_eventq *evq, struct os_event *ev)
Put an event on the event queue.
Definition: os_eventq.h:193
static void os_eventq_run(struct os_eventq *evq)
Gets and runs an event from the queue callback.
Definition: os_eventq.h:214
mynewt-core types
uint32_t os_time_t
time type
Definition: os_types.h:48
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
Callback Event structure definition.
Definition: callback.h:49
event_t super
event_t structure that gets extended
Definition: callback.h:50
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
struct list_node * next
pointer to next list entry
Definition: list.h:41
Event wrapper.
Definition: os_eventq.h:35
void * arg
the event argument
Definition: os_eventq.h:37
event_callback_t e
the event callback
Definition: os_eventq.h:36
Event queue wrapper.
Definition: os_eventq.h:44
event_queue_t q
the event queue
Definition: os_eventq.h:45