1 #ifndef _URCU_WFCQUEUE_H
2 #define _URCU_WFCQUEUE_H
7 * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
9 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Copyright 2011-2012 - Lai Jiangshan <laijs@cn.fujitsu.com>
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.
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.
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
29 #include <urcu/compiler.h>
30 #include <urcu/arch.h>
37 * Concurrent queue with wait-free enqueue/blocking dequeue.
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.
45 #define CDS_WFCQ_WOULDBLOCK ((struct cds_wfcq_node *) -1UL)
48 CDS_WFCQ_RET_WOULDBLOCK
= -1,
49 CDS_WFCQ_RET_DEST_EMPTY
= 0,
50 CDS_WFCQ_RET_DEST_NON_EMPTY
= 1,
51 CDS_WFCQ_RET_SRC_EMPTY
= 2,
55 CDS_WFCQ_STATE_LAST
= (1U << 0),
58 struct cds_wfcq_node
{
59 struct cds_wfcq_node
*next
;
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.
67 struct __cds_wfcq_head
{
68 struct cds_wfcq_node node
;
71 struct cds_wfcq_head
{
72 struct cds_wfcq_node node
;
77 * In C, the transparent union allows calling functions that work on both
78 * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two
81 * In C++, implement static inline wrappers using function overloading
82 * to obtain an API similar to C.
84 * Avoid complaints from clang++ not knowing the transparent union
87 #if defined(__clang__)
88 #pragma clang diagnostic push
89 #pragma clang diagnostic ignored "-Wignored-attributes"
92 struct __cds_wfcq_head
*_h
;
93 struct cds_wfcq_head
*h
;
94 } __attribute__((__transparent_union__
)) cds_wfcq_head_ptr_t
;
95 #if defined(__clang__)
96 #pragma clang diagnostic pop
101 * This static inline is only present for compatibility with C++. It is
104 static inline struct __cds_wfcq_head
*__cds_wfcq_head_cast(struct __cds_wfcq_head
*head
)
110 * This static inline is only present for compatibility with C++. It is
113 static inline struct cds_wfcq_head
*cds_wfcq_head_cast(struct cds_wfcq_head
*head
)
117 #else /* #ifndef __cplusplus */
120 * This static inline is used by internally in the static inline
121 * implementation of the API.
123 static inline cds_wfcq_head_ptr_t
__cds_wfcq_head_cast(struct __cds_wfcq_head
*head
)
125 cds_wfcq_head_ptr_t ret
= { ._h
= head
};
130 * This static inline is used by internally in the static inline
131 * implementation of the API.
133 static inline cds_wfcq_head_ptr_t
cds_wfcq_head_cast(struct cds_wfcq_head
*head
)
135 cds_wfcq_head_ptr_t ret
= { .h
= head
};
138 #endif /* #else #ifndef __cplusplus */
140 struct cds_wfcq_tail
{
141 struct cds_wfcq_node
*p
;
146 #include <urcu/static/wfcqueue.h>
148 #define cds_wfcq_node_init _cds_wfcq_node_init
149 #define cds_wfcq_init _cds_wfcq_init
150 #define __cds_wfcq_init ___cds_wfcq_init
151 #define cds_wfcq_destroy _cds_wfcq_destroy
152 #define cds_wfcq_empty _cds_wfcq_empty
153 #define cds_wfcq_enqueue _cds_wfcq_enqueue
155 /* Dequeue locking */
156 #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
157 #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
159 /* Locking performed within cds_wfcq calls. */
160 #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
161 #define cds_wfcq_dequeue_with_state_blocking \
162 _cds_wfcq_dequeue_with_state_blocking
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
167 /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
168 #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
169 #define __cds_wfcq_dequeue_with_state_blocking \
170 ___cds_wfcq_dequeue_with_state_blocking
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
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.
180 #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
181 #define __cds_wfcq_dequeue_with_state_nonblocking \
182 ___cds_wfcq_dequeue_with_state_nonblocking
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
187 #else /* !_LGPL_SOURCE */
190 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
192 * Synchronization table:
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 "-".
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
206 * [1] [2] [3] [4] [5] [6]
214 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
216 * For convenience, cds_wfcq_dequeue_blocking() and
217 * cds_wfcq_splice_blocking() hold the dequeue lock.
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.
225 * cds_wfcq_node_init: initialize wait-free queue node.
227 extern void cds_wfcq_node_init(struct cds_wfcq_node
*node
);
230 * cds_wfcq_init: initialize wait-free queue. Pair with
231 * cds_wfcq_destroy().
233 extern void cds_wfcq_init(struct cds_wfcq_head
*head
,
234 struct cds_wfcq_tail
*tail
);
237 * cds_wfcq_destroy: destroy wait-free queue. Pair with
240 extern void cds_wfcq_destroy(struct cds_wfcq_head
*head
,
241 struct cds_wfcq_tail
*tail
);
244 * __cds_wfcq_init: initialize wait-free queue (without lock). Don't
245 * pair with any destroy function.
247 extern void __cds_wfcq_init(struct __cds_wfcq_head
*head
,
248 struct cds_wfcq_tail
*tail
);
251 * cds_wfcq_empty: return whether wait-free queue is empty.
253 * No memory barrier is issued. No mutual exclusion is required.
255 extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head
,
256 struct cds_wfcq_tail
*tail
);
259 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
261 extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
262 struct cds_wfcq_tail
*tail
);
265 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
267 extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
268 struct cds_wfcq_tail
*tail
);
271 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
273 * Issues a full memory barrier before enqueue. No mutual exclusion is
276 * Returns false if the queue was empty prior to adding the node.
277 * Returns true otherwise.
279 extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head
,
280 struct cds_wfcq_tail
*tail
,
281 struct cds_wfcq_node
*node
);
284 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
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.
289 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
292 extern struct cds_wfcq_node
*cds_wfcq_dequeue_blocking(
293 struct cds_wfcq_head
*head
,
294 struct cds_wfcq_tail
*tail
);
297 * cds_wfcq_dequeue_with_state_blocking: dequeue with state.
299 * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the
300 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
302 extern struct cds_wfcq_node
*cds_wfcq_dequeue_with_state_blocking(
303 struct cds_wfcq_head
*head
,
304 struct cds_wfcq_tail
*tail
,
308 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
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.
314 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
317 * Returns enum cds_wfcq_ret which indicates the state of the src or
320 extern enum cds_wfcq_ret
cds_wfcq_splice_blocking(
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
);
327 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
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.
332 * Dequeue/splice/iteration mutual exclusion should be ensured by the
335 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_blocking(
336 cds_wfcq_head_ptr_t head
,
337 struct cds_wfcq_tail
*tail
);
340 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
342 * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the
343 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
345 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_blocking(
346 cds_wfcq_head_ptr_t head
,
347 struct cds_wfcq_tail
*tail
,
351 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
353 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
354 * if it needs to block.
356 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_nonblocking(
357 cds_wfcq_head_ptr_t head
,
358 struct cds_wfcq_tail
*tail
);
361 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
363 * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing
364 * the last node of the queue into state (CDS_WFCQ_STATE_LAST).
366 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_nonblocking(
367 cds_wfcq_head_ptr_t head
,
368 struct cds_wfcq_tail
*tail
,
372 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
374 * Dequeue all nodes from src_q.
375 * dest_q must be already initialized.
376 * Mutual exclusion for src_q should be ensured by the caller as
377 * specified in the "Synchronisation table".
378 * Returns enum cds_wfcq_ret which indicates the state of the src or
379 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
381 extern enum cds_wfcq_ret
__cds_wfcq_splice_blocking(
382 cds_wfcq_head_ptr_t dest_q_head
,
383 struct cds_wfcq_tail
*dest_q_tail
,
384 cds_wfcq_head_ptr_t src_q_head
,
385 struct cds_wfcq_tail
*src_q_tail
);
388 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
390 * Same as __cds_wfcq_splice_blocking, but returns
391 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
393 extern enum cds_wfcq_ret
__cds_wfcq_splice_nonblocking(
394 cds_wfcq_head_ptr_t dest_q_head
,
395 struct cds_wfcq_tail
*dest_q_tail
,
396 cds_wfcq_head_ptr_t src_q_head
,
397 struct cds_wfcq_tail
*src_q_tail
);
400 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
402 * Content written into the node before enqueue is guaranteed to be
403 * consistent, but no other memory ordering is ensured.
404 * Dequeue/splice/iteration mutual exclusion should be ensured by the
407 * Used by for-like iteration macros:
408 * __cds_wfcq_for_each_blocking()
409 * __cds_wfcq_for_each_blocking_safe()
411 * Returns NULL if queue is empty, first node otherwise.
413 extern struct cds_wfcq_node
*__cds_wfcq_first_blocking(
414 cds_wfcq_head_ptr_t head
,
415 struct cds_wfcq_tail
*tail
);
418 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
420 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
423 extern struct cds_wfcq_node
*__cds_wfcq_first_nonblocking(
424 cds_wfcq_head_ptr_t head
,
425 struct cds_wfcq_tail
*tail
);
428 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
430 * Content written into the node before enqueue is guaranteed to be
431 * consistent, but no other memory ordering is ensured.
432 * Dequeue/splice/iteration mutual exclusion should be ensured by the
435 * Used by for-like iteration macros:
436 * __cds_wfcq_for_each_blocking()
437 * __cds_wfcq_for_each_blocking_safe()
439 * Returns NULL if reached end of queue, non-NULL next queue node
442 extern struct cds_wfcq_node
*__cds_wfcq_next_blocking(
443 cds_wfcq_head_ptr_t head
,
444 struct cds_wfcq_tail
*tail
,
445 struct cds_wfcq_node
*node
);
448 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
450 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
453 extern struct cds_wfcq_node
*__cds_wfcq_next_nonblocking(
454 cds_wfcq_head_ptr_t head
,
455 struct cds_wfcq_tail
*tail
,
456 struct cds_wfcq_node
*node
);
458 #endif /* !_LGPL_SOURCE */
461 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
462 * without dequeuing them.
463 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
464 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
465 * @node: iterator on the queue (struct cds_wfcq_node pointer).
467 * Content written into each node before enqueue is guaranteed to be
468 * consistent, but no other memory ordering is ensured.
469 * Dequeue/splice/iteration mutual exclusion should be ensured by the
472 #define __cds_wfcq_for_each_blocking(head, tail, node) \
473 for (node = __cds_wfcq_first_blocking(head, tail); \
475 node = __cds_wfcq_next_blocking(head, tail, node))
478 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
479 * without dequeuing them. Safe against deletion.
480 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
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
486 * Content written into each node before enqueue is guaranteed to be
487 * consistent, but no other memory ordering is ensured.
488 * Dequeue/splice/iteration mutual exclusion should be ensured by the
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); \
495 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
501 * In C++, implement static inline wrappers using function overloading
502 * to obtain an API similar to C.
505 static inline cds_wfcq_head_ptr_t
cds_wfcq_head_cast_cpp(struct __cds_wfcq_head
*head
)
507 cds_wfcq_head_ptr_t ret
= { ._h
= head
};
511 static inline cds_wfcq_head_ptr_t
cds_wfcq_head_cast_cpp(struct cds_wfcq_head
*head
)
513 cds_wfcq_head_ptr_t ret
= { .h
= head
};
517 template<typename T
> static inline bool cds_wfcq_empty(T head
,
518 struct cds_wfcq_tail
*tail
)
520 return cds_wfcq_empty(cds_wfcq_head_cast_cpp(head
), tail
);
523 template<typename T
> static inline bool cds_wfcq_enqueue(T head
,
524 struct cds_wfcq_tail
*tail
,
525 struct cds_wfcq_node
*node
)
527 return cds_wfcq_enqueue(cds_wfcq_head_cast_cpp(head
), tail
, node
);
530 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_dequeue_blocking(
531 T head
, struct cds_wfcq_tail
*tail
)
533 return __cds_wfcq_dequeue_blocking(cds_wfcq_head_cast_cpp(head
), tail
);
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
)
539 return __cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_cast_cpp(head
),
543 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_dequeue_nonblocking(
544 T head
, struct cds_wfcq_tail
*tail
)
546 return __cds_wfcq_dequeue_nonblocking(cds_wfcq_head_cast_cpp(head
), tail
);
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
)
552 return __cds_wfcq_dequeue_with_state_nonblocking(cds_wfcq_head_cast_cpp(head
),
556 template<typename T
, typename U
> static inline enum cds_wfcq_ret
__cds_wfcq_splice_blocking(
558 struct cds_wfcq_tail
*dest_q_tail
,
560 struct cds_wfcq_tail
*src_q_tail
)
562 return __cds_wfcq_splice_blocking(cds_wfcq_head_cast_cpp(dest_q_head
),
564 cds_wfcq_head_cast_cpp(src_q_head
),
568 template<typename T
, typename U
> static inline enum cds_wfcq_ret
__cds_wfcq_splice_nonblocking(
570 struct cds_wfcq_tail
*dest_q_tail
,
572 struct cds_wfcq_tail
*src_q_tail
)
574 return __cds_wfcq_splice_nonblocking(cds_wfcq_head_cast_cpp(dest_q_head
),
576 cds_wfcq_head_cast_cpp(src_q_head
),
580 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_first_blocking(
581 T head
, struct cds_wfcq_tail
*tail
)
583 return __cds_wfcq_first_blocking(cds_wfcq_head_cast_cpp(head
), tail
);
586 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_first_nonblocking(
587 T head
, struct cds_wfcq_tail
*tail
)
589 return __cds_wfcq_first_nonblocking(cds_wfcq_head_cast_cpp(head
), tail
);
592 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_next_blocking(
594 struct cds_wfcq_tail
*tail
,
595 struct cds_wfcq_node
*node
)
597 return __cds_wfcq_next_blocking(cds_wfcq_head_cast_cpp(head
), tail
, node
);
600 template<typename T
> static inline struct cds_wfcq_node
*__cds_wfcq_next_nonblocking(
602 struct cds_wfcq_tail
*tail
,
603 struct cds_wfcq_node
*node
)
605 return __cds_wfcq_next_nonblocking(cds_wfcq_head_cast_cpp(head
), tail
, node
);
610 #endif /* _URCU_WFCQUEUE_H */