pthread_queue.h revision 1.2 1 /* $NetBSD: pthread_queue.h,v 1.2 2003/01/18 10:34:16 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /* pthread_queue.h
40 * Definition of a queue interface for the pthread library.
41 * Style modeled on the sys/queue.h macros; implementation taken from
42 * the tail queue, with the added property of static initializability
43 * (and a corresponding extra cost in the _INSERT_TAIL() function.
44 */
45
46
47
48 /*
49 * Queue definitions.
50 */
51 #define PTQ_HEAD(name, type) \
52 struct name { \
53 struct type *ptqh_first;/* first element */ \
54 struct type **ptqh_last;/* addr of last next element */ \
55 }
56
57 #define PTQ_HEAD_INITIALIZER { NULL, NULL }
58
59
60 #define PTQ_ENTRY(type) \
61 struct { \
62 struct type *ptqe_next; /* next element */ \
63 struct type **ptqe_prev;/* address of previous next element */ \
64 }
65
66 /*
67 * Queue functions.
68 */
69
70 #define PTQ_INIT(head) do { \
71 (head)->ptqh_first = NULL; \
72 (head)->ptqh_last = &(head)->ptqh_first; \
73 } while (/*CONSTCOND*/0)
74
75 #define PTQ_INSERT_HEAD(head, elm, field) do { \
76 if (((elm)->field.ptqe_next = (head)->ptqh_first) != NULL) \
77 (head)->ptqh_first->field.ptqe_prev = \
78 &(elm)->field.ptqe_next; \
79 else \
80 (head)->ptqh_last = &(elm)->field.ptqe_next; \
81 (head)->ptqh_first = (elm); \
82 (elm)->field.ptqe_prev = &(head)->ptqh_first; \
83 } while (/*CONSTCOND*/0)
84
85 #define PTQ_INSERT_TAIL(head, elm, field) do { \
86 (elm)->field.ptqe_next = NULL; \
87 if ((head)->ptqh_last == NULL) \
88 (head)->ptqh_last = &(head)->ptqh_first; \
89 (elm)->field.ptqe_prev = (head)->ptqh_last; \
90 *(head)->ptqh_last = (elm); \
91 (head)->ptqh_last = &(elm)->field.ptqe_next; \
92 } while (/*CONSTCOND*/0)
93
94 #define PTQ_INSERT_AFTER(head, listelm, elm, field) do { \
95 if (((elm)->field.ptqe_next = (listelm)->field.ptqe_next) != NULL)\
96 (elm)->field.ptqe_next->field.ptqe_prev = \
97 &(elm)->field.ptqe_next; \
98 else \
99 (head)->ptqh_last = &(elm)->field.ptqe_next; \
100 (listelm)->field.ptqe_next = (elm); \
101 (elm)->field.ptqe_prev = &(listelm)->field.ptqe_next; \
102 } while (/*CONSTCOND*/0)
103
104 #define PTQ_INSERT_BEFORE(listelm, elm, field) do { \
105 (elm)->field.ptqe_prev = (listelm)->field.ptqe_prev; \
106 (elm)->field.ptqe_next = (listelm); \
107 *(listelm)->field.ptqe_prev = (elm); \
108 (listelm)->field.ptqe_prev = &(elm)->field.ptqe_next; \
109 } while (/*CONSTCOND*/0)
110
111 #define PTQ_REMOVE(head, elm, field) do { \
112 if (((elm)->field.ptqe_next) != NULL) \
113 (elm)->field.ptqe_next->field.ptqe_prev = \
114 (elm)->field.ptqe_prev; \
115 else \
116 (head)->ptqh_last = (elm)->field.ptqe_prev; \
117 *(elm)->field.ptqe_prev = (elm)->field.ptqe_next; \
118 } while (/*CONSTCOND*/0)
119
120 /*
121 * Queue access methods.
122 */
123 #define PTQ_EMPTY(head) ((head)->ptqh_first == NULL)
124 #define PTQ_FIRST(head) ((head)->ptqh_first)
125 #define PTQ_NEXT(elm, field) ((elm)->field.ptqe_next)
126
127 #define PTQ_LAST(head, headname) \
128 (*(((struct headname *)((head)->ptqh_last))->ptqh_last))
129 #define PTQ_PREV(elm, headname, field) \
130 (*(((struct headname *)((elm)->field.ptqe_prev))->ptqh_last))
131
132 #define PTQ_FOREACH(var, head, field) \
133 for ((var) = ((head)->ptqh_first); \
134 (var); \
135 (var) = ((var)->field.ptqe_next))
136
137 #define PTQ_FOREACH_REVERSE(var, head, headname, field) \
138 for ((var) = (*(((struct headname *)((head)->ptqh_last))->ptqh_last)); \
139 (var); \
140 (var) = (*(((struct headname *)((var)->field.ptqe_prev))->ptqh_last)))
141