xref: /netbsd-src/lib/libpthread/pthread_types.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: pthread_types.h,v 1.8 2007/09/07 14:09:28 ad 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 #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 	pthread_spin_t	ptm_lock;
100 	pthread_spin_t	ptm_interlock;
101 	volatile pthread_t ptm_owner;
102 	pthread_queue_t	ptm_blocked;
103 	void		*ptm_private;
104 };
105 
106 #define	_PT_MUTEX_MAGIC	0x33330003
107 #define	_PT_MUTEX_DEAD	0xDEAD0003
108 
109 #define _PTHREAD_MUTEX_INITIALIZER { _PT_MUTEX_MAGIC, 			\
110 				    __SIMPLELOCK_UNLOCKED,		\
111 				    __SIMPLELOCK_UNLOCKED,		\
112 				    NULL,				\
113 				    {NULL, NULL},			\
114 				    NULL				\
115 				  }
116 
117 
118 struct	__pthread_mutexattr_st {
119 	unsigned int	ptma_magic;
120 	void	*ptma_private;
121 };
122 
123 #define _PT_MUTEXATTR_MAGIC	0x44440004
124 #define _PT_MUTEXATTR_DEAD	0xDEAD0004
125 
126 
127 struct	__pthread_cond_st {
128 	unsigned int	ptc_magic;
129 
130 	/* Protects the queue of waiters */
131 	pthread_spin_t	ptc_lock;
132 	pthread_queue_t	ptc_waiters;
133 
134 	pthread_mutex_t	*ptc_mutex;	/* Current mutex */
135 	void	*ptc_private;
136 };
137 
138 #define	_PT_COND_MAGIC	0x55550005
139 #define	_PT_COND_DEAD	0xDEAD0005
140 
141 #define _PTHREAD_COND_INITIALIZER { _PT_COND_MAGIC,			\
142 				   __SIMPLELOCK_UNLOCKED,		\
143 				   {NULL, NULL},			\
144 				   NULL,				\
145 				   NULL  				\
146 				 }
147 
148 struct	__pthread_condattr_st {
149 	unsigned int	ptca_magic;
150 	void	*ptca_private;
151 };
152 
153 #define	_PT_CONDATTR_MAGIC	0x66660006
154 #define	_PT_CONDATTR_DEAD	0xDEAD0006
155 
156 struct	__pthread_once_st {
157 	pthread_mutex_t	pto_mutex;
158 	int	pto_done;
159 };
160 
161 #define _PTHREAD_ONCE_INIT	{ PTHREAD_MUTEX_INITIALIZER, 0 }
162 
163 struct	__pthread_spinlock_st {
164 	unsigned int	pts_magic;
165 	pthread_spin_t	pts_spin;
166 	int		pts_flags;
167 };
168 
169 #define	_PT_SPINLOCK_MAGIC	0x77770007
170 #define	_PT_SPINLOCK_DEAD	0xDEAD0007
171 #define _PT_SPINLOCK_PSHARED	0x00000001
172 
173 /* PTHREAD_SPINLOCK_INITIALIZER is an extension not specified by POSIX. */
174 #define _PTHREAD_SPINLOCK_INITIALIZER { _PT_SPINLOCK_MAGIC,		\
175 				       __SIMPLELOCK_UNLOCKED,		\
176 				       0				\
177 				     }
178 
179 struct	__pthread_rwlock_st {
180 	unsigned int	ptr_magic;
181 
182 	/* Protects data below */
183 	pthread_spin_t	ptr_interlock;
184 
185 	pthread_queue_t	ptr_rblocked;
186 	pthread_queue_t	ptr_wblocked;
187 	unsigned int	ptr_nreaders;
188 	volatile pthread_t ptr_writer;
189 	void	*ptr_private;
190 };
191 
192 #define	_PT_RWLOCK_MAGIC	0x99990009
193 #define	_PT_RWLOCK_DEAD		0xDEAD0009
194 
195 #define _PTHREAD_RWLOCK_INITIALIZER { _PT_RWLOCK_MAGIC,			\
196 				     __SIMPLELOCK_UNLOCKED,		\
197 				     {NULL, NULL},			\
198 				     {NULL, NULL},			\
199 				     0,					\
200 				     NULL,				\
201 				     NULL,				\
202 				   }
203 
204 struct	__pthread_rwlockattr_st {
205 	unsigned int	ptra_magic;
206 	void *ptra_private;
207 };
208 
209 #define _PT_RWLOCKATTR_MAGIC	0x99990909
210 #define _PT_RWLOCKATTR_DEAD	0xDEAD0909
211 
212 struct	__pthread_barrier_st {
213 	unsigned int	ptb_magic;
214 
215 	/* Protects data below */
216 	pthread_spin_t	ptb_lock;
217 
218 	pthread_queue_t	ptb_waiters;
219 	unsigned int	ptb_initcount;
220 	unsigned int	ptb_curcount;
221 	unsigned int	ptb_generation;
222 
223 	void		*ptb_private;
224 };
225 
226 #define	_PT_BARRIER_MAGIC	0x88880008
227 #define	_PT_BARRIER_DEAD	0xDEAD0008
228 
229 struct	__pthread_barrierattr_st {
230 	unsigned int	ptba_magic;
231 	void		*ptba_private;
232 };
233 
234 #define	_PT_BARRIERATTR_MAGIC	0x88880808
235 #define	_PT_BARRIERATTR_DEAD	0xDEAD0808
236 
237 #endif	/* _LIB_PTHREAD_TYPES_H */
238