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