pthread_types.h revision 1.14
1/*	$NetBSD: pthread_types.h,v 1.14 2015/06/26 01:33:08 pooka 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LIB_PTHREAD_TYPES_H
33#define _LIB_PTHREAD_TYPES_H
34
35/*
36 * We use the "pthread_spin_t" name internally; "pthread_spinlock_t" is the
37 * POSIX spinlock object.
38 */
39typedef __cpu_simple_lock_t	pthread_spin_t;
40
41/*
42 * Copied from PTQ_HEAD in pthread_queue.h
43 */
44#define _PTQ_HEAD(name, type)	       				\
45struct name {								\
46	struct type *ptqh_first;/* first element */			\
47	struct type **ptqh_last;/* addr of last next element */		\
48}
49
50_PTQ_HEAD(pthread_queue_struct_t, __pthread_st);
51typedef struct pthread_queue_struct_t pthread_queue_t;
52
53struct	__pthread_st;
54struct	__pthread_attr_st;
55struct	__pthread_mutex_st;
56struct	__pthread_mutexattr_st;
57struct	__pthread_cond_st;
58struct	__pthread_condattr_st;
59struct	__pthread_spin_st;
60struct	__pthread_rwlock_st;
61struct	__pthread_rwlockattr_st;
62struct	__pthread_barrier_st;
63struct	__pthread_barrierattr_st;
64
65typedef struct __pthread_st *pthread_t;
66typedef struct __pthread_attr_st pthread_attr_t;
67typedef struct __pthread_mutex_st pthread_mutex_t;
68typedef struct __pthread_mutexattr_st pthread_mutexattr_t;
69typedef struct __pthread_cond_st pthread_cond_t;
70typedef struct __pthread_condattr_st pthread_condattr_t;
71typedef struct __pthread_once_st pthread_once_t;
72typedef struct __pthread_spinlock_st pthread_spinlock_t;
73typedef struct __pthread_rwlock_st pthread_rwlock_t;
74typedef struct __pthread_rwlockattr_st pthread_rwlockattr_t;
75typedef struct __pthread_barrier_st pthread_barrier_t;
76typedef struct __pthread_barrierattr_st pthread_barrierattr_t;
77typedef int pthread_key_t;
78
79struct	__pthread_attr_st {
80	unsigned int	pta_magic;
81
82	int	pta_flags;
83	void	*pta_private;
84};
85
86/*
87 * C++ (namely libc++) expects to be using PTHREAD_FOO_INITIALIZER as a
88 * member initializer. This does not work for volatile types. Since C++
89 * does not touch the guts of those types, we redefine them as non-volatile
90 */
91#ifdef __cplusplus
92# ifdef __CPU_SIMPLE_LOCK_PAD
93#  define __pthread_spin_t unsigned char
94# else
95#  define __pthread_spin_t unsigned int
96# endif
97# define __pthread_volatile
98#else /* __cplusplus */
99# define __pthread_spin_t pthread_spin_t
100# define __pthread_volatile volatile
101#endif /* __cplusplus */
102
103/*
104 * ptm_owner is the actual lock field which is locked via CAS operation.
105 * This structure's layout is designed to compatible with the previous
106 * version used in SA pthreads.
107 */
108#ifdef __CPU_SIMPLE_LOCK_PAD
109/*
110 * If __SIMPLE_UNLOCKED != 0 and we have to pad, we have to worry about
111 * endianness.  Currently that isn't an issue but put in a check in case
112 * something changes in the future.
113 */
114#if __SIMPLELOCK_UNLOCKED != 0
115#error __CPU_SIMPLE_LOCK_PAD incompatible with __SIMPLELOCK_UNLOCKED == 0
116#endif
117#endif
118struct	__pthread_mutex_st {
119	unsigned int	ptm_magic;
120	__pthread_spin_t ptm_errorcheck;
121#ifdef __CPU_SIMPLE_LOCK_PAD
122	uint8_t		ptm_pad1[3];
123#endif
124	__pthread_spin_t ptm_interlock;	/* unused - backwards compat */
125#ifdef __CPU_SIMPLE_LOCK_PAD
126	uint8_t		ptm_pad2[3];
127#endif
128	__pthread_volatile pthread_t ptm_owner;
129	pthread_t * __pthread_volatile ptm_waiters;
130	unsigned int	ptm_recursed;
131	void		*ptm_spare2;	/* unused - backwards compat */
132};
133
134#define	_PT_MUTEX_MAGIC	0x33330003
135#define	_PT_MUTEX_DEAD	0xDEAD0003
136
137#ifdef __CPU_SIMPLE_LOCK_PAD
138#define _PTHREAD_MUTEX_INITIALIZER { _PT_MUTEX_MAGIC, 			\
139				    __SIMPLELOCK_UNLOCKED, { 0, 0, 0 },	\
140				    __SIMPLELOCK_UNLOCKED, { 0, 0, 0 },	\
141				    NULL, NULL, 0, NULL			\
142				  }
143#else
144#define _PTHREAD_MUTEX_INITIALIZER { _PT_MUTEX_MAGIC, 			\
145				    __SIMPLELOCK_UNLOCKED,		\
146				    __SIMPLELOCK_UNLOCKED,		\
147				    NULL, NULL, 0, NULL			\
148				  }
149#endif /* __CPU_SIMPLE_LOCK_PAD */
150
151
152struct	__pthread_mutexattr_st {
153	unsigned int	ptma_magic;
154	void	*ptma_private;
155};
156
157#define _PT_MUTEXATTR_MAGIC	0x44440004
158#define _PT_MUTEXATTR_DEAD	0xDEAD0004
159
160
161struct	__pthread_cond_st {
162	unsigned int	ptc_magic;
163
164	/* Protects the queue of waiters */
165	__pthread_spin_t ptc_lock;
166	pthread_queue_t	ptc_waiters;
167
168	pthread_mutex_t	*ptc_mutex;	/* Current mutex */
169	void	*ptc_private;
170};
171
172#define	_PT_COND_MAGIC	0x55550005
173#define	_PT_COND_DEAD	0xDEAD0005
174
175#define _PTHREAD_COND_INITIALIZER { _PT_COND_MAGIC,			\
176				   __SIMPLELOCK_UNLOCKED,		\
177				   {NULL, NULL},			\
178				   NULL,				\
179				   NULL  				\
180				 }
181
182struct	__pthread_condattr_st {
183	unsigned int	ptca_magic;
184	void	*ptca_private;
185};
186
187#define	_PT_CONDATTR_MAGIC	0x66660006
188#define	_PT_CONDATTR_DEAD	0xDEAD0006
189
190struct	__pthread_once_st {
191	pthread_mutex_t	pto_mutex;
192	int	pto_done;
193};
194
195#define _PTHREAD_ONCE_INIT	{ PTHREAD_MUTEX_INITIALIZER, 0 }
196
197struct	__pthread_spinlock_st {
198	unsigned int	pts_magic;
199	__pthread_spin_t pts_spin;
200	int		pts_flags;
201};
202
203#define	_PT_SPINLOCK_MAGIC	0x77770007
204#define	_PT_SPINLOCK_DEAD	0xDEAD0007
205#define _PT_SPINLOCK_PSHARED	0x00000001
206
207/* PTHREAD_SPINLOCK_INITIALIZER is an extension not specified by POSIX. */
208#define _PTHREAD_SPINLOCK_INITIALIZER { _PT_SPINLOCK_MAGIC,		\
209				       __SIMPLELOCK_UNLOCKED,		\
210				       0				\
211				     }
212
213struct	__pthread_rwlock_st {
214	unsigned int	ptr_magic;
215
216	/* Protects data below */
217	__pthread_spin_t ptr_interlock;
218
219	pthread_queue_t	ptr_rblocked;
220	pthread_queue_t	ptr_wblocked;
221	unsigned int	ptr_nreaders;
222	__pthread_volatile pthread_t ptr_owner;
223	void	*ptr_private;
224};
225
226#define	_PT_RWLOCK_MAGIC	0x99990009
227#define	_PT_RWLOCK_DEAD		0xDEAD0009
228
229#define _PTHREAD_RWLOCK_INITIALIZER { _PT_RWLOCK_MAGIC,			\
230				     __SIMPLELOCK_UNLOCKED,		\
231				     {NULL, NULL},			\
232				     {NULL, NULL},			\
233				     0,					\
234				     NULL,				\
235				     NULL,				\
236				   }
237
238struct	__pthread_rwlockattr_st {
239	unsigned int	ptra_magic;
240	void *ptra_private;
241};
242
243#define _PT_RWLOCKATTR_MAGIC	0x99990909
244#define _PT_RWLOCKATTR_DEAD	0xDEAD0909
245
246struct	__pthread_barrier_st {
247	unsigned int	ptb_magic;
248
249	/* Protects data below */
250	pthread_spin_t	ptb_lock;
251
252	pthread_queue_t	ptb_waiters;
253	unsigned int	ptb_initcount;
254	unsigned int	ptb_curcount;
255	unsigned int	ptb_generation;
256
257	void		*ptb_private;
258};
259
260#define	_PT_BARRIER_MAGIC	0x88880008
261#define	_PT_BARRIER_DEAD	0xDEAD0008
262
263struct	__pthread_barrierattr_st {
264	unsigned int	ptba_magic;
265	void		*ptba_private;
266};
267
268#define	_PT_BARRIERATTR_MAGIC	0x88880808
269#define	_PT_BARRIERATTR_DEAD	0xDEAD0808
270
271#endif	/* _LIB_PTHREAD_TYPES_H */
272