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 ((void *) -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
;
78 * The transparent union allows calling functions that work on both
79 * struct cds_wfcq_head and struct __cds_wfcq_head on any of those two
83 struct __cds_wfcq_head
*_h
;
84 struct cds_wfcq_head
*h
;
85 } __attribute__((__transparent_union__
)) cds_wfcq_head_ptr_t
;
87 struct cds_wfcq_tail
{
88 struct cds_wfcq_node
*p
;
93 #include <urcu/static/wfcqueue.h>
95 #define cds_wfcq_node_init _cds_wfcq_node_init
96 #define cds_wfcq_init _cds_wfcq_init
97 #define cds_wfcq_empty _cds_wfcq_empty
98 #define cds_wfcq_enqueue _cds_wfcq_enqueue
100 /* Dequeue locking */
101 #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
102 #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
104 /* Locking performed within cds_wfcq calls. */
105 #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
106 #define cds_wfcq_dequeue_with_state_blocking \
107 _cds_wfcq_dequeue_with_state_blocking
108 #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking
109 #define cds_wfcq_first_blocking _cds_wfcq_first_blocking
110 #define cds_wfcq_next_blocking _cds_wfcq_next_blocking
112 /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
113 #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
114 #define __cds_wfcq_dequeue_with_state_blocking \
115 ___cds_wfcq_dequeue_with_state_blocking
116 #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking
117 #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking
118 #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking
121 * Locking ensured by caller by holding cds_wfcq_dequeue_lock().
122 * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they
123 * need to block. splice returns nonzero if it needs to block.
125 #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
126 #define __cds_wfcq_dequeue_with_state_nonblocking \
127 ___cds_wfcq_dequeue_with_state_nonblocking
128 #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking
129 #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking
130 #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking
132 #else /* !_LGPL_SOURCE */
135 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
137 * Synchronization table:
139 * External synchronization techniques described in the API below is
140 * required between pairs marked with "X". No external synchronization
141 * required between pairs marked with "-".
144 * [1] cds_wfcq_enqueue
145 * [2] __cds_wfcq_splice (destination queue)
146 * [3] __cds_wfcq_dequeue
147 * [4] __cds_wfcq_splice (source queue)
148 * [5] __cds_wfcq_first
149 * [6] __cds_wfcq_next
151 * [1] [2] [3] [4] [5] [6]
159 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
161 * For convenience, cds_wfcq_dequeue_blocking() and
162 * cds_wfcq_splice_blocking() hold the dequeue lock.
164 * Besides locking, mutual exclusion of dequeue, splice and iteration
165 * can be ensured by performing all of those operations from a single
166 * thread, without requiring any lock.
170 * cds_wfcq_node_init: initialize wait-free queue node.
172 extern void cds_wfcq_node_init(struct cds_wfcq_node
*node
);
175 * cds_wfcq_init: initialize wait-free queue.
177 extern void cds_wfcq_init(struct cds_wfcq_head
*head
,
178 struct cds_wfcq_tail
*tail
);
181 * __cds_wfcq_init: initialize wait-free queue.
183 extern void __cds_wfcq_init(struct __cds_wfcq_head
*head
,
184 struct cds_wfcq_tail
*tail
);
187 * cds_wfcq_empty: return whether wait-free queue is empty.
189 * No memory barrier is issued. No mutual exclusion is required.
191 extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head
,
192 struct cds_wfcq_tail
*tail
);
195 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
197 extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
198 struct cds_wfcq_tail
*tail
);
201 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
203 extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
204 struct cds_wfcq_tail
*tail
);
207 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
209 * Issues a full memory barrier before enqueue. No mutual exclusion is
212 * Returns false if the queue was empty prior to adding the node.
213 * Returns true otherwise.
215 extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head
,
216 struct cds_wfcq_tail
*tail
,
217 struct cds_wfcq_node
*node
);
220 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
222 * Content written into the node before enqueue is guaranteed to be
223 * consistent, but no other memory ordering is ensured.
224 * It is valid to reuse and free a dequeued node immediately.
225 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
228 extern struct cds_wfcq_node
*cds_wfcq_dequeue_blocking(
229 struct cds_wfcq_head
*head
,
230 struct cds_wfcq_tail
*tail
);
233 * cds_wfcq_dequeue_with_state_blocking: dequeue with state.
235 * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the
236 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
238 extern struct cds_wfcq_node
*cds_wfcq_dequeue_with_state_blocking(
239 struct cds_wfcq_head
*head
,
240 struct cds_wfcq_tail
*tail
,
244 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
246 * Dequeue all nodes from src_q.
247 * dest_q must be already initialized.
248 * Content written into the node before enqueue is guaranteed to be
249 * consistent, but no other memory ordering is ensured.
250 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
253 * Returns enum cds_wfcq_ret which indicates the state of the src or
256 extern enum cds_wfcq_ret
cds_wfcq_splice_blocking(
257 struct cds_wfcq_head
*dest_q_head
,
258 struct cds_wfcq_tail
*dest_q_tail
,
259 struct cds_wfcq_head
*src_q_head
,
260 struct cds_wfcq_tail
*src_q_tail
);
263 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
265 * Content written into the node before enqueue is guaranteed to be
266 * consistent, but no other memory ordering is ensured.
267 * It is valid to reuse and free a dequeued node immediately.
268 * Dequeue/splice/iteration mutual exclusion should be ensured by the
271 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_blocking(
272 cds_wfcq_head_ptr_t head
,
273 struct cds_wfcq_tail
*tail
);
276 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
278 * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the
279 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
281 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_blocking(
282 cds_wfcq_head_ptr_t head
,
283 struct cds_wfcq_tail
*tail
,
287 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
289 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
290 * if it needs to block.
292 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_nonblocking(
293 cds_wfcq_head_ptr_t head
,
294 struct cds_wfcq_tail
*tail
);
297 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
299 * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing
300 * the last node of the queue into state (CDS_WFCQ_STATE_LAST).
302 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_with_state_nonblocking(
303 cds_wfcq_head_ptr_t 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 * Mutual exclusion for src_q should be ensured by the caller as
313 * specified in the "Synchronisation table".
314 * Returns enum cds_wfcq_ret which indicates the state of the src or
315 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
317 extern enum cds_wfcq_ret
__cds_wfcq_splice_blocking(
318 cds_wfcq_head_ptr_t dest_q_head
,
319 struct cds_wfcq_tail
*dest_q_tail
,
320 cds_wfcq_head_ptr_t src_q_head
,
321 struct cds_wfcq_tail
*src_q_tail
);
324 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
326 * Same as __cds_wfcq_splice_blocking, but returns
327 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
329 extern enum cds_wfcq_ret
__cds_wfcq_splice_nonblocking(
330 cds_wfcq_head_ptr_t dest_q_head
,
331 struct cds_wfcq_tail
*dest_q_tail
,
332 cds_wfcq_head_ptr_t src_q_head
,
333 struct cds_wfcq_tail
*src_q_tail
);
336 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
338 * Content written into the node before enqueue is guaranteed to be
339 * consistent, but no other memory ordering is ensured.
340 * Dequeue/splice/iteration mutual exclusion should be ensured by the
343 * Used by for-like iteration macros:
344 * __cds_wfcq_for_each_blocking()
345 * __cds_wfcq_for_each_blocking_safe()
347 * Returns NULL if queue is empty, first node otherwise.
349 extern struct cds_wfcq_node
*__cds_wfcq_first_blocking(
350 cds_wfcq_head_ptr_t head
,
351 struct cds_wfcq_tail
*tail
);
354 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
356 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
359 extern struct cds_wfcq_node
*__cds_wfcq_first_nonblocking(
360 cds_wfcq_head_ptr_t head
,
361 struct cds_wfcq_tail
*tail
);
364 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
366 * Content written into the node before enqueue is guaranteed to be
367 * consistent, but no other memory ordering is ensured.
368 * Dequeue/splice/iteration mutual exclusion should be ensured by the
371 * Used by for-like iteration macros:
372 * __cds_wfcq_for_each_blocking()
373 * __cds_wfcq_for_each_blocking_safe()
375 * Returns NULL if reached end of queue, non-NULL next queue node
378 extern struct cds_wfcq_node
*__cds_wfcq_next_blocking(
379 cds_wfcq_head_ptr_t head
,
380 struct cds_wfcq_tail
*tail
,
381 struct cds_wfcq_node
*node
);
384 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
386 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
389 extern struct cds_wfcq_node
*__cds_wfcq_next_nonblocking(
390 cds_wfcq_head_ptr_t head
,
391 struct cds_wfcq_tail
*tail
,
392 struct cds_wfcq_node
*node
);
394 #endif /* !_LGPL_SOURCE */
397 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
398 * without dequeuing them.
399 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
400 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
401 * @node: iterator on the queue (struct cds_wfcq_node pointer).
403 * Content written into each node before enqueue is guaranteed to be
404 * consistent, but no other memory ordering is ensured.
405 * Dequeue/splice/iteration mutual exclusion should be ensured by the
408 #define __cds_wfcq_for_each_blocking(head, tail, node) \
409 for (node = __cds_wfcq_first_blocking(head, tail); \
411 node = __cds_wfcq_next_blocking(head, tail, node))
414 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
415 * without dequeuing them. Safe against deletion.
416 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
417 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
418 * @node: iterator on the queue (struct cds_wfcq_node pointer).
419 * @n: struct cds_wfcq_node pointer holding the next pointer (used
422 * Content written into each node before enqueue is guaranteed to be
423 * consistent, but no other memory ordering is ensured.
424 * Dequeue/splice/iteration mutual exclusion should be ensured by the
427 #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \
428 for (node = __cds_wfcq_first_blocking(head, tail), \
429 n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \
431 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
437 #endif /* _URCU_WFCQUEUE_H */