Home | History | Annotate | Line # | Download | only in libpthread
pthread_int.h revision 1.41
      1 /*	$NetBSD: pthread_int.h,v 1.41 2007/03/24 18:52:00 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2002, 2003, 2006, 2007 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 and Andrew Doran.
      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_INT_H
     40 #define _LIB_PTHREAD_INT_H
     41 
     42 #define PTHREAD__DEBUG
     43 #define ERRORCHECK
     44 
     45 #include "pthread_types.h"
     46 #include "pthread_queue.h"
     47 #include "pthread_debug.h"
     48 #include "pthread_md.h"
     49 
     50 #include <lwp.h>
     51 #include <signal.h>
     52 
     53 #define PTHREAD_KEYS_MAX 256
     54 /*
     55  * The size of this structure needs to be no larger than struct
     56  * __pthread_cleanup_store, defined in pthread.h.
     57  */
     58 struct pt_clean_t {
     59 	PTQ_ENTRY(pt_clean_t)	ptc_next;
     60 	void	(*ptc_cleanup)(void *);
     61 	void	*ptc_arg;
     62 };
     63 
     64 /* Private data for pthread_attr_t */
     65 struct pthread_attr_private {
     66 	char ptap_name[PTHREAD_MAX_NAMELEN_NP];
     67 	void *ptap_namearg;
     68 	void *ptap_stackaddr;
     69 	size_t ptap_stacksize;
     70 	size_t ptap_guardsize;
     71 };
     72 
     73 struct	__pthread_st {
     74 	unsigned int	pt_magic;
     75 	/* Identifier, for debugging and for preventing recycling. */
     76 	int		pt_num;
     77 
     78 	lwpid_t	pt_lid;		/* LWP ID */
     79 	int	pt_state;	/* running, blocked, etc. */
     80 	pthread_spin_t pt_statelock;	/* lock on pt_state */
     81 	int	pt_flags;	/* see PT_FLAG_* below */
     82 	pthread_spin_t pt_flaglock;	/* lock on pt_flag */
     83 	int	pt_cancel;	/* Deferred cancellation */
     84 	int	pt_spinlocks;	/* Number of spinlocks held. */
     85 	void	*pt_mutexhint;	/* Last mutex held. */
     86 	int	pt_sleeponq;	/* on a sleep queue */
     87 	int	pt_errno;	/* Thread-specific errno. */
     88 
     89 	/* Entry on the list of all threads */
     90 	PTQ_ENTRY(__pthread_st)	pt_allq;
     91 	/* Entry on the sleep queue (xxx should be same as run queue?) */
     92 	PTQ_ENTRY(__pthread_st)	pt_sleep;
     93 	/*
     94 	 * Object we're sleeping on.  For 1:1 threads (!SA), this is
     95 	 * protected by the interlock on the object that the thread is
     96 	 * sleeping on.
     97 	 */
     98 	void			*pt_sleepobj;
     99 	/* Queue we're sleeping on */
    100 	struct pthread_queue_t	*pt_sleepq;
    101 
    102 	stack_t		pt_stack;	/* Our stack */
    103 	ucontext_t	*pt_uc;		/* Saved context when we're stopped */
    104 	void		*pt_exitval;	/* Read by pthread_join() */
    105 	char		*pt_name;	/* Thread's name, set by the app. */
    106 
    107 	/* Stack of cancellation cleanup handlers and their arguments */
    108 	PTQ_HEAD(, pt_clean_t)	pt_cleanup_stack;
    109 
    110 	/* Other threads trying to pthread_join() us. */
    111 	struct pthread_queue_t	pt_joiners;
    112 	/* Lock for above, and for changing pt_state to ZOMBIE or DEAD,
    113 	 * and for setting the DETACHED flag.  Also protects pt_name.
    114 	 */
    115 	pthread_spin_t	pt_join_lock;
    116 
    117 	/* Thread-specific data */
    118 	void*		pt_specific[PTHREAD_KEYS_MAX];
    119 };
    120 
    121 /* Thread states */
    122 #define PT_STATE_RUNNING	1
    123 #define PT_STATE_ZOMBIE		5
    124 #define PT_STATE_DEAD		6
    125 
    126 /* Flag values */
    127 
    128 #define PT_FLAG_DETACHED	0x0001
    129 #define PT_FLAG_CS_DISABLED	0x0004	/* Cancellation disabled */
    130 #define PT_FLAG_CS_ASYNC	0x0008  /* Cancellation is async */
    131 #define PT_FLAG_CS_PENDING	0x0010
    132 #define PT_FLAG_SCOPE_SYSTEM	0x0040
    133 #define PT_FLAG_EXPLICIT_SCHED	0x0080
    134 #define PT_FLAG_SUSPENDED	0x0100	/* In the suspended queue */
    135 
    136 #define PT_MAGIC	0x11110001
    137 #define PT_DEAD		0xDEAD0001
    138 
    139 #define PT_ATTR_MAGIC	0x22220002
    140 #define PT_ATTR_DEAD	0xDEAD0002
    141 
    142 #ifdef PT_FIXEDSTACKSIZE_LG
    143 
    144 #define	PT_STACKSIZE_LG	PT_FIXEDSTACKSIZE_LG
    145 #define	PT_STACKSIZE	(1<<(PT_STACKSIZE_LG))
    146 #define	PT_STACKMASK	(PT_STACKSIZE-1)
    147 
    148 #else  /* PT_FIXEDSTACKSIZE_LG */
    149 
    150 extern	int		pthread_stacksize_lg;
    151 extern	size_t		pthread_stacksize;
    152 extern	vaddr_t		pthread_stackmask;
    153 
    154 #define	PT_STACKSIZE_LG	pthread_stacksize_lg
    155 #define	PT_STACKSIZE	pthread_stacksize
    156 #define	PT_STACKMASK	pthread_stackmask
    157 
    158 #endif /* PT_FIXEDSTACKSIZE_LG */
    159 
    160 /* Flag to be used in a ucontext_t's uc_flags indicating that
    161  * the saved register state is "user" state only, not full
    162  * trap state.
    163  */
    164 #define _UC_USER_BIT		30
    165 #define _UC_USER		(1LU << _UC_USER_BIT)
    166 
    167 void	pthread_init(void)  __attribute__ ((__constructor__));
    168 
    169 /* Utility functions */
    170 
    171 /* Set up/clean up a thread's basic state. */
    172 void	pthread__initthread(pthread_t self, pthread_t t);
    173 /* Get offset from stack start to struct sa_stackinfo */
    174 ssize_t	pthread__stackinfo_offset(void);
    175 
    176 void	pthread__unpark_all(pthread_t self, pthread_spin_t *lock,
    177 			    struct pthread_queue_t *threadq);
    178 void	pthread__unpark(pthread_t self, pthread_spin_t *lock,
    179 			struct pthread_queue_t *queue, pthread_t target);
    180 int	pthread__park(pthread_t self, pthread_spin_t *lock,
    181 		      struct pthread_queue_t *threadq,
    182 		      const struct timespec *abs_timeout,
    183 		      int cancelpt, const void *hint);
    184 
    185 int	pthread__stackalloc(pthread_t *t);
    186 void	pthread__initmain(pthread_t *t);
    187 
    188 /* Internal locking primitives */
    189 void	pthread__lockprim_init(int ncpu);
    190 void	pthread_lockinit(pthread_spin_t *lock);
    191 void	pthread_spinlock(pthread_t thread, pthread_spin_t *lock);
    192 int	pthread_spintrylock(pthread_t thread, pthread_spin_t *lock);
    193 void	pthread_spinunlock(pthread_t thread, pthread_spin_t *lock);
    194 
    195 extern const struct pthread_lock_ops *pthread__lock_ops;
    196 
    197 void	pthread__simple_lock_init(__cpu_simple_lock_t *);
    198 int	pthread__simple_lock_try(__cpu_simple_lock_t *);
    199 void	pthread__simple_unlock(__cpu_simple_lock_t *);
    200 
    201 #ifndef _getcontext_u
    202 int	_getcontext_u(ucontext_t *);
    203 #endif
    204 #ifndef _setcontext_u
    205 int	_setcontext_u(const ucontext_t *);
    206 #endif
    207 #ifndef _swapcontext_u
    208 int	_swapcontext_u(ucontext_t *, const ucontext_t *);
    209 #endif
    210 
    211 void	pthread__testcancel(pthread_t self);
    212 int	pthread__find(pthread_t self, pthread_t target);
    213 
    214 #ifndef PTHREAD_MD_INIT
    215 #define PTHREAD_MD_INIT
    216 #endif
    217 
    218 #ifndef _INITCONTEXT_U_MD
    219 #define _INITCONTEXT_U_MD(ucp)
    220 #endif
    221 
    222 #define _INITCONTEXT_U(ucp) do {					\
    223 	(ucp)->uc_flags = _UC_CPU | _UC_STACK;				\
    224 	_INITCONTEXT_U_MD(ucp)						\
    225 	} while (/*CONSTCOND*/0)
    226 
    227 #ifdef PTHREAD_MACHINE_HAS_ID_REGISTER
    228 #define pthread__id(reg) (reg)
    229 #else
    230 /* Stack location of pointer to a particular thread */
    231 #define pthread__id(sp) \
    232 	((pthread_t) (((vaddr_t)(sp)) & ~PT_STACKMASK))
    233 
    234 #define pthread__id_reg() pthread__sp()
    235 #endif
    236 
    237 #define pthread__self() (pthread__id(pthread__id_reg()))
    238 
    239 #define pthread__abort()						\
    240 	pthread__assertfunc(__FILE__, __LINE__, __func__, "unreachable")
    241 
    242 #define pthread__assert(e) do {						\
    243 	if (__predict_false(!(e)))					\
    244        	       pthread__assertfunc(__FILE__, __LINE__, __func__, #e);	\
    245         } while (/*CONSTCOND*/0)
    246 
    247 #define pthread__error(err, msg, e) do {				\
    248 	if (__predict_false(!(e))) {					\
    249        	       pthread__errorfunc(__FILE__, __LINE__, __func__, msg);	\
    250 	       return (err);						\
    251 	} 								\
    252         } while (/*CONSTCOND*/0)
    253 
    254 void	pthread__destroy_tsd(pthread_t self);
    255 void	pthread__assertfunc(const char *file, int line, const char *function,
    256 		const char *expr);
    257 void	pthread__errorfunc(const char *file, int line, const char *function,
    258 		const char *msg);
    259 
    260 #endif /* _LIB_PTHREAD_INT_H */
    261