UCX  1.5
Unified Communication X
callbackq.h
1 
8 #ifndef UCS_CALLBACKQ_H
9 #define UCS_CALLBACKQ_H
10 
11 #include <ucs/datastruct/list_types.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 
19 /*
20  * Thread-safe callback queue:
21  * - only one thread can dispatch
22  * - any thread can add and remove
23  * - add/remove operations are O(1)
24  */
25 
26 #define UCS_CALLBACKQ_FAST_COUNT 7 /* Max. number of fast-path callbacks */
27 #define UCS_CALLBACKQ_ID_NULL (-1) /* Invalid callback identifier */
28 
29 
30 /*
31  * Forward declarations
32  */
33 typedef struct ucs_callbackq ucs_callbackq_t;
35 
36 
46 typedef unsigned (*ucs_callback_t)(void *arg);
47 
48 
57 typedef int (*ucs_callbackq_predicate_t)(const ucs_callbackq_elem_t *elem,
58  void *arg);
59 
60 
66  UCS_CALLBACKQ_FLAG_FAST = UCS_BIT(0),
69 };
70 
71 
76  ucs_callback_t cb;
77  void *arg;
78  unsigned flags;
79  int id;
80 };
81 
82 
86 struct ucs_callbackq {
91  ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT + 1];
92 
97  char priv[72];
98 };
99 
100 
106 ucs_status_t ucs_callbackq_init(ucs_callbackq_t *cbq);
107 
108 
114 void ucs_callbackq_cleanup(ucs_callbackq_t *cbq);
115 
116 
130 int ucs_callbackq_add(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg,
131  unsigned flags);
132 
133 
144 void ucs_callbackq_remove(ucs_callbackq_t *cbq, int id);
145 
146 
160 int ucs_callbackq_add_safe(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg,
161  unsigned flags);
162 
163 
174 void ucs_callbackq_remove_safe(ucs_callbackq_t *cbq, int id);
175 
176 
189 void ucs_callbackq_remove_if(ucs_callbackq_t *cbq, ucs_callbackq_predicate_t pred,
190  void *arg);
191 
192 
201 static inline unsigned ucs_callbackq_dispatch(ucs_callbackq_t *cbq)
202 {
203  ucs_callbackq_elem_t *elem;
204  ucs_callback_t cb;
205  unsigned count;
206 
207  count = 0;
208  for (elem = cbq->fast_elems; (cb = elem->cb) != NULL; ++elem) {
209  count += cb(elem->arg);
210  }
211  return count;
212 }
213 
214 END_C_DECLS
215 
216 #endif
char priv[72]
Definition: callbackq.h:97
Definition: callbackq.h:86
int id
Definition: callbackq.h:79
ucs_status_t
Status codes.
Definition: status.h:43
ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT+1]
Definition: callbackq.h:91
Definition: callbackq.h:75
ucs_callback_t cb
Definition: callbackq.h:76
Definition: callbackq.h:66
unsigned flags
Definition: callbackq.h:78
Definition: callbackq.h:67
ucs_callbackq_flags
Definition: callbackq.h:65
void * arg
Definition: callbackq.h:77