UCX  1.18
Unified Communication X
callbackq.h
1 
8 #ifndef UCS_CALLBACKQ_H
9 #define UCS_CALLBACKQ_H
10 
11 #include <ucs/datastruct/list.h>
12 #include <ucs/sys/compiler_def.h>
13 #include <ucs/type/status.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 BEGIN_C_DECLS
18 
21 /*
22  * Thread-safe callback queue:
23  * - only one thread can dispatch
24  * - any thread can add and remove
25  * - add/remove operations are O(1)
26  */
27 
28 #define UCS_CALLBACKQ_FAST_COUNT 7 /* Max. number of fast-path callbacks */
29 #define UCS_CALLBACKQ_ID_NULL (-1) /* Invalid callback identifier */
30 
31 
32 /*
33  * Forward declarations
34  */
35 typedef struct ucs_callbackq ucs_callbackq_t;
37 typedef struct ucs_callbackq_priv ucs_callbackq_priv_t;
38 typedef void * ucs_callbackq_key_t;
39 
40 
50 typedef unsigned (*ucs_callback_t)(void *arg);
51 
52 
61 typedef int (*ucs_callbackq_predicate_t)(const ucs_callbackq_elem_t *elem,
62  void *arg);
63 
64 
69  ucs_callback_t cb;
70  void *arg;
71 };
72 
73 
77 struct ucs_callbackq {
82  ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT + 1];
83 
88  ucs_callbackq_priv_t *priv;
89 };
90 
91 
97 ucs_status_t ucs_callbackq_init(ucs_callbackq_t *cbq);
98 
99 
105 void ucs_callbackq_cleanup(ucs_callbackq_t *cbq);
106 
107 
120 int ucs_callbackq_add(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg);
121 
122 
135 void *ucs_callbackq_remove(ucs_callbackq_t *cbq, int id);
136 
137 
150 int ucs_callbackq_add_safe(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg);
151 
152 
165 void *ucs_callbackq_remove_safe(ucs_callbackq_t *cbq, int id);
166 
167 
181 void ucs_callbackq_add_oneshot(ucs_callbackq_t *cbq, ucs_callbackq_key_t key,
182  ucs_callback_t cb, void *arg);
183 
184 
195 void ucs_callbackq_remove_oneshot(ucs_callbackq_t *cbq, ucs_callbackq_key_t key,
196  ucs_callbackq_predicate_t pred, void *arg);
197 
198 
207 static inline unsigned ucs_callbackq_dispatch(ucs_callbackq_t *cbq)
208 {
209  ucs_callbackq_elem_t *elem;
210  ucs_callback_t cb;
211  unsigned count;
212 
213  count = 0;
214  for (elem = cbq->fast_elems; (cb = elem->cb) != NULL; ++elem) {
215  count += cb(elem->arg);
216  }
217  return count;
218 }
219 
220 END_C_DECLS
221 
222 #endif
ucs_callbackq_priv_t * priv
Definition: callbackq.h:88
Definition: callbackq.h:77
ucs_status_t
Status codes.
Definition: status.h:45
ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT+1]
Definition: callbackq.h:82
Definition: callbackq.h:68
ucs_callback_t cb
Definition: callbackq.h:69
void * arg
Definition: callbackq.h:70