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
30 #include <urcu/compiler.h>
31 #include <urcu/arch.h>
38 * Concurrent queue with wait-free enqueue/blocking dequeue.
40 * This queue has been designed and implemented collaboratively by
41 * Mathieu Desnoyers and Lai Jiangshan. Inspired from
42 * half-wait-free/half-blocking queue implementation done by Paul E.
46 #define CDS_WFCQ_WOULDBLOCK ((struct cds_wfcq_node *) -1UL)
49 CDS_WFCQ_RET_WOULDBLOCK
= -1,
50 CDS_WFCQ_RET_DEST_EMPTY
= 0,
51 CDS_WFCQ_RET_DEST_NON_EMPTY
= 1,
52 CDS_WFCQ_RET_SRC_EMPTY
= 2,
56 CDS_WFCQ_STATE_LAST
= (1U << 0),
59 struct cds_wfcq_node
{
60 struct cds_wfcq_node
*next
;
64 * Do not put head and tail on the same cache-line if concurrent
65 * enqueue/dequeue are expected from many CPUs. This eliminates
66 * false-sharing between enqueue and dequeue.
68 struct __cds_wfcq_head
{
69 struct cds_wfcq_node node
;
72 struct cds_wfcq_head
{
73 struct cds_wfcq_node node
;
79 * The transparent union allows calling functions that work on both
80 * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two
84 struct __cds_wfcq_head
*_h
;
85 struct cds_wfcq_head
*h
;
86 } __attribute__((__transparent_union__
)) cds_wfcq_head_ptr_t
;
89 * This static inline is only present for compatibility with C++. It is
92 static inline struct __cds_wfcq_head
*__cds_wfcq_head_cast(struct __cds_wfcq_head
*head
)
98 * This static inline is only present for compatibility with C++. It is
101 static inline struct cds_wfcq_head
*cds_wfcq_head_cast(struct cds_wfcq_head
*head
)
105 #else /* #ifndef __cplusplus */
107 /* C++ ignores transparent union. */
109 struct __cds_wfcq_head
*_h
;
110 struct cds_wfcq_head
*h
;
111 } cds_wfcq_head_ptr_t
;
113 /* C++ ignores transparent union. Requires an explicit conversion. */
114 static inline cds_wfcq_head_ptr_t
__cds_wfcq_head_cast(struct __cds_wfcq_head
*head
)
116 cds_wfcq_head_ptr_t ret
= { ._h
= head
};
119 /* C++ ignores transparent union. Requires an explicit conversion. */
120 static inline cds_wfcq_head_ptr_t
cds_wfcq_head_cast(struct cds_wfcq_head
*head
)
122 cds_wfcq_head_ptr_t ret
= { .h
= head
};
125 #endif /* #else #ifndef __cplusplus */
127 struct cds_wfcq_tail
{
128 struct cds_wfcq_node
*p
;
133 #include <urcu/static/wfcqueue.h>
135 #define cds_wfcq_node_init _cds_wfcq_node_init
136 #define cds_wfcq_init _cds_wfcq_init
137 #define cds_wfcq_empty _cds_wfcq_empty
138 #define cds_wfcq_enqueue _cds_wfcq_enqueue
140 /* Dequeue locking */
141 #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
142 #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
144 /* Locking performed within cds_wfcq calls. */
145 #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
146 #define cds_wfcq_dequeue_with_state_blocking \
147 _cds_wfcq_dequeue_with_state_blocking
148 #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking
149 #define cds_wfcq_first_blocking _cds_wfcq_first_blocking
150 #define cds_wfcq_next_blocking _cds_wfcq_next_blocking
152 /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
153 #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
154 #define __cds_wfcq_dequeue_with_state_blocking \
155 ___cds_wfcq_dequeue_with_state_blocking
156 #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking
157 #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking
158 #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking
161 * Locking ensured by caller by holding cds_wfcq_dequeue_lock().
162 * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they
163 * need to block. splice returns nonzero if it needs to block.
165 #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
166 #define __cds_wfcq_dequeue_with_state_nonblocking \
167 ___cds_wfcq_dequeue_with_state_nonblocking
168 #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking
169 #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking
170 #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking
172 #else /* !_LGPL_SOURCE */
175 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
177 * Synchronization table:
179 * External synchronization techniques described in the API below is
180 * required between pairs marked with "X". No external synchronization
181 * required between pairs marked with "-".
184 * [1] cds_wfcq_enqueue
185 * [2] __cds_wfcq_splice (destination queue)
186 * [3] __cds_wfcq_dequeue
187 * [4] __cds_wfcq_splice (source queue)
188 * [5] __cds_wfcq_first
189 * [6] __cds_wfcq_next
191 * [1] [2] [3] [4] [5] [6]
199 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
201 * For convenience, cds_wfcq_dequeue_blocking() and
202 * cds_wfcq_splice_blocking() hold the dequeue lock.
204 * Besides locking, mutual exclusion of dequeue, splice and iteration
205 * can be ensured by performing all of those operations from a single
206 * thread, without requiring any lock.
210 * cds_wfcq_node_init: initialize wait-free queue node.
212 extern void cds_wfcq_node_init(struct cds_wfcq_node
*node
);
215 * cds_wfcq_init: initialize wait-free queue.
217 extern void cds_wfcq_init(struct cds_wfcq_head
*head
,
218 struct cds_wfcq_tail
*tail
);
221 * __cds_wfcq_init: initialize wait-free queue.
223 extern void __cds_wfcq_init(struct __cds_wfcq_head
*head
,
224 struct cds_wfcq_tail
*tail
);
227 * cds_wfcq_empty: return whether wait-free queue is empty.
229 * No memory barrier is issued. No mutual exclusion is required.
231 extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head
,
232 struct cds_wfcq_tail
*tail
);
235 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
237 extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
238 struct cds_wfcq_tail
*tail
);
241 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
243 extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
244 struct cds_wfcq_tail
*tail
);
247 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
249 * Issues a full memory barrier before enqueue. No mutual exclusion is
252 * Returns false if the queue was empty prior to adding the node.
253 * Returns true otherwise.
255 extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head
,
256 struct cds_wfcq_tail
*tail
,
257 struct cds_wfcq_node
*node
);
260 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
262 * Content written into the node before enqueue is guaranteed to be
263 * consistent, but no other memory ordering is ensured.
264 * It is valid to reuse and free a dequeued node immediately.
265 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
268 extern struct cds_wfcq_node
*cds_wfcq_dequeue_blocking(
269 struct cds_wfcq_head
*head
,
270 struct cds_wfcq_tail
*tail
);
273 * cds_wfcq_dequeue_with_state_blocking: dequeue with state.
275 * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the
276 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
278 extern struct cds_wfcq_node
*cds_wfcq_dequeue_with_state_blocking(
279 struct cds_wfcq_head
*head
,
280 struct cds_wfcq_tail
*tail
,
284 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
286 * Dequeue all nodes from src_q.
287 * dest_q must be already initialized.
288 * Content written into the node before enqueue is guaranteed to be
289 * consistent, but no other memory ordering is ensured.
290 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
293 * Returns enum cds_wfcq_ret which indicates the state of the src or
296 extern enum cds_wfcq_ret
cds_wfcq_splice_blocking(
297 struct cds_wfcq_head
*dest_q_head
,
298 struct cds_wfcq_tail
*dest_q_tail
,
299 struct cds_wfcq_head
*src_q_head
,
300 struct cds_wfcq_tail
*src_q_tail
);
303 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
305 * Content written into the node before enqueue is guaranteed to be
306 * consistent, but no other memory ordering is ensured.
307 * It is valid to reuse and free a dequeued node immediately.
308 * Dequeue/splice/iteration mutual exclusion should be ensured by the
311 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_blocking(
312 cds_wfcq_head_ptr_t head
,
313 struct cds_wfcq_tail
*tail
);
316 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
318 * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the
319 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
321 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_blocking(
322 cds_wfcq_head_ptr_t head
,
323 struct cds_wfcq_tail
*tail
,
327 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
329 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
330 * if it needs to block.
332 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_nonblocking(
333 cds_wfcq_head_ptr_t head
,
334 struct cds_wfcq_tail
*tail
);
337 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
339 * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing
340 * the last node of the queue into state (CDS_WFCQ_STATE_LAST).
342 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_nonblocking(
343 cds_wfcq_head_ptr_t head
,
344 struct cds_wfcq_tail
*tail
,
348 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
350 * Dequeue all nodes from src_q.
351 * dest_q must be already initialized.
352 * Mutual exclusion for src_q should be ensured by the caller as
353 * specified in the "Synchronisation table".
354 * Returns enum cds_wfcq_ret which indicates the state of the src or
355 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
357 extern enum cds_wfcq_ret
__cds_wfcq_splice_blocking(
358 cds_wfcq_head_ptr_t dest_q_head
,
359 struct cds_wfcq_tail
*dest_q_tail
,
360 cds_wfcq_head_ptr_t src_q_head
,
361 struct cds_wfcq_tail
*src_q_tail
);
364 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
366 * Same as __cds_wfcq_splice_blocking, but returns
367 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
369 extern enum cds_wfcq_ret
__cds_wfcq_splice_nonblocking(
370 cds_wfcq_head_ptr_t dest_q_head
,
371 struct cds_wfcq_tail
*dest_q_tail
,
372 cds_wfcq_head_ptr_t src_q_head
,
373 struct cds_wfcq_tail
*src_q_tail
);
376 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
378 * Content written into the node before enqueue is guaranteed to be
379 * consistent, but no other memory ordering is ensured.
380 * Dequeue/splice/iteration mutual exclusion should be ensured by the
383 * Used by for-like iteration macros:
384 * __cds_wfcq_for_each_blocking()
385 * __cds_wfcq_for_each_blocking_safe()
387 * Returns NULL if queue is empty, first node otherwise.
389 extern struct cds_wfcq_node
*__cds_wfcq_first_blocking(
390 cds_wfcq_head_ptr_t head
,
391 struct cds_wfcq_tail
*tail
);
394 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
396 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
399 extern struct cds_wfcq_node
*__cds_wfcq_first_nonblocking(
400 cds_wfcq_head_ptr_t head
,
401 struct cds_wfcq_tail
*tail
);
404 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
406 * Content written into the node before enqueue is guaranteed to be
407 * consistent, but no other memory ordering is ensured.
408 * Dequeue/splice/iteration mutual exclusion should be ensured by the
411 * Used by for-like iteration macros:
412 * __cds_wfcq_for_each_blocking()
413 * __cds_wfcq_for_each_blocking_safe()
415 * Returns NULL if reached end of queue, non-NULL next queue node
418 extern struct cds_wfcq_node
*__cds_wfcq_next_blocking(
419 cds_wfcq_head_ptr_t head
,
420 struct cds_wfcq_tail
*tail
,
421 struct cds_wfcq_node
*node
);
424 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
426 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
429 extern struct cds_wfcq_node
*__cds_wfcq_next_nonblocking(
430 cds_wfcq_head_ptr_t head
,
431 struct cds_wfcq_tail
*tail
,
432 struct cds_wfcq_node
*node
);
434 #endif /* !_LGPL_SOURCE */
437 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
438 * without dequeuing them.
439 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
440 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
441 * @node: iterator on the queue (struct cds_wfcq_node pointer).
443 * Content written into each node before enqueue is guaranteed to be
444 * consistent, but no other memory ordering is ensured.
445 * Dequeue/splice/iteration mutual exclusion should be ensured by the
448 #define __cds_wfcq_for_each_blocking(head, tail, node) \
449 for (node = __cds_wfcq_first_blocking(head, tail); \
451 node = __cds_wfcq_next_blocking(head, tail, node))
454 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
455 * without dequeuing them. Safe against deletion.
456 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
457 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
458 * @node: iterator on the queue (struct cds_wfcq_node pointer).
459 * @n: struct cds_wfcq_node pointer holding the next pointer (used
462 * Content written into each node before enqueue is guaranteed to be
463 * consistent, but no other memory ordering is ensured.
464 * Dequeue/splice/iteration mutual exclusion should be ensured by the
467 #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \
468 for (node = __cds_wfcq_first_blocking(head, tail), \
469 n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \
471 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
477 #endif /* _URCU_WFCQUEUE_H */