1#ifndef QEMU_RCU_QUEUE_H
2#define QEMU_RCU_QUEUE_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "qemu/queue.h"
29#include "qemu/atomic.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35
36
37
38
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))
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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; \
69 qatomic_rcu_set(&(listelm)->field.le_next, (elm)); \
70 if ((elm)->field.le_next != NULL) { \
71 (elm)->field.le_next->field.le_prev = \
72 &(elm)->field.le_next; \
73 } \
74} while (0)
75
76
77
78
79
80
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); \
85 qatomic_rcu_set((listelm)->field.le_prev, (elm)); \
86 (listelm)->field.le_prev = &(elm)->field.le_next; \
87} while (0)
88
89
90
91
92
93
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; \
98 qatomic_rcu_set((&(head)->lh_first), (elm)); \
99 if ((elm)->field.le_next != NULL) { \
100 (elm)->field.le_next->field.le_prev = \
101 &(elm)->field.le_next; \
102 } \
103} while (0)
104
105
106
107
108
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 } \
115 qatomic_set((elm)->field.le_prev, (elm)->field.le_next); \
116} while (0)
117
118
119#define QLIST_FOREACH_RCU(var, head, field) \
120 for ((var) = qatomic_rcu_read(&(head)->lh_first); \
121 (var); \
122 (var) = qatomic_rcu_read(&(var)->field.le_next))
123
124
125#define QLIST_FOREACH_SAFE_RCU(var, head, field, next_var) \
126 for ((var) = (qatomic_rcu_read(&(head)->lh_first)); \
127 (var) && \
128 ((next_var) = qatomic_rcu_read(&(var)->field.le_next), 1); \
129 (var) = (next_var))
130
131
132
133
134
135
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)
140
141
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 } \
147 qatomic_rcu_set(&(head)->sqh_first, (elm)); \
148} while (0)
149
150#define QSIMPLEQ_INSERT_TAIL_RCU(head, elm, field) do { \
151 (elm)->field.sqe_next = NULL; \
152 qatomic_rcu_set((head)->sqh_last, (elm)); \
153 (head)->sqh_last = &(elm)->field.sqe_next; \
154} while (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 } \
161 qatomic_rcu_set(&(listelm)->field.sqe_next, (elm)); \
162} while (0)
163
164#define QSIMPLEQ_REMOVE_HEAD_RCU(head, field) do { \
165 qatomic_set(&(head)->sqh_first, (head)->sqh_first->field.sqe_next);\
166 if ((head)->sqh_first == NULL) { \
167 (head)->sqh_last = &(head)->sqh_first; \
168 } \
169} while (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 } \
179 qatomic_set(&curr->field.sqe_next, \
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 (0)
186
187#define QSIMPLEQ_FOREACH_RCU(var, head, field) \
188 for ((var) = qatomic_rcu_read(&(head)->sqh_first); \
189 (var); \
190 (var) = qatomic_rcu_read(&(var)->field.sqe_next))
191
192#define QSIMPLEQ_FOREACH_SAFE_RCU(var, head, field, next) \
193 for ((var) = qatomic_rcu_read(&(head)->sqh_first); \
194 (var) && ((next) = qatomic_rcu_read(&(var)->field.sqe_next), 1);\
195 (var) = (next))
196
197
198
199
200
201
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)
205
206
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) { \
210 (head)->tqh_first->field.tqe_circ.tql_prev = \
211 &(elm)->field.tqe_circ; \
212 } else { \
213 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
214 } \
215 qatomic_rcu_set(&(head)->tqh_first, (elm)); \
216 (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \
217} while (0)
218
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; \
222 qatomic_rcu_set(&(head)->tqh_circ.tql_prev->tql_next, (elm)); \
223 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
224} while (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) { \
229 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
230 &(elm)->field.tqe_circ; \
231 } else { \
232 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
233 } \
234 qatomic_rcu_set(&(listelm)->field.tqe_next, (elm)); \
235 (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \
236} while (0)
237
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); \
241 qatomic_rcu_set(&(listelm)->field.tqe_circ.tql_prev->tql_next, (elm));\
242 (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \
243} while (0)
244
245#define QTAILQ_REMOVE_RCU(head, elm, field) do { \
246 if (((elm)->field.tqe_next) != NULL) { \
247 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
248 (elm)->field.tqe_circ.tql_prev; \
249 } else { \
250 (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
251 } \
252 qatomic_set(&(elm)->field.tqe_circ.tql_prev->tql_next, \
253 (elm)->field.tqe_next); \
254 (elm)->field.tqe_circ.tql_prev = NULL; \
255} while (0)
256
257#define QTAILQ_FOREACH_RCU(var, head, field) \
258 for ((var) = qatomic_rcu_read(&(head)->tqh_first); \
259 (var); \
260 (var) = qatomic_rcu_read(&(var)->field.tqe_next))
261
262#define QTAILQ_FOREACH_SAFE_RCU(var, head, field, next) \
263 for ((var) = qatomic_rcu_read(&(head)->tqh_first); \
264 (var) && ((next) = qatomic_rcu_read(&(var)->field.tqe_next), 1);\
265 (var) = (next))
266
267
268
269
270
271
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)
275
276
277#define QSLIST_INSERT_HEAD_RCU(head, elm, field) do { \
278 (elm)->field.sle_next = (head)->slh_first; \
279 qatomic_rcu_set(&(head)->slh_first, (elm)); \
280} while (0)
281
282#define QSLIST_INSERT_AFTER_RCU(head, listelm, elm, field) do { \
283 (elm)->field.sle_next = (listelm)->field.sle_next; \
284 qatomic_rcu_set(&(listelm)->field.sle_next, (elm)); \
285} while (0)
286
287#define QSLIST_REMOVE_HEAD_RCU(head, field) do { \
288 qatomic_set(&(head)->slh_first, (head)->slh_first->field.sle_next);\
289} while (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 } \
299 qatomic_set(&curr->field.sle_next, \
300 curr->field.sle_next->field.sle_next); \
301 } \
302} while (0)
303
304#define QSLIST_FOREACH_RCU(var, head, field) \
305 for ((var) = qatomic_rcu_read(&(head)->slh_first); \
306 (var); \
307 (var) = qatomic_rcu_read(&(var)->field.sle_next))
308
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); \
312 (var) = (next))
313
314#ifdef __cplusplus
315}
316#endif
317#endif
318