2eaace1c260f99e7562a36649fc5129fdb03cce0
1 #ifndef _URCU_WFQUEUE_H
2 #define _URCU_WFQUEUE_H
7 * Userspace RCU library - Queue with Wait-Free Enqueue/Blocking Dequeue
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <urcu/compiler.h>
34 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
35 #error "Dynamic loader LGPL wrappers not implemented yet"
38 #define WFQ_ADAPT_ATTEMPTS 10 /* Retry if being set */
39 #define WFQ_WAIT 10 /* Wait 10 ms if being set */
42 * Queue with wait-free enqueue/blocking dequeue.
43 * This implementation adds a dummy head node when the queue is empty to ensure
44 * we can always update the queue locklessly.
46 * Inspired from half-wait-free/half-blocking queue implementation done by
51 struct wfq_node
*next
;
55 struct wfq_node
*head
, **tail
;
56 struct wfq_node dummy
; /* Dummy node */
60 void wfq_node_init(struct wfq_node
*node
)
65 void wfq_init(struct wfq_queue
*q
)
69 wfq_node_init(&q
->dummy
);
70 /* Set queue head and tail */
72 q
->tail
= &q
->dummy
.next
;
73 ret
= pthread_mutex_init(&q
->lock
, NULL
);
77 void wfq_enqueue(struct wfq_queue
*q
, struct wfq_node
*node
)
79 struct wfq_node
**old_tail
;
82 * uatomic_xchg() implicit memory barrier orders earlier stores to data
83 * structure containing node and setting node->next to NULL before
86 old_tail
= uatomic_xchg(&q
->tail
, node
);
88 * At this point, dequeuers see a NULL old_tail->next, which indicates
89 * that the queue is being appended to. The following store will append
90 * "node" to the queue from a dequeuer perspective.
92 STORE_SHARED(*old_tail
, node
);
96 * It is valid to reuse and free a dequeued node immediately.
98 * No need to go on a waitqueue here, as there is no possible state in which the
99 * list could cause dequeue to busy-loop needlessly while waiting for another
100 * thread to be scheduled. The queue appears empty until tail->next is set by
104 __wfq_dequeue_blocking(struct wfq_queue
*q
)
106 struct wfq_node
*node
, *next
;
110 * Queue is empty if it only contains the dummy node.
112 if (q
->head
== &q
->dummy
&& LOAD_SHARED(q
->tail
) == &q
->dummy
.next
)
117 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
119 while ((next
= LOAD_SHARED(node
->next
)) == NULL
) {
120 if (++attempt
>= WFQ_ADAPT_ATTEMPTS
) {
121 poll(NULL
, 0, WFQ_WAIT
); /* Wait for 10ms */
127 * Move queue head forward.
131 * Requeue dummy node if we just dequeued it.
133 if (node
== &q
->dummy
) {
135 wfq_enqueue(q
, node
);
136 return __wfq_dequeue_blocking(q
);
142 wfq_dequeue_blocking(struct wfq_queue
*q
)
144 struct wfq_node
*retnode
;
147 ret
= pthread_mutex_lock(&q
->lock
);
149 retnode
= __wfq_dequeue_blocking(q
);
150 ret
= pthread_mutex_unlock(&q
->lock
);
159 #endif /* _URCU_WFQUEUE_H */
This page took 0.032662 seconds and 4 git commands to generate.