aboutsummaryrefslogtreecommitdiff
path: root/include/qemu/rcu_queue.h
blob: 4e6298d47307fd8dbcc1d13f3297ea8ae5bcdbcd (plain)
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#ifndef QEMU_RCU_QUEUE_H
#define QEMU_RCU_QUEUE_H

/*
 * rcu_queue.h
 *
 * RCU-friendly versions of the queue.h primitives.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Copyright (c) 2013 Mike D. Day, IBM Corporation.
 *
 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
 */

#include "qemu/queue.h"
#include "qemu/atomic.h"

/*
 * List access methods.
 */
#define QLIST_EMPTY_RCU(head) (qatomic_read(&(head)->lh_first) == NULL)
#define QLIST_FIRST_RCU(head) (qatomic_rcu_read(&(head)->lh_first))
#define QLIST_NEXT_RCU(elm, field) (qatomic_rcu_read(&(elm)->field.le_next))

/*
 * List functions.
 */


/*
 *  The difference between qatomic_read/set and qatomic_rcu_read/set
 *  is in the including of a read/write memory barrier to the volatile
 *  access. atomic_rcu_* macros include the memory barrier, the
 *  plain atomic macros do not. Therefore, it should be correct to
 *  issue a series of reads or writes to the same element using only
 *  the atomic_* macro, until the last read or write, which should be
 *  atomic_rcu_* to introduce a read or write memory barrier as
 *  appropriate.
 */

/* Upon publication of the listelm->next value, list readers
 * will see the new node when following next pointers from
 * antecedent nodes, but may not see the new node when following
 * prev pointers from subsequent nodes until after the RCU grace
 * period expires.
 * see linux/include/rculist.h __list_add_rcu(new, prev, next)
 */
#define QLIST_INSERT_AFTER_RCU(listelm, elm, field) do {    \
    (elm)->field.le_next = (listelm)->field.le_next;        \
    (elm)->field.le_prev = &(listelm)->field.le_next;       \
    qatomic_rcu_set(&(listelm)->field.le_next, (elm));      \
    if ((elm)->field.le_next != NULL) {                     \
       (elm)->field.le_next->field.le_prev =                \
        &(elm)->field.le_next;                              \
    }                                                       \
} while (/*CONSTCOND*/0)

/* Upon publication of the listelm->prev->next value, list
 * readers will see the new element when following prev pointers
 * from subsequent elements, but may not see the new element
 * when following next pointers from antecedent elements
 * until after the RCU grace period expires.
 */
#define QLIST_INSERT_BEFORE_RCU(listelm, elm, field) do {   \
    (elm)->field.le_prev = (listelm)->field.le_prev;        \
    (elm)->field.le_next = (listelm);                       \
    qatomic_rcu_set((listelm)->field.le_prev, (elm));       \
    (listelm)->field.le_prev = &(elm)->field.le_next;       \
} while (/*CONSTCOND*/0)

/* Upon publication of the head->first value, list readers
 * will see the new element when following the head, but may
 * not see the new element when following prev pointers from
 * subsequent elements until after the RCU grace period has
 * expired.
 */
#define QLIST_INSERT_HEAD_RCU(head, elm, field) do {    \
    (elm)->field.le_prev = &(head)->lh_first;           \
    (elm)->field.le_next = (head)->lh_first;            \
    qatomic_rcu_set((&(head)->lh_first), (elm));        \
    if ((elm)->field.le_next != NULL) {                 \
       (elm)->field.le_next->field.le_prev =            \
        &(elm)->field.le_next;                          \
    }                                                   \
} while (/*CONSTCOND*/0)


/* prior to publication of the elm->prev->next value, some list
 * readers may still see the removed element when following
 * the antecedent's next pointer.
 */
#define QLIST_REMOVE_RCU(elm, field) do {           \
    if ((elm)->field.le_next != NULL) {             \
       (elm)->field.le_next->field.le_prev =        \
        (elm)->field.le_prev;                       \
    }                                               \
    qatomic_set((elm)->field.le_prev, (elm)->field.le_next); \
} while (/*CONSTCOND*/0)

/* List traversal must occur within an RCU critical section.  */
#define QLIST_FOREACH_RCU(var, head, field)                 \
        for ((var) = qatomic_rcu_read(&(head)->lh_first);   \
                (var);                                      \
                (var) = qatomic_rcu_read(&(var)->field.le_next))

/* List traversal must occur within an RCU critical section.  */
#define QLIST_FOREACH_SAFE_RCU(var, head, field, next_var)           \
    for ((var) = (qatomic_rcu_read(&(head)->lh_first));              \
      (var) &&                                                       \
          ((next_var) = qatomic_rcu_read(&(var)->field.le_next), 1); \
           (var) = (next_var))

