Revert "CDS API: removal of rcu_read lock/unlock dep, removal of call_rcu argument...
[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
34#ifdef __cplusplus
35extern "C" {
36#endif
37
e17d9985
MD
38struct cds_lfq_node_rcu_dummy {
39 struct cds_lfq_node_rcu parent;
40 struct rcu_head head;
41 struct cds_lfq_queue_rcu *q;
42};
43
3d02c34d 44/*
e17d9985
MD
45 * Lock-free RCU queue. Enqueue and dequeue operations hold a RCU read
46 * lock to deal with cmpxchg ABA problem. This queue is *not* circular:
47 * head points to the oldest node, tail points to the newest node.
0cca1a2d 48 * A dummy node is kept to ensure enqueue and dequeue can always proceed
e17d9985
MD
49 * concurrently. Keeping a separate head and tail helps with large
50 * queues: enqueue and dequeue can proceed concurrently without
51 * wrestling for exclusive access to the same variables.
52 *
0cca1a2d
MD
53 * Dequeue retry if it detects that it would be dequeueing the last node
54 * (it means a dummy node dequeue-requeue is in progress). This ensures
55 * that there is always at least one node in the queue.
e17d9985 56 *
0cca1a2d
MD
57 * In the dequeue operation, we internally reallocate the dummy node
58 * upon dequeue/requeue and use call_rcu to free the old one after a
59 * grace period.
3d02c34d
MD
60 */
61
e17d9985
MD
62static inline
63struct cds_lfq_node_rcu *make_dummy(struct cds_lfq_queue_rcu *q,
64 struct cds_lfq_node_rcu *next)
65{
66 struct cds_lfq_node_rcu_dummy *dummy;
67
68 dummy = malloc(sizeof(struct cds_lfq_node_rcu_dummy));
8f03ed0d 69 assert(dummy);
e17d9985 70 dummy->parent.next = next;
fbdb32f6 71 dummy->parent.dummy = 1;
e17d9985 72 dummy->q = q;
909292c2 73 return &dummy->parent;
e17d9985
MD
74}
75
76static inline
f6719811 77void free_dummy_cb(struct rcu_head *head)
e17d9985
MD
78{
79 struct cds_lfq_node_rcu_dummy *dummy =
80 caa_container_of(head, struct cds_lfq_node_rcu_dummy, head);
81 free(dummy);
82}
83
84static inline
85void rcu_free_dummy(struct cds_lfq_node_rcu *node)
86{
87 struct cds_lfq_node_rcu_dummy *dummy;
3d02c34d 88
fbdb32f6 89 assert(node->dummy);
909292c2 90 dummy = caa_container_of(node, struct cds_lfq_node_rcu_dummy, parent);
6e5f88cf 91 dummy->q->queue_call_rcu(&dummy->head, free_dummy_cb);
f6719811
MD
92}
93
94static inline
95void free_dummy(struct cds_lfq_node_rcu *node)
96{
97 struct cds_lfq_node_rcu_dummy *dummy;
98
fbdb32f6 99 assert(node->dummy);
f6719811
MD
100 dummy = caa_container_of(node, struct cds_lfq_node_rcu_dummy, parent);
101 free(dummy);
e17d9985
MD
102}
103
104static inline
16aa9ee8 105void _cds_lfq_node_init_rcu(struct cds_lfq_node_rcu *node)
3d02c34d
MD
106{
107 node->next = NULL;
fbdb32f6 108 node->dummy = 0;
3d02c34d
MD
109}
110
e17d9985 111static inline
6e5f88cf
MD
112void _cds_lfq_init_rcu(struct cds_lfq_queue_rcu *q,
113 void queue_call_rcu(struct rcu_head *head,
114 void (*func)(struct rcu_head *head)))
e17d9985
MD
115{
116 q->tail = make_dummy(q, NULL);
0cca1a2d 117 q->head = q->tail;
6e5f88cf 118 q->queue_call_rcu = queue_call_rcu;
e17d9985
MD
119}
120
121/*
122 * The queue should be emptied before calling destroy.
123 *
124 * Return 0 on success, -EPERM if queue is not empty.
125 */
126static inline
127int _cds_lfq_destroy_rcu(struct cds_lfq_queue_rcu *q)
3d02c34d 128{
0cca1a2d 129 struct cds_lfq_node_rcu *head;
e17d9985
MD
130
131 head = rcu_dereference(q->head);
fbdb32f6 132 if (!(head->dummy && head->next == NULL))
e17d9985 133 return -EPERM; /* not empty */
f6719811 134 free_dummy(head);
e17d9985 135 return 0;
3d02c34d
MD
136}
137
d9b52143 138/*
6e5f88cf 139 * Should be called under rcu read lock critical section.
d9b52143 140 */
e17d9985 141static inline
d9b52143
MD
142void _cds_lfq_enqueue_rcu(struct cds_lfq_queue_rcu *q,
143 struct cds_lfq_node_rcu *node)
3d02c34d 144{
3d02c34d
MD
145 /*
146 * uatomic_cmpxchg() implicit memory barrier orders earlier stores to
147 * node before publication.
148 */
149
150 for (;;) {
16aa9ee8 151 struct cds_lfq_node_rcu *tail, *next;
3d02c34d 152
3d02c34d 153 tail = rcu_dereference(q->tail);
909292c2 154 next = uatomic_cmpxchg(&tail->next, NULL, node);
3d02c34d
MD
155 if (next == NULL) {
156 /*
157 * Tail was at the end of queue, we successfully
e17d9985
MD
158 * appended to it. Now move tail (another
159 * enqueue might beat us to it, that's fine).
3d02c34d 160 */
85b57703 161 (void) uatomic_cmpxchg(&q->tail, tail, node);
3d02c34d
MD
162 return;
163 } else {
164 /*
e17d9985
MD
165 * Failure to append to current tail.
166 * Help moving tail further and retry.
3d02c34d 167 */
85b57703 168 (void) uatomic_cmpxchg(&q->tail, tail, next);
3d02c34d
MD
169 continue;
170 }
171 }
172}
173
f6719811 174static inline
fbdb32f6 175void enqueue_dummy(struct cds_lfq_queue_rcu *q)
f6719811
MD
176{
177 struct cds_lfq_node_rcu *node;
178
179 /* We need to reallocate to protect from ABA. */
180 node = make_dummy(q, NULL);
f6719811
MD
181 _cds_lfq_enqueue_rcu(q, node);
182}
183
3d02c34d 184/*
6e5f88cf 185 * Should be called under rcu read lock critical section.
d9b52143 186 *
e17d9985
MD
187 * The caller must wait for a grace period to pass before freeing the returned
188 * node or modifying the cds_lfq_node_rcu structure.
189 * Returns NULL if queue is empty.
3d02c34d 190 */
e17d9985 191static inline
a34df756 192struct cds_lfq_node_rcu *_cds_lfq_dequeue_rcu(struct cds_lfq_queue_rcu *q)
3d02c34d
MD
193{
194 for (;;) {
fbdb32f6 195 struct cds_lfq_node_rcu *head, *next;
3d02c34d 196
3d02c34d 197 head = rcu_dereference(q->head);
909292c2 198 next = rcu_dereference(head->next);
6e5f88cf 199 if (head->dummy && next == NULL)
e17d9985
MD
200 return NULL; /* empty */
201 /*
202 * We never, ever allow dequeue to get to a state where
203 * the queue is empty (we need at least one node in the
204 * queue). This is ensured by checking if the head next
fbdb32f6
MD
205 * is NULL, which means we need to enqueue a dummy node
206 * before we can hope dequeuing anything.
e17d9985 207 */
f6719811 208 if (!next) {
fbdb32f6
MD
209 enqueue_dummy(q);
210 next = rcu_dereference(head->next);
f6719811 211 }
6e5f88cf 212 if (uatomic_cmpxchg(&q->head, head, next) != head)
f6719811 213 continue; /* Concurrently pushed. */
fbdb32f6
MD
214 if (head->dummy) {
215 /* Free dummy after grace period. */
216 rcu_free_dummy(head);
f6719811 217 continue; /* try again */
3d02c34d 218 }
f6719811 219 return head;
3d02c34d
MD
220 }
221}
222
223#ifdef __cplusplus
224}
225#endif
226
227#endif /* _URCU_RCULFQUEUE_STATIC_H */
This page took 0.033493 seconds and 4 git commands to generate.