UCX  1.8
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 
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 
38 
48 typedef unsigned (*ucs_callback_t)(void *arg);
49 
50 
59 typedef int (*ucs_callbackq_predicate_t)(const ucs_callbackq_elem_t *elem,
60  void *arg);
61 
62 
68  UCS_CALLBACKQ_FLAG_FAST = UCS_BIT(0),
71 };
72 
73 
78  ucs_callback_t cb;
79  void *arg;
80  unsigned flags;
81  int id;
82 };
83 
84 
88 struct ucs_callbackq {
93  ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT + 1];
94 
99  char priv[72];
100 };
101 
102 
108 ucs_status_t ucs_callbackq_init(ucs_callbackq_t *cbq);
109 
110 
116 void ucs_callbackq_cleanup(ucs_callbackq_t *cbq);
117 
118 
132 int ucs_callbackq_add(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg,
133  unsigned flags);
134 
135 
146 void ucs_callbackq_remove(ucs_callbackq_t *cbq, int id);
147 
148 
162 int ucs_callbackq_add_safe(ucs_callbackq_t *cbq, ucs_callback_t cb, void *arg,
163  unsigned flags);
164 
165 
176 void ucs_callbackq_remove_safe(ucs_callbackq_t *cbq, int id);
177 
178 
191 void ucs_callbackq_remove_if(ucs_callbackq_t *cbq, ucs_callbackq_predicate_t pred,
192  void *arg);
193 
194 
203 static inline unsigned ucs_callbackq_dispatch(ucs_callbackq_t *cbq)
204 {
205  ucs_callbackq_elem_t *elem;
206  ucs_callback_t cb;
207  unsigned count;
208 
209  count = 0;
210  for (elem = cbq->fast_elems; (cb = elem->cb) != NULL; ++elem) {
211  count += cb(elem->arg);
212  }
213  return count;
214 }
215 
216 END_C_DECLS
217 
218 #endif
char priv[72]
Definition: callbackq.h:99
Definition: callbackq.h:88
int id
Definition: callbackq.h:81
ucs_status_t
Status codes.
Definition: status.h:45
ucs_callbackq_elem_t fast_elems[UCS_CALLBACKQ_FAST_COUNT+1]
Definition: callbackq.h:93
Definition: callbackq.h:77
ucs_callback_t cb
Definition: callbackq.h:78
Definition: callbackq.h:68
unsigned flags
Definition: callbackq.h:80
Definition: callbackq.h:69
ucs_callbackq_flags
Definition: callbackq.h:67
void * arg
Definition: callbackq.h:79