1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright (C) Mellanox Technologies Ltd. 2001-2016. ALL RIGHTS RESERVED.
* Copyright (C) ARM Ltd. 2016-2017. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
/** @file callbackq.h */
/*
* Thread-safe callback queue:
* - only one thread can dispatch
* - any thread can add and remove
* - add/remove operations are O(1)
*/
/*
* Forward declarations
*/
typedef struct ucs_callbackq ucs_callbackq_t;
typedef struct ucs_callbackq_elem ucs_callbackq_elem_t;
/**
* Callback which can be placed in a queue.
*
* @param [in] arg User-defined argument for the callback.
*
* @return Count of how much "work" was done by the callback. For example, zero
* means that no work was done, and any nonzero value means that something
* was done.
*/
typedef unsigned ;
/**
* Callback queue element predicate.
*
* @param [in] elem Callback queue element to check.
* @param [in] arg User-defined argument.
*
* @return Predicate result value - nonzero means "true", zero means "false".
*/
typedef int ;
/**
* @ingroup UCS_RESOURCE
* Callback flags
*/
;
/**
* Callback queue element.
*/
;
/**
* A queue of callback to execute
*/
;
/**
* Initialize the callback queue.
*
* @param [in] cbq Callback queue to initialize.
*/
ucs_status_t ;
/**
* Clean up the callback queue and release associated memory.
*
* @param [in] cbq Callback queue to clean up.
*/
void ;
/**
* Add a callback to the queue.
* This is *not* safe to call while another thread might be dispatching callbacks.
* However, it can be used from the dispatch context (e.g a callback may use this
* function to add another callback).
*
* @param [in] cbq Callback queue to add the callback to.
* @param [in] cb Callback to add.
* @param [in] arg User-defined argument for the callback.
* @param [in] flags Flags for the callback, from @ref ucs_callbackq_flags.
*
* @return Unique identifier of the callback in the queue.
*/
int ;
/**
* Remove a callback from the queue immediately.
* This is *not* safe to call while another thread might be dispatching callbacks.
* However, it can be used from the dispatch context (e.g a callback may use this
* function to remove itself or another callback). In this case, the callback may
* still be dispatched once after this function returned.
*
* @param [in] cbq Callback queue to remove the callback from.
* @param [in] id Callback identifier to remove.
*/
void ;
/**
* Add a callback to the queue.
* This can be used from any context and any thread, including but not limited to:
* - A callback can add another callback.
* - A thread can add a callback while another thread is dispatching callbacks.
*
* @param [in] cbq Callback queue to add the callback to.
* @param [in] cb Callback to add.
* @param [in] arg User-defined argument for the callback.
* @param [in] flags Flags for the callback, from @ref ucs_callbackq_flags.
*
* @return Unique identifier of the callback in the queue.
*/
int ;
/**
* Remove a callback from the queue in a safe but lazy fashion. The callback will
* be removed at some point in the near future.
* This can be used from any context and any thread, including but not limited to:
* - A callback can remove another callback or itself.
* - A thread can't remove a callback while another thread is dispatching callbacks.
*
* @param [in] cbq Callback queue to remove the callback from.
* @param [in] id Callback identifier to remove.
*/
void ;
/**
* Remove all callbacks from the queue for which the given predicate returns
* "true" (nonzero) value.
* This is *not* safe to call while another thread might be dispatching callbacks.
* However, it can be used from the dispatch context (e.g a callback may use this
* function to remove itself or another callback). In this case, the callback may
* still be dispatched once after this function returned.
*
* @param [in] cbq Callback queue.
* @param [in] pred Predicate to check candidates for removal.
* @param [in] arg User-defined argument for the predicate.
*/
void ;
/**
* Dispatch callbacks from the callback queue.
* Must be called from single thread only.
*
* @param [in] cbq Callback queue to dispatch callbacks from.
* @return Sum of all return values from the dispatched callbacks.
*/
static inline unsigned