RCU lfqueue: Now works without reference counting (API change)
[urcu.git] / urcu / static / rculfqueue.h
CommitLineData
3d02c34d
MD
1#ifndef _URCU_RCULFQUEUE_STATIC_H
2#define _URCU_RCULFQUEUE_STATIC_H
3
4/*
5 * rculfqueue-static.h
6 *
7 * Userspace RCU library - Lock-Free RCU Queue
8 *
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See rculfqueue.h for linking
12 * dynamically with the userspace rcu library.
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
e17d9985 29#include <urcu-call-rcu.h>
a2e7bf9c 30#include <urcu/uatomic.h>
3d02c34d 31#include <assert.h>
e17d9985 32#include <errno.h>
3d02c34d
MD
33/* A urcu implementation header should be already included. */
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
e17d9985
MD
39struct cds_lfq_node_rcu_dummy {
40 struct cds_lfq_node_rcu parent;
41 struct rcu_head head;
42 struct cds_lfq_queue_rcu *q;
43};
44
3d02c34d 45/*
e17d9985
MD
46 * Lock-free RCU queue. Enqueue and dequeue operations hold a RCU read
47 * lock to deal with cmpxchg ABA problem. This queue is *not* circular:
48 * head points to the oldest node, tail points to the newest node.
49 * Dummy nodes are kept to ensure enqueue and dequeue can always proceed
50 * concurrently. Keeping a separate head and tail helps with large
51 * queues: enqueue and dequeue can proceed concurrently without
52 * wrestling for exclusive access to the same variables.
53 *
54 * We keep two dummy nodes in the queue to distinguish between empty queue
55 * state and intermediate state while a dummy node dequeue/requeue is
56 * being performed. Dequeue retry if it detects that it would be
57 * dequeueing the last node (it means a dummy node dequeue-requeue is in
58 * progress). This ensures that there is always at least one node in
59 * the queue. In a situation where the two dummy nodes are being
60 * requeued (they therefore don't appear in the queue at a given
61 * moment), we are certain that there is at least one non-dummy node in
62 * the queue (ensured by the test for NULL next node upon dequeue).
63 *
64 * In the dequeue operation, we internally reallocate the dummy nodes
65 * upon dequeue/requeue and use call_rcu to free them after a grace
66 * period.
3d02c34d
MD
67 */
68
e17d9985
MD
69static inline
70int is_dummy(struct cds_lfq_node_rcu *node)
71{
72 return ((unsigned long) node) & 0x1UL;
73}
74
75static inline
76struct cds_lfq_node_rcu *make_dummy(struct cds_lfq_queue_rcu *q,
77 struct cds_lfq_node_rcu *next)
78{
79 struct cds_lfq_node_rcu_dummy *dummy;
80
81 dummy = malloc(sizeof(struct cds_lfq_node_rcu_dummy));
82 dummy->parent.next = next;
83 dummy->q = q;
84 return (struct cds_lfq_node_rcu *) (((unsigned long) &dummy->parent) | 0x1UL);
85}
86
87static inline
88struct cds_lfq_node_rcu *get_node(struct cds_lfq_node_rcu *node)
89{
90 return (struct cds_lfq_node_rcu *) (((unsigned long )node) & ~0x1UL);
91}
92
93static inline
94void free_dummy(struct rcu_head *head)
95{
96 struct cds_lfq_node_rcu_dummy *dummy =
97 caa_container_of(head, struct cds_lfq_node_rcu_dummy, head);
98 free(dummy);
99}
100
101static inline
102void rcu_free_dummy(struct cds_lfq_node_rcu *node)
103{
104 struct cds_lfq_node_rcu_dummy *dummy;
3d02c34d 105
e17d9985
MD
106 dummy = caa_container_of(get_node(node), struct cds_lfq_node_rcu_dummy,
107 parent);
108 dummy->q->queue_call_rcu(&dummy->head, free_dummy);
109}
110
111static inline
16aa9ee8 112void _cds_lfq_node_init_rcu(struct cds_lfq_node_rcu *node)
3d02c34d
MD
113{
114 node->next = NULL;
3d02c34d
MD
115}
116
e17d9985 117static inline
d9b52143 118void _cds_lfq_init_rcu(struct cds_lfq_queue_rcu *q,
e17d9985
MD
119 void queue_call_rcu(struct rcu_head *head,
120 void (*func)(struct rcu_head *head)))
121{
122 q->tail = make_dummy(q, NULL);
123 q->head = make_dummy(q, q->tail);
124 q->queue_call_rcu = queue_call_rcu;
125}
126
127/*
128 * The queue should be emptied before calling destroy.
129 *
130 * Return 0 on success, -EPERM if queue is not empty.
131 */
132static inline
133int _cds_lfq_destroy_rcu(struct cds_lfq_queue_rcu *q)
3d02c34d 134{
e17d9985
MD
135 struct cds_lfq_node_rcu *head, *next;
136
137 head = rcu_dereference(q->head);
138 next = rcu_dereference(get_node(head)->next);
139 if (!(is_dummy(head) && is_dummy(next) && get_node(next)->next == NULL))
140 return -EPERM; /* not empty */
141 rcu_free_dummy(head);
142 rcu_free_dummy(next);
143 return 0;
3d02c34d
MD
144}
145
d9b52143
MD
146/*
147 * Should be called under rcu read lock critical section.
148 */
e17d9985 149static inline
d9b52143
MD
150void _cds_lfq_enqueue_rcu(struct cds_lfq_queue_rcu *q,
151 struct cds_lfq_node_rcu *node)
3d02c34d 152{
3d02c34d
MD
153 /*
154 * uatomic_cmpxchg() implicit memory barrier orders earlier stores to
155 * node before publication.
156 */
157
158 for (;;) {
16aa9ee8 159 struct cds_lfq_node_rcu *tail, *next;
3d02c34d 160
3d02c34d 161 tail = rcu_dereference(q->tail);
e17d9985 162 next = uatomic_cmpxchg(&get_node(tail)->next, NULL, node);
3d02c34d
MD
163 if (next == NULL) {
164 /*
165 * Tail was at the end of queue, we successfully
e17d9985
MD
166 * appended to it. Now move tail (another
167 * enqueue might beat us to it, that's fine).
3d02c34d 168 */
85b57703 169 (void) uatomic_cmpxchg(&q->tail, tail, node);
3d02c34d
MD
170 return;
171 } else {
172 /*
e17d9985
MD
173 * Failure to append to current tail.
174 * Help moving tail further and retry.
3d02c34d 175 */
85b57703 176 (void) uatomic_cmpxchg(&q->tail, tail, next);
3d02c34d
MD
177 continue;
178 }
179 }
180}
181
182/*
d9b52143
MD
183 * Should be called under rcu read lock critical section.
184 *
e17d9985
MD
185 * The caller must wait for a grace period to pass before freeing the returned
186 * node or modifying the cds_lfq_node_rcu structure.
187 * Returns NULL if queue is empty.
3d02c34d 188 */
e17d9985 189static inline
a34df756 190struct cds_lfq_node_rcu *_cds_lfq_dequeue_rcu(struct cds_lfq_queue_rcu *q)
3d02c34d
MD
191{
192 for (;;) {
16aa9ee8 193 struct cds_lfq_node_rcu *head, *next;
3d02c34d 194
3d02c34d 195 head = rcu_dereference(q->head);
e17d9985
MD
196 next = rcu_dereference(get_node(head)->next);
197 if (is_dummy(head) && is_dummy(next) && get_node(next)->next == NULL)
198 return NULL; /* empty */
199 /*
200 * We never, ever allow dequeue to get to a state where
201 * the queue is empty (we need at least one node in the
202 * queue). This is ensured by checking if the head next
203 * is NULL and retry in that case (this means a
204 * concurrent dummy node re-enqueue is in progress).
205 */
3d02c34d
MD
206 if (next) {
207 if (uatomic_cmpxchg(&q->head, head, next) == head) {
e17d9985
MD
208 if (is_dummy(head)) {
209 struct cds_lfq_node_rcu *node;
210 /*
211 * Requeue dummy. We need to
212 * reallocate to protect from
213 * ABA.
214 */
215 rcu_free_dummy(head);
216 node = make_dummy(q, NULL);
217 _cds_lfq_enqueue_rcu(q, node);
218 continue; /* try again */
219 }
220 return head;
3d02c34d
MD
221 } else {
222 /* Concurrently pushed, retry */
3d02c34d
MD
223 continue;
224 }
225 } else {
e17d9985
MD
226 /* Dummy node re-enqueue is in progress, retry. */
227 continue;
3d02c34d
MD
228 }
229 }
230}
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* _URCU_RCULFQUEUE_STATIC_H */
This page took 0.031988 seconds and 4 git commands to generate.