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. | |
087bce43 MD |
83 | * |
84 | * Avoid complaints from clang++ not knowing the transparent union | |
85 | * attribute. | |
f637f191 | 86 | */ |
087bce43 MD |
87 | #if defined(__clang__) |
88 | #pragma clang diagnostic push | |
89 | #pragma clang diagnostic ignored "-Wignored-attributes" | |
90 | #endif | |
5135c0fd | 91 | typedef union { |
f637f191 MD |
92 | struct __cds_wfcq_head *_h; |
93 | struct cds_wfcq_head *h; | |
087bce43 MD |
94 | } __attribute__((__transparent_union__)) cds_wfcq_head_ptr_t; |
95 | #if defined(__clang__) | |
96 | #pragma clang diagnostic pop | |
97 | #endif | |
f637f191 | 98 | |
0cdbb569 | 99 | #ifndef __cplusplus |
4d0d7cbc MD |
100 | /* |
101 | * This static inline is only present for compatibility with C++. It is | |
102 | * effect-less in C. | |
103 | */ | |
104 | static inline struct __cds_wfcq_head *__cds_wfcq_head_cast(struct __cds_wfcq_head *head) | |
105 | { | |
106 | return head; | |
107 | } | |
108 | ||
109 | /* | |
110 | * This static inline is only present for compatibility with C++. It is | |
111 | * effect-less in C. | |
112 | */ | |
113 | static inline struct cds_wfcq_head *cds_wfcq_head_cast(struct cds_wfcq_head *head) | |
114 | { | |
115 | return head; | |
116 | } | |
117 | #else /* #ifndef __cplusplus */ | |
118 | ||
0cdbb569 | 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 | } | |
0cdbb569 MD |
128 | |
129 | /* | |
aaf0064b MD |
130 | * This static inline is used by internally in the static inline |
131 | * implementation of the API. | |
0cdbb569 | 132 | */ |
4d0d7cbc MD |
133 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast(struct cds_wfcq_head *head) |
134 | { | |
135 | cds_wfcq_head_ptr_t ret = { .h = head }; | |
136 | return ret; | |
137 | } | |
138 | #endif /* #else #ifndef __cplusplus */ | |
139 | ||
8ad4ce58 MD |
140 | struct cds_wfcq_tail { |
141 | struct cds_wfcq_node *p; | |
142 | }; | |
143 | ||
144 | #ifdef _LGPL_SOURCE | |
145 | ||
146 | #include <urcu/static/wfcqueue.h> | |
147 | ||
148 | #define cds_wfcq_node_init _cds_wfcq_node_init | |
149 | #define cds_wfcq_init _cds_wfcq_init | |
b4edfa81 | 150 | #define __cds_wfcq_init ___cds_wfcq_init |
200d100e | 151 | #define cds_wfcq_destroy _cds_wfcq_destroy |
8ad4ce58 MD |
152 | #define cds_wfcq_empty _cds_wfcq_empty |
153 | #define cds_wfcq_enqueue _cds_wfcq_enqueue | |
154 | ||
155 | /* Dequeue locking */ | |
156 | #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock | |
157 | #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock | |
158 | ||
159 | /* Locking performed within cds_wfcq calls. */ | |
160 | #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking | |
eec791af MD |
161 | #define cds_wfcq_dequeue_with_state_blocking \ |
162 | _cds_wfcq_dequeue_with_state_blocking | |
8ad4ce58 MD |
163 | #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking |
164 | #define cds_wfcq_first_blocking _cds_wfcq_first_blocking | |
165 | #define cds_wfcq_next_blocking _cds_wfcq_next_blocking | |
166 | ||
167 | /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */ | |
168 | #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking | |
eec791af MD |
169 | #define __cds_wfcq_dequeue_with_state_blocking \ |
170 | ___cds_wfcq_dequeue_with_state_blocking | |
8ad4ce58 MD |
171 | #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking |
172 | #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking | |
173 | #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking | |
174 | ||
47215721 MD |
175 | /* |
176 | * Locking ensured by caller by holding cds_wfcq_dequeue_lock(). | |
177 | * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they | |
178 | * need to block. splice returns nonzero if it needs to block. | |
179 | */ | |
180 | #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking | |
eec791af MD |
181 | #define __cds_wfcq_dequeue_with_state_nonblocking \ |
182 | ___cds_wfcq_dequeue_with_state_nonblocking | |
47215721 MD |
183 | #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking |
184 | #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking | |
185 | #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking | |
186 | ||
8ad4ce58 MD |
187 | #else /* !_LGPL_SOURCE */ |
188 | ||
189 | /* | |
190 | * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API | |
191 | * | |
f878b49e MD |
192 | * Synchronization table: |
193 | * | |
194 | * External synchronization techniques described in the API below is | |
195 | * required between pairs marked with "X". No external synchronization | |
196 | * required between pairs marked with "-". | |
197 | * | |
198 | * Legend: | |
199 | * [1] cds_wfcq_enqueue | |
200 | * [2] __cds_wfcq_splice (destination queue) | |
201 | * [3] __cds_wfcq_dequeue | |
202 | * [4] __cds_wfcq_splice (source queue) | |
203 | * [5] __cds_wfcq_first | |
204 | * [6] __cds_wfcq_next | |
205 | * | |
206 | * [1] [2] [3] [4] [5] [6] | |
207 | * [1] - - - - - - | |
208 | * [2] - - - - - - | |
209 | * [3] - - X X X X | |
210 | * [4] - - X - X X | |
211 | * [5] - - X X - - | |
212 | * [6] - - X X - - | |
213 | * | |
8ad4ce58 MD |
214 | * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock(). |
215 | * | |
216 | * For convenience, cds_wfcq_dequeue_blocking() and | |
217 | * cds_wfcq_splice_blocking() hold the dequeue lock. | |
1fe734e1 MD |
218 | * |
219 | * Besides locking, mutual exclusion of dequeue, splice and iteration | |
220 | * can be ensured by performing all of those operations from a single | |
221 | * thread, without requiring any lock. | |
8ad4ce58 MD |
222 | */ |
223 | ||
224 | /* | |
225 | * cds_wfcq_node_init: initialize wait-free queue node. | |
226 | */ | |
227 | extern void cds_wfcq_node_init(struct cds_wfcq_node *node); | |
228 | ||
229 | /* | |
200d100e MD |
230 | * cds_wfcq_init: initialize wait-free queue. Pair with |
231 | * cds_wfcq_destroy(). | |
8ad4ce58 MD |
232 | */ |
233 | extern void cds_wfcq_init(struct cds_wfcq_head *head, | |
234 | struct cds_wfcq_tail *tail); | |
235 | ||
f637f191 | 236 | /* |
200d100e MD |
237 | * cds_wfcq_destroy: destroy wait-free queue. Pair with |
238 | * cds_wfcq_init(). | |
239 | */ | |
240 | extern void cds_wfcq_destroy(struct cds_wfcq_head *head, | |
241 | struct cds_wfcq_tail *tail); | |
242 | ||
243 | /* | |
244 | * __cds_wfcq_init: initialize wait-free queue (without lock). Don't | |
245 | * pair with any destroy function. | |
f637f191 MD |
246 | */ |
247 | extern void __cds_wfcq_init(struct __cds_wfcq_head *head, | |
248 | struct cds_wfcq_tail *tail); | |
249 | ||
8ad4ce58 MD |
250 | /* |
251 | * cds_wfcq_empty: return whether wait-free queue is empty. | |
252 | * | |
253 | * No memory barrier is issued. No mutual exclusion is required. | |
254 | */ | |
f637f191 | 255 | extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
256 | struct cds_wfcq_tail *tail); |
257 | ||
258 | /* | |
259 | * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock. | |
260 | */ | |
261 | extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head, | |
262 | struct cds_wfcq_tail *tail); | |
263 | ||
264 | /* | |
265 | * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock. | |
266 | */ | |
267 | extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head, | |
268 | struct cds_wfcq_tail *tail); | |
269 | ||
270 | /* | |
271 | * cds_wfcq_enqueue: enqueue a node into a wait-free queue. | |
272 | * | |
273 | * Issues a full memory barrier before enqueue. No mutual exclusion is | |
274 | * required. | |
23773356 MD |
275 | * |
276 | * Returns false if the queue was empty prior to adding the node. | |
277 | * Returns true otherwise. | |
8ad4ce58 | 278 | */ |
f637f191 | 279 | extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
280 | struct cds_wfcq_tail *tail, |
281 | struct cds_wfcq_node *node); | |
282 | ||
283 | /* | |
284 | * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. | |
285 | * | |
286 | * Content written into the node before enqueue is guaranteed to be | |
287 | * consistent, but no other memory ordering is ensured. | |
288 | * It is valid to reuse and free a dequeued node immediately. | |
ffa11a18 | 289 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
1fe734e1 | 290 | * ensured. |
8ad4ce58 MD |
291 | */ |
292 | extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking( | |
293 | struct cds_wfcq_head *head, | |
294 | struct cds_wfcq_tail *tail); | |
295 | ||
eec791af MD |
296 | /* |
297 | * cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
298 | * | |
299 | * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the | |
300 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
301 | */ | |
302 | extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking( | |
303 | struct cds_wfcq_head *head, | |
304 | struct cds_wfcq_tail *tail, | |
305 | int *state); | |
306 | ||
8ad4ce58 MD |
307 | /* |
308 | * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
309 | * | |
310 | * Dequeue all nodes from src_q. | |
311 | * dest_q must be already initialized. | |
312 | * Content written into the node before enqueue is guaranteed to be | |
313 | * consistent, but no other memory ordering is ensured. | |
ffa11a18 | 314 | * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is |
1fe734e1 | 315 | * ensured. |
23773356 MD |
316 | * |
317 | * Returns enum cds_wfcq_ret which indicates the state of the src or | |
b94aaf6f | 318 | * dest queue. |
8ad4ce58 | 319 | */ |
23773356 | 320 | extern enum cds_wfcq_ret cds_wfcq_splice_blocking( |
8ad4ce58 MD |
321 | struct cds_wfcq_head *dest_q_head, |
322 | struct cds_wfcq_tail *dest_q_tail, | |
323 | struct cds_wfcq_head *src_q_head, | |
324 | struct cds_wfcq_tail *src_q_tail); | |
325 | ||
326 | /* | |
47215721 | 327 | * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue. |
8ad4ce58 MD |
328 | * |
329 | * Content written into the node before enqueue is guaranteed to be | |
330 | * consistent, but no other memory ordering is ensured. | |
331 | * It is valid to reuse and free a dequeued node immediately. | |
1fe734e1 MD |
332 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
333 | * caller. | |
8ad4ce58 MD |
334 | */ |
335 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( | |
f637f191 | 336 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
337 | struct cds_wfcq_tail *tail); |
338 | ||
eec791af MD |
339 | /* |
340 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
341 | * | |
342 | * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the | |
343 | * last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
344 | */ | |
345 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( | |
f637f191 | 346 | cds_wfcq_head_ptr_t head, |
eec791af MD |
347 | struct cds_wfcq_tail *tail, |
348 | int *state); | |
349 | ||
47215721 MD |
350 | /* |
351 | * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue. | |
352 | * | |
353 | * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK | |
354 | * if it needs to block. | |
355 | */ | |
356 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( | |
f637f191 | 357 | cds_wfcq_head_ptr_t head, |
47215721 MD |
358 | struct cds_wfcq_tail *tail); |
359 | ||
eec791af MD |
360 | /* |
361 | * __cds_wfcq_dequeue_with_state_blocking: dequeue with state. | |
362 | * | |
363 | * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing | |
364 | * the last node of the queue into state (CDS_WFCQ_STATE_LAST). | |
365 | */ | |
366 | extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( | |
f637f191 | 367 | cds_wfcq_head_ptr_t head, |
eec791af MD |
368 | struct cds_wfcq_tail *tail, |
369 | int *state); | |
370 | ||
8ad4ce58 MD |
371 | /* |
372 | * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q. | |
373 | * | |
374 | * Dequeue all nodes from src_q. | |
375 | * dest_q must be already initialized. | |
f878b49e MD |
376 | * Mutual exclusion for src_q should be ensured by the caller as |
377 | * specified in the "Synchronisation table". | |
23773356 | 378 | * Returns enum cds_wfcq_ret which indicates the state of the src or |
f878b49e | 379 | * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK. |
8ad4ce58 | 380 | */ |
23773356 | 381 | extern enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
f637f191 | 382 | cds_wfcq_head_ptr_t dest_q_head, |
8ad4ce58 | 383 | struct cds_wfcq_tail *dest_q_tail, |
f637f191 | 384 | cds_wfcq_head_ptr_t src_q_head, |
8ad4ce58 MD |
385 | struct cds_wfcq_tail *src_q_tail); |
386 | ||
47215721 MD |
387 | /* |
388 | * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q. | |
389 | * | |
23773356 MD |
390 | * Same as __cds_wfcq_splice_blocking, but returns |
391 | * CDS_WFCQ_RET_WOULDBLOCK if it needs to block. | |
47215721 | 392 | */ |
23773356 | 393 | extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
f637f191 | 394 | cds_wfcq_head_ptr_t dest_q_head, |
47215721 | 395 | struct cds_wfcq_tail *dest_q_tail, |
f637f191 | 396 | cds_wfcq_head_ptr_t src_q_head, |
47215721 MD |
397 | struct cds_wfcq_tail *src_q_tail); |
398 | ||
8ad4ce58 MD |
399 | /* |
400 | * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing. | |
401 | * | |
402 | * Content written into the node before enqueue is guaranteed to be | |
403 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
404 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
405 | * caller. | |
f94061a3 MD |
406 | * |
407 | * Used by for-like iteration macros: | |
408 | * __cds_wfcq_for_each_blocking() | |
409 | * __cds_wfcq_for_each_blocking_safe() | |
131a29a6 MD |
410 | * |
411 | * Returns NULL if queue is empty, first node otherwise. | |
8ad4ce58 MD |
412 | */ |
413 | extern struct cds_wfcq_node *__cds_wfcq_first_blocking( | |
f637f191 | 414 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
415 | struct cds_wfcq_tail *tail); |
416 | ||
47215721 MD |
417 | /* |
418 | * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing. | |
419 | * | |
420 | * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if | |
421 | * it needs to block. | |
422 | */ | |
423 | extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking( | |
f637f191 | 424 | cds_wfcq_head_ptr_t head, |
47215721 MD |
425 | struct cds_wfcq_tail *tail); |
426 | ||
8ad4ce58 MD |
427 | /* |
428 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. | |
429 | * | |
430 | * Content written into the node before enqueue is guaranteed to be | |
431 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
432 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
433 | * caller. | |
f94061a3 MD |
434 | * |
435 | * Used by for-like iteration macros: | |
436 | * __cds_wfcq_for_each_blocking() | |
437 | * __cds_wfcq_for_each_blocking_safe() | |
131a29a6 MD |
438 | * |
439 | * Returns NULL if reached end of queue, non-NULL next queue node | |
440 | * otherwise. | |
8ad4ce58 MD |
441 | */ |
442 | extern struct cds_wfcq_node *__cds_wfcq_next_blocking( | |
f637f191 | 443 | cds_wfcq_head_ptr_t head, |
8ad4ce58 MD |
444 | struct cds_wfcq_tail *tail, |
445 | struct cds_wfcq_node *node); | |
446 | ||
47215721 MD |
447 | /* |
448 | * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing. | |
449 | * | |
450 | * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if | |
451 | * it needs to block. | |
452 | */ | |
453 | extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking( | |
f637f191 | 454 | cds_wfcq_head_ptr_t head, |
47215721 MD |
455 | struct cds_wfcq_tail *tail, |
456 | struct cds_wfcq_node *node); | |
457 | ||
8ad4ce58 MD |
458 | #endif /* !_LGPL_SOURCE */ |
459 | ||
460 | /* | |
461 | * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue, | |
462 | * without dequeuing them. | |
f637f191 | 463 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
8ad4ce58 MD |
464 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
465 | * @node: iterator on the queue (struct cds_wfcq_node pointer). | |
466 | * | |
467 | * Content written into each node before enqueue is guaranteed to be | |
468 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
469 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
470 | * caller. | |
8ad4ce58 MD |
471 | */ |
472 | #define __cds_wfcq_for_each_blocking(head, tail, node) \ | |
473 | for (node = __cds_wfcq_first_blocking(head, tail); \ | |
474 | node != NULL; \ | |
475 | node = __cds_wfcq_next_blocking(head, tail, node)) | |
476 | ||
477 | /* | |
478 | * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue, | |
479 | * without dequeuing them. Safe against deletion. | |
f637f191 | 480 | * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer). |
8ad4ce58 MD |
481 | * @tail: tail of the queue (struct cds_wfcq_tail pointer). |
482 | * @node: iterator on the queue (struct cds_wfcq_node pointer). | |
483 | * @n: struct cds_wfcq_node pointer holding the next pointer (used | |
484 | * internally). | |
485 | * | |
486 | * Content written into each node before enqueue is guaranteed to be | |
487 | * consistent, but no other memory ordering is ensured. | |
1fe734e1 MD |
488 | * Dequeue/splice/iteration mutual exclusion should be ensured by the |
489 | * caller. | |
8ad4ce58 MD |
490 | */ |
491 | #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \ | |
492 | for (node = __cds_wfcq_first_blocking(head, tail), \ | |
493 | n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \ | |
494 | node != NULL; \ | |
495 | node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL)) | |
496 | ||
497 | #ifdef __cplusplus | |
498 | } | |
0cdbb569 MD |
499 | |
500 | /* | |
501 | * In C++, implement static inline wrappers using function overloading | |
502 | * to obtain an API similar to C. | |
503 | */ | |
504 | ||
aaf0064b MD |
505 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct __cds_wfcq_head *head) |
506 | { | |
507 | cds_wfcq_head_ptr_t ret = { ._h = head }; | |
508 | return ret; | |
509 | } | |
510 | ||
511 | static inline cds_wfcq_head_ptr_t cds_wfcq_head_cast_cpp(struct cds_wfcq_head *head) | |
512 | { | |
513 | cds_wfcq_head_ptr_t ret = { .h = head }; | |
514 | return ret; | |
515 | } | |
516 | ||
ee990cac | 517 | template<typename T> static inline bool cds_wfcq_empty(T head, |
0cdbb569 MD |
518 | struct cds_wfcq_tail *tail) |
519 | { | |
aaf0064b | 520 | return cds_wfcq_empty(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
521 | } |
522 | ||
ee990cac | 523 | template<typename T> static inline bool cds_wfcq_enqueue(T head, |
0cdbb569 MD |
524 | struct cds_wfcq_tail *tail, |
525 | struct cds_wfcq_node *node) | |
526 | { | |
aaf0064b | 527 | return cds_wfcq_enqueue(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
528 | } |
529 | ||
ee990cac MD |
530 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_blocking( |
531 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 532 | { |
aaf0064b | 533 | return __cds_wfcq_dequeue_blocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
534 | } |
535 | ||
ee990cac MD |
536 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking( |
537 | T head, struct cds_wfcq_tail *tail, int *state) | |
0cdbb569 | 538 | { |
aaf0064b | 539 | return __cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_cast_cpp(head), |
0cdbb569 MD |
540 | tail, state); |
541 | } | |
542 | ||
ee990cac MD |
543 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking( |
544 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 545 | { |
aaf0064b | 546 | return __cds_wfcq_dequeue_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
547 | } |
548 | ||
ee990cac MD |
549 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking( |
550 | T head, struct cds_wfcq_tail *tail, int *state) | |
0cdbb569 | 551 | { |
aaf0064b | 552 | return __cds_wfcq_dequeue_with_state_nonblocking(cds_wfcq_head_cast_cpp(head), |
0cdbb569 MD |
553 | tail, state); |
554 | } | |
555 | ||
ee990cac MD |
556 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_blocking( |
557 | T dest_q_head, | |
0cdbb569 | 558 | struct cds_wfcq_tail *dest_q_tail, |
ee990cac | 559 | U src_q_head, |
0cdbb569 MD |
560 | struct cds_wfcq_tail *src_q_tail) |
561 | { | |
aaf0064b | 562 | return __cds_wfcq_splice_blocking(cds_wfcq_head_cast_cpp(dest_q_head), |
0cdbb569 | 563 | dest_q_tail, |
aaf0064b | 564 | cds_wfcq_head_cast_cpp(src_q_head), |
0cdbb569 MD |
565 | src_q_tail); |
566 | } | |
567 | ||
ee990cac MD |
568 | template<typename T, typename U> static inline enum cds_wfcq_ret __cds_wfcq_splice_nonblocking( |
569 | T dest_q_head, | |
0cdbb569 | 570 | struct cds_wfcq_tail *dest_q_tail, |
ee990cac | 571 | U *src_q_head, |
0cdbb569 MD |
572 | struct cds_wfcq_tail *src_q_tail) |
573 | { | |
aaf0064b | 574 | return __cds_wfcq_splice_nonblocking(cds_wfcq_head_cast_cpp(dest_q_head), |
0cdbb569 | 575 | dest_q_tail, |
aaf0064b | 576 | cds_wfcq_head_cast_cpp(src_q_head), |
0cdbb569 MD |
577 | src_q_tail); |
578 | } | |
579 | ||
ee990cac MD |
580 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_blocking( |
581 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 582 | { |
aaf0064b | 583 | return __cds_wfcq_first_blocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
584 | } |
585 | ||
ee990cac MD |
586 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_first_nonblocking( |
587 | T head, struct cds_wfcq_tail *tail) | |
0cdbb569 | 588 | { |
aaf0064b | 589 | return __cds_wfcq_first_nonblocking(cds_wfcq_head_cast_cpp(head), tail); |
0cdbb569 MD |
590 | } |
591 | ||
ee990cac MD |
592 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_blocking( |
593 | T head, | |
0cdbb569 MD |
594 | struct cds_wfcq_tail *tail, |
595 | struct cds_wfcq_node *node) | |
596 | { | |
aaf0064b | 597 | return __cds_wfcq_next_blocking(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
598 | } |
599 | ||
ee990cac MD |
600 | template<typename T> static inline struct cds_wfcq_node *__cds_wfcq_next_nonblocking( |
601 | T head, | |
0cdbb569 MD |
602 | struct cds_wfcq_tail *tail, |
603 | struct cds_wfcq_node *node) | |
604 | { | |
aaf0064b | 605 | return __cds_wfcq_next_nonblocking(cds_wfcq_head_cast_cpp(head), tail, node); |
0cdbb569 MD |
606 | } |
607 | ||
8ad4ce58 MD |
608 | #endif |
609 | ||
610 | #endif /* _URCU_WFCQUEUE_H */ |