/*
 * RCU simple queue
 */

/* Simple queue access methods */
#define QSIMPLEQ_EMPTY_RCU(head) \
    (qatomic_read(&(head)->sqh_first) == NULL)
#define QSIMPLEQ_FIRST_RCU(head)       qatomic_rcu_read(&(head)->sqh_first)
#define QSIMPLEQ_NEXT_RCU(elm, field)  qatomic_rcu_read(&(elm)->field.sqe_next)

/* Simple queue functions */
#define QSIMPLEQ_INSERT_HEAD_RCU(head, elm, field) do {         \
    (elm)->field.sqe_next = (head)->sqh_first;                  \
    if ((elm)->field.sqe_next == NULL) {                        \
        (head)->sqh_last = &(elm)->field.sqe_next;              \
    }                                                           \
    qatomic_rcu_set(&(head)->sqh_first, (elm));                 \
} while (/*CONSTCOND*/0)

#define QSIMPLEQ_INSERT_TAIL_RCU(head, elm, field) do {    \
    (elm)->field.sqe_next = NULL;                          \
    qatomic_rcu_set((head)->sqh_last, (elm));              \
    (head)->sqh_last = &(elm)->field.sqe_next;             \
} while (/*CONSTCOND*/0)

#define QSIMPLEQ_INSERT_AFTER_RCU(head, listelm, elm, field) do {       \
    (elm)->field.sqe_next = (listelm)->field.sqe_next;                  \
    if ((elm)->field.sqe_next == NULL) {                                \
        (head)->sqh_last = &(elm)->field.sqe_next;                      \
    }                                                                   \
    qatomic_rcu_set(&(listelm)->field.sqe_next, (elm));                 \
} while (/*CONSTCOND*/0)

#define QSIMPLEQ_REMOVE_HEAD_RCU(head, field) do {                     \
    qatomic_set(&(head)->sqh_first, (head)->sqh_first->field.sqe_next);\
    if ((head)->sqh_first == NULL) {                                   \
        (head)->sqh_last = &(head)->sqh_first;                         \
    }                                                                  \
} while (/*CONSTCOND*/0)

#define QSIMPLEQ_REMOVE_RCU(head, elm, type, field) do {            \
    if ((head)->sqh_first == (elm)) {                               \
        QSIMPLEQ_REMOVE_HEAD_RCU((head), field);                    \
    } else {                                                        \
        struct type *curr = (head)->sqh_first;                      \
        while (curr->field.sqe_next != (elm)) {                     \
            curr = curr->field.sqe_next;                            \
        }                                                           \
        qatomic_set(&curr->field.sqe_next,                          \
                   curr->field.sqe_next->field.sqe_next);           \
        if (curr->field.sqe_next == NULL) {                         \
            (head)->sqh_last = &(curr)->field.sqe_next;             \
        }                                                           \
    }                                                               \
} while (/*CONSTCOND*/0)

#define QSIMPLEQ_FOREACH_RCU(var, head, field)                          \
    for ((var) = qatomic_rcu_read(&(head)->sqh_first);                  \
         (var);                                                         \
         (var) = qatomic_rcu_read(&(var)->field.sqe_next))

#define QSIMPLEQ_FOREACH_SAFE_RCU(var, head, field, next)                \
    for ((var) = qatomic_rcu_read(&(head)->sqh_first);                   \
         (var) && ((next) = qatomic_rcu_read(&(var)->field.sqe_next), 1);\
         (var) = (next))

/*
 * RCU tail queue
 */

/* Tail queue access methods */
#define QTAILQ_EMPTY_RCU(head)      (qatomic_read(&(head)->tqh_first) == NULL)
#define QTAILQ_FIRST_RCU(head)       qatomic_rcu_read(&(head)->tqh_first)
#define QTAILQ_NEXT_RCU(elm, field)  qatomic_rcu_read(&(elm)->field.tqe_next)

