wfcqueue: Implement mutex-free wfcqueue head with transparent union
[urcu.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 */
f637f191
MD
68struct __cds_wfcq_head {
69 struct cds_wfcq_node node;
70};
71
8ad4ce58
MD
72struct cds_wfcq_head {
73 struct cds_wfcq_node node;
74 pthread_mutex_t lock;
75};
76
f637f191
MD
77/*
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
80 * types.
81 */
82typedef union __attribute__((__transparent_union__)) {
83 struct __cds_wfcq_head *_h;
84 struct cds_wfcq_head *h;
85} cds_wfcq_head_ptr_t;
86
8ad4ce58
MD
87struct cds_wfcq_tail {
88 struct cds_wfcq_node *p;
89};
90
91#ifdef _LGPL_SOURCE
92
93#include <urcu/static/wfcqueue.h>
94
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
99
100/* Dequeue locking */
101#define cds_wfcq_dequeue_lock _cds_wfcq_dequeue_lock
102#define cds_wfcq_dequeue_unlock _cds_wfcq_dequeue_unlock
103
104/* Locking performed within cds_wfcq calls. */
105#define cds_wfcq_dequeue_blocking _cds_wfcq_dequeue_blocking
eec791af
MD
106#define cds_wfcq_dequeue_with_state_blocking \
107 _cds_wfcq_dequeue_with_state_blocking
8ad4ce58
MD
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
111
112/* Locking ensured by caller by holding cds_wfcq_dequeue_lock() */
113#define __cds_wfcq_dequeue_blocking ___cds_wfcq_dequeue_blocking
eec791af
MD
114#define __cds_wfcq_dequeue_with_state_blocking \
115 ___cds_wfcq_dequeue_with_state_blocking
8ad4ce58
MD
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
119
47215721
MD
120/*
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.
124 */
125#define __cds_wfcq_dequeue_nonblocking ___cds_wfcq_dequeue_nonblocking
eec791af
MD
126#define __cds_wfcq_dequeue_with_state_nonblocking \
127 ___cds_wfcq_dequeue_with_state_nonblocking
47215721
MD
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
131
8ad4ce58
MD
132#else /* !_LGPL_SOURCE */
133
134/*
135 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
136 *
f878b49e
MD
137 * Synchronization table:
138 *
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 "-".
142 *
143 * Legend:
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
150 *
151 * [1] [2] [3] [4] [5] [6]
152 * [1] - - - - - -
153 * [2] - - - - - -
154 * [3] - - X X X X
155 * [4] - - X - X X
156 * [5] - - X X - -
157 * [6] - - X X - -
158 *
8ad4ce58
MD
159 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
160 *
161 * For convenience, cds_wfcq_dequeue_blocking() and
162 * cds_wfcq_splice_blocking() hold the dequeue lock.
1fe734e1
MD
163 *
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.
8ad4ce58
MD
167 */
168
169/*
170 * cds_wfcq_node_init: initialize wait-free queue node.
171 */
172extern void cds_wfcq_node_init(struct cds_wfcq_node *node);
173
174/*
175 * cds_wfcq_init: initialize wait-free queue.
176 */
177extern void cds_wfcq_init(struct cds_wfcq_head *head,
178 struct cds_wfcq_tail *tail);
179
f637f191
MD
180/*
181 * __cds_wfcq_init: initialize wait-free queue.
182 */
183extern void __cds_wfcq_init(struct __cds_wfcq_head *head,
184 struct cds_wfcq_tail *tail);
185
8ad4ce58
MD
186/*
187 * cds_wfcq_empty: return whether wait-free queue is empty.
188 *
189 * No memory barrier is issued. No mutual exclusion is required.
190 */
f637f191 191extern bool cds_wfcq_empty(cds_wfcq_head_ptr_t head,
8ad4ce58
MD
192 struct cds_wfcq_tail *tail);
193
194/*
195 * cds_wfcq_dequeue_lock: take the dequeue mutual exclusion lock.
196 */
197extern void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head,
198 struct cds_wfcq_tail *tail);
199
200/*
201 * cds_wfcq_dequeue_unlock: release the dequeue mutual exclusion lock.
202 */
203extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head,
204 struct cds_wfcq_tail *tail);
205
206/*
207 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
208 *
209 * Issues a full memory barrier before enqueue. No mutual exclusion is
210 * required.
23773356
MD
211 *
212 * Returns false if the queue was empty prior to adding the node.
213 * Returns true otherwise.
8ad4ce58 214 */
f637f191 215extern bool cds_wfcq_enqueue(cds_wfcq_head_ptr_t head,
8ad4ce58
MD
216 struct cds_wfcq_tail *tail,
217 struct cds_wfcq_node *node);
218
219/*
220 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
221 *
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.
ffa11a18 225 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
1fe734e1 226 * ensured.
8ad4ce58
MD
227 */
228extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking(
229 struct cds_wfcq_head *head,
230 struct cds_wfcq_tail *tail);
231
eec791af
MD
232/*
233 * cds_wfcq_dequeue_with_state_blocking: dequeue with state.
234 *
235 * Same as cds_wfcq_dequeue_blocking, but saves whether dequeueing the
236 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
237 */
238extern struct cds_wfcq_node *cds_wfcq_dequeue_with_state_blocking(
239 struct cds_wfcq_head *head,
240 struct cds_wfcq_tail *tail,
241 int *state);
242
8ad4ce58
MD
243/*
244 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
245 *
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.
ffa11a18 250 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
1fe734e1 251 * ensured.
23773356
MD
252 *
253 * Returns enum cds_wfcq_ret which indicates the state of the src or
b94aaf6f 254 * dest queue.
8ad4ce58 255 */
23773356 256extern enum cds_wfcq_ret cds_wfcq_splice_blocking(
8ad4ce58
MD
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);
261
262/*
47215721 263 * __cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
8ad4ce58
MD
264 *
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.
1fe734e1
MD
268 * Dequeue/splice/iteration mutual exclusion should be ensured by the
269 * caller.
8ad4ce58
MD
270 */
271extern struct cds_wfcq_node *__cds_wfcq_dequeue_blocking(
f637f191 272 cds_wfcq_head_ptr_t head,
8ad4ce58
MD
273 struct cds_wfcq_tail *tail);
274
eec791af
MD
275/*
276 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
277 *
278 * Same as __cds_wfcq_dequeue_blocking, but saves whether dequeueing the
279 * last node of the queue into state (CDS_WFCQ_STATE_LAST).
280 */
281extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_blocking(
f637f191 282 cds_wfcq_head_ptr_t head,
eec791af
MD
283 struct cds_wfcq_tail *tail,
284 int *state);
285
47215721
MD
286/*
287 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
288 *
289 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
290 * if it needs to block.
291 */
292extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
f637f191 293 cds_wfcq_head_ptr_t head,
47215721
MD
294 struct cds_wfcq_tail *tail);
295
eec791af
MD
296/*
297 * __cds_wfcq_dequeue_with_state_blocking: dequeue with state.
298 *
299 * Same as __cds_wfcq_dequeue_nonblocking, but saves whether dequeueing
300 * the last node of the queue into state (CDS_WFCQ_STATE_LAST).
301 */
302extern struct cds_wfcq_node *__cds_wfcq_dequeue_with_state_nonblocking(
f637f191 303 cds_wfcq_head_ptr_t head,
eec791af
MD
304 struct cds_wfcq_tail *tail,
305 int *state);
306
8ad4ce58
MD
307/*
308 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
309 *
310 * Dequeue all nodes from src_q.
311 * dest_q must be already initialized.
f878b49e
MD
312 * Mutual exclusion for src_q should be ensured by the caller as
313 * specified in the "Synchronisation table".
23773356 314 * Returns enum cds_wfcq_ret which indicates the state of the src or
f878b49e 315 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
8ad4ce58 316 */
23773356 317extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
f637f191 318 cds_wfcq_head_ptr_t dest_q_head,
8ad4ce58 319 struct cds_wfcq_tail *dest_q_tail,
f637f191 320 cds_wfcq_head_ptr_t src_q_head,
8ad4ce58
MD
321 struct cds_wfcq_tail *src_q_tail);
322
47215721
MD
323/*
324 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
325 *
23773356
MD
326 * Same as __cds_wfcq_splice_blocking, but returns
327 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
47215721 328 */
23773356 329extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking(
f637f191 330 cds_wfcq_head_ptr_t dest_q_head,
47215721 331 struct cds_wfcq_tail *dest_q_tail,
f637f191 332 cds_wfcq_head_ptr_t src_q_head,
47215721
MD
333 struct cds_wfcq_tail *src_q_tail);
334
8ad4ce58
MD
335/*
336 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
337 *
338 * Content written into the node before enqueue is guaranteed to be
339 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
340 * Dequeue/splice/iteration mutual exclusion should be ensured by the
341 * caller.
f94061a3
MD
342 *
343 * Used by for-like iteration macros:
344 * __cds_wfcq_for_each_blocking()
345 * __cds_wfcq_for_each_blocking_safe()
131a29a6
MD
346 *
347 * Returns NULL if queue is empty, first node otherwise.
8ad4ce58
MD
348 */
349extern struct cds_wfcq_node *__cds_wfcq_first_blocking(
f637f191 350 cds_wfcq_head_ptr_t head,
8ad4ce58
MD
351 struct cds_wfcq_tail *tail);
352
47215721
MD
353/*
354 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
355 *
356 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
357 * it needs to block.
358 */
359extern struct cds_wfcq_node *__cds_wfcq_first_nonblocking(
f637f191 360 cds_wfcq_head_ptr_t head,
47215721
MD
361 struct cds_wfcq_tail *tail);
362
8ad4ce58
MD
363/*
364 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
365 *
366 * Content written into the node before enqueue is guaranteed to be
367 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
368 * Dequeue/splice/iteration mutual exclusion should be ensured by the
369 * caller.
f94061a3
MD
370 *
371 * Used by for-like iteration macros:
372 * __cds_wfcq_for_each_blocking()
373 * __cds_wfcq_for_each_blocking_safe()
131a29a6
MD
374 *
375 * Returns NULL if reached end of queue, non-NULL next queue node
376 * otherwise.
8ad4ce58
MD
377 */
378extern struct cds_wfcq_node *__cds_wfcq_next_blocking(
f637f191 379 cds_wfcq_head_ptr_t head,
8ad4ce58
MD
380 struct cds_wfcq_tail *tail,
381 struct cds_wfcq_node *node);
382
47215721
MD
383/*
384 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
385 *
386 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
387 * it needs to block.
388 */
389extern struct cds_wfcq_node *__cds_wfcq_next_nonblocking(
f637f191 390 cds_wfcq_head_ptr_t head,
47215721
MD
391 struct cds_wfcq_tail *tail,
392 struct cds_wfcq_node *node);
393
8ad4ce58
MD
394#endif /* !_LGPL_SOURCE */
395
396/*
397 * __cds_wfcq_for_each_blocking: Iterate over all nodes in a queue,
398 * without dequeuing them.
f637f191 399 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
8ad4ce58
MD
400 * @tail: tail of the queue (struct cds_wfcq_tail pointer).
401 * @node: iterator on the queue (struct cds_wfcq_node pointer).
402 *
403 * Content written into each node before enqueue is guaranteed to be
404 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
405 * Dequeue/splice/iteration mutual exclusion should be ensured by the
406 * caller.
8ad4ce58
MD
407 */
408#define __cds_wfcq_for_each_blocking(head, tail, node) \
409 for (node = __cds_wfcq_first_blocking(head, tail); \
410 node != NULL; \
411 node = __cds_wfcq_next_blocking(head, tail, node))
412
413/*
414 * __cds_wfcq_for_each_blocking_safe: Iterate over all nodes in a queue,
415 * without dequeuing them. Safe against deletion.
f637f191 416 * @head: head of the queue (struct cds_wfcq_head or __cds_wfcq_head pointer).
8ad4ce58
MD
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
420 * internally).
421 *
422 * Content written into each node before enqueue is guaranteed to be
423 * consistent, but no other memory ordering is ensured.
1fe734e1
MD
424 * Dequeue/splice/iteration mutual exclusion should be ensured by the
425 * caller.
8ad4ce58
MD
426 */
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); \
430 node != NULL; \
431 node = n, n = (node ? __cds_wfcq_next_blocking(head, tail, node) : NULL))
432
433#ifdef __cplusplus
434}
435#endif
436
437#endif /* _URCU_WFCQUEUE_H */
This page took 0.042312 seconds and 4 git commands to generate.