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_prev = &(elm)->field.tqe_next; \
210 } else { \
211 (head)->tqh_last = &(elm)->field.tqe_next; \
212 } \
213 atomic_rcu_set(&(head)->tqh_first, (elm)); \
214 (elm)->field.tqe_prev = &(head)->tqh_first; \
215} while (0)
216
217#define QTAILQ_INSERT_TAIL_RCU(head, elm, field) do { \
218 (elm)->field.tqe_next = NULL; \
219 (elm)->field.tqe_prev = (head)->tqh_last; \
220 atomic_rcu_set((head)->tqh_last, (elm)); \
221 (head)->tqh_last = &(elm)->field.tqe_next; \
222} while (0)
223
224#define QTAILQ_INSERT_AFTER_RCU(head, listelm, elm, field) do { \
225 (elm)->field.tqe_next = (listelm)->field.tqe_next; \
226 if ((elm)->field.tqe_next != NULL) { \
227 (elm)->field.tqe_next->field.tqe_prev = &(elm)->field.tqe_next; \
228 } else { \
229 (head)->tqh_last = &(elm)->field.tqe_next; \
230 } \
231 atomic_rcu_set(&(listelm)->field.tqe_next, (elm)); \
232 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
233} while (0)
234
235#define QTAILQ_INSERT_BEFORE_RCU(listelm, elm, field) do { \
236 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
237 (elm)->field.tqe_next = (listelm); \
238 atomic_rcu_set((listelm)->field.tqe_prev, (elm)); \
239 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
240 } while (0)
241
242#define QTAILQ_REMOVE_RCU(head, elm, field) do { \
243 if (((elm)->field.tqe_next) != NULL) { \
244 (elm)->field.tqe_next->field.tqe_prev = (elm)->field.tqe_prev; \
245 } else { \
246 (head)->tqh_last = (elm)->field.tqe_prev; \
247 } \
248 atomic_set((elm)->field.tqe_prev, (elm)->field.tqe_next); \
249 (elm)->field.tqe_prev = NULL; \
250} while (0)
251
252#define QTAILQ_FOREACH_RCU(var, head, field) \
253 for ((var) = atomic_rcu_read(&(head)->tqh_first); \
254 (var); \
255 (var) = atomic_rcu_read(&(var)->field.tqe_next))
256
257#define QTAILQ_FOREACH_SAFE_RCU(var, head, field, next) \
258 for ((var) = atomic_rcu_read(&(head)->tqh_first); \
259 (var) && ((next) = atomic_rcu_read(&(var)->field.tqe_next), 1); \
260 (var) = (next))
261
262#ifdef __cplusplus
263}
264#endif
265#endif
266