xref: /netbsd/lib/libpthread/pthread_queue.h (revision ce099b40)
1 /*	$NetBSD: pthread_queue.h,v 1.4 2008/04/28 20:23:01 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /* pthread_queue.h
33  * Definition of a queue interface for the pthread library.
34  * Style modeled on the sys/queue.h macros; implementation taken from
35  * the tail queue, with the added property of static initializability
36  * (and a corresponding extra cost in the _INSERT_TAIL() function.
37 */
38 
39 
40 
41 /*
42  * Queue definitions.
43  */
44 #define PTQ_HEAD(name, type)						\
45 struct name {								\
46 	struct type *ptqh_first;/* first element */			\
47 	struct type **ptqh_last;/* addr of last next element */		\
48 }
49 
50 #define PTQ_HEAD_INITIALIZER { NULL, NULL }
51 
52 
53 #define PTQ_ENTRY(type)						\
54 struct {								\
55 	struct type *ptqe_next;	/* next element */			\
56 	struct type **ptqe_prev;/* address of previous next element */	\
57 }
58 
59 /*
60  * Queue functions.
61  */
62 
63 #define	PTQ_INIT(head) do {						\
64 	(head)->ptqh_first = NULL;					\
65 	(head)->ptqh_last = &(head)->ptqh_first;			\
66 } while (/*CONSTCOND*/0)
67 
68 #define PTQ_INSERT_HEAD(head, elm, field) do {			\
69 	if (((elm)->field.ptqe_next = (head)->ptqh_first) != NULL)	\
70 		(head)->ptqh_first->field.ptqe_prev =			\
71 		    &(elm)->field.ptqe_next;				\
72 	else								\
73 		(head)->ptqh_last = &(elm)->field.ptqe_next;		\
74 	(head)->ptqh_first = (elm);					\
75 	(elm)->field.ptqe_prev = &(head)->ptqh_first;			\
76 } while (/*CONSTCOND*/0)
77 
78 #define PTQ_INSERT_TAIL(head, elm, field) do {				\
79 	(elm)->field.ptqe_next = NULL;					\
80 	if ((head)->ptqh_last == NULL)					\
81 		(head)->ptqh_last = &(head)->ptqh_first;		\
82 	(elm)->field.ptqe_prev = (head)->ptqh_last;			\
83 	*(head)->ptqh_last = (elm);					\
84 	(head)->ptqh_last = &(elm)->field.ptqe_next;			\
85 } while (/*CONSTCOND*/0)
86 
87 #define PTQ_INSERT_AFTER(head, listelm, elm, field) do {		\
88 	if (((elm)->field.ptqe_next = (listelm)->field.ptqe_next) != NULL)\
89 		(elm)->field.ptqe_next->field.ptqe_prev = 		\
90 		    &(elm)->field.ptqe_next;				\
91 	else								\
92 		(head)->ptqh_last = &(elm)->field.ptqe_next;		\
93 	(listelm)->field.ptqe_next = (elm);				\
94 	(elm)->field.ptqe_prev = &(listelm)->field.ptqe_next;		\
95 } while (/*CONSTCOND*/0)
96 
97 #define	PTQ_INSERT_BEFORE(listelm, elm, field) do {			\
98 	(elm)->field.ptqe_prev = (listelm)->field.ptqe_prev;		\
99 	(elm)->field.ptqe_next = (listelm);				\
100 	*(listelm)->field.ptqe_prev = (elm);				\
101 	(listelm)->field.ptqe_prev = &(elm)->field.ptqe_next;		\
102 } while (/*CONSTCOND*/0)
103 
104 #define PTQ_REMOVE(head, elm, field) do {				\
105 	if (((elm)->field.ptqe_next) != NULL)				\
106 		(elm)->field.ptqe_next->field.ptqe_prev = 		\
107 		    (elm)->field.ptqe_prev;				\
108 	else								\
109 		(head)->ptqh_last = (elm)->field.ptqe_prev;		\
110 	*(elm)->field.ptqe_prev = (elm)->field.ptqe_next;		\
111 } while (/*CONSTCOND*/0)
112 
113 /*
114  * Queue access methods.
115  */
116 #define	PTQ_EMPTY(head)		((head)->ptqh_first == NULL)
117 #define	PTQ_FIRST(head)		((head)->ptqh_first)
118 #define	PTQ_NEXT(elm, field)		((elm)->field.ptqe_next)
119 
120 #define PTQ_LAST(head, headname) \
121 	(*(((struct headname *)(void *)((head)->ptqh_last))->ptqh_last))
122 #define PTQ_PREV(elm, headname, field) \
123 	(*(((struct headname *)(void *)((elm)->field.ptqe_prev))->ptqh_last))
124 
125 #define PTQ_FOREACH(var, head, field)					\
126 	for ((var) = ((head)->ptqh_first);				\
127 		(var);							\
128 		(var) = ((var)->field.ptqe_next))
129 
130 #define PTQ_FOREACH_REVERSE(var, head, headname, field)		\
131 	for ((var) = (*(((struct headname *)(void *)((head)->ptqh_last))->ptqh_last));	\
132 		(var);							\
133 		(var) = (*(((struct headname *)(void *)((var)->field.ptqe_prev))->ptqh_last)))
134