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