xref: /netbsd-src/lib/libpthread/pthread_types.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: pthread_types.h,v 1.5 2003/09/26 22:48:23 nathanw 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_t, __pthread_st);
58 
59 struct	__pthread_st;
60 struct	__pthread_attr_st;
61 struct	__pthread_mutex_st;
62 struct	__pthread_mutexattr_st;
63 struct	__pthread_cond_st;
64 struct	__pthread_condattr_st;
65 struct	__pthread_spin_st;
66 struct	__pthread_rwlock_st;
67 struct	__pthread_rwlockattr_st;
68 struct	__pthread_barrier_st;
69 struct	__pthread_barrierattr_st;
70 
71 typedef struct __pthread_st *pthread_t;
72 typedef struct __pthread_attr_st pthread_attr_t;
73 typedef struct __pthread_mutex_st pthread_mutex_t;
74 typedef struct __pthread_mutexattr_st pthread_mutexattr_t;
75 typedef struct __pthread_cond_st pthread_cond_t;
76 typedef struct __pthread_condattr_st pthread_condattr_t;
77 typedef struct __pthread_once_st pthread_once_t;
78 typedef struct __pthread_spinlock_st pthread_spinlock_t;
79 typedef struct __pthread_rwlock_st pthread_rwlock_t;
80 typedef struct __pthread_rwlockattr_st pthread_rwlockattr_t;
81 typedef struct __pthread_barrier_st pthread_barrier_t;
82 typedef struct __pthread_barrierattr_st pthread_barrierattr_t;
83 typedef int pthread_key_t;
84 
85 struct	__pthread_attr_st {
86 	unsigned int	pta_magic;
87 
88 	int	pta_flags;
89 	void	*pta_private;
90 };
91 
92 struct	__pthread_mutex_st {
93 	unsigned int	ptm_magic;
94 
95 	/* Not a real spinlock; will never be spun on. Locked with
96 	 * pthread__simple_lock_try() or not at all. Therefore, not
97 	 * subject to preempted-spinlock-continuation.
98 	 *
99 	 * Open research question: Would it help threaded applications if
100 	 * preempted-lock-continuation were applied to mutexes?
101 	 */
102 	pthread_spin_t	ptm_lock;
103 
104 	/* Protects the owner and blocked queue */
105 	pthread_spin_t	ptm_interlock;
106 	pthread_t	ptm_owner;
107 	struct pthread_queue_t	ptm_blocked;
108 	void	*ptm_private;
109 };
110 
111 #define	_PT_MUTEX_MAGIC	0x33330003
112 #define	_PT_MUTEX_DEAD	0xDEAD0003
113 
114 #define _PTHREAD_MUTEX_INITIALIZER { _PT_MUTEX_MAGIC, 			\
115 				    __SIMPLELOCK_UNLOCKED,		\
116 				    __SIMPLELOCK_UNLOCKED,		\
117 				    NULL,				\
118 				    {NULL, NULL},			\
119 				    NULL				\
120 				  }
121 
122 
123 struct	__pthread_mutexattr_st {
124 	unsigned int	ptma_magic;
125 	void	*ptma_private;
126 };
127 
128 #define _PT_MUTEXATTR_MAGIC	0x44440004
129 #define _PT_MUTEXATTR_DEAD	0xDEAD0004
130 
131 
132 struct	__pthread_cond_st {
133 	unsigned int	ptc_magic;
134 
135 	/* Protects the queue of waiters */
136 	pthread_spin_t	ptc_lock;
137 	struct pthread_queue_t	ptc_waiters;
138 
139 	pthread_mutex_t	*ptc_mutex;	/* Current mutex */
140 	void	*ptc_private;
141 };
142 
143 #define	_PT_COND_MAGIC	0x55550005
144 #define	_PT_COND_DEAD	0xDEAD0005
145 
146 #define _PTHREAD_COND_INITIALIZER { _PT_COND_MAGIC,			\
147 				   __SIMPLELOCK_UNLOCKED,		\
148 				   {NULL, NULL},			\
149 				   NULL,				\
150 				   NULL  				\
151 				 }
152 
153 struct	__pthread_condattr_st {
154 	unsigned int	ptca_magic;
155 	void	*ptca_private;
156 };
157 
158 #define	_PT_CONDATTR_MAGIC	0x66660006
159 #define	_PT_CONDATTR_DEAD	0xDEAD0006
160 
161 struct	__pthread_once_st {
162 	pthread_mutex_t	pto_mutex;
163 	int	pto_done;
164 };
165 
166 #define _PTHREAD_ONCE_INIT	{ PTHREAD_MUTEX_INITIALIZER, 0 }
167 
168 struct	__pthread_spinlock_st {
169 	unsigned int	pts_magic;
170 	pthread_spin_t	pts_spin;
171 	int		pts_flags;
172 };
173 
174 #define	_PT_SPINLOCK_MAGIC	0x77770007
175 #define	_PT_SPINLOCK_DEAD	0xDEAD0007
176 #define _PT_SPINLOCK_PSHARED	0x00000001
177 
178 /* PTHREAD_SPINLOCK_INITIALIZER is an extension not specified by POSIX. */
179 #define _PTHREAD_SPINLOCK_INITIALIZER { _PT_SPINLOCK_MAGIC,		\
180 				       __SIMPLELOCK_UNLOCKED,		\
181 				       0				\
182 				     }
183 
184 struct	__pthread_rwlock_st {
185 	unsigned int	ptr_magic;
186 
187 	/* Protects data below */
188 	pthread_spin_t	ptr_interlock;
189 
190 	struct pthread_queue_t	ptr_rblocked;
191 	struct pthread_queue_t	ptr_wblocked;
192 	unsigned int	ptr_nreaders;
193 	pthread_t	ptr_writer;
194 	void	*ptr_private;
195 };
196 
197 #define	_PT_RWLOCK_MAGIC	0x99990009
198 #define	_PT_RWLOCK_DEAD		0xDEAD0009
199 
200 #define _PTHREAD_RWLOCK_INITIALIZER { _PT_RWLOCK_MAGIC,			\
201 				     __SIMPLELOCK_UNLOCKED,		\
202 				     {NULL, NULL},			\
203 				     {NULL, NULL},			\
204 				     0,					\
205 				     NULL,				\
206 				     NULL,				\
207 				   }
208 
209 struct	__pthread_rwlockattr_st {
210 	unsigned int	ptra_magic;
211 	void *ptra_private;
212 };
213 
214 #define _PT_RWLOCKATTR_MAGIC	0x99990909
215 #define _PT_RWLOCKATTR_DEAD	0xDEAD0909
216 
217 struct	__pthread_barrier_st {
218 	unsigned int	ptb_magic;
219 
220 	/* Protects data below */
221 	pthread_spin_t	ptb_lock;
222 
223 	struct pthread_queue_t	ptb_waiters;
224 	unsigned int	ptb_initcount;
225 	unsigned int	ptb_curcount;
226 	unsigned int	ptb_generation;
227 
228 	void		*ptb_private;
229 };
230 
231 #define	_PT_BARRIER_MAGIC	0x88880008
232 #define	_PT_BARRIER_DEAD	0xDEAD0008
233 
234 struct	__pthread_barrierattr_st {
235 	unsigned int	ptba_magic;
236 	void		*ptba_private;
237 };
238 
239 #define	_PT_BARRIERATTR_MAGIC	0x88880808
240 #define	_PT_BARRIERATTR_DEAD	0xDEAD0808
241 
242 #endif	/* _LIB_PTHREAD_TYPES_H */
243