a989984b4030b52f79cc737604aed997f2163d4a
1 #ifndef _URCU_WFCQUEUE_STATIC_H
2 #define _URCU_WFCQUEUE_STATIC_H
7 * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
9 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See wfcqueue.h for linking
10 * 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 * Inspired from half-wait-free/half-blocking queue implementation done by
47 * Mutual exclusion of __cds_wfcq_* API
49 * Unless otherwise stated, the caller must ensure mutual exclusion of
50 * queue update operations "dequeue" and "splice" (for source queue).
51 * Queue read operations "first" and "next" need to be protected against
52 * concurrent "dequeue" and "splice" (for source queue) by the caller.
53 * "enqueue", "splice" (for destination queue), and "empty" are the only
54 * operations that can be used without any mutual exclusion.
55 * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
57 * For convenience, cds_wfcq_dequeue_blocking() and
58 * cds_wfcq_splice_blocking() hold the dequeue lock.
61 #define WFCQ_ADAPT_ATTEMPTS 10 /* Retry if being set */
62 #define WFCQ_WAIT 10 /* Wait 10 ms if being set */
65 * cds_wfcq_node_init: initialize wait-free queue node.
67 static inline void _cds_wfcq_node_init(struct cds_wfcq_node
*node
)
73 * cds_wfcq_init: initialize wait-free queue.
75 static inline void _cds_wfcq_init(struct cds_wfcq_head
*head
,
76 struct cds_wfcq_tail
*tail
)
80 /* Set queue head and tail */
81 _cds_wfcq_node_init(&head
->node
);
82 tail
->p
= &head
->node
;
83 ret
= pthread_mutex_init(&head
->lock
, NULL
);
88 * cds_wfcq_empty: return whether wait-free queue is empty.
90 * No memory barrier is issued. No mutual exclusion is required.
92 static inline bool _cds_wfcq_empty(struct cds_wfcq_head
*head
,
93 struct cds_wfcq_tail
*tail
)
96 * Queue is empty if no node is pointed by head->node.next nor
97 * tail->p. Even though the tail->p check is sufficient to find
98 * out of the queue is empty, we first check head->node.next as a
99 * common case to ensure that dequeuers do not frequently access
100 * enqueuer's tail->p cache line.
102 return CMM_LOAD_SHARED(head
->node
.next
) == NULL
103 && CMM_LOAD_SHARED(tail
->p
) == &head
->node
;
106 static inline void _cds_wfcq_dequeue_lock(struct cds_wfcq_head
*head
,
107 struct cds_wfcq_tail
*tail
)
111 ret
= pthread_mutex_lock(&head
->lock
);
115 static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head
*head
,
116 struct cds_wfcq_tail
*tail
)
120 ret
= pthread_mutex_unlock(&head
->lock
);
124 static inline void ___cds_wfcq_append(struct cds_wfcq_head
*head
,
125 struct cds_wfcq_tail
*tail
,
126 struct cds_wfcq_node
*new_head
,
127 struct cds_wfcq_node
*new_tail
)
129 struct cds_wfcq_node
*old_tail
;
132 * Implicit memory barrier before uatomic_xchg() orders earlier
133 * stores to data structure containing node and setting
134 * node->next to NULL before publication.
136 old_tail
= uatomic_xchg(&tail
->p
, new_tail
);
139 * Implicit memory barrier after uatomic_xchg() orders store to
140 * q->tail before store to old_tail->next.
142 * At this point, dequeuers see a NULL tail->p->next, which
143 * indicates that the queue is being appended to. The following
144 * store will append "node" to the queue from a dequeuer
147 CMM_STORE_SHARED(old_tail
->next
, new_head
);
151 * cds_wfcq_enqueue: enqueue a node into a wait-free queue.
153 * Issues a full memory barrier before enqueue. No mutual exclusion is
156 static inline void _cds_wfcq_enqueue(struct cds_wfcq_head
*head
,
157 struct cds_wfcq_tail
*tail
,
158 struct cds_wfcq_node
*new_tail
)
160 ___cds_wfcq_append(head
, tail
, new_tail
, new_tail
);
164 * Waiting for enqueuer to complete enqueue and return the next node.
166 static inline struct cds_wfcq_node
*
167 ___cds_wfcq_node_sync_next(struct cds_wfcq_node
*node
)
169 struct cds_wfcq_node
*next
;
173 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
175 while ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
176 if (++attempt
>= WFCQ_ADAPT_ATTEMPTS
) {
177 poll(NULL
, 0, WFCQ_WAIT
); /* Wait for 10ms */
188 * __cds_wfcq_first_blocking: get first node of a queue, without dequeuing.
190 * Content written into the node before enqueue is guaranteed to be
191 * consistent, but no other memory ordering is ensured.
192 * Should be called with cds_wfcq_dequeue_lock() held.
194 static inline struct cds_wfcq_node
*
195 ___cds_wfcq_first_blocking(struct cds_wfcq_head
*head
,
196 struct cds_wfcq_tail
*tail
)
198 struct cds_wfcq_node
*node
;
200 if (_cds_wfcq_empty(head
, tail
))
202 node
= ___cds_wfcq_node_sync_next(&head
->node
);
203 /* Load head->node.next before loading node's content */
204 cmm_smp_read_barrier_depends();
209 * __cds_wfcq_next_blocking: get next node of a queue, without dequeuing.
211 * Content written into the node before enqueue is guaranteed to be
212 * consistent, but no other memory ordering is ensured.
213 * Should be called with cds_wfcq_dequeue_lock() held.
215 static inline struct cds_wfcq_node
*
216 ___cds_wfcq_next_blocking(struct cds_wfcq_head
*head
,
217 struct cds_wfcq_tail
*tail
,
218 struct cds_wfcq_node
*node
)
220 struct cds_wfcq_node
*next
;
223 * Even though the following tail->p check is sufficient to find
224 * out if we reached the end of the queue, we first check
225 * node->next as a common case to ensure that iteration on nodes
226 * do not frequently access enqueuer's tail->p cache line.
228 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
229 /* Load node->next before tail->p */
231 if (CMM_LOAD_SHARED(tail
->p
) == node
)
233 next
= ___cds_wfcq_node_sync_next(node
);
235 /* Load node->next before loading next's content */
236 cmm_smp_read_barrier_depends();
241 * __cds_wfcq_dequeue_blocking: dequeue a node from the queue.
243 * No need to go on a waitqueue here, as there is no possible state in which the
244 * list could cause dequeue to busy-loop needlessly while waiting for another
245 * thread to be scheduled. The queue appears empty until tail->next is set by
248 * Content written into the node before enqueue is guaranteed to be
249 * consistent, but no other memory ordering is ensured.
250 * It is valid to reuse and free a dequeued node immediately.
251 * Should be called with cds_wfcq_dequeue_lock() held.
253 static inline struct cds_wfcq_node
*
254 ___cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
255 struct cds_wfcq_tail
*tail
)
257 struct cds_wfcq_node
*node
, *next
;
259 if (_cds_wfcq_empty(head
, tail
))
262 node
= ___cds_wfcq_node_sync_next(&head
->node
);
264 if ((next
= CMM_LOAD_SHARED(node
->next
)) == NULL
) {
266 * @node is probably the only node in the queue.
267 * Try to move the tail to &q->head.
268 * q->head.next is set to NULL here, and stays
269 * NULL if the cmpxchg succeeds. Should the
270 * cmpxchg fail due to a concurrent enqueue, the
271 * q->head.next will be set to the next node.
272 * The implicit memory barrier before
273 * uatomic_cmpxchg() orders load node->next
274 * before loading q->tail.
275 * The implicit memory barrier before uatomic_cmpxchg
276 * orders load q->head.next before loading node's
279 _cds_wfcq_node_init(&head
->node
);
280 if (uatomic_cmpxchg(&tail
->p
, node
, &head
->node
) == node
)
282 next
= ___cds_wfcq_node_sync_next(node
);
286 * Move queue head forward.
288 head
->node
.next
= next
;
290 /* Load q->head.next before loading node's content */
291 cmm_smp_read_barrier_depends();
296 * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
298 * Dequeue all nodes from src_q.
299 * dest_q must be already initialized.
300 * Should be called with cds_wfcq_dequeue_lock() held on src_q.
303 ___cds_wfcq_splice_blocking(
304 struct cds_wfcq_head
*dest_q_head
,
305 struct cds_wfcq_tail
*dest_q_tail
,
306 struct cds_wfcq_head
*src_q_head
,
307 struct cds_wfcq_tail
*src_q_tail
)
309 struct cds_wfcq_node
*head
, *tail
;
311 if (_cds_wfcq_empty(src_q_head
, src_q_tail
))
314 head
= ___cds_wfcq_node_sync_next(&src_q_head
->node
);
315 _cds_wfcq_node_init(&src_q_head
->node
);
318 * Memory barrier implied before uatomic_xchg() orders store to
319 * src_q->head before store to src_q->tail. This is required by
320 * concurrent enqueue on src_q, which exchanges the tail before
321 * updating the previous tail's next pointer.
323 tail
= uatomic_xchg(&src_q_tail
->p
, &src_q_head
->node
);
326 * Append the spliced content of src_q into dest_q. Does not
327 * require mutual exclusion on dest_q (wait-free).
329 ___cds_wfcq_append(dest_q_head
, dest_q_tail
, head
, tail
);
333 * cds_wfcq_dequeue_blocking: dequeue a node from a wait-free queue.
335 * Content written into the node before enqueue is guaranteed to be
336 * consistent, but no other memory ordering is ensured.
337 * Mutual exlusion with (and only with) cds_wfcq_splice_blocking is
339 * It is valid to reuse and free a dequeued node immediately.
341 static inline struct cds_wfcq_node
*
342 _cds_wfcq_dequeue_blocking(struct cds_wfcq_head
*head
,
343 struct cds_wfcq_tail
*tail
)
345 struct cds_wfcq_node
*retval
;
347 _cds_wfcq_dequeue_lock(head
, tail
);
348 retval
= ___cds_wfcq_dequeue_blocking(head
, tail
);
349 _cds_wfcq_dequeue_unlock(head
, tail
);
354 * cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
356 * Dequeue all nodes from src_q.
357 * dest_q must be already initialized.
358 * Content written into the node before enqueue is guaranteed to be
359 * consistent, but no other memory ordering is ensured.
360 * Mutual exlusion with (and only with) cds_wfcq_dequeue_blocking is
364 _cds_wfcq_splice_blocking(
365 struct cds_wfcq_head
*dest_q_head
,
366 struct cds_wfcq_tail
*dest_q_tail
,
367 struct cds_wfcq_head
*src_q_head
,
368 struct cds_wfcq_tail
*src_q_tail
)
370 _cds_wfcq_dequeue_lock(src_q_head
, src_q_tail
);
371 ___cds_wfcq_splice_blocking(dest_q_head
, dest_q_tail
,
372 src_q_head
, src_q_tail
);
373 _cds_wfcq_dequeue_unlock(src_q_head
, src_q_tail
);
380 #endif /* _URCU_WFCQUEUE_STATIC_H */
This page took 0.050505 seconds and 3 git commands to generate.