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