xref: /netbsd-src/lib/libpthread/pthread_types.h (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: pthread_types.h,v 1.9 2008/02/14 21:40:51 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2008 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 #ifndef _LIB_PTHREAD_TYPES_H
40 #define _LIB_PTHREAD_TYPES_H
41 
42 /*
43  * We use the "pthread_spin_t" name internally; "pthread_spinlock_t" is the
44  * POSIX spinlock object.
45  */
46 typedef __cpu_simple_lock_t	pthread_spin_t;
47 
48 /*
49  * Copied from PTQ_HEAD in pthread_queue.h
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 _PTQ_HEAD(pthread_queue_struct_t, __pthread_st);
58 typedef struct pthread_queue_struct_t pthread_queue_t;
59 
60 struct	__pthread_st;
61 struct	__pthread_attr_st;
62 struct	__pthread_mutex_st;
63 struct	__pthread_mutexattr_st;
64 struct	__pthread_cond_st;
65 struct	__pthread_condattr_st;
66 struct	__pthread_spin_st;
67 struct	__pthread_rwlock_st;
68 struct	__pthread_rwlockattr_st;
69 struct	__pthread_barrier_st;
70 struct	__pthread_barrierattr_st;
71 
72 typedef struct __pthread_st *pthread_t;
73 typedef struct __pthread_attr_st pthread_attr_t;
74 typedef struct __pthread_mutex_st pthread_mutex_t;
75 typedef struct __pthread_mutexattr_st pthread_mutexattr_t;
76 typedef struct __pthread_cond_st pthread_cond_t;
77 typedef struct __pthread_condattr_st pthread_condattr_t;
78 typedef struct __pthread_once_st pthread_once_t;
79 typedef struct __pthread_spinlock_st pthread_spinlock_t;
80 typedef struct __pthread_rwlock_st pthread_rwlock_t;
81 typedef struct __pthread_rwlockattr_st pthread_rwlockattr_t;
82 typedef struct __pthread_barrier_st pthread_barrier_t;
83 typedef struct __pthread_barrierattr_st pthread_barrierattr_t;
84 typedef int pthread_key_t;
85 
86 struct	__pthread_attr_st {
87 	unsigned int	pta_magic;
88 
89 	int	pta_flags;
90 	void	*pta_private;
91 };
92 
93 /*
94  * ptm_lock will never be spun on: it's locked with
95  * pthread__simple_lock_try() or not at all.
96  */
97 struct	__pthread_mutex_st {
98 	unsigned int	ptm_magic;
99 	unsigned int	ptm_errorcheck;
100 	unsigned int	ptm_recursed;
101 	pthread_t * volatile ptm_waiters;
102 	volatile pthread_t ptm_owner;
103 };
104 
105 #define	_PT_MUTEX_MAGIC	0x33330003
106 #define	_PT_MUTEX_DEAD	0xDEAD0003
107 
108 #define _PTHREAD_MUTEX_INITIALIZER { _PT_MUTEX_MAGIC, 0, 0, NULL, NULL }
109 
110 struct	__pthread_mutexattr_st {
111 	unsigned int	ptma_magic;
112 	void	*ptma_private;
113 };
114 
115 #define _PT_MUTEXATTR_MAGIC	0x44440004
116 #define _PT_MUTEXATTR_DEAD	0xDEAD0004
117 
118 
119 struct	__pthread_cond_st {
120 	unsigned int	ptc_magic;
121 
122 	/* Protects the queue of waiters */
123 	pthread_spin_t	ptc_lock;
124 	pthread_queue_t	ptc_waiters;
125 
126 	pthread_mutex_t	*ptc_mutex;	/* Current mutex */
127 	void	*ptc_private;
128 };
129 
130 #define	_PT_COND_MAGIC	0x55550005
131 #define	_PT_COND_DEAD	0xDEAD0005
132 
133 #define _PTHREAD_COND_INITIALIZER { _PT_COND_MAGIC,			\
134 				   __SIMPLELOCK_UNLOCKED,		\
135 				   {NULL, NULL},			\
136 				   NULL,				\
137 				   NULL  				\
138 				 }
139 
140 struct	__pthread_condattr_st {
141 	unsigned int	ptca_magic;
142 	void	*ptca_private;
143 };
144 
145 #define	_PT_CONDATTR_MAGIC	0x66660006
146 #define	_PT_CONDATTR_DEAD	0xDEAD0006
147 
148 struct	__pthread_once_st {
149 	pthread_mutex_t	pto_mutex;
150 	int	pto_done;
151 };
152 
153 #define _PTHREAD_ONCE_INIT	{ PTHREAD_MUTEX_INITIALIZER, 0 }
154 
155 struct	__pthread_spinlock_st {
156 	unsigned int	pts_magic;
157 	pthread_spin_t	pts_spin;
158 	int		pts_flags;
159 };
160 
161 #define	_PT_SPINLOCK_MAGIC	0x77770007
162 #define	_PT_SPINLOCK_DEAD	0xDEAD0007
163 #define _PT_SPINLOCK_PSHARED	0x00000001
164 
165 /* PTHREAD_SPINLOCK_INITIALIZER is an extension not specified by POSIX. */
166 #define _PTHREAD_SPINLOCK_INITIALIZER { _PT_SPINLOCK_MAGIC,		\
167 				       __SIMPLELOCK_UNLOCKED,		\
168 				       0				\
169 				     }
170 
171 struct	__pthread_rwlock_st {
172 	unsigned int	ptr_magic;
173 
174 	/* Protects data below */
175 	pthread_spin_t	ptr_interlock;
176 
177 	pthread_queue_t	ptr_rblocked;
178 	pthread_queue_t	ptr_wblocked;
179 	unsigned int	ptr_nreaders;
180 	volatile pthread_t ptr_owner;
181 	void	*ptr_private;
182 };
183 
184 #define	_PT_RWLOCK_MAGIC	0x99990009
185 #define	_PT_RWLOCK_DEAD		0xDEAD0009
186 
187 #define _PTHREAD_RWLOCK_INITIALIZER { _PT_RWLOCK_MAGIC,			\
188 				     __SIMPLELOCK_UNLOCKED,		\
189 				     {NULL, NULL},			\
190 				     {NULL, NULL},			\
191 				     0,					\
192 				     NULL,				\
193 				     NULL,				\
194 				   }
195 
196 struct	__pthread_rwlockattr_st {
197 	unsigned int	ptra_magic;
198 	void *ptra_private;
199 };
200 
201 #define _PT_RWLOCKATTR_MAGIC	0x99990909
202 #define _PT_RWLOCKATTR_DEAD	0xDEAD0909
203 
204 struct	__pthread_barrier_st {
205 	unsigned int	ptb_magic;
206 
207 	/* Protects data below */
208 	pthread_spin_t	ptb_lock;
209 
210 	pthread_queue_t	ptb_waiters;
211 	unsigned int	ptb_initcount;
212 	unsigned int	ptb_curcount;
213 	unsigned int	ptb_generation;
214 
215 	void		*ptb_private;
216 };
217 
218 #define	_PT_BARRIER_MAGIC	0x88880008
219 #define	_PT_BARRIER_DEAD	0xDEAD0008
220 
221 struct	__pthread_barrierattr_st {
222 	unsigned int	ptba_magic;
223 	void		*ptba_private;
224 };
225 
226 #define	_PT_BARRIERATTR_MAGIC	0x88880808
227 #define	_PT_BARRIERATTR_DEAD	0xDEAD0808
228 
229 #endif	/* _LIB_PTHREAD_TYPES_H */
230