/* Tail queue functions */
#define QTAILQ_INSERT_HEAD_RCU(head, elm, field) do {                   \
    (elm)->field.tqe_next = (head)->tqh_first;                          \
    if ((elm)->field.tqe_next != NULL) {                                \
        (head)->tqh_first->field.tqe_circ.tql_prev =                    \
            &(elm)->field.tqe_circ;                                     \
    } else {                                                            \
        (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ;             \
    }                                                                   \
    qatomic_rcu_set(&(head)->tqh_first, (elm));                         \
    (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ;                 \
} while (/*CONSTCOND*/0)

#define QTAILQ_INSERT_TAIL_RCU(head, elm, field) do {                   \
    (elm)->field.tqe_next = NULL;                                       \
    (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev;         \
    qatomic_rcu_set(&(head)->tqh_circ.tql_prev->tql_next, (elm));       \
    (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ;                 \
} while (/*CONSTCOND*/0)

#define QTAILQ_INSERT_AFTER_RCU(head, listelm, elm, field) do {         \
    (elm)->field.tqe_next = (listelm)->field.tqe_next;                  \
    if ((elm)->field.tqe_next != NULL) {                                \
        (elm)->field.tqe_next->field.tqe_circ.tql_prev =                \
            &(elm)->field.tqe_circ;                                     \
    } else {                                                            \
        (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ;             \
    }                                                                   \
    qatomic_rcu_set(&(listelm)->field.tqe_next, (elm));                 \
    (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ;        \
} while (/*CONSTCOND*/0)

#define QTAILQ_INSERT_BEFORE_RCU(listelm, elm, field) do {                \
    (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev;  \
    (elm)->field.tqe_next = (listelm);                                    \
    qatomic_rcu_set(&(listelm)->field.tqe_circ.tql_prev->tql_next, (elm));\
    (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ;          \
} while (/*CONSTCOND*/0)

#define QTAILQ_REMOVE_RCU(head, elm, field) do {                        \
    if (((elm)->field.tqe_next) != NULL) {                              \
        (elm)->field.tqe_next->field.tqe_circ.tql_prev =                \
            (elm)->field.tqe_circ.tql_prev;                             \
    } else {                                                            \
        (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev;     \
    }                                                                   \
    qatomic_set(&(elm)->field.tqe_circ.tql_prev->tql_next,              \
                (elm)->field.tqe_next);                                 \
    (elm)->field.tqe_circ.tql_prev = NULL;                              \
} while (/*CONSTCOND*/0)

#define QTAILQ_FOREACH_RCU(var, head, field)                            \
    for ((var) = qatomic_rcu_read(&(head)->tqh_first);                  \
         (var);                                                         \
         (var) = qatomic_rcu_read(&(var)->field.tqe_next))

#define QTAILQ_FOREACH_SAFE_RCU(var, head, field, next)                  \
    for ((var) = qatomic_rcu_read(&(head)->tqh_first);                   \
         (var) && ((next) = qatomic_rcu_read(&(var)->field.tqe_next), 1);\
         (var) = (next))

/*
 * RCU singly-linked list
 */

/* Singly-linked list access methods */
#define QSLIST_EMPTY_RCU(head)      (qatomic_read(&(head)->slh_first) == NULL)
#define QSLIST_FIRST_RCU(head)       qatomic_rcu_read(&(head)->slh_first)
#define QSLIST_NEXT_RCU(elm, field)  qatomic_rcu_read(&(elm)->field.sle_next)

/* Singly-linked list functions */
#define QSLIST_INSERT_HEAD_RCU(head, elm, field) do {           \
    (elm)->field.sle_next = (head)->slh_first;                  \
    qatomic_rcu_set(&(head)->slh_first, (elm));                 \
} while (/*CONSTCOND*/0)

#define QSLIST_INSERT_AFTER_RCU(head, listelm, elm, field) do {         \
    (elm)->field.sle_next = (listelm)->field.sle_next;                  \
    qatomic_rcu_set(&(listelm)->field.sle_next, (elm));                 \
} while (/*CONSTCOND*/0)

#define QSLIST_REMOVE_HEAD_RCU(head, field) do {                       \
    qatomic_set(&(head)->slh_first, (head)->slh_first->field.sle_next);\
} while (/*CONSTCOND*/0)

#define QSLIST_REMOVE_RCU(head, elm, type, field) do {              \
    if ((head)->slh_first == (elm)) {                               \
        QSLIST_REMOVE_HEAD_RCU((head), field);                      \
    } else {                                                        \
        struct type *curr = (head)->slh_first;                      \
        while (curr->field.sle_next != (elm)) {                     \
            curr = curr->field.sle_next;                            \
        }                                                           \
        qatomic_set(&curr->field.sle_next,                          \
                   curr->field.sle_next->field.sle_next);           \
    }                                                               \
} while (/*CONSTCOND*/0)

#define QSLIST_FOREACH_RCU(var, head, field)                          \
    for ((var) = qatomic_rcu_read(&(head)->slh_first);                \
         (var);                                                       \
         (var) = qatomic_rcu_read(&(var)->field.sle_next))

#define QSLIST_FOREACH_SAFE_RCU(var, head, field, next)                   \
    for ((var) = qatomic_rcu_read(&(head)->slh_first);                    \
         (var) && ((next) = qatomic_rcu_read(&(var)->field.sle_next), 1); \
         (var) = (next))

#endif /* QEMU_RCU_QUEUE_H */