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.
97 static inline void _cds_wfcq_init(struct cds_wfcq_head
*head
,
98 struct cds_wfcq_tail
*tail
)
102 /* Set queue head and tail */
103 _cds_wfcq_node_init(&head
->node
);
104 tail
->p
= &head
->node
;
105 ret
= pthread_mutex_init(&head
->lock
, NULL
);
110 * __cds_wfcq_init: initialize wait-free queue.
112 static inline void ___cds_wfcq_init(struct __cds_wfcq_head
*head
,
113 struct cds_wfcq_tail
*tail
)
115 /* Set queue head and tail */
116 _cds_wfcq_node_init(&head
->node
);
117 tail
->p
= &head
->node
;
121 * cds_wfcq_empty: return whether wait-free queue is empty.
123 * No memory barrier is issued. No mutual exclusion is required.
125 * We perform the test on head->node.next to check if the queue is
126 * possibly empty, but we confirm this by checking if the tail pointer
127 * points to the head node because the tail pointer is the linearisation
128 * point of the enqueuers. Just checking the head next pointer could
129 * make a queue appear empty if an enqueuer is preempted for a long time
130 * between xchg() and setting the previous node's next pointer.
132 static inline bool _cds_wfcq_empty(cds_wfcq_head_ptr_t u_head
,
133 struct cds_wfcq_tail
*tail
)
135 struct __cds_wfcq_head
*head
= u_head
._h
;
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(cds_wfcq_head_ptr_t u_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_head
*head
= u_head
._h
;
171 struct cds_wfcq_node
*old_tail
;
174 * Implicit memory barrier before uatomic_xchg() orders earlier
175 * stores to data structure containing node and setting
176 * node->next to NULL before publication.
178 old_tail
= uatomic_xchg(&tail
->p
, new_tail
);
181 * Implicit memory barrier after uatomic_xchg() orders store to
182 * q->tail before store to old_tail->next.
184 * At this point, dequeuers see a NULL tail->p->next, which
185 * indicates that the queue is being appended to. The following
186 * store will append "node" to the queue from a dequeuer
189 CMM_STORE_SHARED(old_tail
->next
, new_head
);
191 * Return false if queue was empty prior to adding the node,
194 return old_tail
!= &head
->node
;
198 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
200 * Issues a full memory barrier before enqueue. No mutual exclusion is
203 * Returns false if the queue was empty prior to adding the node.
204 * Returns true otherwise.
206 static inline bool _cds_wfcq_enqueue(cds_wfcq_head_ptr_t head
,
207 struct cds_wfcq_tail
*tail
,
208 struct cds_wfcq_node
*new_tail
)
210 return ___cds_wfcq_append(head
, tail
, new_tail
, new_tail
);
214 * ___cds_wfcq_busy_wait: adaptative busy-wait.
216 * Returns 1 if nonblocking and needs to block, 0 otherwise.
219 ___cds_wfcq_busy_wait(int *attempt
, int blocking
)
223 if (++(*attempt
) >= WFCQ_ADAPT_ATTEMPTS
) {
224 (void) poll(NULL
, 0, WFCQ_WAIT
); /* Wait for 10ms */
233 * Waiting for enqueuer to complete enqueue and return the next node.
235 static inline struct cds_wfcq_node
*
236 ___cds_wfcq_node_sync_next(struct cds_wfcq_node
*node
, int blocking
)
238 struct cds_wfcq_node
*next
;
242 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
244 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
245 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
246 return CDS_WFCQ_WOULDBLOCK
;
252 static inline struct cds_wfcq_node
*
253 ___cds_wfcq_first(cds_wfcq_head_ptr_t u_head
,
254 struct cds_wfcq_tail
*tail
,
257 struct __cds_wfcq_head
*head
= u_head
._h
;
258 struct cds_wfcq_node
*node
;
260 if (_cds_wfcq_empty(__cds_wfcq_head_cast(head
), tail
))
262 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
263 /* Load head->node.next before loading node's content */
264 cmm_smp_read_barrier_depends();
269 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
271 * Content written into the node before enqueue is guaranteed to be
272 * consistent, but no other memory ordering is ensured.
273 * Dequeue/splice/iteration mutual exclusion should be ensured by the
276 * Used by for-like iteration macros in urcu/wfqueue.h:
277 * __cds_wfcq_for_each_blocking()
278 * __cds_wfcq_for_each_blocking_safe()
280 * Returns NULL if queue is empty, first node otherwise.
282 static inline struct cds_wfcq_node
*
283 ___cds_wfcq_first_blocking(cds_wfcq_head_ptr_t head
,
284 struct cds_wfcq_tail
*tail
)
286 return ___cds_wfcq_first(head
, tail
, 1);
291 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
293 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
296 static inline struct cds_wfcq_node
*
297 ___cds_wfcq_first_nonblocking(cds_wfcq_head_ptr_t head
,
298 struct cds_wfcq_tail
*tail
)
300 return ___cds_wfcq_first(head
, tail
, 0);
303 static inline struct cds_wfcq_node
*
304 ___cds_wfcq_next(cds_wfcq_head_ptr_t head
,
305 struct cds_wfcq_tail
*tail
,
306 struct cds_wfcq_node
*node
,
309 struct cds_wfcq_node
*next
;
312 * Even though the following tail->p check is sufficient to find
313 * out if we reached the end of the queue, we first check
314 * node->next as a common case to ensure that iteration on nodes
315 * do not frequently access enqueuer's tail->p cache line.
317 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
318 /* Load node->next before tail->p */
320 if (CMM_LOAD_SHARED(tail
->p
) == node
)
322 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
324 /* Load node->next before loading next's content */
325 cmm_smp_read_barrier_depends();
330 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
332 * Content written into the node before enqueue is guaranteed to be
333 * consistent, but no other memory ordering is ensured.
334 * Dequeue/splice/iteration mutual exclusion should be ensured by the
337 * Used by for-like iteration macros in urcu/wfqueue.h:
338 * __cds_wfcq_for_each_blocking()
339 * __cds_wfcq_for_each_blocking_safe()
341 * Returns NULL if reached end of queue, non-NULL next queue node
344 static inline struct cds_wfcq_node
*
345 ___cds_wfcq_next_blocking(cds_wfcq_head_ptr_t head
,
346 struct cds_wfcq_tail
*tail
,
347 struct cds_wfcq_node
*node
)
349 return ___cds_wfcq_next(head
, tail
, node
, 1);
353 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
355 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
358 static inline struct cds_wfcq_node
*
359 ___cds_wfcq_next_nonblocking(cds_wfcq_head_ptr_t head
,
360 struct cds_wfcq_tail
*tail
,
361 struct cds_wfcq_node
*node
)
363 return ___cds_wfcq_next(head
, tail
, node
, 0);
366 static inline struct cds_wfcq_node
*
367 ___cds_wfcq_dequeue_with_state(cds_wfcq_head_ptr_t u_head
,
368 struct cds_wfcq_tail
*tail
,
372 struct __cds_wfcq_head
*head
= u_head
._h
;
373 struct cds_wfcq_node
*node
, *next
;
378 if (_cds_wfcq_empty(__cds_wfcq_head_cast(head
), tail
)) {
382 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
383 if (!blocking
&& node
== CDS_WFCQ_WOULDBLOCK
) {
384 return CDS_WFCQ_WOULDBLOCK
;
387 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
389 * @node is probably the only node in the queue.
390 * Try to move the tail to &q->head.
391 * q->head.next is set to NULL here, and stays
392 * NULL if the cmpxchg succeeds. Should the
393 * cmpxchg fail due to a concurrent enqueue, the
394 * q->head.next will be set to the next node.
395 * The implicit memory barrier before
396 * uatomic_cmpxchg() orders load node->next
397 * before loading q->tail.
398 * The implicit memory barrier before uatomic_cmpxchg
399 * orders load q->head.next before loading node's
402 _cds_wfcq_node_init(&head
->node
);
403 if (uatomic_cmpxchg(&tail
->p
, node
, &head
->node
) == node
) {
405 *state
|= CDS_WFCQ_STATE_LAST
;
408 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
410 * In nonblocking mode, if we would need to block to
411 * get node's next, set the head next node pointer
412 * (currently NULL) back to its original value.
414 if (!blocking
&& next
== CDS_WFCQ_WOULDBLOCK
) {
415 head
->node
.next
= node
;
416 return CDS_WFCQ_WOULDBLOCK
;
421 * Move queue head forward.
423 head
->node
.next
= next
;
425 /* Load q->head.next before loading node's content */
426 cmm_smp_read_barrier_depends();
431 * __cds_wfcq_dequeue_with_state_blocking: dequeue node from queue, with state.
433 * Content written into the node before enqueue is guaranteed to be
434 * consistent, but no other memory ordering is ensured.
435 * It is valid to reuse and free a dequeued node immediately.
436 * Dequeue/splice/iteration mutual exclusion should be ensured by the
439 static inline struct cds_wfcq_node
*
440 ___cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_ptr_t head
,
441 struct cds_wfcq_tail
*tail
, int *state
)
443 return ___cds_wfcq_dequeue_with_state(head
, tail
, state
, 1);
447 * ___cds_wfcq_dequeue_blocking: dequeue node from queue.
449 * Same as __cds_wfcq_dequeue_with_state_blocking, but without saving
452 static inline struct cds_wfcq_node
*
453 ___cds_wfcq_dequeue_blocking(cds_wfcq_head_ptr_t head
,
454 struct cds_wfcq_tail
*tail
)
456 return ___cds_wfcq_dequeue_with_state_blocking(head
, tail
, NULL
);
460 * __cds_wfcq_dequeue_with_state_nonblocking: dequeue node, with state.
462 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
463 * if it needs to block.
465 static inline struct cds_wfcq_node
*
466 ___cds_wfcq_dequeue_with_state_nonblocking(cds_wfcq_head_ptr_t head
,
467 struct cds_wfcq_tail
*tail
, int *state
)
469 return ___cds_wfcq_dequeue_with_state(head
, tail
, state
, 0);
473 * ___cds_wfcq_dequeue_nonblocking: dequeue node from queue.
475 * Same as __cds_wfcq_dequeue_with_state_nonblocking, but without saving
478 static inline struct cds_wfcq_node
*
479 ___cds_wfcq_dequeue_nonblocking(cds_wfcq_head_ptr_t head
,
480 struct cds_wfcq_tail
*tail
)
482 return ___cds_wfcq_dequeue_with_state_nonblocking(head
, tail
, NULL
);
486 * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
488 * Dequeue all nodes from src_q.
489 * dest_q must be already initialized.
490 * Mutual exclusion for src_q should be ensured by the caller as
491 * specified in the "Synchronisation table".
492 * Returns enum cds_wfcq_ret which indicates the state of the src or
495 static inline enum cds_wfcq_ret
497 cds_wfcq_head_ptr_t u_dest_q_head
,
498 struct cds_wfcq_tail
*dest_q_tail
,
499 cds_wfcq_head_ptr_t u_src_q_head
,
500 struct cds_wfcq_tail
*src_q_tail
,
503 struct __cds_wfcq_head
*dest_q_head
= u_dest_q_head
._h
;
504 struct __cds_wfcq_head
*src_q_head
= u_src_q_head
._h
;
505 struct cds_wfcq_node
*head
, *tail
;
509 * Initial emptiness check to speed up cases where queue is
510 * empty: only require loads to check if queue is empty.
512 if (_cds_wfcq_empty(__cds_wfcq_head_cast(src_q_head
), src_q_tail
))
513 return CDS_WFCQ_RET_SRC_EMPTY
;
517 * Open-coded _cds_wfcq_empty() by testing result of
518 * uatomic_xchg, as well as tail pointer vs head node
521 head
= uatomic_xchg(&src_q_head
->node
.next
, NULL
);
523 break; /* non-empty */
524 if (CMM_LOAD_SHARED(src_q_tail
->p
) == &src_q_head
->node
)
525 return CDS_WFCQ_RET_SRC_EMPTY
;
526 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
527 return CDS_WFCQ_RET_WOULDBLOCK
;
531 * Memory barrier implied before uatomic_xchg() orders store to
532 * src_q->head before store to src_q->tail. This is required by
533 * concurrent enqueue on src_q, which exchanges the tail before
534 * updating the previous tail's next pointer.
536 tail
= uatomic_xchg(&src_q_tail
->p
, &src_q_head
->node
);
539 * Append the spliced content of src_q into dest_q. Does not
540 * require mutual exclusion on dest_q (wait-free).
542 if (___cds_wfcq_append(__cds_wfcq_head_cast(dest_q_head
), dest_q_tail
,
544 return CDS_WFCQ_RET_DEST_NON_EMPTY
;
546 return CDS_WFCQ_RET_DEST_EMPTY
;
550 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
552 * Dequeue all nodes from src_q.
553 * dest_q must be already initialized.
554 * Mutual exclusion for src_q should be ensured by the caller as
555 * specified in the "Synchronisation table".
556 * Returns enum cds_wfcq_ret which indicates the state of the src or
557 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
559 static inline enum cds_wfcq_ret
560 ___cds_wfcq_splice_blocking(
561 cds_wfcq_head_ptr_t dest_q_head
,
562 struct cds_wfcq_tail
*dest_q_tail
,
563 cds_wfcq_head_ptr_t src_q_head
,
564 struct cds_wfcq_tail
*src_q_tail
)
566 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
567 src_q_head
, src_q_tail
, 1);
571 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
573 * Same as __cds_wfcq_splice_blocking, but returns
574 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
576 static inline enum cds_wfcq_ret
577 ___cds_wfcq_splice_nonblocking(
578 cds_wfcq_head_ptr_t dest_q_head
,
579 struct cds_wfcq_tail
*dest_q_tail
,
580 cds_wfcq_head_ptr_t src_q_head
,
581 struct cds_wfcq_tail
*src_q_tail
)
583 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
584 src_q_head
, src_q_tail
, 0);
588 * cds_wfcq_dequeue_with_state_blocking: dequeue a node from a wait-free queue.
590 * Content written into the node before enqueue is guaranteed to be
591 * consistent, but no other memory ordering is ensured.
592 * Mutual exclusion with cds_wfcq_splice_blocking and dequeue lock is
594 * It is valid to reuse and free a dequeued node immediately.
596 static inline struct cds_wfcq_node
*
597 _cds_wfcq_dequeue_with_state_blocking(struct cds_wfcq_head
*head
,
598 struct cds_wfcq_tail
*tail
, int *state
)
600 struct cds_wfcq_node
*retval
;
602 _cds_wfcq_dequeue_lock(head
, tail
);
603 retval
= ___cds_wfcq_dequeue_with_state_blocking(cds_wfcq_head_cast(head
),
605 _cds_wfcq_dequeue_unlock(head
, tail
);
610 * cds_wfcq_dequeue_blocking: dequeue node from queue.
612 * Same as cds_wfcq_dequeue_blocking, but without saving state.
614 static inline struct cds_wfcq_node
*
615 _cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
616 struct cds_wfcq_tail
*tail
)
618 return _cds_wfcq_dequeue_with_state_blocking(head
, tail
, NULL
);
622 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
624 * Dequeue all nodes from src_q.
625 * dest_q must be already initialized.
626 * Content written into the node before enqueue is guaranteed to be
627 * consistent, but no other memory ordering is ensured.
628 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
630 * Returns enum cds_wfcq_ret which indicates the state of the src or
631 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
633 static inline enum cds_wfcq_ret
634 _cds_wfcq_splice_blocking(
635 struct cds_wfcq_head
*dest_q_head
,
636 struct cds_wfcq_tail
*dest_q_tail
,
637 struct cds_wfcq_head
*src_q_head
,
638 struct cds_wfcq_tail
*src_q_tail
)
640 enum cds_wfcq_ret ret
;
642 _cds_wfcq_dequeue_lock(src_q_head
, src_q_tail
);
643 ret
= ___cds_wfcq_splice_blocking(cds_wfcq_head_cast(dest_q_head
), dest_q_tail
,
644 cds_wfcq_head_cast(src_q_head
), src_q_tail
);
645 _cds_wfcq_dequeue_unlock(src_q_head
, src_q_tail
);
653 #endif /* _URCU_WFCQUEUE_STATIC_H */