1 #ifndef _URCU_WFCQUEUE_STATIC_H
2 #define _URCU_WFCQUEUE_STATIC_H
5 * urcu/static/wfcqueue.h
7 * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu/wfcqueue.h for
10 * linking dynamically with the userspace rcu library.
12 * Copyright 2010-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Copyright 2011-2012 - Lai Jiangshan <laijs@cn.fujitsu.com>
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <urcu/compiler.h>
35 #include <urcu/uatomic.h>
42 * Concurrent queue with wait-free enqueue/blocking dequeue.
44 * This queue has been designed and implemented collaboratively by
45 * Mathieu Desnoyers and Lai Jiangshan. Inspired from
46 * half-wait-free/half-blocking queue implementation done by Paul E.
49 * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
51 * Synchronization table:
53 * External synchronization techniques described in the API below is
54 * required between pairs marked with "X". No external synchronization
55 * required between pairs marked with "-".
58 * [1] cds_wfcq_enqueue
59 * [2] __cds_wfcq_splice (destination queue)
60 * [3] __cds_wfcq_dequeue
61 * [4] __cds_wfcq_splice (source queue)
62 * [5] __cds_wfcq_first
65 * [1] [2] [3] [4] [5] [6]
73 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
75 * For convenience, cds_wfcq_dequeue_blocking() and
76 * cds_wfcq_splice_blocking() hold the dequeue lock.
78 * Besides locking, mutual exclusion of dequeue, splice and iteration
79 * can be ensured by performing all of those operations from a single
80 * thread, without requiring any lock.
83 #define WFCQ_ADAPT_ATTEMPTS 10 /* Retry if being set */
84 #define WFCQ_WAIT 10 /* Wait 10 ms if being set */
87 * cds_wfcq_node_init: initialize wait-free queue node.
89 static inline void _cds_wfcq_node_init(struct cds_wfcq_node
*node
)
95 * cds_wfcq_init: initialize wait-free queue (with lock). Pair with
98 static inline void _cds_wfcq_init(struct cds_wfcq_head
*head
,
99 struct cds_wfcq_tail
*tail
)
103 /* Set queue head and tail */
104 _cds_wfcq_node_init(&head
->node
);
105 tail
->p
= &head
->node
;
106 ret
= pthread_mutex_init(&head
->lock
, NULL
);
111 * cds_wfcq_destroy: destroy wait-free queue (with lock). Pair with
114 static inline void _cds_wfcq_destroy(struct cds_wfcq_head
*head
,
115 struct cds_wfcq_tail
*tail
)
117 int ret
= pthread_mutex_destroy(&head
->lock
);
122 * cds_wfcq_empty: return whether wait-free queue is empty.
124 * No memory barrier is issued. No mutual exclusion is required.
126 * We perform the test on head->node.next to check if the queue is
127 * possibly empty, but we confirm this by checking if the tail pointer
128 * points to the head node because the tail pointer is the linearisation
129 * point of the enqueuers. Just checking the head next pointer could
130 * make a queue appear empty if an enqueuer is preempted for a long time
131 * between xchg() and setting the previous node's next pointer.
133 static inline bool _cds_wfcq_empty(struct cds_wfcq_head
*head
,
134 struct cds_wfcq_tail
*tail
)
137 * Queue is empty if no node is pointed by head->node.next nor
138 * tail->p. Even though the tail->p check is sufficient to find
139 * out of the queue is empty, we first check head->node.next as a
140 * common case to ensure that dequeuers do not frequently access
141 * enqueuer's tail->p cache line.
143 return CMM_LOAD_SHARED(head
->node
.next
) == NULL
144 && CMM_LOAD_SHARED(tail
->p
) == &head
->node
;
147 static inline void _cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
148 struct cds_wfcq_tail
*tail
)
152 ret
= pthread_mutex_lock(&head
->lock
);
156 static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
157 struct cds_wfcq_tail
*tail
)
161 ret
= pthread_mutex_unlock(&head
->lock
);
165 static inline bool ___cds_wfcq_append(struct cds_wfcq_head
*head
,
166 struct cds_wfcq_tail
*tail
,
167 struct cds_wfcq_node
*new_head
,
168 struct cds_wfcq_node
*new_tail
)
170 struct cds_wfcq_node
*old_tail
;
173 * Implicit memory barrier before uatomic_xchg() orders earlier
174 * stores to data structure containing node and setting
175 * node->next to NULL before publication.
177 old_tail
= uatomic_xchg(&tail
->p
, new_tail
);
180 * Implicit memory barrier after uatomic_xchg() orders store to
181 * q->tail before store to old_tail->next.
183 * At this point, dequeuers see a NULL tail->p->next, which
184 * indicates that the queue is being appended to. The following
185 * store will append "node" to the queue from a dequeuer
188 CMM_STORE_SHARED(old_tail
->next
, new_head
);
190 * Return false if queue was empty prior to adding the node,
193 return old_tail
!= &head
->node
;
197 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
199 * Issues a full memory barrier before enqueue. No mutual exclusion is
202 * Returns false if the queue was empty prior to adding the node.
203 * Returns true otherwise.
205 static inline bool _cds_wfcq_enqueue(struct cds_wfcq_head
*head
,
206 struct cds_wfcq_tail
*tail
,
207 struct cds_wfcq_node
*new_tail
)
209 return ___cds_wfcq_append(head
, tail
, new_tail
, new_tail
);
213 * ___cds_wfcq_busy_wait: adaptative busy-wait.
215 * Returns 1 if nonblocking and needs to block, 0 otherwise.
218 ___cds_wfcq_busy_wait(int *attempt
, int blocking
)
222 if (++(*attempt
) >= WFCQ_ADAPT_ATTEMPTS
) {
223 poll(NULL
, 0, WFCQ_WAIT
); /* Wait for 10ms */
232 * Waiting for enqueuer to complete enqueue and return the next node.
234 static inline struct cds_wfcq_node
*
235 ___cds_wfcq_node_sync_next(struct cds_wfcq_node
*node
, int blocking
)
237 struct cds_wfcq_node
*next
;
241 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
243 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
244 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
245 return CDS_WFCQ_WOULDBLOCK
;
251 static inline struct cds_wfcq_node
*
252 ___cds_wfcq_first(struct cds_wfcq_head
*head
,
253 struct cds_wfcq_tail
*tail
,
256 struct cds_wfcq_node
*node
;
258 if (_cds_wfcq_empty(head
, tail
))
260 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
261 /* Load head->node.next before loading node's content */
262 cmm_smp_read_barrier_depends();
267 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
269 * Content written into the node before enqueue is guaranteed to be
270 * consistent, but no other memory ordering is ensured.
271 * Dequeue/splice/iteration mutual exclusion should be ensured by the
274 * Used by for-like iteration macros in urcu/wfqueue.h:
275 * __cds_wfcq_for_each_blocking()
276 * __cds_wfcq_for_each_blocking_safe()
278 * Returns NULL if queue is empty, first node otherwise.
280 static inline struct cds_wfcq_node
*
281 ___cds_wfcq_first_blocking(struct cds_wfcq_head
*head
,
282 struct cds_wfcq_tail
*tail
)
284 return ___cds_wfcq_first(head
, tail
, 1);
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 static inline struct cds_wfcq_node
*
295 ___cds_wfcq_first_nonblocking(struct cds_wfcq_head
*head
,
296 struct cds_wfcq_tail
*tail
)
298 return ___cds_wfcq_first(head
, tail
, 0);
301 static inline struct cds_wfcq_node
*
302 ___cds_wfcq_next(struct cds_wfcq_head
*head
,
303 struct cds_wfcq_tail
*tail
,
304 struct cds_wfcq_node
*node
,
307 struct cds_wfcq_node
*next
;
310 * Even though the following tail->p check is sufficient to find
311 * out if we reached the end of the queue, we first check
312 * node->next as a common case to ensure that iteration on nodes
313 * do not frequently access enqueuer's tail->p cache line.
315 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
316 /* Load node->next before tail->p */
318 if (CMM_LOAD_SHARED(tail
->p
) == node
)
320 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
322 /* Load node->next before loading next's content */
323 cmm_smp_read_barrier_depends();
328 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
330 * Content written into the node before enqueue is guaranteed to be
331 * consistent, but no other memory ordering is ensured.
332 * Dequeue/splice/iteration mutual exclusion should be ensured by the
335 * Used by for-like iteration macros in urcu/wfqueue.h:
336 * __cds_wfcq_for_each_blocking()
337 * __cds_wfcq_for_each_blocking_safe()
339 * Returns NULL if reached end of queue, non-NULL next queue node
342 static inline struct cds_wfcq_node
*
343 ___cds_wfcq_next_blocking(struct cds_wfcq_head
*head
,
344 struct cds_wfcq_tail
*tail
,
345 struct cds_wfcq_node
*node
)
347 return ___cds_wfcq_next(head
, tail
, node
, 1);
351 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
353 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
356 static inline struct cds_wfcq_node
*
357 ___cds_wfcq_next_nonblocking(struct cds_wfcq_head
*head
,
358 struct cds_wfcq_tail
*tail
,
359 struct cds_wfcq_node
*node
)
361 return ___cds_wfcq_next(head
, tail
, node
, 0);
364 static inline struct cds_wfcq_node
*
365 ___cds_wfcq_dequeue_with_state(struct cds_wfcq_head
*head
,
366 struct cds_wfcq_tail
*tail
,
370 struct cds_wfcq_node
*node
, *next
;
375 if (_cds_wfcq_empty(head
, tail
)) {
379 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
380 if (!blocking
&& node
== CDS_WFCQ_WOULDBLOCK
) {
381 return CDS_WFCQ_WOULDBLOCK
;
384 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
386 * @node is probably the only node in the queue.
387 * Try to move the tail to &q->head.
388 * q->head.next is set to NULL here, and stays
389 * NULL if the cmpxchg succeeds. Should the
390 * cmpxchg fail due to a concurrent enqueue, the
391 * q->head.next will be set to the next node.
392 * The implicit memory barrier before
393 * uatomic_cmpxchg() orders load node->next
394 * before loading q->tail.
395 * The implicit memory barrier before uatomic_cmpxchg
396 * orders load q->head.next before loading node's
399 _cds_wfcq_node_init(&head
->node
);
400 if (uatomic_cmpxchg(&tail
->p
, node
, &head
->node
) == node
) {
402 *state
|= CDS_WFCQ_STATE_LAST
;
405 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
407 * In nonblocking mode, if we would need to block to
408 * get node's next, set the head next node pointer
409 * (currently NULL) back to its original value.
411 if (!blocking
&& next
== CDS_WFCQ_WOULDBLOCK
) {
412 head
->node
.next
= node
;
413 return CDS_WFCQ_WOULDBLOCK
;
418 * Move queue head forward.
420 head
->node
.next
= next
;
422 /* Load q->head.next before loading node's content */
423 cmm_smp_read_barrier_depends();
428 * __cds_wfcq_dequeue_with_state_blocking: dequeue node from queue, with state.
430 * Content written into the node before enqueue is guaranteed to be
431 * consistent, but no other memory ordering is ensured.
432 * It is valid to reuse and free a dequeued node immediately.
433 * Dequeue/splice/iteration mutual exclusion should be ensured by the
436 static inline struct cds_wfcq_node
*
437 ___cds_wfcq_dequeue_with_state_blocking(struct cds_wfcq_head
*head
,
438 struct cds_wfcq_tail
*tail
, int *state
)
440 return ___cds_wfcq_dequeue_with_state(head
, tail
, state
, 1);
444 * ___cds_wfcq_dequeue_blocking: dequeue node from queue.
446 * Same as __cds_wfcq_dequeue_with_state_blocking, but without saving
449 static inline struct cds_wfcq_node
*
450 ___cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
451 struct cds_wfcq_tail
*tail
)
453 return ___cds_wfcq_dequeue_with_state_blocking(head
, tail
, NULL
);
457 * __cds_wfcq_dequeue_with_state_nonblocking: dequeue node, with state.
459 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
460 * if it needs to block.
462 static inline struct cds_wfcq_node
*
463 ___cds_wfcq_dequeue_with_state_nonblocking(struct cds_wfcq_head
*head
,
464 struct cds_wfcq_tail
*tail
, int *state
)
466 return ___cds_wfcq_dequeue_with_state(head
, tail
, state
, 0);
470 * ___cds_wfcq_dequeue_nonblocking: dequeue node from queue.
472 * Same as __cds_wfcq_dequeue_with_state_nonblocking, but without saving
475 static inline struct cds_wfcq_node
*
476 ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head
*head
,
477 struct cds_wfcq_tail
*tail
)
479 return ___cds_wfcq_dequeue_with_state_nonblocking(head
, tail
, NULL
);
483 * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
485 * Dequeue all nodes from src_q.
486 * dest_q must be already initialized.
487 * Mutual exclusion for src_q should be ensured by the caller as
488 * specified in the "Synchronisation table".
489 * Returns enum cds_wfcq_ret which indicates the state of the src or
492 static inline enum cds_wfcq_ret
494 struct cds_wfcq_head
*dest_q_head
,
495 struct cds_wfcq_tail
*dest_q_tail
,
496 struct cds_wfcq_head
*src_q_head
,
497 struct cds_wfcq_tail
*src_q_tail
,
500 struct cds_wfcq_node
*head
, *tail
;
504 * Initial emptiness check to speed up cases where queue is
505 * empty: only require loads to check if queue is empty.
507 if (_cds_wfcq_empty(src_q_head
, src_q_tail
))
508 return CDS_WFCQ_RET_SRC_EMPTY
;
512 * Open-coded _cds_wfcq_empty() by testing result of
513 * uatomic_xchg, as well as tail pointer vs head node
516 head
= uatomic_xchg(&src_q_head
->node
.next
, NULL
);
518 break; /* non-empty */
519 if (CMM_LOAD_SHARED(src_q_tail
->p
) == &src_q_head
->node
)
520 return CDS_WFCQ_RET_SRC_EMPTY
;
521 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
522 return CDS_WFCQ_RET_WOULDBLOCK
;
526 * Memory barrier implied before uatomic_xchg() orders store to
527 * src_q->head before store to src_q->tail. This is required by
528 * concurrent enqueue on src_q, which exchanges the tail before
529 * updating the previous tail's next pointer.
531 tail
= uatomic_xchg(&src_q_tail
->p
, &src_q_head
->node
);
534 * Append the spliced content of src_q into dest_q. Does not
535 * require mutual exclusion on dest_q (wait-free).
537 if (___cds_wfcq_append(dest_q_head
, dest_q_tail
, head
, tail
))
538 return CDS_WFCQ_RET_DEST_NON_EMPTY
;
540 return CDS_WFCQ_RET_DEST_EMPTY
;
544 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
546 * Dequeue all nodes from src_q.
547 * dest_q must be already initialized.
548 * Mutual exclusion for src_q should be ensured by the caller as
549 * specified in the "Synchronisation table".
550 * Returns enum cds_wfcq_ret which indicates the state of the src or
551 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
553 static inline enum cds_wfcq_ret
554 ___cds_wfcq_splice_blocking(
555 struct cds_wfcq_head
*dest_q_head
,
556 struct cds_wfcq_tail
*dest_q_tail
,
557 struct cds_wfcq_head
*src_q_head
,
558 struct cds_wfcq_tail
*src_q_tail
)
560 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
561 src_q_head
, src_q_tail
, 1);
565 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
567 * Same as __cds_wfcq_splice_blocking, but returns
568 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
570 static inline enum cds_wfcq_ret
571 ___cds_wfcq_splice_nonblocking(
572 struct cds_wfcq_head
*dest_q_head
,
573 struct cds_wfcq_tail
*dest_q_tail
,
574 struct cds_wfcq_head
*src_q_head
,
575 struct cds_wfcq_tail
*src_q_tail
)
577 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
578 src_q_head
, src_q_tail
, 0);
582 * cds_wfcq_dequeue_with_state_blocking: dequeue a node from a wait-free queue.
584 * Content written into the node before enqueue is guaranteed to be
585 * consistent, but no other memory ordering is ensured.
586 * Mutual exclusion with cds_wfcq_splice_blocking and dequeue lock is
588 * It is valid to reuse and free a dequeued node immediately.
590 static inline struct cds_wfcq_node
*
591 _cds_wfcq_dequeue_with_state_blocking(struct cds_wfcq_head
*head
,
592 struct cds_wfcq_tail
*tail
, int *state
)
594 struct cds_wfcq_node
*retval
;
596 _cds_wfcq_dequeue_lock(head
, tail
);
597 retval
= ___cds_wfcq_dequeue_with_state_blocking(head
, tail
, state
);
598 _cds_wfcq_dequeue_unlock(head
, tail
);
603 * cds_wfcq_dequeue_blocking: dequeue node from queue.
605 * Same as cds_wfcq_dequeue_blocking, but without saving state.
607 static inline struct cds_wfcq_node
*
608 _cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
609 struct cds_wfcq_tail
*tail
)
611 return _cds_wfcq_dequeue_with_state_blocking(head
, tail
, NULL
);
615 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
617 * Dequeue all nodes from src_q.
618 * dest_q must be already initialized.
619 * Content written into the node before enqueue is guaranteed to be
620 * consistent, but no other memory ordering is ensured.
621 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
623 * Returns enum cds_wfcq_ret which indicates the state of the src or
624 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
626 static inline enum cds_wfcq_ret
627 _cds_wfcq_splice_blocking(
628 struct cds_wfcq_head
*dest_q_head
,
629 struct cds_wfcq_tail
*dest_q_tail
,
630 struct cds_wfcq_head
*src_q_head
,
631 struct cds_wfcq_tail
*src_q_tail
)
633 enum cds_wfcq_ret ret
;
635 _cds_wfcq_dequeue_lock(src_q_head
, src_q_tail
);
636 ret
= ___cds_wfcq_splice_blocking(dest_q_head
, dest_q_tail
,
637 src_q_head
, src_q_tail
);
638 _cds_wfcq_dequeue_unlock(src_q_head
, src_q_tail
);
646 #endif /* _URCU_WFCQUEUE_STATIC_H */