Version 0.8.11
[userspace-rcu.git] / urcu / wfcqueue.h
CommitLineData
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>
28#include <assert.h>
29#include <stdbool.h>
30#include <urcu/compiler.h>
31#include <urcu/arch.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/*
38 * Concurrent queue with wait-free enqueue/blocking dequeue.
39 *
ebfd2673
MD
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.
43 * McKenney.
8ad4ce58
MD
44 */
45
47215721
MD
46#define CDS_WFCQ_WOULDBLOCK ((void *) -1UL)
47
23773356 48enum cds_wfcq_ret {
f878b49e 49 CDS_WFCQ_RET_WOULDBLOCK = -1,
23773356
MD
50 CDS_WFCQ_RET_DEST_EMPTY = 0,
51 CDS_WFCQ_RET_DEST_NON_EMPTY = 1,
52 CDS_WFCQ_RET_SRC_EMPTY = 2,
53};
54
eec791af
MD
55enum cds_wfcq_state {
56 CDS_WFCQ_STATE_LAST = (1U << 0),
57};
58
8ad4ce58
MD
59struct cds_wfcq_node {
60 struct cds_wfcq_node *next;
61};
62
63/*
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.
67 */
68struct cds_wfcq_head {
69 struct cds_wfcq_node node;
70 pthread_mutex_t lock;
71};
72
73struct cds_wfcq_tail {
74 struct cds_wfcq_node *p;
75};
76
77#ifdef _LGPL_SOURCE
78
79#include <urcu/static/wfcqueue.h>
80
81#define cds_wfcq_node_init _cds_wfcq_node_init
82#define cds_wfcq_init _cds_wfcq_init
93aee570 83#define cds_wfcq_destroy _cds_wfcq_destroy
8ad4ce58
MD
84#define cds_wfcq_empty _cds_wfcq_empty
85#define cds_wfcq_enqueue _cds_wfcq_enqueue
86
87/* Dequeue locking */
88#define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
89#define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
90
91/* Locking performed within cds_wfcq calls. */
92#define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
eec791af
MD
93#define cds_wfcq_dequeue_with_state_blocking \
94 _cds_wfcq_dequeue_with_state_blocking
8ad4ce58
MD
95#define cds_wfcq_splice_blocking _cds_wfcq_splice_blocking
96#define cds_wfcq_first_blocking _cds_wfcq_first_blocking
97#define cds_wfcq_next_blocking _cds_wfcq_next_blocking
98
99/* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
100#define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
eec791af
MD
101#define __cds_wfcq_dequeue_with_state_blocking \
102 ___cds_wfcq_dequeue_with_state_blocking
8ad4ce58
MD
103#define __cds_wfcq_splice_blocking ___cds_wfcq_splice_blocking
104#define __cds_wfcq_first_blocking ___cds_wfcq_first_blocking
105#define __cds_wfcq_next_blocking ___cds_wfcq_next_blocking
106
47215721
MD
107/*
108 * Locking ensured by caller by holding cds_wfcq_dequeue_lock().
109 * Non-blocking: deque, first, next return CDS_WFCQ_WOULDBLOCK if they
110 * need to block. splice returns nonzero if it needs to block.
111 */
112#define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
eec791af
MD
113#define __cds_wfcq_dequeue_with_state_nonblocking \
114 ___cds_wfcq_dequeue_with_state_nonblocking
47215721
MD
115#define __cds_wfcq_splice_nonblocking ___cds_wfcq_splice_nonblocking
116#define __cds_wfcq_first_nonblocking ___cds_wfcq_first_nonblocking
117#define __cds_wfcq_next_nonblocking ___cds_wfcq_next_nonblocking
118
8ad4ce58
MD
119#else /* !_LGPL_SOURCE */
120
121/*
122 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
123 *
f878b49e
MD
124 * Synchronization table:
125 *
126 * External synchronization techniques described in the API below is
127 * required between pairs marked with "X". No external synchronization
128 * required between pairs marked with "-".
129 *
130 * Legend:
131 * [1] cds_wfcq_enqueue
132 * [2] __cds_wfcq_splice (destination queue)
133 * [3] __cds_wfcq_dequeue
134 * [4] __cds_wfcq_splice (source queue)
135 * [5] __cds_wfcq_first
136 * [6] __cds_wfcq_next
137 *
138 * [1] [2] [3] [4] [5] [6]
139 * [1] - - - - - -
140 * [2] - - - - - -
141 * [3] - - X X X X
142 * [4] - - X - X X
143 * [5] - - X X - -
144 * [6] - - X X - -
145 *
8ad4ce58
MD
146 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
147 *
148 * For convenience, cds_wfcq_dequeue_blocking() and
149 * cds_wfcq_splice_blocking() hold the dequeue lock.
1fe734e1
MD
150 *
151 * Besides locking, mutual exclusion of dequeue, splice and iteration
152 * can be ensured by performing all of those operations from a single
153 * thread, without requiring any lock.
8ad4ce58
MD
154 */
155
156/*
157 * cds_wfcq_node_init: initialize wait-free queue node.
158 */
159extern void cds_wfcq_node_init(struct cds_wfcq_node *node);
160
161/*
93aee570
MD
162 * cds_wfcq_init: initialize wait-free queue. Pair with
163 * cds_wfcq_destroy().
8ad4ce58
MD
164 */
165extern void cds_wfcq_init(struct cds_wfcq_head *head,
166 struct cds_wfcq_tail *tail);
167
93aee570
MD
168/*
169 * cds_wfcq_destroy: destroy wait-free queue. Pair with
170 * cds_wfcq_init().
171 */
172extern void cds_wfcq_destroy(struct cds_wfcq_head *head,
173 struct cds_wfcq_tail *tail);
174
8ad4ce58
MD
175/*
176 * cds_wfcq_empty: return whether wait-free queue is empty.
177 *
178 * No memory barrier is issued. No mutual exclusion is required.
179 */
180extern bool cds_wfcq_empty(struct cds_wfcq_head *head,
181 struct cds_wfcq_tail *tail);
182
183/*
184 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
185 */
186extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head,
187 struct cds_wfcq_tail *tail);
188
189/*
190 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
191 */
192extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head,
193 struct cds_wfcq_tail *tail);
194
195/*
196 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
197 *
198 * Issues a full memory barrier before enqueue. No mutual exclusion is
199 * required.
23773356
MD
200 *
201 * Returns false if the queue was empty prior to adding the node.
202 * Returns true otherwise.
8ad4ce58 203 */
23773356 204extern bool cds_wfcq_enqueue(struct cds_wfcq_head *head,
8ad4ce58
MD
205 struct cds_wfcq_tail *tail,
206 struct cds_wfcq_node *node);
207
208/*
209 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
210 *
211 * Content written into the node before enqueue is guaranteed to be
212 * consistent, but no other memory ordering is ensured.
213 * It is valid to reuse and free a dequeued node immediately.
ffa11a18 214 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
1fe734e1 215 * ensured.
8ad4ce58
MD
216 */
217extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking(
218 struct cds_wfcq_head *head,
219 struct cds_wfcq_tail *tail);
220
eec791af
MD
221/*
222 * cds_wfcq_dequeue_with_state_blocking: dequeue with state.
223 *
224 * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the
225 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
226 */
227extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking(
228 struct cds_wfcq_head *head,
229 struct cds_wfcq_tail *tail,
230 int *state);
231
8ad4ce58
MD
232/*
233 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
234 *
235 * Dequeue all nodes from src_q.
236 * dest_q must be already initialized.
237 * Content written into the node before enqueue is guaranteed to be
238 * consistent, but no other memory ordering is ensured.
ffa11a18 239 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
1fe734e1 240 * ensured.
23773356
MD
241 *
242 * Returns enum cds_wfcq_ret which indicates the state of the src or
243 * dest queue. Cannot block.
8ad4ce58 244 */
23773356 245extern enum cds_wfcq_ret cds_wfcq_splice_blocking(
8ad4ce58
MD
246 struct cds_wfcq_head *dest_q_head,
247 struct cds_wfcq_tail *dest_q_tail,
248 struct cds_wfcq_head *src_q_head,
249 struct cds_wfcq_tail *src_q_tail);
250
251/*
47215721 252 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
8ad4ce58
MD
253 *
254 * Content written into the node before enqueue is guaranteed to be
255 * consistent, but no other memory ordering is ensured.
256 * It is valid to reuse and free a dequeued node immediately.
1fe734e1
MD
257 * Dequeue/splice/iteration mutual exclusion should be ensured by the
258 * caller.
8ad4ce58
MD
259 */
260extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking(
261 struct cds_wfcq_head *head,
262 struct cds_wfcq_tail *tail);
263
eec791af
MD
264/*
265 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
266 *
267 * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the
268 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
269 */
270extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking(
271 struct cds_wfcq_head *head,
272 struct cds_wfcq_tail *tail,
273 int *state);
274
47215721
MD
275/*
276 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
277 *
278 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
279 * if it needs to block.
280 */
281extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
282 struct cds_wfcq_head *head,
283 struct cds_wfcq_tail *tail);
284
eec791af
MD
285/*
286 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
287 *
288 * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing
289 * the last node of the queue into state (CDS_WFCQ_STATE_LAST).
290 */
291extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking(
292 struct cds_wfcq_head *head,
293 struct cds_wfcq_tail *tail,
294 int *state);
295
8ad4ce58
MD
296/*
297 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
298 *
299 * Dequeue all nodes from src_q.
300 * dest_q must be already initialized.
f878b49e
MD
301 * Mutual exclusion for src_q should be ensured by the caller as
302 * specified in the "Synchronisation table".
23773356 303 * Returns enum cds_wfcq_ret which indicates the state of the src or
f878b49e 304 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
8ad4ce58 305 */
23773356 306extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
8ad4ce58
MD
307 struct cds_wfcq_head *dest_q_head,
308 struct cds_wfcq_tail *dest_q_tail,
309 struct cds_wfcq_head *src_q_head,
310 struct cds_wfcq_tail *src_q_tail);
311
47215721
MD
312/*
313 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
314 *
23773356
MD
315 * Same as __cds_wfcq_splice_blocking, but returns
316 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
47215721 317 */
23773356 318extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking(
47215721
MD
319 struct cds_wfcq_head *dest_q_head,
320 struct cds_wfcq_tail *dest_q_tail,
321 struct cds_wfcq_head *src_q_head,
322 struct cds_wfcq_tail *src_q_tail);
323
8ad4ce58
MD
324/*
325 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
326 *
327 * Content written into the node before enqueue is guaranteed to be
328 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
329 * Dequeue/splice/iteration mutual exclusion should be ensured by the
330 * caller.
f94061a3
MD
331 *
332 * Used by for-like iteration macros:
333 * __cds_wfcq_for_each_blocking()
334 * __cds_wfcq_for_each_blocking_safe()
131a29a6
MD
335 *
336 * Returns NULL if queue is empty, first node otherwise.
8ad4ce58
MD
337 */
338extern struct cds_wfcq_node *__cds_wfcq_first_blocking(
339 struct cds_wfcq_head *head,
340 struct cds_wfcq_tail *tail);
341
47215721
MD
342/*
343 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
344 *
345 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
346 * it needs to block.
347 */
348extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking(
349 struct cds_wfcq_head *head,
350 struct cds_wfcq_tail *tail);
351
8ad4ce58
MD
352/*
353 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
354 *
355 * Content written into the node before enqueue is guaranteed to be
356 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
357 * Dequeue/splice/iteration mutual exclusion should be ensured by the
358 * caller.
f94061a3
MD
359 *
360 * Used by for-like iteration macros:
361 * __cds_wfcq_for_each_blocking()
362 * __cds_wfcq_for_each_blocking_safe()
131a29a6
MD
363 *
364 * Returns NULL if reached end of queue, non-NULL next queue node
365 * otherwise.
8ad4ce58
MD
366 */
367extern struct cds_wfcq_node *__cds_wfcq_next_blocking(
368 struct cds_wfcq_head *head,
369 struct cds_wfcq_tail *tail,
370 struct cds_wfcq_node *node);
371
47215721
MD
372/*
373 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
374 *
375 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
376 * it needs to block.
377 */
378extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking(
379 struct cds_wfcq_head *head,
380 struct cds_wfcq_tail *tail,
381 struct cds_wfcq_node *node);
382
8ad4ce58
MD
383#endif /* !_LGPL_SOURCE */
384
385/*
386 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
387 * without dequeuing them.
388 * @head: head of the queue (struct cds_wfcq_head pointer).
389 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
390 * @node: iterator on the queue (struct cds_wfcq_node pointer).
391 *
392 * Content written into each node before enqueue is guaranteed to be
393 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
394 * Dequeue/splice/iteration mutual exclusion should be ensured by the
395 * caller.
8ad4ce58
MD
396 */
397#define __cds_wfcq_for_each_blocking(head, tail, node) \
398 for (node = __cds_wfcq_first_blocking(head, tail); \
399 node != NULL; \
400 node = __cds_wfcq_next_blocking(head, tail, node))
401
402/*
403 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
404 * without dequeuing them. Safe against deletion.
405 * @head: head of the queue (struct cds_wfcq_head pointer).
406 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
407 * @node: iterator on the queue (struct cds_wfcq_node pointer).
408 * @n: struct cds_wfcq_node pointer holding the next pointer (used
409 * internally).
410 *
411 * Content written into each node before enqueue is guaranteed to be
412 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
413 * Dequeue/splice/iteration mutual exclusion should be ensured by the
414 * caller.
8ad4ce58
MD
415 */
416#define __cds_wfcq_for_each_blocking_safe(head, tail, node, n) \
417 for (node = __cds_wfcq_first_blocking(head, tail), \
418 n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL); \
419 node != NULL; \
420 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
421
422#ifdef __cplusplus
423}
424#endif
425
426#endif /* _URCU_WFCQUEUE_H */
This page took 0.052306 seconds and 4 git commands to generate.