dpl_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 DPL_DPL_EVENTQ_H
21 #define DPL_DPL_EVENTQ_H
22 
23 #include <dpl/dpl_types.h>
24 
25 #include "uwb_core.h"
26 #include "event/callback.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 struct dpl_event
36 {
38  void *arg;
39 };
40 
44 struct dpl_eventq
45 {
47 };
48 
52 typedef void dpl_event_fn(struct dpl_event *ev);
53 
61 static inline void dpl_event_init(struct dpl_event *ev, dpl_event_fn * fn,
62  void *arg)
63 {
64  /*
65  * Need to clear list_node manually since init function below does not do
66  * this.
67  */
68  ev->e.super.list_node.next = NULL;
69  event_callback_init(&ev->e, (void(*)(void *))fn, ev);
70  ev->arg = arg;
71 }
72 
80 static inline bool dpl_event_is_queued(struct dpl_event *ev)
81 {
82  return (ev->e.super.list_node.next != NULL);
83 }
84 
90 static inline void *dpl_event_get_arg(struct dpl_event *ev)
91 {
92  return ev->arg;
93 }
94 
101 static inline void dpl_event_set_arg(struct dpl_event *ev, void *arg)
102 {
103  ev->arg = arg;
104 }
105 
111 static inline void dpl_event_run(struct dpl_event *ev)
112 {
113  ev->e.super.handler(&ev->e.super);
114 }
115 
121 static inline void dpl_eventq_init(struct dpl_eventq *evq)
122 {
124 }
125 
131 static inline int dpl_eventq_inited(struct dpl_eventq *evq)
132 {
133  return evq->q.waiter != NULL;
134 }
135 
143 static inline void dpl_eventq_deinit(struct dpl_eventq *evq)
144 {
145  (void) evq;
146  /* Can't deinit an eventq in RIOT */
147 }
148 
156 static inline struct dpl_event * dpl_eventq_get(struct dpl_eventq *evq)
157 {
158  if (evq->q.waiter == NULL) {
159  event_queue_claim(&evq->q);
160  }
161 
162  return (struct dpl_event *) event_wait(&evq->q);
163 }
164 
170 static inline struct dpl_event * dpl_eventq_get_no_wait(struct dpl_eventq *evq)
171 {
172  if (evq->q.waiter == NULL) {
173  event_queue_claim(&evq->q);
174  }
175 
176  return (struct dpl_event *) event_get(&evq->q);
177 }
178 
185 static inline void dpl_eventq_put(struct dpl_eventq *evq, struct dpl_event *ev)
186 {
187  event_post(&evq->q, &ev->e.super);
188 }
189 
196 static inline void dpl_eventq_remove(struct dpl_eventq *evq, struct dpl_event *ev)
197 {
198  event_cancel(&evq->q, &ev->e.super);
199 }
200 
206 static inline void dpl_eventq_run(struct dpl_eventq *evq)
207 {
208  struct dpl_event *ev = dpl_eventq_get(evq);
209  dpl_event_run(ev);
210 }
211 
219 static inline bool dpl_eventq_is_empty(struct dpl_eventq *evq)
220 {
221  return clist_count(&(evq->q.event_list)) == 0;
222 }
223 
232 static inline struct dpl_eventq * dpl_eventq_dflt_get(void)
233 {
234  return (struct dpl_eventq*) uwb_core_get_eventq();
235 }
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif /* DPL_DPL_EVENTQ_H */
event_callback_t::super
event_t super
event_t structure that gets extended
Definition: callback.h:49
dpl_event::e
event_callback_t e
the event callback
Definition: dpl_eventq.h:37
dpl_eventq_dflt_get
static struct dpl_eventq * dpl_eventq_dflt_get(void)
Retrieves the default event queue.
Definition: dpl_eventq.h:232
dpl_event_fn
void dpl_event_fn(struct dpl_event *ev)
dpl event callback function
Definition: dpl_eventq.h:52
event::list_node
clist_node_t list_node
event queue list entry
Definition: event.h:143
event_cancel
void event_cancel(event_queue_t *queue, event_t *event)
Cancel a queued event.
uwb_core.h
dpl_event
dpl event wrapper
Definition: dpl_eventq.h:35
uwb_core_get_eventq
event_queue_t * uwb_core_get_eventq(void)
Retrieves the default event queue.
callback.h
Provides a callback-with-argument event type.
event_queue_t
event queue structure
Definition: event.h:150
dpl_eventq::q
event_queue_t q
the event queue
Definition: dpl_eventq.h:46
event_callback_t
Callback Event structure definition.
Definition: callback.h:48
dpl_event::arg
void * arg
the event argument
Definition: dpl_eventq.h:38
dpl_eventq_run
static void dpl_eventq_run(struct dpl_eventq *evq)
Gets and runs an event from the queue callback.
Definition: dpl_eventq.h:206
dpl_eventq_get_no_wait
static struct dpl_event * dpl_eventq_get_no_wait(struct dpl_eventq *evq)
Get next event from event queue, non-blocking.
Definition: dpl_eventq.h:170
dpl_event_set_arg
static void dpl_event_set_arg(struct dpl_event *ev, void *arg)
Set the vent arg.
Definition: dpl_eventq.h:101
dpl_event_get_arg
static void * dpl_event_get_arg(struct dpl_event *ev)
Runs an event.
Definition: dpl_eventq.h:90
event_queue_init_detached
static void event_queue_init_detached(event_queue_t *queue)
Initialize an event queue not binding it to a thread.
Definition: event.h:207
event_post
void event_post(event_queue_t *queue, event_t *event)
Queue an event.
dpl_event_run
static void dpl_event_run(struct dpl_event *ev)
Runs an event.
Definition: dpl_eventq.h:111
dpl_eventq_get
static struct dpl_event * dpl_eventq_get(struct dpl_eventq *evq)
Get next event from event queue, blocking.
Definition: dpl_eventq.h:156
dpl_eventq_inited
static int dpl_eventq_inited(struct dpl_eventq *evq)
Check whether the event queue is initialized.
Definition: dpl_eventq.h:131
dpl_eventq_remove
static void dpl_eventq_remove(struct dpl_eventq *evq, struct dpl_event *ev)
Remove an event from the queue.
Definition: dpl_eventq.h:196
dpl_eventq_put
static void dpl_eventq_put(struct dpl_eventq *evq, struct dpl_event *ev)
Put an event on the event queue.
Definition: dpl_eventq.h:185
dpl_eventq_is_empty
static bool dpl_eventq_is_empty(struct dpl_eventq *evq)
Check if queue is empty.
Definition: dpl_eventq.h:219
dpl_eventq
dpl event queue wrapper
Definition: dpl_eventq.h:44
dpl_eventq_deinit
static void dpl_eventq_deinit(struct dpl_eventq *evq)
Deinitialize an event queue.
Definition: dpl_eventq.h:143
event_wait
static event_t * event_wait(event_queue_t *queue)
Get next event from event queue, blocking.
Definition: event.h:329
event_queue_t::event_list
clist_node_t event_list
list of queued events
Definition: event.h:151
list_node::next
struct list_node * next
pointer to next list entry
Definition: list.h:41
dpl_event_is_queued
static bool dpl_event_is_queued(struct dpl_event *ev)
Check if event is in queue.
Definition: dpl_eventq.h:80
event_queue_t::waiter
thread_t * waiter
thread ownning event queue
Definition: event.h:152
dpl_types.h
uwb-core DPL (Decawave Porting Layer) types
clist_count
static size_t clist_count(clist_node_t *list)
Count the number of items in the given list.
Definition: clist.h:438
dpl_event_init
static void dpl_event_init(struct dpl_event *ev, dpl_event_fn *fn, void *arg)
Init a event.
Definition: dpl_eventq.h:61
event::handler
event_handler_t handler
pointer to event handler function
Definition: event.h:144
dpl_eventq_init
static void dpl_eventq_init(struct dpl_eventq *evq)
Initialize the event queue.
Definition: dpl_eventq.h:121
event_get
event_t * event_get(event_queue_t *queue)
Get next event from event queue, non-blocking.
event_queue_claim
static void event_queue_claim(event_queue_t *queue)
Bind an event queue to the calling thread.
Definition: event.h:243
event_callback_init
void event_callback_init(event_callback_t *event_callback, void(*callback)(void *), void *arg)
event callback initialization function