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) (atomic_read(&(head)->lh_first) == NULL)
40#define QLIST_FIRST_RCU(head) (atomic_rcu_read(&(head)->lh_first))
41#define QLIST_NEXT_RCU(elm, field) (atomic_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 atomic_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 atomic_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 atomic_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 atomic_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) = atomic_rcu_read(&(head)->lh_first); \
121 (var); \
122 (var) = atomic_rcu_read(&(var)->field.le_next))
123
124
125#define QLIST_FOREACH_SAFE_RCU(var, head, field, next_var) \
126 for ((var) = (atomic_rcu_read(&(head)->lh_first)); \
127 (var) && \
128 ((next_var) = atomic_rcu_read(&(var)->field.le_next), 1); \
129 (var) = (next_var))
130
131
132
133
134
135
136#define QSIMPLEQ_EMPTY_RCU(head) (atomic_read(&(head)->sqh_first) == NULL)
137#define QSIMPLEQ_FIRST_RCU(head) atomic_rcu_read(&(head)->sqh_first)
138#define QSIMPLEQ_NEXT_RCU(elm, field) atomic_rcu_read(&(elm)->field.sqe_next)
139
140
141#define QSIMPLEQ_INSERT_HEAD_RCU(head, elm, field) do { \
142 (elm)->field.sqe_next = (head)->sqh_first; \
143 if ((elm)->field.sqe_next == NULL) { \
144 (head)->sqh_last = &(elm)->field.sqe_next; \
145 } \
146 atomic_rcu_set(&(head)->sqh_first, (elm)); \
147} while (0)
148
149#define QSIMPLEQ_INSERT_TAIL_RCU(head, elm, field) do { \
150 (elm)->field.sqe_next = NULL; \
151 atomic_rcu_set((head)->sqh_last, (elm)); \
152 (head)->sqh_last = &(elm)->field.sqe_next; \
153} while (0)
154
155#define QSIMPLEQ_INSERT_AFTER_RCU(head, listelm, elm, field) do { \
156 (elm)->field.sqe_next = (listelm)->field.sqe_next; \
157 if ((elm)->field.sqe_next == NULL) { \
158 (head)->sqh_last = &(elm)->field.sqe_next; \
159 } \
160 atomic_rcu_set(&(listelm)->field.sqe_next, (elm)); \
161} while (0)
162
163#define QSIMPLEQ_REMOVE_HEAD_RCU(head, field) do { \
164 atomic_set(&(head)->sqh_first, (head)->sqh_first->field.sqe_next); \
165 if ((head)->sqh_first == NULL) { \
166 (head)->sqh_last = &(head)->sqh_first; \
167 } \
168} while (0)
169
170#define QSIMPLEQ_REMOVE_RCU(head, elm, type, field) do { \
171 if ((head)->sqh_first == (elm)) { \
172 QSIMPLEQ_REMOVE_HEAD_RCU((head), field); \
173 } else { \
174 struct type *curr = (head)->sqh_first; \
175 while (curr->field.sqe_next != (elm)) { \
176 curr = curr->field.sqe_next; \
177 } \
178 atomic_set(&curr->field.sqe_next, \
179 curr->field.sqe_next->field.sqe_next); \
180 if (curr->field.sqe_next == NULL) { \
181 (head)->sqh_last = &(curr)->field.sqe_next; \
182 } \
183 } \
184} while (0)
185
186#define QSIMPLEQ_FOREACH_RCU(var, head, field) \
187 for ((var) = atomic_rcu_read(&(head)->sqh_first); \
188 (var); \
189 (var) = atomic_rcu_read(&(var)->field.sqe_next))
190
191#define QSIMPLEQ_FOREACH_SAFE_RCU(var, head, field, next) \
192 for ((var) = atomic_rcu_read(&(head)->sqh_first); \
193 (var) && ((next) = atomic_rcu_read(&(var)->field.sqe_next), 1); \
194 (var) = (next))
195
196
197
198
199
200
201#define QTAILQ_EMPTY_RCU(head) (atomic_read(&(head)->tqh_first) == NULL)
202#define QTAILQ_FIRST_RCU(head) atomic_rcu_read(&(head)->tqh_first)
203#define QTAILQ_NEXT_RCU(elm, field) atomic_rcu_read(&(elm)->field.tqe_next)
204
205
206#define QTAILQ_INSERT_HEAD_RCU(head, elm, field) do { \
207 (elm)->field.tqe_next = (head)->tqh_first; \
208 if ((elm)->field.tqe_next != NULL) { \
209 (head)->tqh_first->field.tqe_circ.tql_prev = \
210 &(elm)->field.tqe_circ; \
211 } else { \
212 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
213 } \
214 atomic_rcu_set(&(head)->tqh_first, (elm)); \
215 (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \
216} while (0)
217
218#define QTAILQ_INSERT_TAIL_RCU(head, elm, field) do { \
219 (elm)->field.tqe_next = NULL; \
220 (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev; \
221 atomic_rcu_set(&(head)->tqh_circ.tql_prev->tql_next, (elm)); \
222 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
223} while (0)
224
225#define QTAILQ_INSERT_AFTER_RCU(head, listelm, elm, field) do { \
226 (elm)->field.tqe_next = (listelm)->field.tqe_next; \
227 if ((elm)->field.tqe_next != NULL) { \
228 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
229 &(elm)->field.tqe_circ; \
230 } else { \
231 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
232 } \
233 atomic_rcu_set(&(listelm)->field.tqe_next, (elm)); \
234 (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \
235} while (0)
236
237#define QTAILQ_INSERT_BEFORE_RCU(listelm, elm, field) do { \
238 (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev; \
239 (elm)->field.tqe_next = (listelm); \
240 atomic_rcu_set(&(listelm)->field.tqe_circ.tql_prev->tql_next, (elm)); \
241 (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \
242} while (0)
243
244#define QTAILQ_REMOVE_RCU(head, elm, field) do { \
245 if (((elm)->field.tqe_next) != NULL) { \
246 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
247 (elm)->field.tqe_circ.tql_prev; \
248 } else { \
249 (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
250 } \
251 atomic_set(&(elm)->field.tqe_circ.tql_prev->tql_next, (elm)->field.tqe_next); \
252 (elm)->field.tqe_circ.tql_prev = NULL; \
253} while (0)
254
255#define QTAILQ_FOREACH_RCU(var, head, field) \
256 for ((var) = atomic_rcu_read(&(head)->tqh_first); \
257 (var); \
258 (var) = atomic_rcu_read(&(var)->field.tqe_next))
259
260#define QTAILQ_FOREACH_SAFE_RCU(var, head, field, next) \
261 for ((var) = atomic_rcu_read(&(head)->tqh_first); \
262 (var) && ((next) = atomic_rcu_read(&(var)->field.tqe_next), 1); \
263 (var) = (next))
264
265#ifdef __cplusplus
266}
267#endif
268#endif
269