tests/iothread: Always connect iothread GSource to a GMainContext
[qemu/ar7.git] / include / qemu / queue.h
blob4764d93ea3fc822b17e20847d1e56b7bdd2775fa
1 /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
3 /*
4 * QEMU version: Copy from netbsd, removed debug code, removed some of
5 * the implementations. Left in singly-linked lists, lists, simple
6 * queues, and tail queues.
7 */
9 /*
10 * Copyright (c) 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)queue.h 8.5 (Berkeley) 8/20/94
40 #ifndef QEMU_SYS_QUEUE_H
41 #define QEMU_SYS_QUEUE_H
44 * This file defines four types of data structures: singly-linked lists,
45 * lists, simple queues, and tail queues.
47 * A singly-linked list is headed by a single forward pointer. The
48 * elements are singly linked for minimum space and pointer manipulation
49 * overhead at the expense of O(n) removal for arbitrary elements. New
50 * elements can be added to the list after an existing element or at the
51 * head of the list. Elements being removed from the head of the list
52 * should use the explicit macro for this purpose for optimum
53 * efficiency. A singly-linked list may only be traversed in the forward
54 * direction. Singly-linked lists are ideal for applications with large
55 * datasets and few or no removals or for implementing a LIFO queue.
57 * A list is headed by a single forward pointer (or an array of forward
58 * pointers for a hash table header). The elements are doubly linked
59 * so that an arbitrary element can be removed without a need to
60 * traverse the list. New elements can be added to the list before
61 * or after an existing element or at the head of the list. A list
62 * may only be traversed in the forward direction.
64 * A simple queue is headed by a pair of pointers, one the head of the
65 * list and the other to the tail of the list. The elements are singly
66 * linked to save space, so elements can only be removed from the
67 * head of the list. New elements can be added to the list after
68 * an existing element, at the head of the list, or at the end of the
69 * list. A simple queue may only be traversed in the forward direction.
71 * A tail queue is headed by a pair of pointers, one to the head of the
72 * list and the other to the tail of the list. The elements are doubly
73 * linked so that an arbitrary element can be removed without a need to
74 * traverse the list. New elements can be added to the list before or
75 * after an existing element, at the head of the list, or at the end of
76 * the list. A tail queue may be traversed in either direction.
78 * For details on the use of these macros, see the queue(3) manual page.
82 * List definitions.
84 #define QLIST_HEAD(name, type) \
85 struct name { \
86 struct type *lh_first; /* first element */ \
89 #define QLIST_HEAD_INITIALIZER(head) \
90 { NULL }
92 #define QLIST_ENTRY(type) \
93 struct { \
94 struct type *le_next; /* next element */ \
95 struct type **le_prev; /* address of previous next element */ \
99 * List functions.
101 #define QLIST_INIT(head) do { \
102 (head)->lh_first = NULL; \
103 } while (/*CONSTCOND*/0)
105 #define QLIST_SWAP(dstlist, srclist, field) do { \
106 void *tmplist; \
107 tmplist = (srclist)->lh_first; \
108 (srclist)->lh_first = (dstlist)->lh_first; \
109 if ((srclist)->lh_first != NULL) { \
110 (srclist)->lh_first->field.le_prev = &(srclist)->lh_first; \
112 (dstlist)->lh_first = tmplist; \
113 if ((dstlist)->lh_first != NULL) { \
114 (dstlist)->lh_first->field.le_prev = &(dstlist)->lh_first; \
116 } while (/*CONSTCOND*/0)
118 #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
119 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
120 (listelm)->field.le_next->field.le_prev = \
121 &(elm)->field.le_next; \
122 (listelm)->field.le_next = (elm); \
123 (elm)->field.le_prev = &(listelm)->field.le_next; \
124 } while (/*CONSTCOND*/0)
126 #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
127 (elm)->field.le_prev = (listelm)->field.le_prev; \
128 (elm)->field.le_next = (listelm); \
129 *(listelm)->field.le_prev = (elm); \
130 (listelm)->field.le_prev = &(elm)->field.le_next; \
131 } while (/*CONSTCOND*/0)
133 #define QLIST_INSERT_HEAD(head, elm, field) do { \
134 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
135 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
136 (head)->lh_first = (elm); \
137 (elm)->field.le_prev = &(head)->lh_first; \
138 } while (/*CONSTCOND*/0)
140 #define QLIST_REMOVE(elm, field) do { \
141 if ((elm)->field.le_next != NULL) \
142 (elm)->field.le_next->field.le_prev = \
143 (elm)->field.le_prev; \
144 *(elm)->field.le_prev = (elm)->field.le_next; \
145 } while (/*CONSTCOND*/0)
147 #define QLIST_FOREACH(var, head, field) \
148 for ((var) = ((head)->lh_first); \
149 (var); \
150 (var) = ((var)->field.le_next))
152 #define QLIST_FOREACH_SAFE(var, head, field, next_var) \
153 for ((var) = ((head)->lh_first); \
154 (var) && ((next_var) = ((var)->field.le_next), 1); \
155 (var) = (next_var))
158 * List access methods.
160 #define QLIST_EMPTY(head) ((head)->lh_first == NULL)
161 #define QLIST_FIRST(head) ((head)->lh_first)
162 #define QLIST_NEXT(elm, field) ((elm)->field.le_next)
166 * Singly-linked List definitions.
168 #define QSLIST_HEAD(name, type) \
169 struct name { \
170 struct type *slh_first; /* first element */ \
173 #define QSLIST_HEAD_INITIALIZER(head) \
174 { NULL }
176 #define QSLIST_ENTRY(type) \
177 struct { \
178 struct type *sle_next; /* next element */ \
182 * Singly-linked List functions.
184 #define QSLIST_INIT(head) do { \
185 (head)->slh_first = NULL; \
186 } while (/*CONSTCOND*/0)
188 #define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \
189 (elm)->field.sle_next = (slistelm)->field.sle_next; \
190 (slistelm)->field.sle_next = (elm); \
191 } while (/*CONSTCOND*/0)
193 #define QSLIST_INSERT_HEAD(head, elm, field) do { \
194 (elm)->field.sle_next = (head)->slh_first; \
195 (head)->slh_first = (elm); \
196 } while (/*CONSTCOND*/0)
198 #define QSLIST_INSERT_HEAD_ATOMIC(head, elm, field) do { \
199 typeof(elm) save_sle_next; \
200 do { \
201 save_sle_next = (elm)->field.sle_next = (head)->slh_first; \
202 } while (atomic_cmpxchg(&(head)->slh_first, save_sle_next, (elm)) != \
203 save_sle_next); \
204 } while (/*CONSTCOND*/0)
206 #define QSLIST_MOVE_ATOMIC(dest, src) do { \
207 (dest)->slh_first = atomic_xchg(&(src)->slh_first, NULL); \
208 } while (/*CONSTCOND*/0)
210 #define QSLIST_REMOVE_HEAD(head, field) do { \
211 (head)->slh_first = (head)->slh_first->field.sle_next; \
212 } while (/*CONSTCOND*/0)
214 #define QSLIST_REMOVE_AFTER(slistelm, field) do { \
215 (slistelm)->field.sle_next = \
216 QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \
217 } while (/*CONSTCOND*/0)
219 #define QSLIST_FOREACH(var, head, field) \
220 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
222 #define QSLIST_FOREACH_SAFE(var, head, field, tvar) \
223 for ((var) = QSLIST_FIRST((head)); \
224 (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \
225 (var) = (tvar))
228 * Singly-linked List access methods.
230 #define QSLIST_EMPTY(head) ((head)->slh_first == NULL)
231 #define QSLIST_FIRST(head) ((head)->slh_first)
232 #define QSLIST_NEXT(elm, field) ((elm)->field.sle_next)
236 * Simple queue definitions.
238 #define QSIMPLEQ_HEAD(name, type) \
239 struct name { \
240 struct type *sqh_first; /* first element */ \
241 struct type **sqh_last; /* addr of last next element */ \
244 #define QSIMPLEQ_HEAD_INITIALIZER(head) \
245 { NULL, &(head).sqh_first }
247 #define QSIMPLEQ_ENTRY(type) \
248 struct { \
249 struct type *sqe_next; /* next element */ \
253 * Simple queue functions.
255 #define QSIMPLEQ_INIT(head) do { \
256 (head)->sqh_first = NULL; \
257 (head)->sqh_last = &(head)->sqh_first; \
258 } while (/*CONSTCOND*/0)
260 #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
261 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
262 (head)->sqh_last = &(elm)->field.sqe_next; \
263 (head)->sqh_first = (elm); \
264 } while (/*CONSTCOND*/0)
266 #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
267 (elm)->field.sqe_next = NULL; \
268 *(head)->sqh_last = (elm); \
269 (head)->sqh_last = &(elm)->field.sqe_next; \
270 } while (/*CONSTCOND*/0)
272 #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
273 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
274 (head)->sqh_last = &(elm)->field.sqe_next; \
275 (listelm)->field.sqe_next = (elm); \
276 } while (/*CONSTCOND*/0)
278 #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
279 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
280 (head)->sqh_last = &(head)->sqh_first; \
281 } while (/*CONSTCOND*/0)
283 #define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do { \
284 QSIMPLEQ_INIT(removed); \
285 if (((removed)->sqh_first = (head)->sqh_first) != NULL) { \
286 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) { \
287 (head)->sqh_last = &(head)->sqh_first; \
289 (removed)->sqh_last = &(elm)->field.sqe_next; \
290 (elm)->field.sqe_next = NULL; \
292 } while (/*CONSTCOND*/0)
294 #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
295 if ((head)->sqh_first == (elm)) { \
296 QSIMPLEQ_REMOVE_HEAD((head), field); \
297 } else { \
298 struct type *curelm = (head)->sqh_first; \
299 while (curelm->field.sqe_next != (elm)) \
300 curelm = curelm->field.sqe_next; \
301 if ((curelm->field.sqe_next = \
302 curelm->field.sqe_next->field.sqe_next) == NULL) \
303 (head)->sqh_last = &(curelm)->field.sqe_next; \
305 } while (/*CONSTCOND*/0)
307 #define QSIMPLEQ_FOREACH(var, head, field) \
308 for ((var) = ((head)->sqh_first); \
309 (var); \
310 (var) = ((var)->field.sqe_next))
312 #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
313 for ((var) = ((head)->sqh_first); \
314 (var) && ((next = ((var)->field.sqe_next)), 1); \
315 (var) = (next))
317 #define QSIMPLEQ_CONCAT(head1, head2) do { \
318 if (!QSIMPLEQ_EMPTY((head2))) { \
319 *(head1)->sqh_last = (head2)->sqh_first; \
320 (head1)->sqh_last = (head2)->sqh_last; \
321 QSIMPLEQ_INIT((head2)); \
323 } while (/*CONSTCOND*/0)
325 #define QSIMPLEQ_PREPEND(head1, head2) do { \
326 if (!QSIMPLEQ_EMPTY((head2))) { \
327 *(head2)->sqh_last = (head1)->sqh_first; \
328 (head1)->sqh_first = (head2)->sqh_first; \
329 QSIMPLEQ_INIT((head2)); \
331 } while (/*CONSTCOND*/0)
333 #define QSIMPLEQ_LAST(head, type, field) \
334 (QSIMPLEQ_EMPTY((head)) ? \
335 NULL : \
336 ((struct type *)(void *) \
337 ((char *)((head)->sqh_last) - offsetof(struct type, field))))
340 * Simple queue access methods.
342 #define QSIMPLEQ_EMPTY_ATOMIC(head) (atomic_read(&((head)->sqh_first)) == NULL)
343 #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
344 #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
345 #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
347 typedef struct QTailQLink {
348 void *tql_next;
349 struct QTailQLink *tql_prev;
350 } QTailQLink;
353 * Tail queue definitions. The union acts as a poor man template, as if
354 * it were QTailQLink<type>.
356 #define QTAILQ_HEAD(name, type) \
357 union name { \
358 struct type *tqh_first; /* first element */ \
359 QTailQLink tqh_circ; /* link for circular backwards list */ \
362 #define QTAILQ_HEAD_INITIALIZER(head) \
363 { .tqh_circ = { NULL, &(head).tqh_circ } }
365 #define QTAILQ_ENTRY(type) \
366 union { \
367 struct type *tqe_next; /* next element */ \
368 QTailQLink tqe_circ; /* link for circular backwards list */ \
372 * Tail queue functions.
374 #define QTAILQ_INIT(head) do { \
375 (head)->tqh_first = NULL; \
376 (head)->tqh_circ.tql_prev = &(head)->tqh_circ; \
377 } while (/*CONSTCOND*/0)
379 #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
380 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
381 (head)->tqh_first->field.tqe_circ.tql_prev = \
382 &(elm)->field.tqe_circ; \
383 else \
384 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
385 (head)->tqh_first = (elm); \
386 (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \
387 } while (/*CONSTCOND*/0)
389 #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
390 (elm)->field.tqe_next = NULL; \
391 (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev; \
392 (head)->tqh_circ.tql_prev->tql_next = (elm); \
393 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
394 } while (/*CONSTCOND*/0)
396 #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
397 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
398 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
399 &(elm)->field.tqe_circ; \
400 else \
401 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
402 (listelm)->field.tqe_next = (elm); \
403 (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \
404 } while (/*CONSTCOND*/0)
406 #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
407 (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev; \
408 (elm)->field.tqe_next = (listelm); \
409 (listelm)->field.tqe_circ.tql_prev->tql_next = (elm); \
410 (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \
411 } while (/*CONSTCOND*/0)
413 #define QTAILQ_REMOVE(head, elm, field) do { \
414 if (((elm)->field.tqe_next) != NULL) \
415 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
416 (elm)->field.tqe_circ.tql_prev; \
417 else \
418 (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
419 (elm)->field.tqe_circ.tql_prev->tql_next = (elm)->field.tqe_next; \
420 (elm)->field.tqe_circ.tql_prev = NULL; \
421 } while (/*CONSTCOND*/0)
423 /* remove @left, @right and all elements in between from @head */
424 #define QTAILQ_REMOVE_SEVERAL(head, left, right, field) do { \
425 if (((right)->field.tqe_next) != NULL) \
426 (right)->field.tqe_next->field.tqe_circ.tql_prev = \
427 (left)->field.tqe_circ.tql_prev; \
428 else \
429 (head)->tqh_circ.tql_prev = (left)->field.tqe_circ.tql_prev; \
430 (left)->field.tqe_circ.tql_prev->tql_next = (right)->field.tqe_next; \
431 } while (/*CONSTCOND*/0)
433 #define QTAILQ_FOREACH(var, head, field) \
434 for ((var) = ((head)->tqh_first); \
435 (var); \
436 (var) = ((var)->field.tqe_next))
438 #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
439 for ((var) = ((head)->tqh_first); \
440 (var) && ((next_var) = ((var)->field.tqe_next), 1); \
441 (var) = (next_var))
443 #define QTAILQ_FOREACH_REVERSE(var, head, field) \
444 for ((var) = QTAILQ_LAST(head); \
445 (var); \
446 (var) = QTAILQ_PREV(var, field))
448 #define QTAILQ_FOREACH_REVERSE_SAFE(var, head, field, prev_var) \
449 for ((var) = QTAILQ_LAST(head); \
450 (var) && ((prev_var) = QTAILQ_PREV(var, field), 1); \
451 (var) = (prev_var))
454 * Tail queue access methods.
456 #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
457 #define QTAILQ_FIRST(head) ((head)->tqh_first)
458 #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
459 #define QTAILQ_IN_USE(elm, field) ((elm)->field.tqe_circ.tql_prev != NULL)
461 #define QTAILQ_LINK_PREV(link) \
462 ((link).tql_prev->tql_prev->tql_next)
463 #define QTAILQ_LAST(head) \
464 ((typeof((head)->tqh_first)) QTAILQ_LINK_PREV((head)->tqh_circ))
465 #define QTAILQ_PREV(elm, field) \
466 ((typeof((elm)->field.tqe_next)) QTAILQ_LINK_PREV((elm)->field.tqe_circ))
468 #define field_at_offset(base, offset, type) \
469 ((type *) (((char *) (base)) + (offset)))
472 * Raw access of elements of a tail queue head. Offsets are all zero
473 * because it's a union.
475 #define QTAILQ_RAW_FIRST(head) \
476 field_at_offset(head, 0, void *)
477 #define QTAILQ_RAW_TQH_CIRC(head) \
478 field_at_offset(head, 0, QTailQLink)
481 * Raw access of elements of a tail entry
483 #define QTAILQ_RAW_NEXT(elm, entry) \
484 field_at_offset(elm, entry, void *)
485 #define QTAILQ_RAW_TQE_CIRC(elm, entry) \
486 field_at_offset(elm, entry, QTailQLink)
488 * Tail queue traversal using pointer arithmetic.
490 #define QTAILQ_RAW_FOREACH(elm, head, entry) \
491 for ((elm) = *QTAILQ_RAW_FIRST(head); \
492 (elm); \
493 (elm) = *QTAILQ_RAW_NEXT(elm, entry))
495 * Tail queue insertion using pointer arithmetic.
497 #define QTAILQ_RAW_INSERT_TAIL(head, elm, entry) do { \
498 *QTAILQ_RAW_NEXT(elm, entry) = NULL; \
499 QTAILQ_RAW_TQE_CIRC(elm, entry)->tql_prev = QTAILQ_RAW_TQH_CIRC(head)->tql_prev; \
500 QTAILQ_RAW_TQH_CIRC(head)->tql_prev->tql_next = (elm); \
501 QTAILQ_RAW_TQH_CIRC(head)->tql_prev = QTAILQ_RAW_TQE_CIRC(elm, entry); \
502 } while (/*CONSTCOND*/0)
504 #endif /* QEMU_SYS_QUEUE_H */