Commit | Line | Data |
---|---|---|
8ad4ce58 MD |
1 | #ifndef _URCU_WFCQUEUE_H |
2 | #define _URCU_WFCQUEUE_H | |
3 | ||
4 | /* | |
47215721 | 5 | * urcu/wfcqueue.h |
8ad4ce58 MD |
6 | * |
7 | * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue | |
8 | * | |
9 | * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * Copyright 2011-2012 - Lai Jiangshan <laijs@cn.fujitsu.com> | |
11 | * | |
12 | * This library is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public | |
14 | * License as published by the Free Software Foundation; either | |
15 | * version 2.1 of the License, or (at your option) any later version. | |
16 | * | |
17 | * This library is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * Lesser General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this library; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
25 | */ | |
26 | ||
27 | #include <pthread.h> | |
8ad4ce58 MD |
28 | #include <stdbool.h> |
29 | #include <urcu/compiler.h> | |
30 | #include <urcu/arch.h> | |
31 | ||
32 | #ifdef __cplusplus | |
33 | extern "C" { | |
34 | #endif | |
35 | ||
36 | /* | |
37 | * Concurrent queue with wait-free enqueue/blocking dequeue. | |
38 | * | |
ebfd2673 MD |
39 | * This queue has been designed and implemented collaboratively by |
40 | * Mathieu Desnoyers and Lai Jiangshan. Inspired from | |
41 | * half-wait-free/half-blocking queue implementation done by Paul E. | |
42 | * McKenney. | |
8ad4ce58 MD |
43 | */ |
44 | ||
0b73c819 | 45 | #define CDS_WFCQ_WOULDBLOCK ((struct cds_wfcq_node *) -1UL) |
47215721 | 46 | |
23773356 | 47 | enum cds_wfcq_ret { |
f878b49e | 48 | CDS_WFCQ_RET_WOULDBLOCK = -1, |
23773356 MD |
49 | CDS_WFCQ_RET_DEST_EMPTY = 0, |
50 | CDS_WFCQ_RET_DEST_NON_EMPTY = 1, | |
51 | CDS_WFCQ_RET_SRC_EMPTY = 2, | |
52 | }; | |
53 | ||
eec791af MD |
54 | enum cds_wfcq_state { |
55 | CDS_WFCQ_STATE_LAST = (1U << 0), | |
56 | }; | |
57 | ||
8ad4ce58 MD |
58 | struct cds_wfcq_node { |
59 | struct cds_wfcq_node *next; | |
60 | }; | |
61 | ||
62 | /* | |
63 | * Do not put head and tail on the same cache-line if concurrent | |
64 | * enqueue/dequeue are expected from many CPUs. This eliminates | |
65 | * false-sharing between enqueue and dequeue. | |
66 | */ | |
f637f191 MD |
67 | struct __cds_wfcq_head { |
68 | struct cds_wfcq_node node; | |
69 | }; | |
70 | ||
8ad4ce58 MD |
71 | struct cds_wfcq_head { |
72 | struct cds_wfcq_node node; | |
73 | pthread_mutex_t lock; | |
74 | }; | |
75 | ||
f637f191 | 76 | /* |
0cdbb569 | 77 | * In C, the transparent union allows calling functions that work on both |
f637f191 MD |
78 | * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two |
79 | * types. | |
0cdbb569 MD |
80 | * |
81 | * In C++, implement static inline wrappers using function overloading | |
82 | * to obtain an API similar to C. | |
f637f191 | 83 | */ |
5135c0fd | 84 | typedef union { |
f637f191 MD |
85 | struct __cds_wfcq_head *_h; |
86 | struct cds_wfcq_head *h; | |
0cdbb569 | 87 | } caa_c_transparent_union cds_wfcq_head_ptr_t; |
f637f191 | 88 | |
0cdbb569 | 89 | #ifndef __cplusplus |
4d0d7cbc MD |
90 | /* |
91 | * This static inline is only present for compatibility with C++. It is | |
92 | * effect-less in C. | |
93 | */ | |
94 | static inline struct __cds_wfcq_head *__cds_wfcq_head_cast(struct __cds_wfcq_head *head) | |
95 | { | |
96 | return head; | |
97 | } | |
98 | ||
99 | /* | |
100 | * This static inline is only present for compatibility with C++. It is | |
101 | * effect-less in C. | |
102 | */ | |
103 | static inline struct cds_wfcq_head *cds_wfcq_head_cast(struct cds_wfcq_head *head) | |
104 | { | |
105 | return head; | |
106 | } | |
107 | #else /* #ifndef __cplusplus */ | |
108 | ||
0cdbb569 | 109 | /* |
aaf0064b MD |
110 | * This static inline is used by internally in the static inline |
111 | * implementation of the API. | |
0cdbb569 | 112 | */ |
4d0d7cbc MD |
113 | static inline cds_wfcq_head_ptr_t __cds_wfcq_head_cast(struct __cds_wfcq_head *head) |
114 | { | |
115 | cds_wfcq_head_ptr_t ret = { ._h = head }; | |
116 | return ret; | |
117 | } | |
0cdbb569 MD |
118 | |
119 | /* | |
aaf0064b MD |
120 | * This static inline is used by internally in the static inline |
121 | * implementation of the API. | |
0cdbb569 | 122 | */ |
4d0d7cbc MD |
123 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast(struct cds_wfcq_head *head) |
124 | { | |
125 | cds_wfcq_head_ptr_t ret = { .h = head }; | |
126 | return ret; | |
127 | } | |
128 | #endif /* #else #ifndef __cplusplus */ | |
129 | ||
8ad4ce58 MD |
130 | struct cds_wfcq_tail { |
131 | struct cds_wfcq_node *p; | |
132 | }; | |
133 | ||
134 | #ifdef _LGPL_SOURCE | |
135 | ||
136 | #include <urcu/static/wfcqueue.h> | |
137 | ||
138 | #define cds_wfcq_node_init _cds_wfcq_node_init | |
139 | #define cds_wfcq_init _cds_wfcq_init | |
b4edfa81 | 140 | #define __cds_wfcq_init ___cds_wfcq_init |
200d100e | 141 | #define cds_wfcq_destroy _cds_wfcq_destroy |
8ad4ce58 MD |
142 | #define cds_wfcq_empty _cds_wfcq_empty |
143 | #define cds_wfcq_enqueue _cds_wfcq_enqueue | |
144 | ||
145 | /* Dequeue locking */ | |
146 | #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock | |
147 | #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock | |
148 | ||
149 | /* Locking performed within cds_wfcq calls. */ | |
150 | #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking | |
eec791af MD |
151 | #define cds_wfcq_dequeue_with_state_blocking \ |
152 | _cds_wfcq_dequeue_with_state_blocking | |
8ad4ce58 MD |
153 | #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking |
154 | #define cds_wfcq_first_blocking _cds_wfcq_first_blocking | |
155 | #define cds_wfcq_next_blocking _cds_wfcq_next_blocking | |
156 | ||
157 | /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */ | |
158 | #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking | |
eec791af MD |
159 | #define __cds_wfcq_dequeue_with_state_blocking \ |
160 | ___cds_wfcq_dequeue_with_state_blocking | |
8ad4ce58 MD |
161 | #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking |
162 | #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking | |
163 | #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking | |
164 | ||
47215721 MD |
165 | /* |
166 | * Locking ensured by caller by holding cds_wfcq_dequeue_lock(). | |
167 | * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they | |
168 | * need to block. splice returns nonzero if it needs to block. | |
169 | */ | |
170 | #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking | |
eec791af MD |
171 | #define __cds_wfcq_dequeue_with_state_nonblocking \ |
172 | ___cds_wfcq_dequeue_with_state_nonblocking | |
47215721 MD |
173 | #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking |
174 | #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking | |
175 | #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking | |
176 | ||
8ad4ce58 MD |
177 | #else /* !_LGPL_SOURCE */ |
178 | ||
179 | /* | |
180 | * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API | |
181 | * | |
f878b49e MD |
182 | * Synchronization table: |
183 | * | |
184 | * External synchronization techniques described in the API below is | |
185 | * required between pairs marked with "X". No external synchronization | |
186 | * required between pairs marked with "-". | |
187 | * | |
188 | * Legend: | |
189 | * [1] cds_wfcq_enqueue | |
190 | * [2] __cds_wfcq_splice (destination queue) | |
191 | * [3] __cds_wfcq_dequeue | |
192 | * [4] __cds_wfcq_splice (source queue) | |
193 | * [5] __cds_wfcq_first | |
194 | * [6] __cds_wfcq_next | |
195 | * | |
196 | * [1] [2] [3] [4] [5] [6] | |
197 | * [1] - - - - - - | |
198 | * [2] - - - - - - | |
199 | * [3] - - X X X X | |
200 | * [4] - - X - X X | |
201 | * [5] - - X X - - | |
202 | * [6] - - X X - - | |
203 | * | |
8ad4ce58 MD |
204 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). |
205 | * | |
206 | * For convenience, cds_wfcq_dequeue_blocking() and | |
207 | * cds_wfcq_splice_blocking() hold the dequeue lock. | |
1fe734e1 MD |
208 | * |
209 | * Besides locking, mutual exclusion of dequeue, splice and iteration | |
210 | * can be ensured by performing all of those operations from a single | |
211 | * thread, without requiring any lock. | |
8ad4ce58 MD |
212 | */ |
213 | ||
214 | /* | |
215 | * cds_wfcq_node_init: initialize wait-free queue node. | |
216 | */ | |
217 | extern void cds_wfcq_node_init(struct cds_wfcq_node *node); | |
218 | ||
219 | /* | |
200d100e MD |
220 | * cds_wfcq_init: initialize wait-free queue. Pair with |
221 | * cds_wfcq_destroy(). | |
8ad4ce58 MD |
222 | */ |
223 | extern void cds_wfcq_init(struct cds_wfcq_head *head, | |
224 | struct cds_wfcq_tail *tail); | |
225 | ||
f637f191 | 226 | /* |
200d100e MD |
227 | * cds_wfcq_destroy: destroy wait-free queue. Pair with |
228 | * cds_wfcq_init(). | |
229 | */ | |
230 | extern void cds_wfcq_destroy(struct cds_wfcq_head *head, | |
231 | struct cds_wfcq_tail *tail); | |
232 | ||
233 | /* | |
234 | * __cds_wfcq_init: initialize wait-free queue (without lock). Don't | |
235 | * pair with any destroy function. | |
f637f191 MD |
236 | */ |
237 | extern void __cds_wfcq_init(struct __cds_wfcq_head *head, | |
238 | struct cds_wfcq_tail *tail); | |
239 | ||
8ad4ce58 MD |
240 | /* |
241 | * cds_wfcq_empty: return whether wait-free queue is empty. | |
242 | * | |
243 | * No memory barrier is issued. No mutual exclusion is required. | |
244 | */ | |
f637f191 | 245 | extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
246 | struct cds_wfcq_tail *tail); |
247 | ||
248 | /* | |
249 | * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock. | |
250 | */ | |
251 | extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, | |
252 | struct cds_wfcq_tail *tail); | |
253 | ||
254 | /* | |
255 | * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock. | |
256 | */ | |
257 | extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, | |
258 | struct cds_wfcq_tail *tail); | |
259 | ||
260 | /* | |
261 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. | |
262 | * | |
263 | * Issues a full memory barrier before enqueue. No mutual exclusion is | |
264 | * required. | |
23773356 MD |
265 | * |
266 | * Returns false if the queue was empty prior to adding the node. | |
267 | * Returns true otherwise. | |
8ad4ce58 | 268 | */ |
f637f191 | 269 | extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
270 | struct cds_wfcq_tail *tail, |
271 | struct cds_wfcq_node *node); | |
272 | ||
273 | /* | |
274 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. | |
275 | * | |
276 | * Content written into the node before enqueue is guaranteed to be | |
277 | * consistent, but no other memory ordering is ensured. | |
278 | * It is valid to reuse and free a dequeued node immediately. | |
ffa11a18 | 279 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
1fe734e1 | 280 | * ensured. |
8ad4ce58 MD |
281 | */ |
282 | extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking( | |
283 | struct cds_wfcq_head *head, | |
284 | struct cds_wfcq_tail *tail); | |
285 | ||
eec791af MD |
286 | /* |
287 | * cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
288 | * | |
289 | * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the | |
290 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
291 | */ | |
292 | extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking( | |
293 | struct cds_wfcq_head *head, | |
294 | struct cds_wfcq_tail *tail, | |
295 | int *state); | |
296 | ||
8ad4ce58 MD |
297 | /* |
298 | * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
299 | * | |
300 | * Dequeue all nodes from src_q. | |
301 | * dest_q must be already initialized. | |
302 | * Content written into the node before enqueue is guaranteed to be | |
303 | * consistent, but no other memory ordering is ensured. | |
ffa11a18 | 304 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
1fe734e1 | 305 | * ensured. |
23773356 MD |
306 | * |
307 | * Returns enum cds_wfcq_ret which indicates the state of the src or | |
b94aaf6f | 308 | * dest queue. |
8ad4ce58 | 309 | */ |
23773356 | 310 | extern enum cds_wfcq_ret cds_wfcq_splice_blocking( |
8ad4ce58 MD |
311 | struct cds_wfcq_head *dest_q_head, |
312 | struct cds_wfcq_tail *dest_q_tail, | |
313 | struct cds_wfcq_head *src_q_head, | |
314 | struct cds_wfcq_tail *src_q_tail); | |
315 | ||
316 | /* | |
47215721 | 317 | * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
8ad4ce58 MD |
318 | * |
319 | * Content written into the node before enqueue is guaranteed to be | |
320 | * consistent, but no other memory ordering is ensured. | |
321 | * It is valid to reuse and free a dequeued node immediately. | |
1fe734e1 MD |
322 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
323 | * caller. | |
8ad4ce58 MD |
324 | */ |
325 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( | |
f637f191 | 326 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
327 | struct cds_wfcq_tail *tail); |
328 | ||
eec791af MD |
329 | /* |
330 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
331 | * | |
332 | * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the | |
333 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
334 | */ | |
335 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( | |
f637f191 | 336 | cds_wfcq_head_ptr_t head, |
eec791af MD |
337 | struct cds_wfcq_tail *tail, |
338 | int *state); | |
339 | ||
47215721 MD |
340 | /* |
341 | * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue. | |
342 | * | |
343 | * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK | |
344 | * if it needs to block. | |
345 | */ | |
346 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( | |
f637f191 | 347 | cds_wfcq_head_ptr_t head, |
47215721 MD |
348 | struct cds_wfcq_tail *tail); |
349 | ||
eec791af MD |
350 | /* |
351 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
352 | * | |
353 | * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing | |
354 | * the last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
355 | */ | |
356 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( | |
f637f191 | 357 | cds_wfcq_head_ptr_t head, |
eec791af MD |
358 | struct cds_wfcq_tail *tail, |
359 | int *state); | |
360 | ||
8ad4ce58 MD |
361 | /* |
362 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
363 | * | |
364 | * Dequeue all nodes from src_q. | |
365 | * dest_q must be already initialized. | |
f878b49e MD |
366 | * Mutual exclusion for src_q should be ensured by the caller as |
367 | * specified in the "Synchronisation table". | |
23773356 | 368 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
f878b49e | 369 | * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK. |
8ad4ce58 | 370 | */ |
23773356 | 371 | extern enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
f637f191 | 372 | cds_wfcq_head_ptr_t dest_q_head, |
8ad4ce58 | 373 | struct cds_wfcq_tail *dest_q_tail, |
f637f191 | 374 | cds_wfcq_head_ptr_t src_q_head, |
8ad4ce58 MD |
375 | struct cds_wfcq_tail *src_q_tail); |
376 | ||
47215721 MD |
377 | /* |
378 | * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q. | |
379 | * | |
23773356 MD |
380 | * Same as __cds_wfcq_splice_blocking, but returns |
381 | * CDS_WFCQ_RET_WOULDBLOCK if it needs to block. | |
47215721 | 382 | */ |
23773356 | 383 | extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
f637f191 | 384 | cds_wfcq_head_ptr_t dest_q_head, |
47215721 | 385 | struct cds_wfcq_tail *dest_q_tail, |
f637f191 | 386 | cds_wfcq_head_ptr_t src_q_head, |
47215721 MD |
387 | struct cds_wfcq_tail *src_q_tail); |
388 | ||
8ad4ce58 MD |
389 | /* |
390 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. | |
391 | * | |
392 | * Content written into the node before enqueue is guaranteed to be | |
393 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
394 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
395 | * caller. | |
f94061a3 MD |
396 | * |
397 | * Used by for-like iteration macros: | |
398 | * __cds_wfcq_for_each_blocking() | |
399 | * __cds_wfcq_for_each_blocking_safe() | |
131a29a6 MD |
400 | * |
401 | * Returns NULL if queue is empty, first node otherwise. | |
8ad4ce58 MD |
402 | */ |
403 | extern struct cds_wfcq_node *__cds_wfcq_first_blocking( | |
f637f191 | 404 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
405 | struct cds_wfcq_tail *tail); |
406 | ||
47215721 MD |
407 | /* |
408 | * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing. | |
409 | * | |
410 | * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if | |
411 | * it needs to block. | |
412 | */ | |
413 | extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking( | |
f637f191 | 414 | cds_wfcq_head_ptr_t head, |
47215721 MD |
415 | struct cds_wfcq_tail *tail); |
416 | ||
8ad4ce58 MD |
417 | /* |
418 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. | |
419 | * | |
420 | * Content written into the node before enqueue is guaranteed to be | |
421 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
422 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
423 | * caller. | |
f94061a3 MD |
424 | * |
425 | * Used by for-like iteration macros: | |
426 | * __cds_wfcq_for_each_blocking() | |
427 | * __cds_wfcq_for_each_blocking_safe() | |
131a29a6 MD |
428 | * |
429 | * Returns NULL if reached end of queue, non-NULL next queue node | |
430 | * otherwise. | |
8ad4ce58 MD |
431 | */ |
432 | extern struct cds_wfcq_node *__cds_wfcq_next_blocking( | |
f637f191 | 433 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
434 | struct cds_wfcq_tail *tail, |
435 | struct cds_wfcq_node *node); | |
436 | ||
47215721 MD |
437 | /* |
438 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. | |
439 | * | |
440 | * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if | |
441 | * it needs to block. | |
442 | */ | |
443 | extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking( | |
f637f191 | 444 | cds_wfcq_head_ptr_t head, |
47215721 MD |
445 | struct cds_wfcq_tail *tail, |
446 | struct cds_wfcq_node *node); | |
447 | ||
8ad4ce58 MD |
448 | #endif /* !_LGPL_SOURCE */ |
449 | ||
450 | /* | |
451 | * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue, | |
452 | * without dequeuing them. | |
f637f191 | 453 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
8ad4ce58 MD |
454 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
455 | * @node: iterator on the queue (struct cds_wfcq_node pointer). | |
456 | * | |
457 | * Content written into each node before enqueue is guaranteed to be | |
458 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
459 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
460 | * caller. | |
8ad4ce58 MD |
461 | */ |
462 | #define __cds_wfcq_for_each_blocking(head, tail, node) \ | |
463 | for (node = __cds_wfcq_first_blocking(head, tail); \ | |
464 | node != NULL; \ | |
465 | node = __cds_wfcq_next_blocking(head, tail, node)) | |
466 | ||
467 | /* | |
468 | * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue, | |
469 | * without dequeuing them. Safe against deletion. | |
f637f191 | 470 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
8ad4ce58 MD |
471 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
472 | * @node: iterator on the queue (struct cds_wfcq_node pointer). | |
473 | * @n: struct cds_wfcq_node pointer holding the next pointer (used | |
474 | * internally). | |
475 | * | |
476 | * Content written into each node before enqueue is guaranteed to be | |
477 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
478 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
479 | * caller. | |
8ad4ce58 MD |
480 | */ |
481 | #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \ | |
482 | for (node = __cds_wfcq_first_blocking(head, tail), \ | |
483 | n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \ | |
484 | node != NULL; \ | |
485 | node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL)) | |
486 | ||
487 | #ifdef __cplusplus | |
488 | } | |
0cdbb569 MD |
489 | |
490 | /* | |
491 | * In C++, implement static inline wrappers using function overloading | |
492 | * to obtain an API similar to C. | |
493 | */ | |
494 | ||
aaf0064b MD |
495 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct __cds_wfcq_head *head) |
496 | { | |
497 | cds_wfcq_head_ptr_t ret = { ._h = head }; | |
498 | return ret; | |
499 | } | |
500 | ||
501 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct cds_wfcq_head *head) | |
502 | { | |
503 | cds_wfcq_head_ptr_t ret = { .h = head }; | |
504 | return ret; | |
505 | } | |
506 | ||
ee990cac | 507 | template<typename T> static inline bool cds_wfcq_empty(T head, |
0cdbb569 MD |
508 | struct cds_wfcq_tail *tail) |
509 | { | |
aaf0064b | 510 | return cds_wfcq_empty(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
511 | } |
512 | ||
ee990cac | 513 | template<typename T> static inline bool cds_wfcq_enqueue(T head, |
0cdbb569 MD |
514 | struct cds_wfcq_tail *tail, |
515 | struct cds_wfcq_node *node) | |
516 | { | |
aaf0064b | 517 | return cds_wfcq_enqueue(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
518 | } |
519 | ||
ee990cac MD |
520 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( |
521 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 522 | { |
aaf0064b | 523 | return __cds_wfcq_dequeue_blocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
524 | } |
525 | ||
ee990cac MD |
526 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( |
527 | T head, struct cds_wfcq_tail *tail, int *state) | |
0cdbb569 | 528 | { |
aaf0064b | 529 | return __cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_cast_cpp(head), |
0cdbb569 MD |
530 | tail, state); |
531 | } | |
532 | ||
ee990cac MD |
533 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( |
534 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 535 | { |
aaf0064b | 536 | return __cds_wfcq_dequeue_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
537 | } |
538 | ||
ee990cac MD |
539 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( |
540 | T head, struct cds_wfcq_tail *tail, int *state) | |
0cdbb569 | 541 | { |
aaf0064b | 542 | return __cds_wfcq_dequeue_with_state_nonblocking(cds_wfcq_head_cast_cpp(head), |
0cdbb569 MD |
543 | tail, state); |
544 | } | |
545 | ||
ee990cac MD |
546 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
547 | T dest_q_head, | |
0cdbb569 | 548 | struct cds_wfcq_tail *dest_q_tail, |
ee990cac | 549 | U src_q_head, |
0cdbb569 MD |
550 | struct cds_wfcq_tail *src_q_tail) |
551 | { | |
aaf0064b | 552 | return __cds_wfcq_splice_blocking(cds_wfcq_head_cast_cpp(dest_q_head), |
0cdbb569 | 553 | dest_q_tail, |
aaf0064b | 554 | cds_wfcq_head_cast_cpp(src_q_head), |
0cdbb569 MD |
555 | src_q_tail); |
556 | } | |
557 | ||
ee990cac MD |
558 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
559 | T dest_q_head, | |
0cdbb569 | 560 | struct cds_wfcq_tail *dest_q_tail, |
ee990cac | 561 | U *src_q_head, |
0cdbb569 MD |
562 | struct cds_wfcq_tail *src_q_tail) |
563 | { | |
aaf0064b | 564 | return __cds_wfcq_splice_nonblocking(cds_wfcq_head_cast_cpp(dest_q_head), |
0cdbb569 | 565 | dest_q_tail, |
aaf0064b | 566 | cds_wfcq_head_cast_cpp(src_q_head), |
0cdbb569 MD |
567 | src_q_tail); |
568 | } | |
569 | ||
ee990cac MD |
570 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_blocking( |
571 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 572 | { |
aaf0064b | 573 | return __cds_wfcq_first_blocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
574 | } |
575 | ||
ee990cac MD |
576 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_nonblocking( |
577 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 578 | { |
aaf0064b | 579 | return __cds_wfcq_first_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
580 | } |
581 | ||
ee990cac MD |
582 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_blocking( |
583 | T head, | |
0cdbb569 MD |
584 | struct cds_wfcq_tail *tail, |
585 | struct cds_wfcq_node *node) | |
586 | { | |
aaf0064b | 587 | return __cds_wfcq_next_blocking(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
588 | } |
589 | ||
ee990cac MD |
590 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_nonblocking( |
591 | T head, | |
0cdbb569 MD |
592 | struct cds_wfcq_tail *tail, |
593 | struct cds_wfcq_node *node) | |
594 | { | |
aaf0064b | 595 | return __cds_wfcq_next_nonblocking(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
596 | } |
597 | ||
8ad4ce58 MD |
598 | #endif |
599 | ||
600 | #endif /* _URCU_WFCQUEUE_H */ |