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,
55 struct cds_wfcq_node
{
56 struct cds_wfcq_node
*next
;
60 * Do not put head and tail on the same cache-line if concurrent
61 * enqueue/dequeue are expected from many CPUs. This eliminates
62 * false-sharing between enqueue and dequeue.
64 struct cds_wfcq_head
{
65 struct cds_wfcq_node node
;
69 struct cds_wfcq_tail
{
70 struct cds_wfcq_node
*p
;
75 #include <urcu/static/wfcqueue.h>
77 #define cds_wfcq_node_init _cds_wfcq_node_init
78 #define cds_wfcq_init _cds_wfcq_init
79 #define cds_wfcq_empty _cds_wfcq_empty
80 #define cds_wfcq_enqueue _cds_wfcq_enqueue
83 #define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
84 #define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
86 /* Locking performed within cds_wfcq calls. */
87 #define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
88 #define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking
89 #define cds_wfcq_first_blocking _cds_wfcq_first_blocking
90 #define cds_wfcq_next_blocking _cds_wfcq_next_blocking
92 /* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
93 #define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
94 #define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking
95 #define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking
96 #define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking
99 * Locking ensured by caller by holding cds_wfcq_dequeue_lock().
100 * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they
101 * need to block. splice returns nonzero if it needs to block.
103 #define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
104 #define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking
105 #define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking
106 #define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking
108 #else /* !_LGPL_SOURCE */
111 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
113 * Synchronization table:
115 * External synchronization techniques described in the API below is
116 * required between pairs marked with "X". No external synchronization
117 * required between pairs marked with "-".
120 * [1] cds_wfcq_enqueue
121 * [2] __cds_wfcq_splice (destination queue)
122 * [3] __cds_wfcq_dequeue
123 * [4] __cds_wfcq_splice (source queue)
124 * [5] __cds_wfcq_first
125 * [6] __cds_wfcq_next
127 * [1] [2] [3] [4] [5] [6]
135 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
137 * For convenience, cds_wfcq_dequeue_blocking() and
138 * cds_wfcq_splice_blocking() hold the dequeue lock.
140 * Besides locking, mutual exclusion of dequeue, splice and iteration
141 * can be ensured by performing all of those operations from a single
142 * thread, without requiring any lock.
146 * cds_wfcq_node_init: initialize wait-free queue node.
148 extern void cds_wfcq_node_init(struct cds_wfcq_node
*node
);
151 * cds_wfcq_init: initialize wait-free queue.
153 extern void cds_wfcq_init(struct cds_wfcq_head
*head
,
154 struct cds_wfcq_tail
*tail
);
157 * cds_wfcq_empty: return whether wait-free queue is empty.
159 * No memory barrier is issued. No mutual exclusion is required.
161 extern bool cds_wfcq_empty(struct cds_wfcq_head
*head
,
162 struct cds_wfcq_tail
*tail
);
165 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
167 extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
168 struct cds_wfcq_tail
*tail
);
171 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
173 extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
174 struct cds_wfcq_tail
*tail
);
177 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
179 * Issues a full memory barrier before enqueue. No mutual exclusion is
182 * Returns false if the queue was empty prior to adding the node.
183 * Returns true otherwise.
185 extern bool cds_wfcq_enqueue(struct cds_wfcq_head
*head
,
186 struct cds_wfcq_tail
*tail
,
187 struct cds_wfcq_node
*node
);
190 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
192 * Content written into the node before enqueue is guaranteed to be
193 * consistent, but no other memory ordering is ensured.
194 * It is valid to reuse and free a dequeued node immediately.
195 * Mutual exlusion with cds_wfcq_dequeue_blocking and dequeue lock is
198 extern struct cds_wfcq_node
*cds_wfcq_dequeue_blocking(
199 struct cds_wfcq_head
*head
,
200 struct cds_wfcq_tail
*tail
);
203 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
205 * Dequeue all nodes from src_q.
206 * dest_q must be already initialized.
207 * Content written into the node before enqueue is guaranteed to be
208 * consistent, but no other memory ordering is ensured.
209 * Mutual exlusion with cds_wfcq_dequeue_blocking and dequeue lock is
212 * Returns enum cds_wfcq_ret which indicates the state of the src or
213 * dest queue. Cannot block.
215 extern enum cds_wfcq_ret
cds_wfcq_splice_blocking(
216 struct cds_wfcq_head
*dest_q_head
,
217 struct cds_wfcq_tail
*dest_q_tail
,
218 struct cds_wfcq_head
*src_q_head
,
219 struct cds_wfcq_tail
*src_q_tail
);
222 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
224 * Content written into the node before enqueue is guaranteed to be
225 * consistent, but no other memory ordering is ensured.
226 * It is valid to reuse and free a dequeued node immediately.
227 * Dequeue/splice/iteration mutual exclusion should be ensured by the
230 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_blocking(
231 struct cds_wfcq_head
*head
,
232 struct cds_wfcq_tail
*tail
);
235 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
237 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
238 * if it needs to block.
240 extern struct cds_wfcq_node
*__cds_wfcq_dequeue_nonblocking(
241 struct cds_wfcq_head
*head
,
242 struct cds_wfcq_tail
*tail
);
245 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
247 * Dequeue all nodes from src_q.
248 * dest_q must be already initialized.
249 * Mutual exclusion for src_q should be ensured by the caller as
250 * specified in the "Synchronisation table".
251 * Returns enum cds_wfcq_ret which indicates the state of the src or
252 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
254 extern enum cds_wfcq_ret
__cds_wfcq_splice_blocking(
255 struct cds_wfcq_head
*dest_q_head
,
256 struct cds_wfcq_tail
*dest_q_tail
,
257 struct cds_wfcq_head
*src_q_head
,
258 struct cds_wfcq_tail
*src_q_tail
);
261 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
263 * Same as __cds_wfcq_splice_blocking, but returns
264 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
266 extern enum cds_wfcq_ret
__cds_wfcq_splice_nonblocking(
267 struct cds_wfcq_head
*dest_q_head
,
268 struct cds_wfcq_tail
*dest_q_tail
,
269 struct cds_wfcq_head
*src_q_head
,
270 struct cds_wfcq_tail
*src_q_tail
);
273 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
275 * Content written into the node before enqueue is guaranteed to be
276 * consistent, but no other memory ordering is ensured.
277 * Dequeue/splice/iteration mutual exclusion should be ensured by the
280 * Used by for-like iteration macros:
281 * __cds_wfcq_for_each_blocking()
282 * __cds_wfcq_for_each_blocking_safe()
284 extern struct cds_wfcq_node
*__cds_wfcq_first_blocking(
285 struct cds_wfcq_head
*head
,
286 struct cds_wfcq_tail
*tail
);
289 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
291 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
294 extern struct cds_wfcq_node
*__cds_wfcq_first_nonblocking(
295 struct cds_wfcq_head
*head
,
296 struct cds_wfcq_tail
*tail
);
299 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
301 * Content written into the node before enqueue is guaranteed to be
302 * consistent, but no other memory ordering is ensured.
303 * Dequeue/splice/iteration mutual exclusion should be ensured by the
306 * Used by for-like iteration macros:
307 * __cds_wfcq_for_each_blocking()
308 * __cds_wfcq_for_each_blocking_safe()
310 extern struct cds_wfcq_node
*__cds_wfcq_next_blocking(
311 struct cds_wfcq_head
*head
,
312 struct cds_wfcq_tail
*tail
,
313 struct cds_wfcq_node
*node
);
316 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
318 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
321 extern struct cds_wfcq_node
*__cds_wfcq_next_nonblocking(
322 struct cds_wfcq_head
*head
,
323 struct cds_wfcq_tail
*tail
,
324 struct cds_wfcq_node
*node
);
326 #endif /* !_LGPL_SOURCE */
329 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
330 * without dequeuing them.
331 * @head: head of the queue (struct cds_wfcq_head pointer).
332 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
333 * @node: iterator on the queue (struct cds_wfcq_node pointer).
335 * Content written into each node before enqueue is guaranteed to be
336 * consistent, but no other memory ordering is ensured.
337 * Dequeue/splice/iteration mutual exclusion should be ensured by the
340 #define __cds_wfcq_for_each_blocking(head, tail, node) \
341 for (node = __cds_wfcq_first_blocking(head, tail); \
343 node = __cds_wfcq_next_blocking(head, tail, node))
346 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
347 * without dequeuing them. Safe against deletion.
348 * @head: head of the queue (struct cds_wfcq_head pointer).
349 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
350 * @node: iterator on the queue (struct cds_wfcq_node pointer).
351 * @n: struct cds_wfcq_node pointer holding the next pointer (used
354 * Content written into each node before enqueue is guaranteed to be
355 * consistent, but no other memory ordering is ensured.
356 * Dequeue/splice/iteration mutual exclusion should be ensured by the
359 #define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \
360 for (node = __cds_wfcq_first_blocking(head, tail), \
361 n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \
363 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
369 #endif /* _URCU_WFCQUEUE_H */