]>
Commit | Line | Data |
---|---|---|
341774fe MD |
1 | #ifndef QEMU_RCU_QUEUE_H |
2 | #define QEMU_RCU_QUEUE_H | |
3 | ||
4 | /* | |
5 | * rcu_queue.h | |
6 | * | |
7 | * RCU-friendly versions of the queue.h primitives. | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | * | |
23 | * Copyright (c) 2013 Mike D. Day, IBM Corporation. | |
24 | * | |
25 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
26 | */ | |
27 | ||
28 | #include "qemu/queue.h" | |
29 | #include "qemu/atomic.h" | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | ||
36 | /* | |
37 | * List access methods. | |
38 | */ | |
d73415a3 SH |
39 | #define QLIST_EMPTY_RCU(head) (qatomic_read(&(head)->lh_first) == NULL) |
40 | #define QLIST_FIRST_RCU(head) (qatomic_rcu_read(&(head)->lh_first)) | |
41 | #define QLIST_NEXT_RCU(elm, field) (qatomic_rcu_read(&(elm)->field.le_next)) | |
341774fe MD |
42 | |
43 | /* | |
44 | * List functions. | |
45 | */ | |
46 | ||
47 | ||
48 | /* | |
d73415a3 | 49 | * The difference between qatomic_read/set and qatomic_rcu_read/set |
341774fe MD |
50 | * is in the including of a read/write memory barrier to the volatile |
51 | * access. atomic_rcu_* macros include the memory barrier, the | |
52 | * plain atomic macros do not. Therefore, it should be correct to | |
53 | * issue a series of reads or writes to the same element using only | |
54 | * the atomic_* macro, until the last read or write, which should be | |
55 | * atomic_rcu_* to introduce a read or write memory barrier as | |
56 | * appropriate. | |
57 | */ | |
58 | ||
59 | /* Upon publication of the listelm->next value, list readers | |
60 | * will see the new node when following next pointers from | |
61 | * antecedent nodes, but may not see the new node when following | |
62 | * prev pointers from subsequent nodes until after the RCU grace | |
63 | * period expires. | |
64 | * see linux/include/rculist.h __list_add_rcu(new, prev, next) | |
65 | */ | |
66 | #define QLIST_INSERT_AFTER_RCU(listelm, elm, field) do { \ | |
67 | (elm)->field.le_next = (listelm)->field.le_next; \ | |
68 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | |
d73415a3 | 69 | qatomic_rcu_set(&(listelm)->field.le_next, (elm)); \ |
341774fe MD |
70 | if ((elm)->field.le_next != NULL) { \ |
71 | (elm)->field.le_next->field.le_prev = \ | |
72 | &(elm)->field.le_next; \ | |
73 | } \ | |
74 | } while (/*CONSTCOND*/0) | |
75 | ||
76 | /* Upon publication of the listelm->prev->next value, list | |
77 | * readers will see the new element when following prev pointers | |
78 | * from subsequent elements, but may not see the new element | |
79 | * when following next pointers from antecedent elements | |
80 | * until after the RCU grace period expires. | |
81 | */ | |
82 | #define QLIST_INSERT_BEFORE_RCU(listelm, elm, field) do { \ | |
83 | (elm)->field.le_prev = (listelm)->field.le_prev; \ | |
84 | (elm)->field.le_next = (listelm); \ | |
d73415a3 | 85 | qatomic_rcu_set((listelm)->field.le_prev, (elm)); \ |
341774fe MD |
86 | (listelm)->field.le_prev = &(elm)->field.le_next; \ |
87 | } while (/*CONSTCOND*/0) | |
88 | ||
89 | /* Upon publication of the head->first value, list readers | |
90 | * will see the new element when following the head, but may | |
91 | * not see the new element when following prev pointers from | |
92 | * subsequent elements until after the RCU grace period has | |
93 | * expired. | |
94 | */ | |
95 | #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \ | |
96 | (elm)->field.le_prev = &(head)->lh_first; \ | |
97 | (elm)->field.le_next = (head)->lh_first; \ | |
d73415a3 | 98 | qatomic_rcu_set((&(head)->lh_first), (elm)); \ |
341774fe MD |
99 | if ((elm)->field.le_next != NULL) { \ |
100 | (elm)->field.le_next->field.le_prev = \ | |
101 | &(elm)->field.le_next; \ | |
102 | } \ | |
103 | } while (/*CONSTCOND*/0) | |
104 | ||
105 | ||
106 | /* prior to publication of the elm->prev->next value, some list | |
107 | * readers may still see the removed element when following | |
108 | * the antecedent's next pointer. | |
109 | */ | |
110 | #define QLIST_REMOVE_RCU(elm, field) do { \ | |
111 | if ((elm)->field.le_next != NULL) { \ | |
112 | (elm)->field.le_next->field.le_prev = \ | |
113 | (elm)->field.le_prev; \ | |
114 | } \ | |
d73415a3 | 115 | qatomic_set((elm)->field.le_prev, (elm)->field.le_next); \ |
341774fe MD |
116 | } while (/*CONSTCOND*/0) |
117 | ||
118 | /* List traversal must occur within an RCU critical section. */ | |
119 | #define QLIST_FOREACH_RCU(var, head, field) \ | |
d73415a3 | 120 | for ((var) = qatomic_rcu_read(&(head)->lh_first); \ |
341774fe | 121 | (var); \ |
d73415a3 | 122 | (var) = qatomic_rcu_read(&(var)->field.le_next)) |
341774fe MD |
123 | |
124 | /* List traversal must occur within an RCU critical section. */ | |
125 | #define QLIST_FOREACH_SAFE_RCU(var, head, field, next_var) \ | |
d73415a3 | 126 | for ((var) = (qatomic_rcu_read(&(head)->lh_first)); \ |
341774fe | 127 | (var) && \ |
d73415a3 | 128 | ((next_var) = qatomic_rcu_read(&(var)->field.le_next), 1); \ |
341774fe MD |
129 | (var) = (next_var)) |
130 | ||
13d8ef7d EC |
131 | /* |
132 | * RCU simple queue | |
133 | */ | |
134 | ||
135 | /* Simple queue access methods */ | |
d73415a3 SH |
136 | #define QSIMPLEQ_EMPTY_RCU(head) \ |
137 | (qatomic_read(&(head)->sqh_first) == NULL) | |
138 | #define QSIMPLEQ_FIRST_RCU(head) qatomic_rcu_read(&(head)->sqh_first) | |
139 | #define QSIMPLEQ_NEXT_RCU(elm, field) qatomic_rcu_read(&(elm)->field.sqe_next) | |
13d8ef7d EC |
140 | |
141 | /* Simple queue functions */ | |
142 | #define QSIMPLEQ_INSERT_HEAD_RCU(head, elm, field) do { \ | |
143 | (elm)->field.sqe_next = (head)->sqh_first; \ | |
144 | if ((elm)->field.sqe_next == NULL) { \ | |
145 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
146 | } \ | |
d73415a3 | 147 | qatomic_rcu_set(&(head)->sqh_first, (elm)); \ |
13d8ef7d EC |
148 | } while (/*CONSTCOND*/0) |
149 | ||
150 | #define QSIMPLEQ_INSERT_TAIL_RCU(head, elm, field) do { \ | |
151 | (elm)->field.sqe_next = NULL; \ | |
d73415a3 | 152 | qatomic_rcu_set((head)->sqh_last, (elm)); \ |
13d8ef7d EC |
153 | (head)->sqh_last = &(elm)->field.sqe_next; \ |
154 | } while (/*CONSTCOND*/0) | |
155 | ||
156 | #define QSIMPLEQ_INSERT_AFTER_RCU(head, listelm, elm, field) do { \ | |
157 | (elm)->field.sqe_next = (listelm)->field.sqe_next; \ | |
158 | if ((elm)->field.sqe_next == NULL) { \ | |
159 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
160 | } \ | |
d73415a3 | 161 | qatomic_rcu_set(&(listelm)->field.sqe_next, (elm)); \ |
13d8ef7d EC |
162 | } while (/*CONSTCOND*/0) |
163 | ||
164 | #define QSIMPLEQ_REMOVE_HEAD_RCU(head, field) do { \ | |
d73415a3 | 165 | qatomic_set(&(head)->sqh_first, (head)->sqh_first->field.sqe_next);\ |
13d8ef7d EC |
166 | if ((head)->sqh_first == NULL) { \ |
167 | (head)->sqh_last = &(head)->sqh_first; \ | |
168 | } \ | |
169 | } while (/*CONSTCOND*/0) | |
170 | ||
171 | #define QSIMPLEQ_REMOVE_RCU(head, elm, type, field) do { \ | |
172 | if ((head)->sqh_first == (elm)) { \ | |
173 | QSIMPLEQ_REMOVE_HEAD_RCU((head), field); \ | |
174 | } else { \ | |
175 | struct type *curr = (head)->sqh_first; \ | |
176 | while (curr->field.sqe_next != (elm)) { \ | |
177 | curr = curr->field.sqe_next; \ | |
178 | } \ | |
d73415a3 | 179 | qatomic_set(&curr->field.sqe_next, \ |
13d8ef7d EC |
180 | curr->field.sqe_next->field.sqe_next); \ |
181 | if (curr->field.sqe_next == NULL) { \ | |
182 | (head)->sqh_last = &(curr)->field.sqe_next; \ | |
183 | } \ | |
184 | } \ | |
185 | } while (/*CONSTCOND*/0) | |
186 | ||
187 | #define QSIMPLEQ_FOREACH_RCU(var, head, field) \ | |
d73415a3 | 188 | for ((var) = qatomic_rcu_read(&(head)->sqh_first); \ |
13d8ef7d | 189 | (var); \ |
d73415a3 | 190 | (var) = qatomic_rcu_read(&(var)->field.sqe_next)) |
13d8ef7d EC |
191 | |
192 | #define QSIMPLEQ_FOREACH_SAFE_RCU(var, head, field, next) \ | |
d73415a3 SH |
193 | for ((var) = qatomic_rcu_read(&(head)->sqh_first); \ |
194 | (var) && ((next) = qatomic_rcu_read(&(var)->field.sqe_next), 1);\ | |
13d8ef7d EC |
195 | (var) = (next)) |
196 | ||
945d9c75 EC |
197 | /* |
198 | * RCU tail queue | |
199 | */ | |
200 | ||
201 | /* Tail queue access methods */ | |
d73415a3 SH |
202 | #define QTAILQ_EMPTY_RCU(head) (qatomic_read(&(head)->tqh_first) == NULL) |
203 | #define QTAILQ_FIRST_RCU(head) qatomic_rcu_read(&(head)->tqh_first) | |
204 | #define QTAILQ_NEXT_RCU(elm, field) qatomic_rcu_read(&(elm)->field.tqe_next) | |
945d9c75 EC |
205 | |
206 | /* Tail queue functions */ | |
207 | #define QTAILQ_INSERT_HEAD_RCU(head, elm, field) do { \ | |
208 | (elm)->field.tqe_next = (head)->tqh_first; \ | |
209 | if ((elm)->field.tqe_next != NULL) { \ | |
7274f01b PB |
210 | (head)->tqh_first->field.tqe_circ.tql_prev = \ |
211 | &(elm)->field.tqe_circ; \ | |
945d9c75 | 212 | } else { \ |
7274f01b | 213 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ |
945d9c75 | 214 | } \ |
d73415a3 | 215 | qatomic_rcu_set(&(head)->tqh_first, (elm)); \ |
7274f01b | 216 | (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \ |
945d9c75 EC |
217 | } while (/*CONSTCOND*/0) |
218 | ||
7274f01b PB |
219 | #define QTAILQ_INSERT_TAIL_RCU(head, elm, field) do { \ |
220 | (elm)->field.tqe_next = NULL; \ | |
221 | (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev; \ | |
d73415a3 | 222 | qatomic_rcu_set(&(head)->tqh_circ.tql_prev->tql_next, (elm)); \ |
7274f01b | 223 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ |
945d9c75 EC |
224 | } while (/*CONSTCOND*/0) |
225 | ||
226 | #define QTAILQ_INSERT_AFTER_RCU(head, listelm, elm, field) do { \ | |
227 | (elm)->field.tqe_next = (listelm)->field.tqe_next; \ | |
228 | if ((elm)->field.tqe_next != NULL) { \ | |
7274f01b PB |
229 | (elm)->field.tqe_next->field.tqe_circ.tql_prev = \ |
230 | &(elm)->field.tqe_circ; \ | |
945d9c75 | 231 | } else { \ |
7274f01b | 232 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ |
945d9c75 | 233 | } \ |
d73415a3 | 234 | qatomic_rcu_set(&(listelm)->field.tqe_next, (elm)); \ |
7274f01b | 235 | (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \ |
945d9c75 EC |
236 | } while (/*CONSTCOND*/0) |
237 | ||
7274f01b PB |
238 | #define QTAILQ_INSERT_BEFORE_RCU(listelm, elm, field) do { \ |
239 | (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev; \ | |
240 | (elm)->field.tqe_next = (listelm); \ | |
d73415a3 | 241 | qatomic_rcu_set(&(listelm)->field.tqe_circ.tql_prev->tql_next, (elm));\ |
7274f01b PB |
242 | (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \ |
243 | } while (/*CONSTCOND*/0) | |
945d9c75 EC |
244 | |
245 | #define QTAILQ_REMOVE_RCU(head, elm, field) do { \ | |
246 | if (((elm)->field.tqe_next) != NULL) { \ | |
7274f01b PB |
247 | (elm)->field.tqe_next->field.tqe_circ.tql_prev = \ |
248 | (elm)->field.tqe_circ.tql_prev; \ | |
945d9c75 | 249 | } else { \ |
7274f01b | 250 | (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \ |
945d9c75 | 251 | } \ |
d73415a3 SH |
252 | qatomic_set(&(elm)->field.tqe_circ.tql_prev->tql_next, \ |
253 | (elm)->field.tqe_next); \ | |
7274f01b | 254 | (elm)->field.tqe_circ.tql_prev = NULL; \ |
945d9c75 EC |
255 | } while (/*CONSTCOND*/0) |
256 | ||
257 | #define QTAILQ_FOREACH_RCU(var, head, field) \ | |
d73415a3 | 258 | for ((var) = qatomic_rcu_read(&(head)->tqh_first); \ |
945d9c75 | 259 | (var); \ |
d73415a3 | 260 | (var) = qatomic_rcu_read(&(var)->field.tqe_next)) |
945d9c75 EC |
261 | |
262 | #define QTAILQ_FOREACH_SAFE_RCU(var, head, field, next) \ | |
d73415a3 SH |
263 | for ((var) = qatomic_rcu_read(&(head)->tqh_first); \ |
264 | (var) && ((next) = qatomic_rcu_read(&(var)->field.tqe_next), 1);\ | |
945d9c75 EC |
265 | (var) = (next)) |
266 | ||
8c3570e3 PB |
267 | /* |
268 | * RCU singly-linked list | |
269 | */ | |
270 | ||
271 | /* Singly-linked list access methods */ | |
d73415a3 SH |
272 | #define QSLIST_EMPTY_RCU(head) (qatomic_read(&(head)->slh_first) == NULL) |
273 | #define QSLIST_FIRST_RCU(head) qatomic_rcu_read(&(head)->slh_first) | |
274 | #define QSLIST_NEXT_RCU(elm, field) qatomic_rcu_read(&(elm)->field.sle_next) | |
8c3570e3 PB |
275 | |
276 | /* Singly-linked list functions */ | |
277 | #define QSLIST_INSERT_HEAD_RCU(head, elm, field) do { \ | |
278 | (elm)->field.sle_next = (head)->slh_first; \ | |
d73415a3 | 279 | qatomic_rcu_set(&(head)->slh_first, (elm)); \ |
8c3570e3 PB |
280 | } while (/*CONSTCOND*/0) |
281 | ||
282 | #define QSLIST_INSERT_AFTER_RCU(head, listelm, elm, field) do { \ | |
283 | (elm)->field.sle_next = (listelm)->field.sle_next; \ | |
d73415a3 | 284 | qatomic_rcu_set(&(listelm)->field.sle_next, (elm)); \ |
8c3570e3 PB |
285 | } while (/*CONSTCOND*/0) |
286 | ||
287 | #define QSLIST_REMOVE_HEAD_RCU(head, field) do { \ | |
d73415a3 | 288 | qatomic_set(&(head)->slh_first, (head)->slh_first->field.sle_next);\ |
8c3570e3 PB |
289 | } while (/*CONSTCOND*/0) |
290 | ||
291 | #define QSLIST_REMOVE_RCU(head, elm, type, field) do { \ | |
292 | if ((head)->slh_first == (elm)) { \ | |
293 | QSLIST_REMOVE_HEAD_RCU((head), field); \ | |
294 | } else { \ | |
295 | struct type *curr = (head)->slh_first; \ | |
296 | while (curr->field.sle_next != (elm)) { \ | |
297 | curr = curr->field.sle_next; \ | |
298 | } \ | |
d73415a3 | 299 | qatomic_set(&curr->field.sle_next, \ |
8c3570e3 PB |
300 | curr->field.sle_next->field.sle_next); \ |
301 | } \ | |
302 | } while (/*CONSTCOND*/0) | |
303 | ||
304 | #define QSLIST_FOREACH_RCU(var, head, field) \ | |
d73415a3 SH |
305 | for ((var) = qatomic_rcu_read(&(head)->slh_first); \ |
306 | (var); \ | |
307 | (var) = qatomic_rcu_read(&(var)->field.sle_next)) | |
8c3570e3 | 308 | |
d73415a3 SH |
309 | #define QSLIST_FOREACH_SAFE_RCU(var, head, field, next) \ |
310 | for ((var) = qatomic_rcu_read(&(head)->slh_first); \ | |
311 | (var) && ((next) = qatomic_rcu_read(&(var)->field.sle_next), 1); \ | |
8c3570e3 PB |
312 | (var) = (next)) |
313 | ||
341774fe MD |
314 | #ifdef __cplusplus |
315 | } | |
316 | #endif | |
175de524 | 317 | #endif /* QEMU_RCU_QUEUE_H */ |