b4208dae65f28e0f0564c07dd77f338f72b4862b
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"
39 * Queue with wait-free enqueue/blocking dequeue.
40 * This implementation adds a dummy head node when the queue is empty to ensure
41 * we can always update the queue locklessly.
43 * Inspired from half-wait-free/half-blocking queue implementation done by
48 struct wfq_node
*next
;
52 struct wfq_node
*head
, **tail
;
53 struct wfq_node dummy
; /* Dummy node */
57 void wfq_node_init(struct wfq_node
*node
)
62 void wfq_init(struct wfq_queue
*q
)
66 wfq_node_init(&q
->dummy
);
67 /* Set queue head and tail */
69 q
->tail
= &q
->dummy
.next
;
70 ret
= pthread_mutex_init(&q
->lock
, NULL
);
74 void wfq_enqueue(struct wfq_queue
*q
, struct wfq_node
*node
)
76 struct wfq_node
**old_tail
;
79 * uatomic_xchg() implicit memory barrier orders earlier stores to data
80 * structure containing node and setting node->next to NULL before
83 old_tail
= uatomic_xchg(&q
->tail
, node
);
85 * At this point, dequeuers see a NULL old_tail->next, which indicates
86 * that the queue is being appended to. The following store will append
87 * "node" to the queue from a dequeuer perspective.
89 STORE_SHARED(*old_tail
, node
);
93 * It is valid to reuse and free a dequeued node immediately.
95 * No need to go on a waitqueue here, as there is no possible state in which the
96 * list could cause dequeue to busy-loop needlessly while waiting for another
97 * thread to be scheduled. The queue appears empty until tail->next is set by
101 __wfq_dequeue_blocking(struct wfq_queue
*q
)
103 struct wfq_node
*node
, *next
;
107 * Queue is empty if it only contains the dummy node.
109 if (q
->head
== &q
->dummy
&& LOAD_SHARED(q
->tail
) == &q
->dummy
.next
)
114 * Adaptative busy-looping waiting for enqueuer to complete enqueue.
116 while ((next
= LOAD_SHARED(node
->next
)) == NULL
) {
121 poll(NULL
, 0, 1); /* Wait for 1ms */
124 * Move queue head forward.
128 * Requeue dummy node if we just dequeued it.
130 if (node
== &q
->dummy
) {
132 wfq_enqueue(q
, node
);
133 return __wfq_dequeue_blocking(q
);
139 wfq_dequeue_blocking(struct wfq_queue
*q
)
141 struct wfq_node
*retnode
;
144 ret
= pthread_mutex_lock(&q
->lock
);
146 retnode
= __wfq_dequeue_blocking(q
);
147 ret
= pthread_mutex_unlock(&q
->lock
);
156 #endif /* _URCU_WFQUEUE_H */
This page took 0.031159 seconds and 3 git commands to generate.