Home | History | Annotate | Line # | Download | only in libpthread
pthread_int.h revision 1.49.2.1
      1 /*	$NetBSD: pthread_int.h,v 1.49.2.1 2007/11/06 23:11:41 matt 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 /*
     40  * NOTE: when changing anything in this file, please ensure that
     41  * libpthread_dbg still compiles.
     42  */
     43 
     44 #ifndef _LIB_PTHREAD_INT_H
     45 #define _LIB_PTHREAD_INT_H
     46 
     47 /* #define PTHREAD__DEBUG */
     48 #define ERRORCHECK
     49 
     50 #include "pthread_types.h"
     51 #include "pthread_queue.h"
     52 #include "pthread_debug.h"
     53 #include "pthread_md.h"
     54 
     55 #include <sys/tree.h>
     56 
     57 #include <lwp.h>
     58 #include <signal.h>
     59 
     60 #define PTHREAD_KEYS_MAX 	256
     61 #define	PTHREAD__UNPARK_MAX	32
     62 
     63 /*
     64  * The size of this structure needs to be no larger than struct
     65  * __pthread_cleanup_store, defined in pthread.h.
     66  */
     67 struct pt_clean_t {
     68 	PTQ_ENTRY(pt_clean_t)	ptc_next;
     69 	void	(*ptc_cleanup)(void *);
     70 	void	*ptc_arg;
     71 };
     72 
     73 /* Private data for pthread_attr_t */
     74 struct pthread_attr_private {
     75 	char ptap_name[PTHREAD_MAX_NAMELEN_NP];
     76 	void *ptap_namearg;
     77 	void *ptap_stackaddr;
     78 	size_t ptap_stacksize;
     79 	size_t ptap_guardsize;
     80 };
     81 
     82 struct	__pthread_st {
     83 	unsigned int	pt_magic;	/* Magic number */
     84 	int		pt_num;		/* ID XXX should die */
     85 	int		pt_state;	/* running, blocked, etc. */
     86 	pthread_mutex_t	pt_lock;	/* lock on state */
     87 	int		pt_flags;	/* see PT_FLAG_* below */
     88 	int		pt_cancel;	/* Deferred cancellation */
     89 	int		pt_spinlocks;	/* Number of spinlocks held. */
     90 	int		pt_errno;	/* Thread-specific errno. */
     91 	stack_t		pt_stack;	/* Our stack */
     92 	void		*pt_exitval;	/* Read by pthread_join() */
     93 	char		*pt_name;	/* Thread's name, set by the app. */
     94 	int		pt_willpark;	/* About to park */
     95 	lwpid_t		pt_unpark;	/* Unpark this when parking */
     96 	void		*pt_unparkhint;	/* Hint for the above */
     97 
     98 	/* Threads to defer waking, usually until pthread_mutex_unlock(). */
     99 	lwpid_t		pt_waiters[PTHREAD__UNPARK_MAX];
    100 	size_t		pt_nwaiters;
    101 
    102 	/* Stack of cancellation cleanup handlers and their arguments */
    103 	PTQ_HEAD(, pt_clean_t)	pt_cleanup_stack;
    104 
    105 	/* For debugger: LWPs waiting to join. */
    106 	pthread_queue_t	pt_joiners;
    107 	PTQ_ENTRY(__pthread_st) pt_joinq;
    108 
    109 	/* LWP ID and entry on the list of all threads. */
    110 	lwpid_t		pt_lid;
    111 	RB_ENTRY(__pthread_st) pt_alltree;
    112 	PTQ_ENTRY(__pthread_st) pt_allq;
    113 	PTQ_ENTRY(__pthread_st)	pt_deadq;
    114 
    115 	/*
    116 	 * General synchronization data.  We try to align, as threads
    117 	 * on other CPUs will access this data frequently.
    118 	 */
    119 	int		pt_dummy1 __aligned(128);
    120 	volatile int	pt_rwlocked;	/* Handed rwlock successfully */
    121 	volatile int	pt_sleeponq;	/* On a sleep queue */
    122 	volatile int	pt_signalled;	/* Received pthread_cond_signal() */
    123 	void * volatile	pt_sleepobj;	/* Object slept on */
    124 	PTQ_ENTRY(__pthread_st) pt_sleep;
    125 	void		(*pt_early)(void *);
    126 	int		pt_dummy2 __aligned(128);
    127 
    128 	/* Thread-specific data.  Large so it sits close to the end. */
    129 	int		pt_havespecific;
    130 	void		*pt_specific[PTHREAD_KEYS_MAX];
    131 
    132 	/*
    133 	 * Context for thread creation.  At the end as it's cached
    134 	 * and then only ever passed to _lwp_create().
    135 	 */
    136 	ucontext_t	pt_uc;
    137 };
    138 
    139 struct pthread_lock_ops {
    140 	void	(*plo_init)(__cpu_simple_lock_t *);
    141 	int	(*plo_try)(__cpu_simple_lock_t *);
    142 	void	(*plo_unlock)(__cpu_simple_lock_t *);
    143 };
    144 
    145 /* Thread states */
    146 #define PT_STATE_RUNNING	1
    147 #define PT_STATE_ZOMBIE		5
    148 #define PT_STATE_DEAD		6
    149 
    150 /* Flag values */
    151 
    152 #define PT_FLAG_DETACHED	0x0001
    153 #define PT_FLAG_CS_DISABLED	0x0004	/* Cancellation disabled */
    154 #define PT_FLAG_CS_ASYNC	0x0008  /* Cancellation is async */
    155 #define PT_FLAG_CS_PENDING	0x0010
    156 #define PT_FLAG_SCOPE_SYSTEM	0x0040
    157 #define PT_FLAG_EXPLICIT_SCHED	0x0080
    158 #define PT_FLAG_SUSPENDED	0x0100	/* In the suspended queue */
    159 
    160 #define PT_MAGIC	0x11110001
    161 #define PT_DEAD		0xDEAD0001
    162 
    163 #define PT_ATTR_MAGIC	0x22220002
    164 #define PT_ATTR_DEAD	0xDEAD0002
    165 
    166 extern int	pthread__stacksize_lg;
    167 extern size_t	pthread__stacksize;
    168 extern vaddr_t	pthread__stackmask;
    169 extern vaddr_t	pthread__threadmask;
    170 extern int	pthread__nspins;
    171 extern int	pthread__concurrency;
    172 extern int 	pthread__osrev;
    173 extern int 	pthread__unpark_max;
    174 
    175 /* Flag to be used in a ucontext_t's uc_flags indicating that
    176  * the saved register state is "user" state only, not full
    177  * trap state.
    178  */
    179 #define _UC_USER_BIT		30
    180 #define _UC_USER		(1LU << _UC_USER_BIT)
    181 
    182 void	pthread_init(void)  __attribute__ ((__constructor__));
    183 
    184 /* Utility functions */
    185 void	pthread__unpark_all(pthread_t self, pthread_spin_t *lock,
    186 			    pthread_queue_t *threadq);
    187 void	pthread__unpark(pthread_t self, pthread_spin_t *lock,
    188 			pthread_queue_t *queue, pthread_t target);
    189 int	pthread__park(pthread_t self, pthread_spin_t *lock,
    190 		      pthread_queue_t *threadq,
    191 		      const struct timespec *abs_timeout,
    192 		      int cancelpt, const void *hint);
    193 
    194 /* Internal locking primitives */
    195 void	pthread__lockprim_init(void);
    196 void	pthread_lockinit(pthread_spin_t *);
    197 void	pthread_spinlock(pthread_spin_t *);
    198 int	pthread_spintrylock(pthread_spin_t *);
    199 void	pthread_spinunlock(pthread_spin_t *);
    200 
    201 extern const struct pthread_lock_ops *pthread__lock_ops;
    202 
    203 int	pthread__simple_locked_p(__cpu_simple_lock_t *);
    204 #define	pthread__simple_lock_init(alp)	(*pthread__lock_ops->plo_init)(alp)
    205 #define	pthread__simple_lock_try(alp)	(*pthread__lock_ops->plo_try)(alp)
    206 #define	pthread__simple_unlock(alp)	(*pthread__lock_ops->plo_unlock)(alp)
    207 
    208 #ifndef _getcontext_u
    209 int	_getcontext_u(ucontext_t *);
    210 #endif
    211 #ifndef _setcontext_u
    212 int	_setcontext_u(const ucontext_t *);
    213 #endif
    214 #ifndef _swapcontext_u
    215 int	_swapcontext_u(ucontext_t *, const ucontext_t *);
    216 #endif
    217 
    218 void	pthread__testcancel(pthread_t);
    219 int	pthread__find(pthread_t);
    220 
    221 #ifndef PTHREAD_MD_INIT
    222 #define PTHREAD_MD_INIT
    223 #endif
    224 
    225 #ifndef _INITCONTEXT_U_MD
    226 #define _INITCONTEXT_U_MD(ucp)
    227 #endif
    228 
    229 #define _INITCONTEXT_U(ucp) do {					\
    230 	(ucp)->uc_flags = _UC_CPU | _UC_STACK;				\
    231 	_INITCONTEXT_U_MD(ucp)						\
    232 	} while (/*CONSTCOND*/0)
    233 
    234 #ifdef PTHREAD_MACHINE_HAS_ID_REGISTER
    235 #define pthread__id(reg) (reg)
    236 #else
    237 /* Stack location of pointer to a particular thread */
    238 #define pthread__id(sp) \
    239 	((pthread_t) (((vaddr_t)(sp)) & pthread__threadmask))
    240 
    241 #define pthread__id_reg() pthread__sp()
    242 #endif
    243 
    244 #define pthread__self() (pthread__id(pthread__id_reg()))
    245 
    246 #define pthread__abort()						\
    247 	pthread__assertfunc(__FILE__, __LINE__, __func__, "unreachable")
    248 
    249 #define pthread__assert(e) do {						\
    250 	if (__predict_false(!(e)))					\
    251        	       pthread__assertfunc(__FILE__, __LINE__, __func__, #e);	\
    252         } while (/*CONSTCOND*/0)
    253 
    254 #define pthread__error(err, msg, e) do {				\
    255 	if (__predict_false(!(e))) {					\
    256        	       pthread__errorfunc(__FILE__, __LINE__, __func__, msg);	\
    257 	       return (err);						\
    258 	} 								\
    259         } while (/*CONSTCOND*/0)
    260 
    261 void	pthread__destroy_tsd(pthread_t self);
    262 void	pthread__assertfunc(const char *file, int line, const char *function,
    263 		const char *expr);
    264 void	pthread__errorfunc(const char *file, int line, const char *function,
    265 		const char *msg);
    266 
    267 int	pthread__atomic_cas_ptr(volatile void *, void **, void *);
    268 void	*pthread__atomic_swap_ptr(volatile void *, void *);
    269 void	pthread__membar_full(void);
    270 void	pthread__membar_producer(void);
    271 void	pthread__membar_consumer(void);
    272 
    273 int	pthread__mutex_deferwake(pthread_t, pthread_mutex_t *);
    274 int	pthread__mutex_catchup(pthread_mutex_t *);
    275 
    276 #ifndef pthread__smt_pause
    277 #define	pthread__smt_pause()	/* nothing */
    278 #endif
    279 
    280 /*
    281  * Bits in the owner field of the lock that indicate lock state.  If the
    282  * WRITE_LOCKED bit is clear, then the owner field is actually a count of
    283  * the number of readers.
    284  */
    285 #define	RW_HAS_WAITERS		0x01	/* lock has waiters */
    286 #define	RW_WRITE_WANTED		0x02	/* >= 1 waiter is a writer */
    287 #define	RW_WRITE_LOCKED		0x04	/* lock is currently write locked */
    288 #define	RW_UNUSED		0x08	/* currently unused */
    289 
    290 #define	RW_FLAGMASK		0x0f
    291 
    292 #define	RW_READ_COUNT_SHIFT	4
    293 #define	RW_READ_INCR		(1 << RW_READ_COUNT_SHIFT)
    294 #define	RW_THREAD		((uintptr_t)-RW_READ_INCR)
    295 #define	RW_OWNER(rw)		((rw)->rw_owner & RW_THREAD)
    296 #define	RW_COUNT(rw)		((rw)->rw_owner & RW_THREAD)
    297 #define	RW_FLAGS(rw)		((rw)->rw_owner & ~RW_THREAD)
    298 
    299 #define	ptr_owner		ptr_writer
    300 
    301 #endif /* _LIB_PTHREAD_INT_H */
    302