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_empty: return whether wait-free queue is empty.
112 * No memory barrier is issued. No mutual exclusion is required.
114 * We perform the test on head->node.next to check if the queue is
115 * possibly empty, but we confirm this by checking if the tail pointer
116 * points to the head node because the tail pointer is the linearisation
117 * point of the enqueuers. Just checking the head next pointer could
118 * make a queue appear empty if an enqueuer is preempted for a long time
119 * between xchg() and setting the previous node's next pointer.
121 static inline bool _cds_wfcq_empty(struct cds_wfcq_head
*head
,
122 struct cds_wfcq_tail
*tail
)
125 * Queue is empty if no node is pointed by head->node.next nor
126 * tail->p. Even though the tail->p check is sufficient to find
127 * out of the queue is empty, we first check head->node.next as a
128 * common case to ensure that dequeuers do not frequently access
129 * enqueuer's tail->p cache line.
131 return CMM_LOAD_SHARED(head
->node
.next
) == NULL
132 && CMM_LOAD_SHARED(tail
->p
) == &head
->node
;
135 static inline void _cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
136 struct cds_wfcq_tail
*tail
)
140 ret
= pthread_mutex_lock(&head
->lock
);
144 static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
145 struct cds_wfcq_tail
*tail
)
149 ret
= pthread_mutex_unlock(&head
->lock
);
153 static inline bool ___cds_wfcq_append(struct cds_wfcq_head
*head
,
154 struct cds_wfcq_tail
*tail
,
155 struct cds_wfcq_node
*new_head
,
156 struct cds_wfcq_node
*new_tail
)
158 struct cds_wfcq_node
*old_tail
;
161 * Implicit memory barrier before uatomic_xchg() orders earlier
162 * stores to data structure containing node and setting
163 * node->next to NULL before publication.
165 old_tail
= uatomic_xchg(&tail
->p
, new_tail
);
168 * Implicit memory barrier after uatomic_xchg() orders store to
169 * q->tail before store to old_tail->next.
171 * At this point, dequeuers see a NULL tail->p->next, which
172 * indicates that the queue is being appended to. The following
173 * store will append "node" to the queue from a dequeuer
176 CMM_STORE_SHARED(old_tail
->next
, new_head
);
178 * Return false if queue was empty prior to adding the node,
181 return old_tail
!= &head
->node
;
185 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
187 * Issues a full memory barrier before enqueue. No mutual exclusion is
190 * Returns false if the queue was empty prior to adding the node.
191 * Returns true otherwise.
193 static inline bool _cds_wfcq_enqueue(struct cds_wfcq_head
*head
,
194 struct cds_wfcq_tail
*tail
,
195 struct cds_wfcq_node
*new_tail
)
197 return ___cds_wfcq_append(head
, tail
, new_tail
, new_tail
);
201 * ___cds_wfcq_busy_wait: adaptative busy-wait.
203 * Returns 1 if nonblocking and needs to block, 0 otherwise.
206 ___cds_wfcq_busy_wait(int *attempt
, int blocking
)
210 if (++(*attempt
) >= WFCQ_ADAPT_ATTEMPTS
) {
211 poll(NULL
, 0, WFCQ_WAIT
); /* Wait for 10ms */
220 * Waiting for enqueuer to complete enqueue and return the next node.
222 static inline struct cds_wfcq_node
*
223 ___cds_wfcq_node_sync_next(struct cds_wfcq_node
*node
, int blocking
)
225 struct cds_wfcq_node
*next
;
229 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
231 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
232 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
233 return CDS_WFCQ_WOULDBLOCK
;
239 static inline struct cds_wfcq_node
*
240 ___cds_wfcq_first(struct cds_wfcq_head
*head
,
241 struct cds_wfcq_tail
*tail
,
244 struct cds_wfcq_node
*node
;
246 if (_cds_wfcq_empty(head
, tail
))
248 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
249 /* Load head->node.next before loading node's content */
250 cmm_smp_read_barrier_depends();
255 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
257 * Content written into the node before enqueue is guaranteed to be
258 * consistent, but no other memory ordering is ensured.
259 * Dequeue/splice/iteration mutual exclusion should be ensured by the
262 * Used by for-like iteration macros in urcu/wfqueue.h:
263 * __cds_wfcq_for_each_blocking()
264 * __cds_wfcq_for_each_blocking_safe()
266 * Returns NULL if queue is empty, first node otherwise.
268 static inline struct cds_wfcq_node
*
269 ___cds_wfcq_first_blocking(struct cds_wfcq_head
*head
,
270 struct cds_wfcq_tail
*tail
)
272 return ___cds_wfcq_first(head
, tail
, 1);
277 * __cds_wfcq_first_nonblocking: get first node of a queue, without dequeuing.
279 * Same as __cds_wfcq_first_blocking, but returns CDS_WFCQ_WOULDBLOCK if
282 static inline struct cds_wfcq_node
*
283 ___cds_wfcq_first_nonblocking(struct cds_wfcq_head
*head
,
284 struct cds_wfcq_tail
*tail
)
286 return ___cds_wfcq_first(head
, tail
, 0);
289 static inline struct cds_wfcq_node
*
290 ___cds_wfcq_next(struct cds_wfcq_head
*head
,
291 struct cds_wfcq_tail
*tail
,
292 struct cds_wfcq_node
*node
,
295 struct cds_wfcq_node
*next
;
298 * Even though the following tail->p check is sufficient to find
299 * out if we reached the end of the queue, we first check
300 * node->next as a common case to ensure that iteration on nodes
301 * do not frequently access enqueuer's tail->p cache line.
303 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
304 /* Load node->next before tail->p */
306 if (CMM_LOAD_SHARED(tail
->p
) == node
)
308 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
310 /* Load node->next before loading next's content */
311 cmm_smp_read_barrier_depends();
316 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
318 * Content written into the node before enqueue is guaranteed to be
319 * consistent, but no other memory ordering is ensured.
320 * Dequeue/splice/iteration mutual exclusion should be ensured by the
323 * Used by for-like iteration macros in urcu/wfqueue.h:
324 * __cds_wfcq_for_each_blocking()
325 * __cds_wfcq_for_each_blocking_safe()
327 * Returns NULL if reached end of queue, non-NULL next queue node
330 static inline struct cds_wfcq_node
*
331 ___cds_wfcq_next_blocking(struct cds_wfcq_head
*head
,
332 struct cds_wfcq_tail
*tail
,
333 struct cds_wfcq_node
*node
)
335 return ___cds_wfcq_next(head
, tail
, node
, 1);
339 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
341 * Same as __cds_wfcq_next_blocking, but returns CDS_WFCQ_WOULDBLOCK if
344 static inline struct cds_wfcq_node
*
345 ___cds_wfcq_next_nonblocking(struct cds_wfcq_head
*head
,
346 struct cds_wfcq_tail
*tail
,
347 struct cds_wfcq_node
*node
)
349 return ___cds_wfcq_next(head
, tail
, node
, 0);
352 static inline struct cds_wfcq_node
*
353 ___cds_wfcq_dequeue(struct cds_wfcq_head
*head
,
354 struct cds_wfcq_tail
*tail
,
357 struct cds_wfcq_node
*node
, *next
;
359 if (_cds_wfcq_empty(head
, tail
))
362 node
= ___cds_wfcq_node_sync_next(&head
->node
, blocking
);
363 if (!blocking
&& node
== CDS_WFCQ_WOULDBLOCK
)
364 return CDS_WFCQ_WOULDBLOCK
;
366 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
368 * @node is probably the only node in the queue.
369 * Try to move the tail to &q->head.
370 * q->head.next is set to NULL here, and stays
371 * NULL if the cmpxchg succeeds. Should the
372 * cmpxchg fail due to a concurrent enqueue, the
373 * q->head.next will be set to the next node.
374 * The implicit memory barrier before
375 * uatomic_cmpxchg() orders load node->next
376 * before loading q->tail.
377 * The implicit memory barrier before uatomic_cmpxchg
378 * orders load q->head.next before loading node's
381 _cds_wfcq_node_init(&head
->node
);
382 if (uatomic_cmpxchg(&tail
->p
, node
, &head
->node
) == node
)
384 next
= ___cds_wfcq_node_sync_next(node
, blocking
);
386 * In nonblocking mode, if we would need to block to
387 * get node's next, set the head next node pointer
388 * (currently NULL) back to its original value.
390 if (!blocking
&& next
== CDS_WFCQ_WOULDBLOCK
) {
391 head
->node
.next
= node
;
392 return CDS_WFCQ_WOULDBLOCK
;
397 * Move queue head forward.
399 head
->node
.next
= next
;
401 /* Load q->head.next before loading node's content */
402 cmm_smp_read_barrier_depends();
407 * __cds_wfcq_dequeue_blocking: dequeue a node from the queue.
409 * Content written into the node before enqueue is guaranteed to be
410 * consistent, but no other memory ordering is ensured.
411 * It is valid to reuse and free a dequeued node immediately.
412 * Dequeue/splice/iteration mutual exclusion should be ensured by the
415 static inline struct cds_wfcq_node
*
416 ___cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
417 struct cds_wfcq_tail
*tail
)
419 return ___cds_wfcq_dequeue(head
, tail
, 1);
423 * __cds_wfcq_dequeue_nonblocking: dequeue a node from a wait-free queue.
425 * Same as __cds_wfcq_dequeue_blocking, but returns CDS_WFCQ_WOULDBLOCK
426 * if it needs to block.
428 static inline struct cds_wfcq_node
*
429 ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head
*head
,
430 struct cds_wfcq_tail
*tail
)
432 return ___cds_wfcq_dequeue(head
, tail
, 0);
436 * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
438 * Dequeue all nodes from src_q.
439 * dest_q must be already initialized.
440 * Mutual exclusion for src_q should be ensured by the caller as
441 * specified in the "Synchronisation table".
442 * Returns enum cds_wfcq_ret which indicates the state of the src or
445 static inline enum cds_wfcq_ret
447 struct cds_wfcq_head
*dest_q_head
,
448 struct cds_wfcq_tail
*dest_q_tail
,
449 struct cds_wfcq_head
*src_q_head
,
450 struct cds_wfcq_tail
*src_q_tail
,
453 struct cds_wfcq_node
*head
, *tail
;
457 * Initial emptiness check to speed up cases where queue is
458 * empty: only require loads to check if queue is empty.
460 if (_cds_wfcq_empty(src_q_head
, src_q_tail
))
461 return CDS_WFCQ_RET_SRC_EMPTY
;
465 * Open-coded _cds_wfcq_empty() by testing result of
466 * uatomic_xchg, as well as tail pointer vs head node
469 head
= uatomic_xchg(&src_q_head
->node
.next
, NULL
);
471 break; /* non-empty */
472 if (CMM_LOAD_SHARED(src_q_tail
->p
) == &src_q_head
->node
)
473 return CDS_WFCQ_RET_SRC_EMPTY
;
474 if (___cds_wfcq_busy_wait(&attempt
, blocking
))
475 return CDS_WFCQ_RET_WOULDBLOCK
;
479 * Memory barrier implied before uatomic_xchg() orders store to
480 * src_q->head before store to src_q->tail. This is required by
481 * concurrent enqueue on src_q, which exchanges the tail before
482 * updating the previous tail's next pointer.
484 tail
= uatomic_xchg(&src_q_tail
->p
, &src_q_head
->node
);
487 * Append the spliced content of src_q into dest_q. Does not
488 * require mutual exclusion on dest_q (wait-free).
490 if (___cds_wfcq_append(dest_q_head
, dest_q_tail
, head
, tail
))
491 return CDS_WFCQ_RET_DEST_NON_EMPTY
;
493 return CDS_WFCQ_RET_DEST_EMPTY
;
497 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
499 * Dequeue all nodes from src_q.
500 * dest_q must be already initialized.
501 * Mutual exclusion for src_q should be ensured by the caller as
502 * specified in the "Synchronisation table".
503 * Returns enum cds_wfcq_ret which indicates the state of the src or
504 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
506 static inline enum cds_wfcq_ret
507 ___cds_wfcq_splice_blocking(
508 struct cds_wfcq_head
*dest_q_head
,
509 struct cds_wfcq_tail
*dest_q_tail
,
510 struct cds_wfcq_head
*src_q_head
,
511 struct cds_wfcq_tail
*src_q_tail
)
513 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
514 src_q_head
, src_q_tail
, 1);
518 * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
520 * Same as __cds_wfcq_splice_blocking, but returns
521 * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
523 static inline enum cds_wfcq_ret
524 ___cds_wfcq_splice_nonblocking(
525 struct cds_wfcq_head
*dest_q_head
,
526 struct cds_wfcq_tail
*dest_q_tail
,
527 struct cds_wfcq_head
*src_q_head
,
528 struct cds_wfcq_tail
*src_q_tail
)
530 return ___cds_wfcq_splice(dest_q_head
, dest_q_tail
,
531 src_q_head
, src_q_tail
, 0);
535 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
537 * Content written into the node before enqueue is guaranteed to be
538 * consistent, but no other memory ordering is ensured.
539 * Mutual exclusion with cds_wfcq_splice_blocking and dequeue lock is
541 * It is valid to reuse and free a dequeued node immediately.
543 static inline struct cds_wfcq_node
*
544 _cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
545 struct cds_wfcq_tail
*tail
)
547 struct cds_wfcq_node
*retval
;
549 _cds_wfcq_dequeue_lock(head
, tail
);
550 retval
= ___cds_wfcq_dequeue_blocking(head
, tail
);
551 _cds_wfcq_dequeue_unlock(head
, tail
);
556 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
558 * Dequeue all nodes from src_q.
559 * dest_q must be already initialized.
560 * Content written into the node before enqueue is guaranteed to be
561 * consistent, but no other memory ordering is ensured.
562 * Mutual exclusion with cds_wfcq_dequeue_blocking and dequeue lock is
564 * Returns enum cds_wfcq_ret which indicates the state of the src or
565 * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
567 static inline enum cds_wfcq_ret
568 _cds_wfcq_splice_blocking(
569 struct cds_wfcq_head
*dest_q_head
,
570 struct cds_wfcq_tail
*dest_q_tail
,
571 struct cds_wfcq_head
*src_q_head
,
572 struct cds_wfcq_tail
*src_q_tail
)
574 enum cds_wfcq_ret ret
;
576 _cds_wfcq_dequeue_lock(src_q_head
, src_q_tail
);
577 ret
= ___cds_wfcq_splice_blocking(dest_q_head
, dest_q_tail
,
578 src_q_head
, src_q_tail
);
579 _cds_wfcq_dequeue_unlock(src_q_head
, src_q_tail
);
587 #endif /* _URCU_WFCQUEUE_STATIC_H */