Index: head/lib/libc_r/Makefile =================================================================== --- head/lib/libc_r/Makefile (revision 48608) +++ head/lib/libc_r/Makefile (revision 48609) @@ -1,42 +1,39 @@ -# $Id: Makefile,v 1.16 1999/07/05 00:35:14 jasone Exp $ +# $Id: Makefile,v 1.17 1999/07/05 00:38:12 jasone Exp $ # # All library objects contain rcsid strings by default; they may be # excluded as a space-saving measure. To produce a library that does # not contain these strings, delete -DLIBC_RCS and -DSYSLIBC_RCS # from CFLAGS below. To remove these strings from just the system call # stubs, remove just -DSYSLIBC_RCS from CFLAGS. LIB=c_r SHLIB_MAJOR= 4 SHLIB_MINOR= 0 CFLAGS+=-DLIBC_RCS -DSYSLIBC_RCS -I${.CURDIR}/../libc/include CFLAGS+=-DPTHREAD_KERNEL -D_THREAD_SAFE -I${.CURDIR}/uthread # Uncomment this if you want libc_r to contain debug information for # thread locking. CFLAGS+=-D_LOCK_DEBUG - -# Uncomment this if you want libc_r to use growable stacks. -#CFLAGS+=-D_PTHREAD_GSTACK AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes # # This is a list of syscalls that are renamed as _thread_sys_{syscall} # so that libc_r can provide replacement functions. # HIDDEN_SYSCALLS= accept.o bind.o close.o connect.o dup.o dup2.o \ execve.o fchflags.o fchmod.o fchown.o fcntl.o \ flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ getpeername.o getsockname.o getsockopt.o ioctl.o listen.o \ nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ recvmsg.o sched_yield.o select.o sendmsg.o sendto.o \ setsockopt.o shutdown.o sigaction.o sigaltstack.o \ signanosleep.o socket.o socketpair.o wait4.o write.o writev.o .include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" .include Index: head/lib/libc_r/uthread/pthread_private.h =================================================================== --- head/lib/libc_r/uthread/pthread_private.h (revision 48608) +++ head/lib/libc_r/uthread/pthread_private.h (revision 48609) @@ -1,1164 +1,1160 @@ /* * Copyright (c) 1995-1998 John Birrell . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Private thread definitions for the uthread kernel. * - * $Id: pthread_private.h,v 1.20 1999/06/20 08:28:08 jb Exp $ + * $Id: pthread_private.h,v 1.21 1999/07/05 00:35:17 jasone Exp $ */ #ifndef _PTHREAD_PRIVATE_H #define _PTHREAD_PRIVATE_H /* * Evaluate the storage class specifier. */ #ifdef GLOBAL_PTHREAD_PRIVATE #define SCLASS #else #define SCLASS extern #endif /* * Include files. */ #include #include #include #include #include #include #include #include /* * Kernel fatal error handler macro. */ #define PANIC(string) _thread_exit(__FILE__,__LINE__,string) /* Output debug messages like this: */ #define stdout_debug(_x) _thread_sys_write(1,_x,strlen(_x)); #define stderr_debug(_x) _thread_sys_write(2,_x,strlen(_x)); /* * Priority queue manipulation macros (using pqe link): */ #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd) #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd) #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd) #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq) /* * Waiting queue manipulation macros (using pqe link): */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd) #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd) #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive() #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive() #else #define PTHREAD_WAITQ_REMOVE(thrd) TAILQ_REMOVE(&_waitingq,thrd,pqe) #define PTHREAD_WAITQ_INSERT(thrd) do { \ if ((thrd)->wakeup_time.tv_sec == -1) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else { \ pthread_t tid = TAILQ_FIRST(&_waitingq); \ while ((tid != NULL) && (tid->wakeup_time.tv_sec != -1) && \ ((tid->wakeup_time.tv_sec < (thrd)->wakeup_time.tv_sec) || \ ((tid->wakeup_time.tv_sec == (thrd)->wakeup_time.tv_sec) && \ (tid->wakeup_time.tv_nsec <= (thrd)->wakeup_time.tv_nsec)))) \ tid = TAILQ_NEXT(tid, pqe); \ if (tid == NULL) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else \ TAILQ_INSERT_BEFORE(tid,thrd,pqe); \ } \ } while (0) #define PTHREAD_WAITQ_CLEARACTIVE() #define PTHREAD_WAITQ_SETACTIVE() #endif /* * Work queue manipulation macros (using qe link): */ #define PTHREAD_WORKQ_INSERT(thrd) do { \ TAILQ_INSERT_TAIL(&_workq,thrd,qe); \ (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \ } while (0) #define PTHREAD_WORKQ_REMOVE(thrd) do { \ TAILQ_REMOVE(&_workq,thrd,qe); \ (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \ } while (0) /* * State change macro without scheduling queue change: */ #define PTHREAD_SET_STATE(thrd, newstate) do { \ (thrd)->state = newstate; \ (thrd)->fname = __FILE__; \ (thrd)->lineno = __LINE__; \ } while (0) /* * State change macro with scheduling queue change - This must be * called with preemption deferred (see thread_kern_sched_[un]defer). */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if (_thread_kern_new_state != 0) \ PANIC("Recursive PTHREAD_NEW_STATE"); \ _thread_kern_new_state = 1; \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ _thread_kern_new_state = 0; \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #else #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #endif /* * Define the signals to be used for scheduling. */ #if defined(_PTHREADS_COMPAT_SCHED) #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL #define _SCHED_SIGNAL SIGVTALRM #else #define _ITIMER_SCHED_TIMER ITIMER_PROF #define _SCHED_SIGNAL SIGPROF #endif /* * Priority queues. * * XXX It'd be nice if these were contained in uthread_priority_queue.[ch]. */ typedef struct pq_list { TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */ TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */ int pl_prio; /* the priority of this list */ int pl_queued; /* is this in the priority queue */ } pq_list_t; typedef struct pq_queue { TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */ pq_list_t *pq_lists; /* array of all priority lists */ int pq_size; /* number of priority lists */ } pq_queue_t; /* * TailQ initialization values. */ #define TAILQ_INITIALIZER { NULL, NULL } /* * Mutex definitions. */ union pthread_mutex_data { void *m_ptr; int m_count; }; struct pthread_mutex { enum pthread_mutextype m_type; int m_protocol; TAILQ_HEAD(mutex_head, pthread) m_queue; struct pthread *m_owner; union pthread_mutex_data m_data; long m_flags; int m_refcount; /* * Used for priority inheritence and protection. * * m_prio - For priority inheritence, the highest active * priority (threads locking the mutex inherit * this priority). For priority protection, the * ceiling priority of this mutex. * m_saved_prio - mutex owners inherited priority before * taking the mutex, restored when the owner * unlocks the mutex. */ int m_prio; int m_saved_prio; /* * Link for list of all mutexes a thread currently owns. */ TAILQ_ENTRY(pthread_mutex) m_qe; /* * Lock for accesses to this structure. */ spinlock_t lock; }; /* * Flags for mutexes. */ #define MUTEX_FLAGS_PRIVATE 0x01 #define MUTEX_FLAGS_INITED 0x02 #define MUTEX_FLAGS_BUSY 0x04 /* * Static mutex initialization values. */ #define PTHREAD_MUTEX_STATIC_INITIALIZER \ { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \ NULL, { NULL }, 0, 0, 0, 0, TAILQ_INITIALIZER, \ _SPINLOCK_INITIALIZER } struct pthread_mutex_attr { enum pthread_mutextype m_type; int m_protocol; int m_ceiling; long m_flags; }; /* * Condition variable definitions. */ enum pthread_cond_type { COND_TYPE_FAST, COND_TYPE_MAX }; struct pthread_cond { enum pthread_cond_type c_type; TAILQ_HEAD(cond_head, pthread) c_queue; pthread_mutex_t c_mutex; void *c_data; long c_flags; /* * Lock for accesses to this structure. */ spinlock_t lock; }; struct pthread_cond_attr { enum pthread_cond_type c_type; long c_flags; }; /* * Flags for condition variables. */ #define COND_FLAGS_PRIVATE 0x01 #define COND_FLAGS_INITED 0x02 #define COND_FLAGS_BUSY 0x04 /* * Static cond initialization values. */ #define PTHREAD_COND_STATIC_INITIALIZER \ { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \ 0, _SPINLOCK_INITIALIZER } /* * Cleanup definitions. */ struct pthread_cleanup { struct pthread_cleanup *next; void (*routine) (); void *routine_arg; }; struct pthread_attr { int sched_policy; int sched_inherit; int sched_interval; int prio; int suspend; int flags; void *arg_attr; void (*cleanup_attr) (); void *stackaddr_attr; size_t stacksize_attr; }; /* * Thread creation state attributes. */ #define PTHREAD_CREATE_RUNNING 0 #define PTHREAD_CREATE_SUSPENDED 1 /* * Miscellaneous definitions. */ #define PTHREAD_STACK_DEFAULT 65536 -#ifdef _PTHREAD_GSTACK /* Size of red zone at the end of each stack. */ #define PTHREAD_STACK_GUARD 4096 /* Maximum size of initial thread's stack. This perhaps deserves to be larger - * than the stacks of other threads, since legacy applications are likely to run + * than the stacks of other threads, since many applications are likely to run * almost entirely on this stack. */ #define PTHREAD_STACK_INITIAL 0x100000 /* Address immediately beyond the beginning of the initial thread stack. */ #if defined(__FreeBSD__) -# if defined(__alpha__) -# define PTHREAD_STACK_TOP 0x160022000 -# else -# define PTHREAD_STACK_TOP 0xbfbde000 -# endif +# if defined(__i386__) +# define PTHREAD_STACK_TOP 0xbfbde000 +# elif defined(__alpha__) +# define PTHREAD_STACK_TOP 0x160022000 +# else +# error "Don't recognize this architecture!" +# endif #else -# error "Don't recognize this operating system!" +# error "Don't recognize this operating system!" #endif -#endif #define PTHREAD_DEFAULT_PRIORITY 64 #define PTHREAD_MAX_PRIORITY 126 #define PTHREAD_MIN_PRIORITY 0 #define _POSIX_THREAD_ATTR_STACKSIZE /* * Clock resolution in nanoseconds. */ #define CLOCK_RES_NSEC 10000000 /* * Time slice period in microseconds. */ #define TIMESLICE_USEC 100000 struct pthread_key { spinlock_t lock; volatile int allocated; volatile int count; void (*destructor) (); }; struct pthread_rwlockattr { int pshared; }; struct pthread_rwlock { pthread_mutex_t lock; /* monitor lock */ int state; /* 0 = idle >0 = # of readers -1 = writer */ pthread_cond_t read_signal; pthread_cond_t write_signal; int blocked_writers; }; /* * Thread states. */ enum pthread_state { PS_RUNNING, PS_SIGTHREAD, PS_MUTEX_WAIT, PS_COND_WAIT, PS_FDLR_WAIT, PS_FDLW_WAIT, PS_FDR_WAIT, PS_FDW_WAIT, PS_FILE_WAIT, PS_POLL_WAIT, PS_SELECT_WAIT, PS_SLEEP_WAIT, PS_WAIT_WAIT, PS_SIGSUSPEND, PS_SIGWAIT, PS_SPINBLOCK, PS_JOIN, PS_SUSPENDED, PS_DEAD, PS_DEADLOCK, PS_STATE_MAX }; /* * File descriptor locking definitions. */ #define FD_READ 0x1 #define FD_WRITE 0x2 #define FD_RDWR (FD_READ | FD_WRITE) /* * File descriptor table structure. */ struct fd_table_entry { /* * Lock for accesses to this file descriptor table * entry. This is passed to _spinlock() to provide atomic * access to this structure. It does *not* represent the * state of the lock on the file descriptor. */ spinlock_t lock; TAILQ_HEAD(, pthread) r_queue; /* Read queue. */ TAILQ_HEAD(, pthread) w_queue; /* Write queue. */ struct pthread *r_owner; /* Ptr to thread owning read lock. */ struct pthread *w_owner; /* Ptr to thread owning write lock. */ char *r_fname; /* Ptr to read lock source file name */ int r_lineno; /* Read lock source line number. */ char *w_fname; /* Ptr to write lock source file name */ int w_lineno; /* Write lock source line number. */ int r_lockcount; /* Count for FILE read locks. */ int w_lockcount; /* Count for FILE write locks. */ int flags; /* Flags used in open. */ }; struct pthread_poll_data { int nfds; struct pollfd *fds; }; union pthread_wait_data { pthread_mutex_t mutex; pthread_cond_t cond; const sigset_t *sigwait; /* Waiting on a signal in sigwait */ struct { short fd; /* Used when thread waiting on fd */ short branch; /* Line number, for debugging. */ char *fname; /* Source file name for debugging.*/ } fd; struct pthread_poll_data * poll_data; spinlock_t *spinlock; }; /* * Thread structure. */ struct pthread { /* * Magic value to help recognize a valid thread structure * from an invalid one: */ #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115) u_int32_t magic; char *name; /* * Lock for accesses to this thread structure. */ spinlock_t lock; /* Queue entry for list of all threads: */ TAILQ_ENTRY(pthread) tle; /* Queue entry for list of dead threads: */ TAILQ_ENTRY(pthread) dle; /* * Thread start routine, argument, stack pointer and thread * attributes. */ void *(*start_routine)(void *); void *arg; void *stack; struct pthread_attr attr; #if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(__i386__) /* * Saved floating point registers on systems where they are not * saved in the signal context. */ char saved_fp[108]; #endif /* * Saved signal context used in call to sigreturn by * _thread_kern_sched if sig_saved is TRUE. */ struct sigcontext saved_sigcontext; /* * Saved jump buffer used in call to longjmp by _thread_kern_sched * if sig_saved is FALSE. */ jmp_buf saved_jmp_buf; /* * TRUE if the last state saved was a signal context. FALSE if the * last state saved was a jump buffer. */ int sig_saved; /* * Current signal mask and pending signals. */ sigset_t sigmask; sigset_t sigpend; /* Thread state: */ enum pthread_state state; /* Time that this thread was last made active. */ struct timeval last_active; /* Time that this thread was last made inactive. */ struct timeval last_inactive; /* * Number of microseconds accumulated by this thread when * time slicing is active. */ long slice_usec; /* * Incremental priority accumulated by thread while it is ready to * run but is denied being run. */ int inc_prio; /* * Time to wake up thread. This is used for sleeping threads and * for any operation which may time out (such as select). */ struct timespec wakeup_time; /* TRUE if operation has timed out. */ int timeout; /* * Error variable used instead of errno. The function __error() * returns a pointer to this. */ int error; /* Join queue head and link for waiting threads: */ TAILQ_HEAD(join_head, pthread) join_queue; /* * The current thread can belong to only one scheduling queue at * a time (ready or waiting queue). It can also belong to (only) * one of: * * o A queue of threads waiting for a mutex * o A queue of threads waiting for a condition variable * o A queue of threads waiting for another thread to terminate * (the join queue above) * o A queue of threads waiting for a file descriptor lock * o A queue of threads needing work done by the kernel thread * (waiting for a spinlock or file I/O) * * Use pqe for the scheduling queue link (both ready and waiting), * and qe for other links. */ /* Priority queue entry for this thread: */ TAILQ_ENTRY(pthread) pqe; /* Queue entry for this thread: */ TAILQ_ENTRY(pthread) qe; /* Wait data. */ union pthread_wait_data data; /* * Allocated for converting select into poll. */ struct pthread_poll_data poll_data; /* * Set to TRUE if a blocking operation was * interrupted by a signal: */ int interrupted; /* Signal number when in state PS_SIGWAIT: */ int signo; /* * Set to non-zero when this thread has deferred signals. * We allow for recursive deferral. */ int sig_defer_count; /* * Set to TRUE if this thread should yield after undeferring * signals. */ int yield_on_sig_undefer; /* Miscellaneous data. */ int flags; #define PTHREAD_FLAGS_PRIVATE 0x0001 #define PTHREAD_EXITING 0x0002 #define PTHREAD_FLAGS_IN_CONDQ 0x0004 /* in condition queue using qe link*/ #define PTHREAD_FLAGS_IN_WORKQ 0x0008 /* in work queue using qe link */ #define PTHREAD_FLAGS_IN_WAITQ 0x0010 /* in waiting queue using pqe link*/ #define PTHREAD_FLAGS_IN_PRIOQ 0x0020 /* in priority queue using pqe link*/ #define PTHREAD_FLAGS_TRACE 0x0040 /* for debugging purposes */ /* * Base priority is the user setable and retrievable priority * of the thread. It is only affected by explicit calls to * set thread priority and upon thread creation via a thread * attribute or default priority. */ char base_priority; /* * Inherited priority is the priority a thread inherits by * taking a priority inheritence or protection mutex. It * is not affected by base priority changes. Inherited * priority defaults to and remains 0 until a mutex is taken * that is being waited on by any other thread whose priority * is non-zero. */ char inherited_priority; /* * Active priority is always the maximum of the threads base * priority and inherited priority. When there is a change * in either the base or inherited priority, the active * priority must be recalculated. */ char active_priority; /* Number of priority ceiling or protection mutexes owned. */ int priority_mutex_count; /* * Queue of currently owned mutexes. */ TAILQ_HEAD(, pthread_mutex) mutexq; void *ret; const void **specific_data; int specific_data_count; /* Cleanup handlers Link List */ struct pthread_cleanup *cleanup; char *fname; /* Ptr to source file name */ int lineno; /* Source line number. */ }; -#ifdef _PTHREAD_GSTACK /* Spare thread stack. */ struct stack { SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */ }; -#endif /* * Global variables for the uthread kernel. */ /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; /* Ptr to the thread structure for the running thread: */ SCLASS struct pthread * volatile _thread_run #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* Ptr to the thread structure for the last user thread to run: */ SCLASS struct pthread * volatile _last_user_thread #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* * Ptr to the thread running in single-threaded mode or NULL if * running multi-threaded (default POSIX behaviour). */ SCLASS struct pthread * volatile _thread_single #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* List of all threads: */ SCLASS TAILQ_HEAD(, pthread) _thread_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_thread_list); #else ; #endif /* * Array of kernel pipe file descriptors that are used to ensure that * no signals are missed in calls to _select. */ SCLASS int _thread_kern_pipe[2] #ifdef GLOBAL_PTHREAD_PRIVATE = { -1, -1 }; #else ; #endif SCLASS int volatile _queue_signals #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif SCLASS int _thread_kern_in_sched #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif /* Last time that an incremental priority update was performed: */ SCLASS struct timeval kern_inc_prio_time #ifdef GLOBAL_PTHREAD_PRIVATE = { 0, 0 }; #else ; #endif /* Dead threads: */ SCLASS TAILQ_HEAD(, pthread) _dead_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_dead_list); #else ; #endif /* Initial thread: */ SCLASS struct pthread *_thread_initial #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Default thread attributes: */ SCLASS struct pthread_attr pthread_attr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING, PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT }; #else ; #endif /* Default mutex attributes: */ SCLASS struct pthread_mutex_attr pthread_mutexattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 }; #else ; #endif /* Default condition variable attributes: */ SCLASS struct pthread_cond_attr pthread_condattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { COND_TYPE_FAST, 0 }; #else ; #endif /* * Standard I/O file descriptors need special flag treatment since * setting one to non-blocking does all on *BSD. Sigh. This array * is used to store the initial flag settings. */ SCLASS int _pthread_stdio_flags[3]; /* File table information: */ SCLASS struct fd_table_entry **_thread_fd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Table for polling file descriptors: */ SCLASS struct pollfd *_thread_pfd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif SCLASS const int dtablecount #ifdef GLOBAL_PTHREAD_PRIVATE = 4096/sizeof(struct fd_table_entry); #else ; #endif SCLASS int _thread_dtablesize /* Descriptor table size. */ #ifdef GLOBAL_PTHREAD_PRIVATE = 1024; #else ; #endif SCLASS int _clock_res_nsec /* Clock resolution in nsec. */ #ifdef GLOBAL_PTHREAD_PRIVATE = CLOCK_RES_NSEC; #else ; #endif /* Garbage collector mutex and condition variable. */ SCLASS pthread_mutex_t _gc_mutex #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; SCLASS pthread_cond_t _gc_cond #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; /* * Array of signal actions for this process. */ struct sigaction _thread_sigact[NSIG]; /* * Scheduling queues: */ SCLASS pq_queue_t _readyq; SCLASS TAILQ_HEAD(, pthread) _waitingq; /* * Work queue: */ SCLASS TAILQ_HEAD(, pthread) _workq; /* Tracks the number of threads blocked while waiting for a spinlock. */ SCLASS volatile int _spinblock_count #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Indicates that the signal queue needs to be checked. */ SCLASS volatile int _sigq_check_reqd #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Thread switch hook. */ SCLASS pthread_switch_routine_t _sched_switch_hook #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; -#ifdef _PTHREAD_GSTACK /* Spare stack queue. Stacks of default size are cached in order to reduce * thread creation time. Spare stacks are used in LIFO order to increase cache * locality. */ SCLASS SLIST_HEAD(, stack) _stackq; /* Base address of next unallocated default-size stack. Stacks are allocated * contiguously, starting below the beginning of the main stack. When a new * stack is created, a guard page is created just above it in order to (usually) * detect attempts by the adjacent stack to trounce the next thread stack. */ SCLASS void * _next_stack #ifdef GLOBAL_PTHREAD_PRIVATE /* main stack top - main stack size - stack size - (red zone + main stack red zone) */ = (void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD) #endif ; -#endif /* Used for _PTHREADS_INVARIANTS checking. */ SCLASS int _thread_kern_new_state #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Undefine the storage class specifier: */ #undef SCLASS #ifdef _LOCK_DEBUG #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \ _ts, __FILE__, __LINE__) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \ __FILE__, __LINE__) #else #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type) #endif /* * Function prototype definitions. */ __BEGIN_DECLS char *__ttyname_basic(int); char *__ttyname_r_basic(int, char *, size_t); char *ttyname_r(int, char *, size_t); int _find_dead_thread(pthread_t); int _find_thread(pthread_t); int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t); int _thread_fd_lock(int, int, struct timespec *); int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno); void _dispatch_signals(void); void _thread_signal(pthread_t, int); int _mutex_cv_lock(pthread_mutex_t *); int _mutex_cv_unlock(pthread_mutex_t *); int _mutex_reinit(pthread_mutex_t *); void _mutex_notify_priochange(struct pthread *); int _cond_reinit(pthread_cond_t *); int _pq_alloc(struct pq_queue *, int, int); int _pq_init(struct pq_queue *); void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); #if defined(_PTHREADS_INVARIANTS) void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); void _waitq_setactive(void); void _waitq_clearactive(void); #endif void _thread_exit(char *, int, char *); void _thread_fd_unlock(int, int); void _thread_fd_unlock_debug(int, int, char *, int); void *_thread_cleanup(pthread_t); void _thread_cleanupspecific(void); void _thread_dump_info(void); void _thread_init(void); void _thread_kern_sched(struct sigcontext *); void _thread_kern_sched_state(enum pthread_state,char *fname,int lineno); void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno); void _thread_kern_set_timeout(struct timespec *); void _thread_kern_sig_defer(void); void _thread_kern_sig_undefer(void); void _thread_sig_handler(int, int, struct sigcontext *); void _thread_sig_handle(int, struct sigcontext *); void _thread_sig_init(void); void _thread_start(void); void _thread_start_sig_handler(void); void _thread_seterrno(pthread_t,int); int _thread_fd_table_init(int fd); pthread_addr_t _thread_gc(pthread_addr_t); /* #include */ int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); int _thread_sys_sigpending(sigset_t *); int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); int _thread_sys_sigsuspend(const sigset_t *); int _thread_sys_siginterrupt(int, int); int _thread_sys_sigpause(int); int _thread_sys_sigreturn(struct sigcontext *); int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); void _thread_sys_psignal(unsigned int, const char *); void (*_thread_sys_signal(int, void (*)(int)))(int); /* #include */ #ifdef _SYS_STAT_H_ int _thread_sys_fchmod(int, mode_t); int _thread_sys_fstat(int, struct stat *); int _thread_sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ int _thread_sys_fstatfs(int, struct statfs *); #endif int _thread_sys_pipe(int *); /* #include */ #ifdef _SYS_SOCKET_H_ int _thread_sys_accept(int, struct sockaddr *, int *); int _thread_sys_bind(int, const struct sockaddr *, int); int _thread_sys_connect(int, const struct sockaddr *, int); int _thread_sys_getpeername(int, struct sockaddr *, int *); int _thread_sys_getsockname(int, struct sockaddr *, int *); int _thread_sys_getsockopt(int, int, int, void *, int *); int _thread_sys_listen(int, int); int _thread_sys_setsockopt(int, int, int, const void *, int); int _thread_sys_shutdown(int, int); int _thread_sys_socket(int, int, int); int _thread_sys_socketpair(int, int, int, int *); ssize_t _thread_sys_recv(int, void *, size_t, int); ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); ssize_t _thread_sys_send(int, const void *, size_t, int); ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _STDIO_H_ FILE *_thread_sys_fdopen(int, const char *); FILE *_thread_sys_fopen(const char *, const char *); FILE *_thread_sys_freopen(const char *, const char *, FILE *); FILE *_thread_sys_popen(const char *, const char *); FILE *_thread_sys_tmpfile(void); char *_thread_sys_ctermid(char *); char *_thread_sys_cuserid(char *); char *_thread_sys_fgetln(FILE *, size_t *); char *_thread_sys_fgets(char *, int, FILE *); char *_thread_sys_gets(char *); char *_thread_sys_tempnam(const char *, const char *); char *_thread_sys_tmpnam(char *); int _thread_sys_fclose(FILE *); int _thread_sys_feof(FILE *); int _thread_sys_ferror(FILE *); int _thread_sys_fflush(FILE *); int _thread_sys_fgetc(FILE *); int _thread_sys_fgetpos(FILE *, fpos_t *); int _thread_sys_fileno(FILE *); int _thread_sys_fprintf(FILE *, const char *, ...); int _thread_sys_fpurge(FILE *); int _thread_sys_fputc(int, FILE *); int _thread_sys_fputs(const char *, FILE *); int _thread_sys_fscanf(FILE *, const char *, ...); int _thread_sys_fseek(FILE *, long, int); int _thread_sys_fsetpos(FILE *, const fpos_t *); int _thread_sys_getc(FILE *); int _thread_sys_getchar(void); int _thread_sys_getw(FILE *); int _thread_sys_pclose(FILE *); int _thread_sys_printf(const char *, ...); int _thread_sys_putc(int, FILE *); int _thread_sys_putchar(int); int _thread_sys_puts(const char *); int _thread_sys_putw(int, FILE *); int _thread_sys_remove(const char *); int _thread_sys_rename (const char *, const char *); int _thread_sys_scanf(const char *, ...); int _thread_sys_setlinebuf(FILE *); int _thread_sys_setvbuf(FILE *, char *, int, size_t); int _thread_sys_snprintf(char *, size_t, const char *, ...); int _thread_sys_sprintf(char *, const char *, ...); int _thread_sys_sscanf(const char *, const char *, ...); int _thread_sys_ungetc(int, FILE *); int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); long _thread_sys_ftell(FILE *); size_t _thread_sys_fread(void *, size_t, size_t, FILE *); size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); void _thread_sys_clearerr(FILE *); void _thread_sys_perror(const char *); void _thread_sys_rewind(FILE *); void _thread_sys_setbuf(FILE *, char *); void _thread_sys_setbuffer(FILE *, char *, int); #endif /* #include */ #ifdef _UNISTD_H_ char *_thread_sys_ttyname(int); int _thread_sys_close(int); int _thread_sys_dup(int); int _thread_sys_dup2(int, int); int _thread_sys_exect(const char *, char * const *, char * const *); int _thread_sys_execve(const char *, char * const *, char * const *); int _thread_sys_fchdir(int); int _thread_sys_fchown(int, uid_t, gid_t); int _thread_sys_fsync(int); int _thread_sys_ftruncate(int, off_t); int _thread_sys_pause(void); int _thread_sys_pipe(int *); int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); off_t _thread_sys_lseek(int, off_t, int); pid_t _thread_sys_fork(void); pid_t _thread_sys_tcgetpgrp(int); ssize_t _thread_sys_read(int, void *, size_t); ssize_t _thread_sys_write(int, const void *, size_t); void _thread_sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ int _thread_sys_creat(const char *, mode_t); int _thread_sys_fcntl(int, int, ...); int _thread_sys_flock(int, int); int _thread_sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ int _thread_sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ DIR *___thread_sys_opendir2(const char *, int); DIR *_thread_sys_opendir(const char *); int _thread_sys_alphasort(const void *, const void *); int _thread_sys_scandir(const char *, struct dirent ***, int (*)(struct dirent *), int (*)(const void *, const void *)); int _thread_sys_closedir(DIR *); int _thread_sys_getdirentries(int, char *, int, long *); long _thread_sys_telldir(const DIR *); struct dirent *_thread_sys_readdir(DIR *); void _thread_sys_rewinddir(DIR *); void _thread_sys_seekdir(DIR *, long); #endif /* #include */ #ifdef _SYS_UIO_H_ ssize_t _thread_sys_readv(int, const struct iovec *, int); ssize_t _thread_sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG pid_t _thread_sys_wait(int *); pid_t _thread_sys_waitpid(pid_t, int *, int); pid_t _thread_sys_wait3(int *, int, struct rusage *); pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ int _thread_sys_poll(struct pollfd *, unsigned, int); #endif __END_DECLS #endif /* !_PTHREAD_PRIVATE_H */ Index: head/lib/libc_r/uthread/uthread_create.c =================================================================== --- head/lib/libc_r/uthread/uthread_create.c (revision 48608) +++ head/lib/libc_r/uthread/uthread_create.c (revision 48609) @@ -1,305 +1,303 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_create.c,v 1.13 1999/06/20 08:28:14 jb Exp $ + * $Id: uthread_create.c,v 1.14 1999/07/05 00:35:17 jasone Exp $ */ #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #include "libc_private.h" int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { int f_gc = 0; int i; int ret = 0; int status; pthread_t gc_thread; pthread_t new_thread; pthread_attr_t pattr; void *stack; /* * Locking functions in libc are required when there are * threads other than the initial thread. */ __isthreaded = 1; /* Allocate memory for the thread structure: */ if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; } else { /* Check if default thread attributes are required: */ if (attr == NULL || *attr == NULL) { /* Use the default thread attributes: */ pattr = &pthread_attr_default; } else { pattr = *attr; } /* Check if a stack was specified in the thread attributes: */ if ((stack = pattr->stackaddr_attr) != NULL) { } -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Allocate memory for a default-size stack: */ else if (pattr->stacksize_attr == PTHREAD_STACK_DEFAULT) { struct stack * spare_stack; /* Allocate or re-use a default-size stack. */ /* Use the garbage collector mutex for synchronization * of the spare stack list. * * XXX This may not be ideal. */ if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); if (NULL != (spare_stack = SLIST_FIRST(&_stackq))) { /* Use the spare stack. */ SLIST_REMOVE_HEAD(&_stackq, qe); stack = sizeof(struct stack) + (void *) spare_stack - PTHREAD_STACK_DEFAULT; } else { /* Allocate a new stack. */ stack = _next_stack + PTHREAD_STACK_GUARD; /* Even if stack allocation fails, we don't want to try to use this location again, so unconditionally * decrement _next_stack. Under normal operating conditions, the most likely reason for an mmap() * error is a stack overflow of the adjacent thread stack. */ _next_stack -= (PTHREAD_STACK_DEFAULT + PTHREAD_STACK_GUARD); /* Red zone: */ if (MAP_FAILED == mmap(_next_stack, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { ret = EAGAIN; free(new_thread); } /* Stack: */ else if (MAP_FAILED == mmap(stack, PTHREAD_STACK_DEFAULT, PROT_READ | PROT_WRITE, MAP_STACK, -1, 0)) { ret = EAGAIN; munmap(_next_stack, PTHREAD_STACK_GUARD); free(new_thread); } } /* Unlock the garbage collector mutex. */ if (pthread_mutex_unlock(&_gc_mutex) != 0) PANIC("Cannot unlock gc mutex"); } /* The user wants a stack of a particular size. Lets hope they really know what they want, and simply malloc the * stack. */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #else /* Allocate memory for the stack: */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #endif /* Check for errors: */ if (ret != 0) { } else { /* Initialise the thread structure: */ memset(new_thread, 0, sizeof(struct pthread)); new_thread->slice_usec = -1; new_thread->sig_saved = 0; new_thread->stack = stack; new_thread->start_routine = start_routine; new_thread->arg = arg; /* * Write a magic value to the thread structure * to help identify valid ones: */ new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ new_thread->sigmask = _thread_run->sigmask; /* Initialise the jump buffer: */ setjmp(new_thread->saved_jmp_buf); /* * Set up new stack frame so that it looks like it * returned from a longjmp() to the beginning of * _thread_start(). */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[2] = (long) _thread_start; new_thread->saved_jmp_buf[0]._jb[4 + R_RA] = 0; new_thread->saved_jmp_buf[0]._jb[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0]._jb[0] = (long) _thread_start; #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[2] = (long) _thread_start; new_thread->saved_jmp_buf[4 + R_RA] = 0; new_thread->saved_jmp_buf[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0] = (long) _thread_start; #endif #else #error "Don't recognize this operating system!" #endif /* The stack starts high and builds down: */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[0]._jb[2] = (int) (new_thread->stack + pattr->stacksize_attr - sizeof(double)); #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[2] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #endif #else #error "Don't recognize this operating system!" #endif /* Copy the thread attributes: */ memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr)); /* * Check if this thread is to inherit the scheduling * attributes from its parent: */ if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = _thread_run->base_priority; new_thread->attr.prio = _thread_run->base_priority; new_thread->attr.sched_policy = _thread_run->attr.sched_policy; } else { /* * Use just the thread priority, leaving the * other scheduling attributes as their * default values: */ new_thread->base_priority = new_thread->attr.prio; } new_thread->active_priority = new_thread->base_priority; new_thread->inherited_priority = 0; /* Initialise the join queue for the new thread: */ TAILQ_INIT(&(new_thread->join_queue)); /* Initialize the mutex queue: */ TAILQ_INIT(&new_thread->mutexq); /* Initialise hooks in the thread structure: */ new_thread->specific_data = NULL; new_thread->cleanup = NULL; new_thread->flags = 0; new_thread->poll_data.nfds = 0; new_thread->poll_data.fds = NULL; /* * Defer signals to protect the scheduling queues * from access by the signal handler: */ _thread_kern_sig_defer(); /* * Check if the garbage collector thread * needs to be started. */ f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial); /* Add the thread to the linked list of all threads: */ TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle); if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) { new_thread->state = PS_SUSPENDED; PTHREAD_WAITQ_INSERT(new_thread); } else { new_thread->state = PS_RUNNING; PTHREAD_PRIOQ_INSERT_TAIL(new_thread); } /* * Undefer and handle pending signals, yielding * if necessary. */ _thread_kern_sig_undefer(); /* Return a pointer to the thread structure: */ (*thread) = new_thread; /* Schedule the new user thread: */ _thread_kern_sched(NULL); /* * Start a garbage collector thread * if necessary. */ if (f_gc && pthread_create(&gc_thread,NULL, _thread_gc,NULL) != 0) PANIC("Can't create gc thread"); } } /* Return the status: */ return (ret); } void _thread_start(void) { /* We just left the scheduler via longjmp: */ _thread_kern_in_sched = 0; /* Run the current thread's start routine with argument: */ pthread_exit(_thread_run->start_routine(_thread_run->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } #endif Index: head/lib/libc_r/uthread/uthread_gc.c =================================================================== --- head/lib/libc_r/uthread/uthread_gc.c (revision 48608) +++ head/lib/libc_r/uthread/uthread_gc.c (revision 48609) @@ -1,245 +1,243 @@ /* * Copyright (c) 1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_gc.c,v 1.4 1999/06/20 08:28:25 jb Exp $ + * $Id: uthread_gc.c,v 1.5 1999/07/05 00:35:18 jasone Exp $ * * Garbage collector thread. Frees memory allocated for dead threads. * */ #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #include #include "pthread_private.h" pthread_addr_t _thread_gc(pthread_addr_t arg) { int f_debug; int f_done = 0; int ret; sigset_t mask; pthread_t pthread; pthread_t pthread_cln; pthread_t pthread_nxt; pthread_t pthread_prv; struct timespec abstime; void *p_stack; /* Block all signals */ sigfillset (&mask); sigprocmask (SIG_BLOCK, &mask, NULL); /* Mark this thread as a library thread (not a user thread). */ _thread_run->flags |= PTHREAD_FLAGS_PRIVATE; /* Set a debug flag based on an environment variable. */ f_debug = (getenv("LIBC_R_DEBUG") != NULL); /* Set the name of this thread. */ pthread_set_name_np(_thread_run,"GC"); while (!f_done) { /* Check if debugging this application. */ if (f_debug) /* Dump thread info to file. */ _thread_dump_info(); /* * Defer signals to protect the scheduling queues from * access by the signal handler: */ _thread_kern_sig_defer(); /* Check if this is the last running thread: */ if (TAILQ_FIRST(&_thread_list) == _thread_run && TAILQ_NEXT(_thread_run, tle) == NULL) /* * This is the last thread, so it can exit * now. */ f_done = 1; /* * Undefer and handle pending signals, yielding if * necessary: */ _thread_kern_sig_undefer(); /* * Lock the garbage collector mutex which ensures that * this thread sees another thread exit: */ if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); /* No stack of thread structure to free yet: */ p_stack = NULL; pthread_cln = NULL; /* * Enter a loop to search for the first dead thread that * has memory to free. */ for (pthread = TAILQ_FIRST(&_dead_list); p_stack == NULL && pthread_cln == NULL && pthread != NULL; pthread = TAILQ_NEXT(pthread, dle)) { /* Check if the initial thread: */ if (pthread == _thread_initial) { /* Don't destroy the initial thread. */ } /* * Check if this thread has detached: */ else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) { /* Remove this thread from the dead list: */ TAILQ_REMOVE(&_dead_list, pthread, dle); /* * Check if the stack was not specified by * the caller to pthread_create and has not * been destroyed yet: */ if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) { -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ if (pthread->attr.stacksize_attr == PTHREAD_STACK_DEFAULT) { /* Default-size stack. Cache it: */ struct stack * spare_stack = (pthread->stack + PTHREAD_STACK_DEFAULT - sizeof(struct stack)); SLIST_INSERT_HEAD(&_stackq, spare_stack, qe); } else { /* Non-standard stack size. free() it outside the locks: */ p_stack = pthread->stack; } #else /* * Point to the stack that must * be freed outside the locks: */ p_stack = pthread->stack; #endif } /* * Point to the thread structure that must * be freed outside the locks: */ pthread_cln = pthread; } else { /* * This thread has not detached, so do * not destroy it. * * Check if the stack was not specified by * the caller to pthread_create and has not * been destroyed yet: */ if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) { -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ if (pthread->attr.stacksize_attr == PTHREAD_STACK_DEFAULT) { /* Default-size stack. Cache it: */ struct stack * spare_stack = (pthread->stack + PTHREAD_STACK_DEFAULT - sizeof(struct stack)); SLIST_INSERT_HEAD(&_stackq, spare_stack, qe); } else { /* Non-standard stack size. free() it outside the locks: */ p_stack = pthread->stack; } #else /* * Point to the stack that must * be freed outside the locks: */ p_stack = pthread->stack; #endif /* * NULL the stack pointer now * that the memory has been freed: */ pthread->stack = NULL; } } } /* * Check if this is not the last thread and there is no * memory to free this time around. */ if (!f_done && p_stack == NULL && pthread_cln == NULL) { /* Get the current time. */ if (clock_gettime(CLOCK_REALTIME,&abstime) != 0) PANIC("gc cannot get time"); /* * Do a backup poll in 10 seconds if no threads * die before then. */ abstime.tv_sec += 10; /* * Wait for a signal from a dying thread or a * timeout (for a backup poll). */ if ((ret = pthread_cond_timedwait(&_gc_cond, &_gc_mutex, &abstime)) != 0 && ret != ETIMEDOUT) PANIC("gc cannot wait for a signal"); } /* Unlock the garbage collector mutex: */ if (pthread_mutex_unlock(&_gc_mutex) != 0) PANIC("Cannot unlock gc mutex"); /* * If there is memory to free, do it now. The call to * free() might block, so this must be done outside the * locks. */ if (p_stack != NULL) free(p_stack); if (pthread_cln != NULL) /* * Free the memory allocated for the thread * structure. */ free(pthread_cln); } return (NULL); } Index: head/lib/libc_r/uthread/uthread_init.c =================================================================== --- head/lib/libc_r/uthread/uthread_init.c (revision 48608) +++ head/lib/libc_r/uthread/uthread_init.c (revision 48609) @@ -1,358 +1,356 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_init.c,v 1.12 1999/06/23 15:01:21 dt Exp $ + * $Id: uthread_init.c,v 1.13 1999/07/05 00:35:19 jasone Exp $ */ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE #include #include #include #include #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #ifdef GCC_2_8_MADE_THREAD_AWARE typedef void *** (*dynamic_handler_allocator)(); extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); static pthread_key_t except_head_key; typedef struct { void **__dynamic_handler_chain; void *top_elt[2]; } except_struct; static void ***dynamic_allocator_handler_fn() { except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); if(dh == NULL) { dh = (except_struct *)malloc( sizeof(except_struct) ); memset(dh, '\0', sizeof(except_struct)); dh->__dynamic_handler_chain= dh->top_elt; pthread_setspecific(except_head_key, (void *)dh); } return &dh->__dynamic_handler_chain; } #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization */ void _thread_init(void) { int fd; int flags; int i; size_t len; int mib[2]; struct clockinfo clockinfo; struct sigaction act; /* Check if this function has already been called: */ if (_thread_initial) /* Only initialise the threaded application once. */ return; /* * Check for the special case of this process running as * or in place of init as pid = 1: */ if (getpid() == 1) { /* * Setup a new session for this process which is * assumed to be running as root. */ if (setsid() == -1) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); if (_thread_sys_dup2(fd,0) == -1 || _thread_sys_dup2(fd,1) == -1 || _thread_sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if ((_pthread_stdio_flags[i] = _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) PANIC("Cannot get stdio flags"); /* * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ if (_thread_sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Allocate and initialize the ready queue: */ else if (_pq_alloc(&_readyq, PTHREAD_MIN_PRIORITY, PTHREAD_MAX_PRIORITY) != 0) { /* Abort this application: */ PANIC("Cannot allocate priority ready queue."); } /* Allocate memory for the thread structure of the initial thread: */ else if ((_thread_initial = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* * Insufficient memory to initialise this application, so * abort: */ PANIC("Cannot allocate memory for initial thread"); } else { /* Zero the global kernel thread structure: */ memset(&_thread_kern_thread, 0, sizeof(struct pthread)); _thread_kern_thread.flags = PTHREAD_FLAGS_PRIVATE; memset(_thread_initial, 0, sizeof(struct pthread)); /* Initialize the waiting and work queues: */ TAILQ_INIT(&_waitingq); TAILQ_INIT(&_workq); /* Initialize the scheduling switch hook routine: */ _sched_switch_hook = NULL; -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Initialize the thread stack cache: */ SLIST_INIT(&_stackq); /* Create the red zone for the main stack. */ if (MAP_FAILED == mmap((void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { PANIC("Cannot allocate red zone for initial thread"); } #endif /* * Write a magic value to the thread structure * to help identify valid ones: */ _thread_initial->magic = PTHREAD_MAGIC; /* Default the priority of the initial thread: */ _thread_initial->base_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->active_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->inherited_priority = 0; /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); /* Initialize the owned mutex queue and count: */ TAILQ_INIT(&(_thread_initial->mutexq)); _thread_initial->priority_mutex_count = 0; /* Initialise the rest of the fields: */ _thread_initial->poll_data.nfds = 0; _thread_initial->poll_data.fds = NULL; _thread_initial->sig_defer_count = 0; _thread_initial->yield_on_sig_undefer = 0; _thread_initial->specific_data = NULL; _thread_initial->cleanup = NULL; _thread_initial->flags = 0; _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); _thread_run = _thread_initial; /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); act.sa_handler = (void (*) ()) _thread_sig_handler; act.sa_flags = 0; /* Initialize signal handling: */ _thread_sig_init(); /* Enter a loop to get the existing signal status: */ for (i = 1; i < NSIG; i++) { /* Check for signals which cannot be trapped: */ if (i == SIGKILL || i == SIGSTOP) { } /* Get the signal handler details: */ else if (_thread_sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal * initialisation fails: */ PANIC("Cannot read signal handler info"); } } /* * Install the signal handler for the most important * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ PANIC("Cannot initialise signal handler"); } /* Get the kernel clockrate: */ mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; len = sizeof (struct clockinfo); if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) _clock_res_nsec = clockinfo.tick * 1000; /* Get the table size: */ if ((_thread_dtablesize = getdtablesize()) < 0) { /* * Cannot get the system defined table size, so abort * this process. */ PANIC("Cannot get dtablesize"); } /* Allocate memory for the file descriptor table: */ if ((_thread_fd_table = (struct fd_table_entry **) malloc(sizeof(struct fd_table_entry *) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for file descriptor table"); } /* Allocate memory for the pollfd table: */ if ((_thread_pfd_table = (struct pollfd *) malloc(sizeof(struct pollfd) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for pollfd table"); } else { /* * Enter a loop to initialise the file descriptor * table: */ for (i = 0; i < _thread_dtablesize; i++) { /* Initialise the file descriptor table: */ _thread_fd_table[i] = NULL; } /* Initialize stdio file descriptor table entries: */ if ((_thread_fd_table_init(0) != 0) || (_thread_fd_table_init(1) != 0) || (_thread_fd_table_init(2) != 0)) { PANIC("Cannot initialize stdio file descriptor " "table entries"); } } } #ifdef GCC_2_8_MADE_THREAD_AWARE /* Create the thread-specific data for the exception linked list. */ if(pthread_key_create(&except_head_key, NULL) != 0) PANIC("Failed to create thread specific execption head"); /* Setup the gcc exception handler per thread. */ __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* Initialise the garbage collector mutex and condition variable. */ if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); gettimeofday(&kern_inc_prio_time, NULL); return; } /* * Special start up code for NetBSD/Alpha */ #if defined(__NetBSD__) && defined(__alpha__) int main(int argc, char *argv[], char *env); int _thread_main(int argc, char *argv[], char *env) { _thread_init(); return (main(argc, argv, env)); } #endif #else /* * A stub for non-threaded programs. */ void _thread_init(void) { } #endif Index: head/lib/libkse/Makefile =================================================================== --- head/lib/libkse/Makefile (revision 48608) +++ head/lib/libkse/Makefile (revision 48609) @@ -1,42 +1,39 @@ -# $Id: Makefile,v 1.16 1999/07/05 00:35:14 jasone Exp $ +# $Id: Makefile,v 1.17 1999/07/05 00:38:12 jasone Exp $ # # All library objects contain rcsid strings by default; they may be # excluded as a space-saving measure. To produce a library that does # not contain these strings, delete -DLIBC_RCS and -DSYSLIBC_RCS # from CFLAGS below. To remove these strings from just the system call # stubs, remove just -DSYSLIBC_RCS from CFLAGS. LIB=c_r SHLIB_MAJOR= 4 SHLIB_MINOR= 0 CFLAGS+=-DLIBC_RCS -DSYSLIBC_RCS -I${.CURDIR}/../libc/include CFLAGS+=-DPTHREAD_KERNEL -D_THREAD_SAFE -I${.CURDIR}/uthread # Uncomment this if you want libc_r to contain debug information for # thread locking. CFLAGS+=-D_LOCK_DEBUG - -# Uncomment this if you want libc_r to use growable stacks. -#CFLAGS+=-D_PTHREAD_GSTACK AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes # # This is a list of syscalls that are renamed as _thread_sys_{syscall} # so that libc_r can provide replacement functions. # HIDDEN_SYSCALLS= accept.o bind.o close.o connect.o dup.o dup2.o \ execve.o fchflags.o fchmod.o fchown.o fcntl.o \ flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ getpeername.o getsockname.o getsockopt.o ioctl.o listen.o \ nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ recvmsg.o sched_yield.o select.o sendmsg.o sendto.o \ setsockopt.o shutdown.o sigaction.o sigaltstack.o \ signanosleep.o socket.o socketpair.o wait4.o write.o writev.o .include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" .include Index: head/lib/libkse/thread/thr_create.c =================================================================== --- head/lib/libkse/thread/thr_create.c (revision 48608) +++ head/lib/libkse/thread/thr_create.c (revision 48609) @@ -1,305 +1,303 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_create.c,v 1.13 1999/06/20 08:28:14 jb Exp $ + * $Id: uthread_create.c,v 1.14 1999/07/05 00:35:17 jasone Exp $ */ #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #include "libc_private.h" int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { int f_gc = 0; int i; int ret = 0; int status; pthread_t gc_thread; pthread_t new_thread; pthread_attr_t pattr; void *stack; /* * Locking functions in libc are required when there are * threads other than the initial thread. */ __isthreaded = 1; /* Allocate memory for the thread structure: */ if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; } else { /* Check if default thread attributes are required: */ if (attr == NULL || *attr == NULL) { /* Use the default thread attributes: */ pattr = &pthread_attr_default; } else { pattr = *attr; } /* Check if a stack was specified in the thread attributes: */ if ((stack = pattr->stackaddr_attr) != NULL) { } -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Allocate memory for a default-size stack: */ else if (pattr->stacksize_attr == PTHREAD_STACK_DEFAULT) { struct stack * spare_stack; /* Allocate or re-use a default-size stack. */ /* Use the garbage collector mutex for synchronization * of the spare stack list. * * XXX This may not be ideal. */ if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); if (NULL != (spare_stack = SLIST_FIRST(&_stackq))) { /* Use the spare stack. */ SLIST_REMOVE_HEAD(&_stackq, qe); stack = sizeof(struct stack) + (void *) spare_stack - PTHREAD_STACK_DEFAULT; } else { /* Allocate a new stack. */ stack = _next_stack + PTHREAD_STACK_GUARD; /* Even if stack allocation fails, we don't want to try to use this location again, so unconditionally * decrement _next_stack. Under normal operating conditions, the most likely reason for an mmap() * error is a stack overflow of the adjacent thread stack. */ _next_stack -= (PTHREAD_STACK_DEFAULT + PTHREAD_STACK_GUARD); /* Red zone: */ if (MAP_FAILED == mmap(_next_stack, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { ret = EAGAIN; free(new_thread); } /* Stack: */ else if (MAP_FAILED == mmap(stack, PTHREAD_STACK_DEFAULT, PROT_READ | PROT_WRITE, MAP_STACK, -1, 0)) { ret = EAGAIN; munmap(_next_stack, PTHREAD_STACK_GUARD); free(new_thread); } } /* Unlock the garbage collector mutex. */ if (pthread_mutex_unlock(&_gc_mutex) != 0) PANIC("Cannot unlock gc mutex"); } /* The user wants a stack of a particular size. Lets hope they really know what they want, and simply malloc the * stack. */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #else /* Allocate memory for the stack: */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #endif /* Check for errors: */ if (ret != 0) { } else { /* Initialise the thread structure: */ memset(new_thread, 0, sizeof(struct pthread)); new_thread->slice_usec = -1; new_thread->sig_saved = 0; new_thread->stack = stack; new_thread->start_routine = start_routine; new_thread->arg = arg; /* * Write a magic value to the thread structure * to help identify valid ones: */ new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ new_thread->sigmask = _thread_run->sigmask; /* Initialise the jump buffer: */ setjmp(new_thread->saved_jmp_buf); /* * Set up new stack frame so that it looks like it * returned from a longjmp() to the beginning of * _thread_start(). */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[2] = (long) _thread_start; new_thread->saved_jmp_buf[0]._jb[4 + R_RA] = 0; new_thread->saved_jmp_buf[0]._jb[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0]._jb[0] = (long) _thread_start; #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[2] = (long) _thread_start; new_thread->saved_jmp_buf[4 + R_RA] = 0; new_thread->saved_jmp_buf[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0] = (long) _thread_start; #endif #else #error "Don't recognize this operating system!" #endif /* The stack starts high and builds down: */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[0]._jb[2] = (int) (new_thread->stack + pattr->stacksize_attr - sizeof(double)); #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[2] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #endif #else #error "Don't recognize this operating system!" #endif /* Copy the thread attributes: */ memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr)); /* * Check if this thread is to inherit the scheduling * attributes from its parent: */ if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = _thread_run->base_priority; new_thread->attr.prio = _thread_run->base_priority; new_thread->attr.sched_policy = _thread_run->attr.sched_policy; } else { /* * Use just the thread priority, leaving the * other scheduling attributes as their * default values: */ new_thread->base_priority = new_thread->attr.prio; } new_thread->active_priority = new_thread->base_priority; new_thread->inherited_priority = 0; /* Initialise the join queue for the new thread: */ TAILQ_INIT(&(new_thread->join_queue)); /* Initialize the mutex queue: */ TAILQ_INIT(&new_thread->mutexq); /* Initialise hooks in the thread structure: */ new_thread->specific_data = NULL; new_thread->cleanup = NULL; new_thread->flags = 0; new_thread->poll_data.nfds = 0; new_thread->poll_data.fds = NULL; /* * Defer signals to protect the scheduling queues * from access by the signal handler: */ _thread_kern_sig_defer(); /* * Check if the garbage collector thread * needs to be started. */ f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial); /* Add the thread to the linked list of all threads: */ TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle); if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) { new_thread->state = PS_SUSPENDED; PTHREAD_WAITQ_INSERT(new_thread); } else { new_thread->state = PS_RUNNING; PTHREAD_PRIOQ_INSERT_TAIL(new_thread); } /* * Undefer and handle pending signals, yielding * if necessary. */ _thread_kern_sig_undefer(); /* Return a pointer to the thread structure: */ (*thread) = new_thread; /* Schedule the new user thread: */ _thread_kern_sched(NULL); /* * Start a garbage collector thread * if necessary. */ if (f_gc && pthread_create(&gc_thread,NULL, _thread_gc,NULL) != 0) PANIC("Can't create gc thread"); } } /* Return the status: */ return (ret); } void _thread_start(void) { /* We just left the scheduler via longjmp: */ _thread_kern_in_sched = 0; /* Run the current thread's start routine with argument: */ pthread_exit(_thread_run->start_routine(_thread_run->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } #endif Index: head/lib/libkse/thread/thr_init.c =================================================================== --- head/lib/libkse/thread/thr_init.c (revision 48608) +++ head/lib/libkse/thread/thr_init.c (revision 48609) @@ -1,358 +1,356 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_init.c,v 1.12 1999/06/23 15:01:21 dt Exp $ + * $Id: uthread_init.c,v 1.13 1999/07/05 00:35:19 jasone Exp $ */ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE #include #include #include #include #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #ifdef GCC_2_8_MADE_THREAD_AWARE typedef void *** (*dynamic_handler_allocator)(); extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); static pthread_key_t except_head_key; typedef struct { void **__dynamic_handler_chain; void *top_elt[2]; } except_struct; static void ***dynamic_allocator_handler_fn() { except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); if(dh == NULL) { dh = (except_struct *)malloc( sizeof(except_struct) ); memset(dh, '\0', sizeof(except_struct)); dh->__dynamic_handler_chain= dh->top_elt; pthread_setspecific(except_head_key, (void *)dh); } return &dh->__dynamic_handler_chain; } #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization */ void _thread_init(void) { int fd; int flags; int i; size_t len; int mib[2]; struct clockinfo clockinfo; struct sigaction act; /* Check if this function has already been called: */ if (_thread_initial) /* Only initialise the threaded application once. */ return; /* * Check for the special case of this process running as * or in place of init as pid = 1: */ if (getpid() == 1) { /* * Setup a new session for this process which is * assumed to be running as root. */ if (setsid() == -1) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); if (_thread_sys_dup2(fd,0) == -1 || _thread_sys_dup2(fd,1) == -1 || _thread_sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if ((_pthread_stdio_flags[i] = _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) PANIC("Cannot get stdio flags"); /* * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ if (_thread_sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Allocate and initialize the ready queue: */ else if (_pq_alloc(&_readyq, PTHREAD_MIN_PRIORITY, PTHREAD_MAX_PRIORITY) != 0) { /* Abort this application: */ PANIC("Cannot allocate priority ready queue."); } /* Allocate memory for the thread structure of the initial thread: */ else if ((_thread_initial = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* * Insufficient memory to initialise this application, so * abort: */ PANIC("Cannot allocate memory for initial thread"); } else { /* Zero the global kernel thread structure: */ memset(&_thread_kern_thread, 0, sizeof(struct pthread)); _thread_kern_thread.flags = PTHREAD_FLAGS_PRIVATE; memset(_thread_initial, 0, sizeof(struct pthread)); /* Initialize the waiting and work queues: */ TAILQ_INIT(&_waitingq); TAILQ_INIT(&_workq); /* Initialize the scheduling switch hook routine: */ _sched_switch_hook = NULL; -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Initialize the thread stack cache: */ SLIST_INIT(&_stackq); /* Create the red zone for the main stack. */ if (MAP_FAILED == mmap((void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { PANIC("Cannot allocate red zone for initial thread"); } #endif /* * Write a magic value to the thread structure * to help identify valid ones: */ _thread_initial->magic = PTHREAD_MAGIC; /* Default the priority of the initial thread: */ _thread_initial->base_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->active_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->inherited_priority = 0; /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); /* Initialize the owned mutex queue and count: */ TAILQ_INIT(&(_thread_initial->mutexq)); _thread_initial->priority_mutex_count = 0; /* Initialise the rest of the fields: */ _thread_initial->poll_data.nfds = 0; _thread_initial->poll_data.fds = NULL; _thread_initial->sig_defer_count = 0; _thread_initial->yield_on_sig_undefer = 0; _thread_initial->specific_data = NULL; _thread_initial->cleanup = NULL; _thread_initial->flags = 0; _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); _thread_run = _thread_initial; /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); act.sa_handler = (void (*) ()) _thread_sig_handler; act.sa_flags = 0; /* Initialize signal handling: */ _thread_sig_init(); /* Enter a loop to get the existing signal status: */ for (i = 1; i < NSIG; i++) { /* Check for signals which cannot be trapped: */ if (i == SIGKILL || i == SIGSTOP) { } /* Get the signal handler details: */ else if (_thread_sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal * initialisation fails: */ PANIC("Cannot read signal handler info"); } } /* * Install the signal handler for the most important * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ PANIC("Cannot initialise signal handler"); } /* Get the kernel clockrate: */ mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; len = sizeof (struct clockinfo); if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) _clock_res_nsec = clockinfo.tick * 1000; /* Get the table size: */ if ((_thread_dtablesize = getdtablesize()) < 0) { /* * Cannot get the system defined table size, so abort * this process. */ PANIC("Cannot get dtablesize"); } /* Allocate memory for the file descriptor table: */ if ((_thread_fd_table = (struct fd_table_entry **) malloc(sizeof(struct fd_table_entry *) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for file descriptor table"); } /* Allocate memory for the pollfd table: */ if ((_thread_pfd_table = (struct pollfd *) malloc(sizeof(struct pollfd) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for pollfd table"); } else { /* * Enter a loop to initialise the file descriptor * table: */ for (i = 0; i < _thread_dtablesize; i++) { /* Initialise the file descriptor table: */ _thread_fd_table[i] = NULL; } /* Initialize stdio file descriptor table entries: */ if ((_thread_fd_table_init(0) != 0) || (_thread_fd_table_init(1) != 0) || (_thread_fd_table_init(2) != 0)) { PANIC("Cannot initialize stdio file descriptor " "table entries"); } } } #ifdef GCC_2_8_MADE_THREAD_AWARE /* Create the thread-specific data for the exception linked list. */ if(pthread_key_create(&except_head_key, NULL) != 0) PANIC("Failed to create thread specific execption head"); /* Setup the gcc exception handler per thread. */ __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* Initialise the garbage collector mutex and condition variable. */ if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); gettimeofday(&kern_inc_prio_time, NULL); return; } /* * Special start up code for NetBSD/Alpha */ #if defined(__NetBSD__) && defined(__alpha__) int main(int argc, char *argv[], char *env); int _thread_main(int argc, char *argv[], char *env) { _thread_init(); return (main(argc, argv, env)); } #endif #else /* * A stub for non-threaded programs. */ void _thread_init(void) { } #endif Index: head/lib/libkse/thread/thr_private.h =================================================================== --- head/lib/libkse/thread/thr_private.h (revision 48608) +++ head/lib/libkse/thread/thr_private.h (revision 48609) @@ -1,1164 +1,1160 @@ /* * Copyright (c) 1995-1998 John Birrell . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Private thread definitions for the uthread kernel. * - * $Id: pthread_private.h,v 1.20 1999/06/20 08:28:08 jb Exp $ + * $Id: pthread_private.h,v 1.21 1999/07/05 00:35:17 jasone Exp $ */ #ifndef _PTHREAD_PRIVATE_H #define _PTHREAD_PRIVATE_H /* * Evaluate the storage class specifier. */ #ifdef GLOBAL_PTHREAD_PRIVATE #define SCLASS #else #define SCLASS extern #endif /* * Include files. */ #include #include #include #include #include #include #include #include /* * Kernel fatal error handler macro. */ #define PANIC(string) _thread_exit(__FILE__,__LINE__,string) /* Output debug messages like this: */ #define stdout_debug(_x) _thread_sys_write(1,_x,strlen(_x)); #define stderr_debug(_x) _thread_sys_write(2,_x,strlen(_x)); /* * Priority queue manipulation macros (using pqe link): */ #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd) #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd) #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd) #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq) /* * Waiting queue manipulation macros (using pqe link): */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd) #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd) #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive() #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive() #else #define PTHREAD_WAITQ_REMOVE(thrd) TAILQ_REMOVE(&_waitingq,thrd,pqe) #define PTHREAD_WAITQ_INSERT(thrd) do { \ if ((thrd)->wakeup_time.tv_sec == -1) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else { \ pthread_t tid = TAILQ_FIRST(&_waitingq); \ while ((tid != NULL) && (tid->wakeup_time.tv_sec != -1) && \ ((tid->wakeup_time.tv_sec < (thrd)->wakeup_time.tv_sec) || \ ((tid->wakeup_time.tv_sec == (thrd)->wakeup_time.tv_sec) && \ (tid->wakeup_time.tv_nsec <= (thrd)->wakeup_time.tv_nsec)))) \ tid = TAILQ_NEXT(tid, pqe); \ if (tid == NULL) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else \ TAILQ_INSERT_BEFORE(tid,thrd,pqe); \ } \ } while (0) #define PTHREAD_WAITQ_CLEARACTIVE() #define PTHREAD_WAITQ_SETACTIVE() #endif /* * Work queue manipulation macros (using qe link): */ #define PTHREAD_WORKQ_INSERT(thrd) do { \ TAILQ_INSERT_TAIL(&_workq,thrd,qe); \ (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \ } while (0) #define PTHREAD_WORKQ_REMOVE(thrd) do { \ TAILQ_REMOVE(&_workq,thrd,qe); \ (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \ } while (0) /* * State change macro without scheduling queue change: */ #define PTHREAD_SET_STATE(thrd, newstate) do { \ (thrd)->state = newstate; \ (thrd)->fname = __FILE__; \ (thrd)->lineno = __LINE__; \ } while (0) /* * State change macro with scheduling queue change - This must be * called with preemption deferred (see thread_kern_sched_[un]defer). */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if (_thread_kern_new_state != 0) \ PANIC("Recursive PTHREAD_NEW_STATE"); \ _thread_kern_new_state = 1; \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ _thread_kern_new_state = 0; \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #else #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #endif /* * Define the signals to be used for scheduling. */ #if defined(_PTHREADS_COMPAT_SCHED) #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL #define _SCHED_SIGNAL SIGVTALRM #else #define _ITIMER_SCHED_TIMER ITIMER_PROF #define _SCHED_SIGNAL SIGPROF #endif /* * Priority queues. * * XXX It'd be nice if these were contained in uthread_priority_queue.[ch]. */ typedef struct pq_list { TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */ TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */ int pl_prio; /* the priority of this list */ int pl_queued; /* is this in the priority queue */ } pq_list_t; typedef struct pq_queue { TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */ pq_list_t *pq_lists; /* array of all priority lists */ int pq_size; /* number of priority lists */ } pq_queue_t; /* * TailQ initialization values. */ #define TAILQ_INITIALIZER { NULL, NULL } /* * Mutex definitions. */ union pthread_mutex_data { void *m_ptr; int m_count; }; struct pthread_mutex { enum pthread_mutextype m_type; int m_protocol; TAILQ_HEAD(mutex_head, pthread) m_queue; struct pthread *m_owner; union pthread_mutex_data m_data; long m_flags; int m_refcount; /* * Used for priority inheritence and protection. * * m_prio - For priority inheritence, the highest active * priority (threads locking the mutex inherit * this priority). For priority protection, the * ceiling priority of this mutex. * m_saved_prio - mutex owners inherited priority before * taking the mutex, restored when the owner * unlocks the mutex. */ int m_prio; int m_saved_prio; /* * Link for list of all mutexes a thread currently owns. */ TAILQ_ENTRY(pthread_mutex) m_qe; /* * Lock for accesses to this structure. */ spinlock_t lock; }; /* * Flags for mutexes. */ #define MUTEX_FLAGS_PRIVATE 0x01 #define MUTEX_FLAGS_INITED 0x02 #define MUTEX_FLAGS_BUSY 0x04 /* * Static mutex initialization values. */ #define PTHREAD_MUTEX_STATIC_INITIALIZER \ { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \ NULL, { NULL }, 0, 0, 0, 0, TAILQ_INITIALIZER, \ _SPINLOCK_INITIALIZER } struct pthread_mutex_attr { enum pthread_mutextype m_type; int m_protocol; int m_ceiling; long m_flags; }; /* * Condition variable definitions. */ enum pthread_cond_type { COND_TYPE_FAST, COND_TYPE_MAX }; struct pthread_cond { enum pthread_cond_type c_type; TAILQ_HEAD(cond_head, pthread) c_queue; pthread_mutex_t c_mutex; void *c_data; long c_flags; /* * Lock for accesses to this structure. */ spinlock_t lock; }; struct pthread_cond_attr { enum pthread_cond_type c_type; long c_flags; }; /* * Flags for condition variables. */ #define COND_FLAGS_PRIVATE 0x01 #define COND_FLAGS_INITED 0x02 #define COND_FLAGS_BUSY 0x04 /* * Static cond initialization values. */ #define PTHREAD_COND_STATIC_INITIALIZER \ { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \ 0, _SPINLOCK_INITIALIZER } /* * Cleanup definitions. */ struct pthread_cleanup { struct pthread_cleanup *next; void (*routine) (); void *routine_arg; }; struct pthread_attr { int sched_policy; int sched_inherit; int sched_interval; int prio; int suspend; int flags; void *arg_attr; void (*cleanup_attr) (); void *stackaddr_attr; size_t stacksize_attr; }; /* * Thread creation state attributes. */ #define PTHREAD_CREATE_RUNNING 0 #define PTHREAD_CREATE_SUSPENDED 1 /* * Miscellaneous definitions. */ #define PTHREAD_STACK_DEFAULT 65536 -#ifdef _PTHREAD_GSTACK /* Size of red zone at the end of each stack. */ #define PTHREAD_STACK_GUARD 4096 /* Maximum size of initial thread's stack. This perhaps deserves to be larger - * than the stacks of other threads, since legacy applications are likely to run + * than the stacks of other threads, since many applications are likely to run * almost entirely on this stack. */ #define PTHREAD_STACK_INITIAL 0x100000 /* Address immediately beyond the beginning of the initial thread stack. */ #if defined(__FreeBSD__) -# if defined(__alpha__) -# define PTHREAD_STACK_TOP 0x160022000 -# else -# define PTHREAD_STACK_TOP 0xbfbde000 -# endif +# if defined(__i386__) +# define PTHREAD_STACK_TOP 0xbfbde000 +# elif defined(__alpha__) +# define PTHREAD_STACK_TOP 0x160022000 +# else +# error "Don't recognize this architecture!" +# endif #else -# error "Don't recognize this operating system!" +# error "Don't recognize this operating system!" #endif -#endif #define PTHREAD_DEFAULT_PRIORITY 64 #define PTHREAD_MAX_PRIORITY 126 #define PTHREAD_MIN_PRIORITY 0 #define _POSIX_THREAD_ATTR_STACKSIZE /* * Clock resolution in nanoseconds. */ #define CLOCK_RES_NSEC 10000000 /* * Time slice period in microseconds. */ #define TIMESLICE_USEC 100000 struct pthread_key { spinlock_t lock; volatile int allocated; volatile int count; void (*destructor) (); }; struct pthread_rwlockattr { int pshared; }; struct pthread_rwlock { pthread_mutex_t lock; /* monitor lock */ int state; /* 0 = idle >0 = # of readers -1 = writer */ pthread_cond_t read_signal; pthread_cond_t write_signal; int blocked_writers; }; /* * Thread states. */ enum pthread_state { PS_RUNNING, PS_SIGTHREAD, PS_MUTEX_WAIT, PS_COND_WAIT, PS_FDLR_WAIT, PS_FDLW_WAIT, PS_FDR_WAIT, PS_FDW_WAIT, PS_FILE_WAIT, PS_POLL_WAIT, PS_SELECT_WAIT, PS_SLEEP_WAIT, PS_WAIT_WAIT, PS_SIGSUSPEND, PS_SIGWAIT, PS_SPINBLOCK, PS_JOIN, PS_SUSPENDED, PS_DEAD, PS_DEADLOCK, PS_STATE_MAX }; /* * File descriptor locking definitions. */ #define FD_READ 0x1 #define FD_WRITE 0x2 #define FD_RDWR (FD_READ | FD_WRITE) /* * File descriptor table structure. */ struct fd_table_entry { /* * Lock for accesses to this file descriptor table * entry. This is passed to _spinlock() to provide atomic * access to this structure. It does *not* represent the * state of the lock on the file descriptor. */ spinlock_t lock; TAILQ_HEAD(, pthread) r_queue; /* Read queue. */ TAILQ_HEAD(, pthread) w_queue; /* Write queue. */ struct pthread *r_owner; /* Ptr to thread owning read lock. */ struct pthread *w_owner; /* Ptr to thread owning write lock. */ char *r_fname; /* Ptr to read lock source file name */ int r_lineno; /* Read lock source line number. */ char *w_fname; /* Ptr to write lock source file name */ int w_lineno; /* Write lock source line number. */ int r_lockcount; /* Count for FILE read locks. */ int w_lockcount; /* Count for FILE write locks. */ int flags; /* Flags used in open. */ }; struct pthread_poll_data { int nfds; struct pollfd *fds; }; union pthread_wait_data { pthread_mutex_t mutex; pthread_cond_t cond; const sigset_t *sigwait; /* Waiting on a signal in sigwait */ struct { short fd; /* Used when thread waiting on fd */ short branch; /* Line number, for debugging. */ char *fname; /* Source file name for debugging.*/ } fd; struct pthread_poll_data * poll_data; spinlock_t *spinlock; }; /* * Thread structure. */ struct pthread { /* * Magic value to help recognize a valid thread structure * from an invalid one: */ #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115) u_int32_t magic; char *name; /* * Lock for accesses to this thread structure. */ spinlock_t lock; /* Queue entry for list of all threads: */ TAILQ_ENTRY(pthread) tle; /* Queue entry for list of dead threads: */ TAILQ_ENTRY(pthread) dle; /* * Thread start routine, argument, stack pointer and thread * attributes. */ void *(*start_routine)(void *); void *arg; void *stack; struct pthread_attr attr; #if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(__i386__) /* * Saved floating point registers on systems where they are not * saved in the signal context. */ char saved_fp[108]; #endif /* * Saved signal context used in call to sigreturn by * _thread_kern_sched if sig_saved is TRUE. */ struct sigcontext saved_sigcontext; /* * Saved jump buffer used in call to longjmp by _thread_kern_sched * if sig_saved is FALSE. */ jmp_buf saved_jmp_buf; /* * TRUE if the last state saved was a signal context. FALSE if the * last state saved was a jump buffer. */ int sig_saved; /* * Current signal mask and pending signals. */ sigset_t sigmask; sigset_t sigpend; /* Thread state: */ enum pthread_state state; /* Time that this thread was last made active. */ struct timeval last_active; /* Time that this thread was last made inactive. */ struct timeval last_inactive; /* * Number of microseconds accumulated by this thread when * time slicing is active. */ long slice_usec; /* * Incremental priority accumulated by thread while it is ready to * run but is denied being run. */ int inc_prio; /* * Time to wake up thread. This is used for sleeping threads and * for any operation which may time out (such as select). */ struct timespec wakeup_time; /* TRUE if operation has timed out. */ int timeout; /* * Error variable used instead of errno. The function __error() * returns a pointer to this. */ int error; /* Join queue head and link for waiting threads: */ TAILQ_HEAD(join_head, pthread) join_queue; /* * The current thread can belong to only one scheduling queue at * a time (ready or waiting queue). It can also belong to (only) * one of: * * o A queue of threads waiting for a mutex * o A queue of threads waiting for a condition variable * o A queue of threads waiting for another thread to terminate * (the join queue above) * o A queue of threads waiting for a file descriptor lock * o A queue of threads needing work done by the kernel thread * (waiting for a spinlock or file I/O) * * Use pqe for the scheduling queue link (both ready and waiting), * and qe for other links. */ /* Priority queue entry for this thread: */ TAILQ_ENTRY(pthread) pqe; /* Queue entry for this thread: */ TAILQ_ENTRY(pthread) qe; /* Wait data. */ union pthread_wait_data data; /* * Allocated for converting select into poll. */ struct pthread_poll_data poll_data; /* * Set to TRUE if a blocking operation was * interrupted by a signal: */ int interrupted; /* Signal number when in state PS_SIGWAIT: */ int signo; /* * Set to non-zero when this thread has deferred signals. * We allow for recursive deferral. */ int sig_defer_count; /* * Set to TRUE if this thread should yield after undeferring * signals. */ int yield_on_sig_undefer; /* Miscellaneous data. */ int flags; #define PTHREAD_FLAGS_PRIVATE 0x0001 #define PTHREAD_EXITING 0x0002 #define PTHREAD_FLAGS_IN_CONDQ 0x0004 /* in condition queue using qe link*/ #define PTHREAD_FLAGS_IN_WORKQ 0x0008 /* in work queue using qe link */ #define PTHREAD_FLAGS_IN_WAITQ 0x0010 /* in waiting queue using pqe link*/ #define PTHREAD_FLAGS_IN_PRIOQ 0x0020 /* in priority queue using pqe link*/ #define PTHREAD_FLAGS_TRACE 0x0040 /* for debugging purposes */ /* * Base priority is the user setable and retrievable priority * of the thread. It is only affected by explicit calls to * set thread priority and upon thread creation via a thread * attribute or default priority. */ char base_priority; /* * Inherited priority is the priority a thread inherits by * taking a priority inheritence or protection mutex. It * is not affected by base priority changes. Inherited * priority defaults to and remains 0 until a mutex is taken * that is being waited on by any other thread whose priority * is non-zero. */ char inherited_priority; /* * Active priority is always the maximum of the threads base * priority and inherited priority. When there is a change * in either the base or inherited priority, the active * priority must be recalculated. */ char active_priority; /* Number of priority ceiling or protection mutexes owned. */ int priority_mutex_count; /* * Queue of currently owned mutexes. */ TAILQ_HEAD(, pthread_mutex) mutexq; void *ret; const void **specific_data; int specific_data_count; /* Cleanup handlers Link List */ struct pthread_cleanup *cleanup; char *fname; /* Ptr to source file name */ int lineno; /* Source line number. */ }; -#ifdef _PTHREAD_GSTACK /* Spare thread stack. */ struct stack { SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */ }; -#endif /* * Global variables for the uthread kernel. */ /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; /* Ptr to the thread structure for the running thread: */ SCLASS struct pthread * volatile _thread_run #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* Ptr to the thread structure for the last user thread to run: */ SCLASS struct pthread * volatile _last_user_thread #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* * Ptr to the thread running in single-threaded mode or NULL if * running multi-threaded (default POSIX behaviour). */ SCLASS struct pthread * volatile _thread_single #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* List of all threads: */ SCLASS TAILQ_HEAD(, pthread) _thread_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_thread_list); #else ; #endif /* * Array of kernel pipe file descriptors that are used to ensure that * no signals are missed in calls to _select. */ SCLASS int _thread_kern_pipe[2] #ifdef GLOBAL_PTHREAD_PRIVATE = { -1, -1 }; #else ; #endif SCLASS int volatile _queue_signals #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif SCLASS int _thread_kern_in_sched #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif /* Last time that an incremental priority update was performed: */ SCLASS struct timeval kern_inc_prio_time #ifdef GLOBAL_PTHREAD_PRIVATE = { 0, 0 }; #else ; #endif /* Dead threads: */ SCLASS TAILQ_HEAD(, pthread) _dead_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_dead_list); #else ; #endif /* Initial thread: */ SCLASS struct pthread *_thread_initial #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Default thread attributes: */ SCLASS struct pthread_attr pthread_attr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING, PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT }; #else ; #endif /* Default mutex attributes: */ SCLASS struct pthread_mutex_attr pthread_mutexattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 }; #else ; #endif /* Default condition variable attributes: */ SCLASS struct pthread_cond_attr pthread_condattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { COND_TYPE_FAST, 0 }; #else ; #endif /* * Standard I/O file descriptors need special flag treatment since * setting one to non-blocking does all on *BSD. Sigh. This array * is used to store the initial flag settings. */ SCLASS int _pthread_stdio_flags[3]; /* File table information: */ SCLASS struct fd_table_entry **_thread_fd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Table for polling file descriptors: */ SCLASS struct pollfd *_thread_pfd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif SCLASS const int dtablecount #ifdef GLOBAL_PTHREAD_PRIVATE = 4096/sizeof(struct fd_table_entry); #else ; #endif SCLASS int _thread_dtablesize /* Descriptor table size. */ #ifdef GLOBAL_PTHREAD_PRIVATE = 1024; #else ; #endif SCLASS int _clock_res_nsec /* Clock resolution in nsec. */ #ifdef GLOBAL_PTHREAD_PRIVATE = CLOCK_RES_NSEC; #else ; #endif /* Garbage collector mutex and condition variable. */ SCLASS pthread_mutex_t _gc_mutex #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; SCLASS pthread_cond_t _gc_cond #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; /* * Array of signal actions for this process. */ struct sigaction _thread_sigact[NSIG]; /* * Scheduling queues: */ SCLASS pq_queue_t _readyq; SCLASS TAILQ_HEAD(, pthread) _waitingq; /* * Work queue: */ SCLASS TAILQ_HEAD(, pthread) _workq; /* Tracks the number of threads blocked while waiting for a spinlock. */ SCLASS volatile int _spinblock_count #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Indicates that the signal queue needs to be checked. */ SCLASS volatile int _sigq_check_reqd #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Thread switch hook. */ SCLASS pthread_switch_routine_t _sched_switch_hook #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; -#ifdef _PTHREAD_GSTACK /* Spare stack queue. Stacks of default size are cached in order to reduce * thread creation time. Spare stacks are used in LIFO order to increase cache * locality. */ SCLASS SLIST_HEAD(, stack) _stackq; /* Base address of next unallocated default-size stack. Stacks are allocated * contiguously, starting below the beginning of the main stack. When a new * stack is created, a guard page is created just above it in order to (usually) * detect attempts by the adjacent stack to trounce the next thread stack. */ SCLASS void * _next_stack #ifdef GLOBAL_PTHREAD_PRIVATE /* main stack top - main stack size - stack size - (red zone + main stack red zone) */ = (void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD) #endif ; -#endif /* Used for _PTHREADS_INVARIANTS checking. */ SCLASS int _thread_kern_new_state #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Undefine the storage class specifier: */ #undef SCLASS #ifdef _LOCK_DEBUG #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \ _ts, __FILE__, __LINE__) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \ __FILE__, __LINE__) #else #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type) #endif /* * Function prototype definitions. */ __BEGIN_DECLS char *__ttyname_basic(int); char *__ttyname_r_basic(int, char *, size_t); char *ttyname_r(int, char *, size_t); int _find_dead_thread(pthread_t); int _find_thread(pthread_t); int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t); int _thread_fd_lock(int, int, struct timespec *); int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno); void _dispatch_signals(void); void _thread_signal(pthread_t, int); int _mutex_cv_lock(pthread_mutex_t *); int _mutex_cv_unlock(pthread_mutex_t *); int _mutex_reinit(pthread_mutex_t *); void _mutex_notify_priochange(struct pthread *); int _cond_reinit(pthread_cond_t *); int _pq_alloc(struct pq_queue *, int, int); int _pq_init(struct pq_queue *); void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); #if defined(_PTHREADS_INVARIANTS) void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); void _waitq_setactive(void); void _waitq_clearactive(void); #endif void _thread_exit(char *, int, char *); void _thread_fd_unlock(int, int); void _thread_fd_unlock_debug(int, int, char *, int); void *_thread_cleanup(pthread_t); void _thread_cleanupspecific(void); void _thread_dump_info(void); void _thread_init(void); void _thread_kern_sched(struct sigcontext *); void _thread_kern_sched_state(enum pthread_state,char *fname,int lineno); void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno); void _thread_kern_set_timeout(struct timespec *); void _thread_kern_sig_defer(void); void _thread_kern_sig_undefer(void); void _thread_sig_handler(int, int, struct sigcontext *); void _thread_sig_handle(int, struct sigcontext *); void _thread_sig_init(void); void _thread_start(void); void _thread_start_sig_handler(void); void _thread_seterrno(pthread_t,int); int _thread_fd_table_init(int fd); pthread_addr_t _thread_gc(pthread_addr_t); /* #include */ int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); int _thread_sys_sigpending(sigset_t *); int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); int _thread_sys_sigsuspend(const sigset_t *); int _thread_sys_siginterrupt(int, int); int _thread_sys_sigpause(int); int _thread_sys_sigreturn(struct sigcontext *); int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); void _thread_sys_psignal(unsigned int, const char *); void (*_thread_sys_signal(int, void (*)(int)))(int); /* #include */ #ifdef _SYS_STAT_H_ int _thread_sys_fchmod(int, mode_t); int _thread_sys_fstat(int, struct stat *); int _thread_sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ int _thread_sys_fstatfs(int, struct statfs *); #endif int _thread_sys_pipe(int *); /* #include */ #ifdef _SYS_SOCKET_H_ int _thread_sys_accept(int, struct sockaddr *, int *); int _thread_sys_bind(int, const struct sockaddr *, int); int _thread_sys_connect(int, const struct sockaddr *, int); int _thread_sys_getpeername(int, struct sockaddr *, int *); int _thread_sys_getsockname(int, struct sockaddr *, int *); int _thread_sys_getsockopt(int, int, int, void *, int *); int _thread_sys_listen(int, int); int _thread_sys_setsockopt(int, int, int, const void *, int); int _thread_sys_shutdown(int, int); int _thread_sys_socket(int, int, int); int _thread_sys_socketpair(int, int, int, int *); ssize_t _thread_sys_recv(int, void *, size_t, int); ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); ssize_t _thread_sys_send(int, const void *, size_t, int); ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _STDIO_H_ FILE *_thread_sys_fdopen(int, const char *); FILE *_thread_sys_fopen(const char *, const char *); FILE *_thread_sys_freopen(const char *, const char *, FILE *); FILE *_thread_sys_popen(const char *, const char *); FILE *_thread_sys_tmpfile(void); char *_thread_sys_ctermid(char *); char *_thread_sys_cuserid(char *); char *_thread_sys_fgetln(FILE *, size_t *); char *_thread_sys_fgets(char *, int, FILE *); char *_thread_sys_gets(char *); char *_thread_sys_tempnam(const char *, const char *); char *_thread_sys_tmpnam(char *); int _thread_sys_fclose(FILE *); int _thread_sys_feof(FILE *); int _thread_sys_ferror(FILE *); int _thread_sys_fflush(FILE *); int _thread_sys_fgetc(FILE *); int _thread_sys_fgetpos(FILE *, fpos_t *); int _thread_sys_fileno(FILE *); int _thread_sys_fprintf(FILE *, const char *, ...); int _thread_sys_fpurge(FILE *); int _thread_sys_fputc(int, FILE *); int _thread_sys_fputs(const char *, FILE *); int _thread_sys_fscanf(FILE *, const char *, ...); int _thread_sys_fseek(FILE *, long, int); int _thread_sys_fsetpos(FILE *, const fpos_t *); int _thread_sys_getc(FILE *); int _thread_sys_getchar(void); int _thread_sys_getw(FILE *); int _thread_sys_pclose(FILE *); int _thread_sys_printf(const char *, ...); int _thread_sys_putc(int, FILE *); int _thread_sys_putchar(int); int _thread_sys_puts(const char *); int _thread_sys_putw(int, FILE *); int _thread_sys_remove(const char *); int _thread_sys_rename (const char *, const char *); int _thread_sys_scanf(const char *, ...); int _thread_sys_setlinebuf(FILE *); int _thread_sys_setvbuf(FILE *, char *, int, size_t); int _thread_sys_snprintf(char *, size_t, const char *, ...); int _thread_sys_sprintf(char *, const char *, ...); int _thread_sys_sscanf(const char *, const char *, ...); int _thread_sys_ungetc(int, FILE *); int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); long _thread_sys_ftell(FILE *); size_t _thread_sys_fread(void *, size_t, size_t, FILE *); size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); void _thread_sys_clearerr(FILE *); void _thread_sys_perror(const char *); void _thread_sys_rewind(FILE *); void _thread_sys_setbuf(FILE *, char *); void _thread_sys_setbuffer(FILE *, char *, int); #endif /* #include */ #ifdef _UNISTD_H_ char *_thread_sys_ttyname(int); int _thread_sys_close(int); int _thread_sys_dup(int); int _thread_sys_dup2(int, int); int _thread_sys_exect(const char *, char * const *, char * const *); int _thread_sys_execve(const char *, char * const *, char * const *); int _thread_sys_fchdir(int); int _thread_sys_fchown(int, uid_t, gid_t); int _thread_sys_fsync(int); int _thread_sys_ftruncate(int, off_t); int _thread_sys_pause(void); int _thread_sys_pipe(int *); int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); off_t _thread_sys_lseek(int, off_t, int); pid_t _thread_sys_fork(void); pid_t _thread_sys_tcgetpgrp(int); ssize_t _thread_sys_read(int, void *, size_t); ssize_t _thread_sys_write(int, const void *, size_t); void _thread_sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ int _thread_sys_creat(const char *, mode_t); int _thread_sys_fcntl(int, int, ...); int _thread_sys_flock(int, int); int _thread_sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ int _thread_sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ DIR *___thread_sys_opendir2(const char *, int); DIR *_thread_sys_opendir(const char *); int _thread_sys_alphasort(const void *, const void *); int _thread_sys_scandir(const char *, struct dirent ***, int (*)(struct dirent *), int (*)(const void *, const void *)); int _thread_sys_closedir(DIR *); int _thread_sys_getdirentries(int, char *, int, long *); long _thread_sys_telldir(const DIR *); struct dirent *_thread_sys_readdir(DIR *); void _thread_sys_rewinddir(DIR *); void _thread_sys_seekdir(DIR *, long); #endif /* #include */ #ifdef _SYS_UIO_H_ ssize_t _thread_sys_readv(int, const struct iovec *, int); ssize_t _thread_sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG pid_t _thread_sys_wait(int *); pid_t _thread_sys_waitpid(pid_t, int *, int); pid_t _thread_sys_wait3(int *, int, struct rusage *); pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ int _thread_sys_poll(struct pollfd *, unsigned, int); #endif __END_DECLS #endif /* !_PTHREAD_PRIVATE_H */ Index: head/lib/libpthread/Makefile =================================================================== --- head/lib/libpthread/Makefile (revision 48608) +++ head/lib/libpthread/Makefile (revision 48609) @@ -1,42 +1,39 @@ -# $Id: Makefile,v 1.16 1999/07/05 00:35:14 jasone Exp $ +# $Id: Makefile,v 1.17 1999/07/05 00:38:12 jasone Exp $ # # All library objects contain rcsid strings by default; they may be # excluded as a space-saving measure. To produce a library that does # not contain these strings, delete -DLIBC_RCS and -DSYSLIBC_RCS # from CFLAGS below. To remove these strings from just the system call # stubs, remove just -DSYSLIBC_RCS from CFLAGS. LIB=c_r SHLIB_MAJOR= 4 SHLIB_MINOR= 0 CFLAGS+=-DLIBC_RCS -DSYSLIBC_RCS -I${.CURDIR}/../libc/include CFLAGS+=-DPTHREAD_KERNEL -D_THREAD_SAFE -I${.CURDIR}/uthread # Uncomment this if you want libc_r to contain debug information for # thread locking. CFLAGS+=-D_LOCK_DEBUG - -# Uncomment this if you want libc_r to use growable stacks. -#CFLAGS+=-D_PTHREAD_GSTACK AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes # # This is a list of syscalls that are renamed as _thread_sys_{syscall} # so that libc_r can provide replacement functions. # HIDDEN_SYSCALLS= accept.o bind.o close.o connect.o dup.o dup2.o \ execve.o fchflags.o fchmod.o fchown.o fcntl.o \ flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ getpeername.o getsockname.o getsockopt.o ioctl.o listen.o \ nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ recvmsg.o sched_yield.o select.o sendmsg.o sendto.o \ setsockopt.o shutdown.o sigaction.o sigaltstack.o \ signanosleep.o socket.o socketpair.o wait4.o write.o writev.o .include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" .include Index: head/lib/libpthread/thread/thr_create.c =================================================================== --- head/lib/libpthread/thread/thr_create.c (revision 48608) +++ head/lib/libpthread/thread/thr_create.c (revision 48609) @@ -1,305 +1,303 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_create.c,v 1.13 1999/06/20 08:28:14 jb Exp $ + * $Id: uthread_create.c,v 1.14 1999/07/05 00:35:17 jasone Exp $ */ #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #include "libc_private.h" int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { int f_gc = 0; int i; int ret = 0; int status; pthread_t gc_thread; pthread_t new_thread; pthread_attr_t pattr; void *stack; /* * Locking functions in libc are required when there are * threads other than the initial thread. */ __isthreaded = 1; /* Allocate memory for the thread structure: */ if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; } else { /* Check if default thread attributes are required: */ if (attr == NULL || *attr == NULL) { /* Use the default thread attributes: */ pattr = &pthread_attr_default; } else { pattr = *attr; } /* Check if a stack was specified in the thread attributes: */ if ((stack = pattr->stackaddr_attr) != NULL) { } -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Allocate memory for a default-size stack: */ else if (pattr->stacksize_attr == PTHREAD_STACK_DEFAULT) { struct stack * spare_stack; /* Allocate or re-use a default-size stack. */ /* Use the garbage collector mutex for synchronization * of the spare stack list. * * XXX This may not be ideal. */ if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); if (NULL != (spare_stack = SLIST_FIRST(&_stackq))) { /* Use the spare stack. */ SLIST_REMOVE_HEAD(&_stackq, qe); stack = sizeof(struct stack) + (void *) spare_stack - PTHREAD_STACK_DEFAULT; } else { /* Allocate a new stack. */ stack = _next_stack + PTHREAD_STACK_GUARD; /* Even if stack allocation fails, we don't want to try to use this location again, so unconditionally * decrement _next_stack. Under normal operating conditions, the most likely reason for an mmap() * error is a stack overflow of the adjacent thread stack. */ _next_stack -= (PTHREAD_STACK_DEFAULT + PTHREAD_STACK_GUARD); /* Red zone: */ if (MAP_FAILED == mmap(_next_stack, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { ret = EAGAIN; free(new_thread); } /* Stack: */ else if (MAP_FAILED == mmap(stack, PTHREAD_STACK_DEFAULT, PROT_READ | PROT_WRITE, MAP_STACK, -1, 0)) { ret = EAGAIN; munmap(_next_stack, PTHREAD_STACK_GUARD); free(new_thread); } } /* Unlock the garbage collector mutex. */ if (pthread_mutex_unlock(&_gc_mutex) != 0) PANIC("Cannot unlock gc mutex"); } /* The user wants a stack of a particular size. Lets hope they really know what they want, and simply malloc the * stack. */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #else /* Allocate memory for the stack: */ else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) { /* Insufficient memory to create a thread: */ ret = EAGAIN; free(new_thread); } #endif /* Check for errors: */ if (ret != 0) { } else { /* Initialise the thread structure: */ memset(new_thread, 0, sizeof(struct pthread)); new_thread->slice_usec = -1; new_thread->sig_saved = 0; new_thread->stack = stack; new_thread->start_routine = start_routine; new_thread->arg = arg; /* * Write a magic value to the thread structure * to help identify valid ones: */ new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ new_thread->sigmask = _thread_run->sigmask; /* Initialise the jump buffer: */ setjmp(new_thread->saved_jmp_buf); /* * Set up new stack frame so that it looks like it * returned from a longjmp() to the beginning of * _thread_start(). */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[2] = (long) _thread_start; new_thread->saved_jmp_buf[0]._jb[4 + R_RA] = 0; new_thread->saved_jmp_buf[0]._jb[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0]._jb[0] = (long) _thread_start; #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[2] = (long) _thread_start; new_thread->saved_jmp_buf[4 + R_RA] = 0; new_thread->saved_jmp_buf[4 + R_T12] = (long) _thread_start; #else new_thread->saved_jmp_buf[0] = (long) _thread_start; #endif #else #error "Don't recognize this operating system!" #endif /* The stack starts high and builds down: */ #if defined(__FreeBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[0]._jb[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[0]._jb[2] = (int) (new_thread->stack + pattr->stacksize_attr - sizeof(double)); #endif #elif defined(__NetBSD__) #if defined(__alpha__) new_thread->saved_jmp_buf[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #else new_thread->saved_jmp_buf[2] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double); #endif #else #error "Don't recognize this operating system!" #endif /* Copy the thread attributes: */ memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr)); /* * Check if this thread is to inherit the scheduling * attributes from its parent: */ if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = _thread_run->base_priority; new_thread->attr.prio = _thread_run->base_priority; new_thread->attr.sched_policy = _thread_run->attr.sched_policy; } else { /* * Use just the thread priority, leaving the * other scheduling attributes as their * default values: */ new_thread->base_priority = new_thread->attr.prio; } new_thread->active_priority = new_thread->base_priority; new_thread->inherited_priority = 0; /* Initialise the join queue for the new thread: */ TAILQ_INIT(&(new_thread->join_queue)); /* Initialize the mutex queue: */ TAILQ_INIT(&new_thread->mutexq); /* Initialise hooks in the thread structure: */ new_thread->specific_data = NULL; new_thread->cleanup = NULL; new_thread->flags = 0; new_thread->poll_data.nfds = 0; new_thread->poll_data.fds = NULL; /* * Defer signals to protect the scheduling queues * from access by the signal handler: */ _thread_kern_sig_defer(); /* * Check if the garbage collector thread * needs to be started. */ f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial); /* Add the thread to the linked list of all threads: */ TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle); if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) { new_thread->state = PS_SUSPENDED; PTHREAD_WAITQ_INSERT(new_thread); } else { new_thread->state = PS_RUNNING; PTHREAD_PRIOQ_INSERT_TAIL(new_thread); } /* * Undefer and handle pending signals, yielding * if necessary. */ _thread_kern_sig_undefer(); /* Return a pointer to the thread structure: */ (*thread) = new_thread; /* Schedule the new user thread: */ _thread_kern_sched(NULL); /* * Start a garbage collector thread * if necessary. */ if (f_gc && pthread_create(&gc_thread,NULL, _thread_gc,NULL) != 0) PANIC("Can't create gc thread"); } } /* Return the status: */ return (ret); } void _thread_start(void) { /* We just left the scheduler via longjmp: */ _thread_kern_in_sched = 0; /* Run the current thread's start routine with argument: */ pthread_exit(_thread_run->start_routine(_thread_run->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } #endif Index: head/lib/libpthread/thread/thr_gc.c =================================================================== --- head/lib/libpthread/thread/thr_gc.c (revision 48608) +++ head/lib/libpthread/thread/thr_gc.c (revision 48609) @@ -1,245 +1,243 @@ /* * Copyright (c) 1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_gc.c,v 1.4 1999/06/20 08:28:25 jb Exp $ + * $Id: uthread_gc.c,v 1.5 1999/07/05 00:35:18 jasone Exp $ * * Garbage collector thread. Frees memory allocated for dead threads. * */ #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #include #include "pthread_private.h" pthread_addr_t _thread_gc(pthread_addr_t arg) { int f_debug; int f_done = 0; int ret; sigset_t mask; pthread_t pthread; pthread_t pthread_cln; pthread_t pthread_nxt; pthread_t pthread_prv; struct timespec abstime; void *p_stack; /* Block all signals */ sigfillset (&mask); sigprocmask (SIG_BLOCK, &mask, NULL); /* Mark this thread as a library thread (not a user thread). */ _thread_run->flags |= PTHREAD_FLAGS_PRIVATE; /* Set a debug flag based on an environment variable. */ f_debug = (getenv("LIBC_R_DEBUG") != NULL); /* Set the name of this thread. */ pthread_set_name_np(_thread_run,"GC"); while (!f_done) { /* Check if debugging this application. */ if (f_debug) /* Dump thread info to file. */ _thread_dump_info(); /* * Defer signals to protect the scheduling queues from * access by the signal handler: */ _thread_kern_sig_defer(); /* Check if this is the last running thread: */ if (TAILQ_FIRST(&_thread_list) == _thread_run && TAILQ_NEXT(_thread_run, tle) == NULL) /* * This is the last thread, so it can exit * now. */ f_done = 1; /* * Undefer and handle pending signals, yielding if * necessary: */ _thread_kern_sig_undefer(); /* * Lock the garbage collector mutex which ensures that * this thread sees another thread exit: */ if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); /* No stack of thread structure to free yet: */ p_stack = NULL; pthread_cln = NULL; /* * Enter a loop to search for the first dead thread that * has memory to free. */ for (pthread = TAILQ_FIRST(&_dead_list); p_stack == NULL && pthread_cln == NULL && pthread != NULL; pthread = TAILQ_NEXT(pthread, dle)) { /* Check if the initial thread: */ if (pthread == _thread_initial) { /* Don't destroy the initial thread. */ } /* * Check if this thread has detached: */ else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) { /* Remove this thread from the dead list: */ TAILQ_REMOVE(&_dead_list, pthread, dle); /* * Check if the stack was not specified by * the caller to pthread_create and has not * been destroyed yet: */ if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) { -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ if (pthread->attr.stacksize_attr == PTHREAD_STACK_DEFAULT) { /* Default-size stack. Cache it: */ struct stack * spare_stack = (pthread->stack + PTHREAD_STACK_DEFAULT - sizeof(struct stack)); SLIST_INSERT_HEAD(&_stackq, spare_stack, qe); } else { /* Non-standard stack size. free() it outside the locks: */ p_stack = pthread->stack; } #else /* * Point to the stack that must * be freed outside the locks: */ p_stack = pthread->stack; #endif } /* * Point to the thread structure that must * be freed outside the locks: */ pthread_cln = pthread; } else { /* * This thread has not detached, so do * not destroy it. * * Check if the stack was not specified by * the caller to pthread_create and has not * been destroyed yet: */ if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) { -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ if (pthread->attr.stacksize_attr == PTHREAD_STACK_DEFAULT) { /* Default-size stack. Cache it: */ struct stack * spare_stack = (pthread->stack + PTHREAD_STACK_DEFAULT - sizeof(struct stack)); SLIST_INSERT_HEAD(&_stackq, spare_stack, qe); } else { /* Non-standard stack size. free() it outside the locks: */ p_stack = pthread->stack; } #else /* * Point to the stack that must * be freed outside the locks: */ p_stack = pthread->stack; #endif /* * NULL the stack pointer now * that the memory has been freed: */ pthread->stack = NULL; } } } /* * Check if this is not the last thread and there is no * memory to free this time around. */ if (!f_done && p_stack == NULL && pthread_cln == NULL) { /* Get the current time. */ if (clock_gettime(CLOCK_REALTIME,&abstime) != 0) PANIC("gc cannot get time"); /* * Do a backup poll in 10 seconds if no threads * die before then. */ abstime.tv_sec += 10; /* * Wait for a signal from a dying thread or a * timeout (for a backup poll). */ if ((ret = pthread_cond_timedwait(&_gc_cond, &_gc_mutex, &abstime)) != 0 && ret != ETIMEDOUT) PANIC("gc cannot wait for a signal"); } /* Unlock the garbage collector mutex: */ if (pthread_mutex_unlock(&_gc_mutex) != 0) PANIC("Cannot unlock gc mutex"); /* * If there is memory to free, do it now. The call to * free() might block, so this must be done outside the * locks. */ if (p_stack != NULL) free(p_stack); if (pthread_cln != NULL) /* * Free the memory allocated for the thread * structure. */ free(pthread_cln); } return (NULL); } Index: head/lib/libpthread/thread/thr_init.c =================================================================== --- head/lib/libpthread/thread/thr_init.c (revision 48608) +++ head/lib/libpthread/thread/thr_init.c (revision 48609) @@ -1,358 +1,356 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_init.c,v 1.12 1999/06/23 15:01:21 dt Exp $ + * $Id: uthread_init.c,v 1.13 1999/07/05 00:35:19 jasone Exp $ */ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE #include #include #include #include #include #include #include #include #include #include -#ifdef _PTHREAD_GSTACK #include #include -#endif #ifdef _THREAD_SAFE #include #include #include "pthread_private.h" #ifdef GCC_2_8_MADE_THREAD_AWARE typedef void *** (*dynamic_handler_allocator)(); extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); static pthread_key_t except_head_key; typedef struct { void **__dynamic_handler_chain; void *top_elt[2]; } except_struct; static void ***dynamic_allocator_handler_fn() { except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); if(dh == NULL) { dh = (except_struct *)malloc( sizeof(except_struct) ); memset(dh, '\0', sizeof(except_struct)); dh->__dynamic_handler_chain= dh->top_elt; pthread_setspecific(except_head_key, (void *)dh); } return &dh->__dynamic_handler_chain; } #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization */ void _thread_init(void) { int fd; int flags; int i; size_t len; int mib[2]; struct clockinfo clockinfo; struct sigaction act; /* Check if this function has already been called: */ if (_thread_initial) /* Only initialise the threaded application once. */ return; /* * Check for the special case of this process running as * or in place of init as pid = 1: */ if (getpid() == 1) { /* * Setup a new session for this process which is * assumed to be running as root. */ if (setsid() == -1) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); if (_thread_sys_dup2(fd,0) == -1 || _thread_sys_dup2(fd,1) == -1 || _thread_sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if ((_pthread_stdio_flags[i] = _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) PANIC("Cannot get stdio flags"); /* * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ if (_thread_sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Allocate and initialize the ready queue: */ else if (_pq_alloc(&_readyq, PTHREAD_MIN_PRIORITY, PTHREAD_MAX_PRIORITY) != 0) { /* Abort this application: */ PANIC("Cannot allocate priority ready queue."); } /* Allocate memory for the thread structure of the initial thread: */ else if ((_thread_initial = (pthread_t) malloc(sizeof(struct pthread))) == NULL) { /* * Insufficient memory to initialise this application, so * abort: */ PANIC("Cannot allocate memory for initial thread"); } else { /* Zero the global kernel thread structure: */ memset(&_thread_kern_thread, 0, sizeof(struct pthread)); _thread_kern_thread.flags = PTHREAD_FLAGS_PRIVATE; memset(_thread_initial, 0, sizeof(struct pthread)); /* Initialize the waiting and work queues: */ TAILQ_INIT(&_waitingq); TAILQ_INIT(&_workq); /* Initialize the scheduling switch hook routine: */ _sched_switch_hook = NULL; -#ifdef _PTHREAD_GSTACK +#ifdef __i386__ /* Initialize the thread stack cache: */ SLIST_INIT(&_stackq); /* Create the red zone for the main stack. */ if (MAP_FAILED == mmap((void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) { PANIC("Cannot allocate red zone for initial thread"); } #endif /* * Write a magic value to the thread structure * to help identify valid ones: */ _thread_initial->magic = PTHREAD_MAGIC; /* Default the priority of the initial thread: */ _thread_initial->base_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->active_priority = PTHREAD_DEFAULT_PRIORITY; _thread_initial->inherited_priority = 0; /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); /* Initialize the owned mutex queue and count: */ TAILQ_INIT(&(_thread_initial->mutexq)); _thread_initial->priority_mutex_count = 0; /* Initialise the rest of the fields: */ _thread_initial->poll_data.nfds = 0; _thread_initial->poll_data.fds = NULL; _thread_initial->sig_defer_count = 0; _thread_initial->yield_on_sig_undefer = 0; _thread_initial->specific_data = NULL; _thread_initial->cleanup = NULL; _thread_initial->flags = 0; _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); _thread_run = _thread_initial; /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); act.sa_handler = (void (*) ()) _thread_sig_handler; act.sa_flags = 0; /* Initialize signal handling: */ _thread_sig_init(); /* Enter a loop to get the existing signal status: */ for (i = 1; i < NSIG; i++) { /* Check for signals which cannot be trapped: */ if (i == SIGKILL || i == SIGSTOP) { } /* Get the signal handler details: */ else if (_thread_sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal * initialisation fails: */ PANIC("Cannot read signal handler info"); } } /* * Install the signal handler for the most important * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ PANIC("Cannot initialise signal handler"); } /* Get the kernel clockrate: */ mib[0] = CTL_KERN; mib[1] = KERN_CLOCKRATE; len = sizeof (struct clockinfo); if (sysctl(mib, 2, &clockinfo, &len, NULL, 0) == 0) _clock_res_nsec = clockinfo.tick * 1000; /* Get the table size: */ if ((_thread_dtablesize = getdtablesize()) < 0) { /* * Cannot get the system defined table size, so abort * this process. */ PANIC("Cannot get dtablesize"); } /* Allocate memory for the file descriptor table: */ if ((_thread_fd_table = (struct fd_table_entry **) malloc(sizeof(struct fd_table_entry *) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for file descriptor table"); } /* Allocate memory for the pollfd table: */ if ((_thread_pfd_table = (struct pollfd *) malloc(sizeof(struct pollfd) * _thread_dtablesize)) == NULL) { /* * Cannot allocate memory for the file descriptor * table, so abort this process. */ PANIC("Cannot allocate memory for pollfd table"); } else { /* * Enter a loop to initialise the file descriptor * table: */ for (i = 0; i < _thread_dtablesize; i++) { /* Initialise the file descriptor table: */ _thread_fd_table[i] = NULL; } /* Initialize stdio file descriptor table entries: */ if ((_thread_fd_table_init(0) != 0) || (_thread_fd_table_init(1) != 0) || (_thread_fd_table_init(2) != 0)) { PANIC("Cannot initialize stdio file descriptor " "table entries"); } } } #ifdef GCC_2_8_MADE_THREAD_AWARE /* Create the thread-specific data for the exception linked list. */ if(pthread_key_create(&except_head_key, NULL) != 0) PANIC("Failed to create thread specific execption head"); /* Setup the gcc exception handler per thread. */ __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); #endif /* GCC_2_8_MADE_THREAD_AWARE */ /* Initialise the garbage collector mutex and condition variable. */ if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); gettimeofday(&kern_inc_prio_time, NULL); return; } /* * Special start up code for NetBSD/Alpha */ #if defined(__NetBSD__) && defined(__alpha__) int main(int argc, char *argv[], char *env); int _thread_main(int argc, char *argv[], char *env) { _thread_init(); return (main(argc, argv, env)); } #endif #else /* * A stub for non-threaded programs. */ void _thread_init(void) { } #endif Index: head/lib/libpthread/thread/thr_private.h =================================================================== --- head/lib/libpthread/thread/thr_private.h (revision 48608) +++ head/lib/libpthread/thread/thr_private.h (revision 48609) @@ -1,1164 +1,1160 @@ /* * Copyright (c) 1995-1998 John Birrell . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by John Birrell. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Private thread definitions for the uthread kernel. * - * $Id: pthread_private.h,v 1.20 1999/06/20 08:28:08 jb Exp $ + * $Id: pthread_private.h,v 1.21 1999/07/05 00:35:17 jasone Exp $ */ #ifndef _PTHREAD_PRIVATE_H #define _PTHREAD_PRIVATE_H /* * Evaluate the storage class specifier. */ #ifdef GLOBAL_PTHREAD_PRIVATE #define SCLASS #else #define SCLASS extern #endif /* * Include files. */ #include #include #include #include #include #include #include #include /* * Kernel fatal error handler macro. */ #define PANIC(string) _thread_exit(__FILE__,__LINE__,string) /* Output debug messages like this: */ #define stdout_debug(_x) _thread_sys_write(1,_x,strlen(_x)); #define stderr_debug(_x) _thread_sys_write(2,_x,strlen(_x)); /* * Priority queue manipulation macros (using pqe link): */ #define PTHREAD_PRIOQ_INSERT_HEAD(thrd) _pq_insert_head(&_readyq,thrd) #define PTHREAD_PRIOQ_INSERT_TAIL(thrd) _pq_insert_tail(&_readyq,thrd) #define PTHREAD_PRIOQ_REMOVE(thrd) _pq_remove(&_readyq,thrd) #define PTHREAD_PRIOQ_FIRST() _pq_first(&_readyq) /* * Waiting queue manipulation macros (using pqe link): */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_WAITQ_REMOVE(thrd) _waitq_remove(thrd) #define PTHREAD_WAITQ_INSERT(thrd) _waitq_insert(thrd) #define PTHREAD_WAITQ_CLEARACTIVE() _waitq_clearactive() #define PTHREAD_WAITQ_SETACTIVE() _waitq_setactive() #else #define PTHREAD_WAITQ_REMOVE(thrd) TAILQ_REMOVE(&_waitingq,thrd,pqe) #define PTHREAD_WAITQ_INSERT(thrd) do { \ if ((thrd)->wakeup_time.tv_sec == -1) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else { \ pthread_t tid = TAILQ_FIRST(&_waitingq); \ while ((tid != NULL) && (tid->wakeup_time.tv_sec != -1) && \ ((tid->wakeup_time.tv_sec < (thrd)->wakeup_time.tv_sec) || \ ((tid->wakeup_time.tv_sec == (thrd)->wakeup_time.tv_sec) && \ (tid->wakeup_time.tv_nsec <= (thrd)->wakeup_time.tv_nsec)))) \ tid = TAILQ_NEXT(tid, pqe); \ if (tid == NULL) \ TAILQ_INSERT_TAIL(&_waitingq,thrd,pqe); \ else \ TAILQ_INSERT_BEFORE(tid,thrd,pqe); \ } \ } while (0) #define PTHREAD_WAITQ_CLEARACTIVE() #define PTHREAD_WAITQ_SETACTIVE() #endif /* * Work queue manipulation macros (using qe link): */ #define PTHREAD_WORKQ_INSERT(thrd) do { \ TAILQ_INSERT_TAIL(&_workq,thrd,qe); \ (thrd)->flags |= PTHREAD_FLAGS_IN_WORKQ; \ } while (0) #define PTHREAD_WORKQ_REMOVE(thrd) do { \ TAILQ_REMOVE(&_workq,thrd,qe); \ (thrd)->flags &= ~PTHREAD_FLAGS_IN_WORKQ; \ } while (0) /* * State change macro without scheduling queue change: */ #define PTHREAD_SET_STATE(thrd, newstate) do { \ (thrd)->state = newstate; \ (thrd)->fname = __FILE__; \ (thrd)->lineno = __LINE__; \ } while (0) /* * State change macro with scheduling queue change - This must be * called with preemption deferred (see thread_kern_sched_[un]defer). */ #if defined(_PTHREADS_INVARIANTS) #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if (_thread_kern_new_state != 0) \ PANIC("Recursive PTHREAD_NEW_STATE"); \ _thread_kern_new_state = 1; \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ _thread_kern_new_state = 0; \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #else #define PTHREAD_NEW_STATE(thrd, newstate) do { \ if ((thrd)->state != newstate) { \ if ((thrd)->state == PS_RUNNING) { \ PTHREAD_PRIOQ_REMOVE(thrd); \ PTHREAD_WAITQ_INSERT(thrd); \ } else if (newstate == PS_RUNNING) { \ PTHREAD_WAITQ_REMOVE(thrd); \ PTHREAD_PRIOQ_INSERT_TAIL(thrd); \ } \ } \ PTHREAD_SET_STATE(thrd, newstate); \ } while (0) #endif /* * Define the signals to be used for scheduling. */ #if defined(_PTHREADS_COMPAT_SCHED) #define _ITIMER_SCHED_TIMER ITIMER_VIRTUAL #define _SCHED_SIGNAL SIGVTALRM #else #define _ITIMER_SCHED_TIMER ITIMER_PROF #define _SCHED_SIGNAL SIGPROF #endif /* * Priority queues. * * XXX It'd be nice if these were contained in uthread_priority_queue.[ch]. */ typedef struct pq_list { TAILQ_HEAD(, pthread) pl_head; /* list of threads at this priority */ TAILQ_ENTRY(pq_list) pl_link; /* link for queue of priority lists */ int pl_prio; /* the priority of this list */ int pl_queued; /* is this in the priority queue */ } pq_list_t; typedef struct pq_queue { TAILQ_HEAD(, pq_list) pq_queue; /* queue of priority lists */ pq_list_t *pq_lists; /* array of all priority lists */ int pq_size; /* number of priority lists */ } pq_queue_t; /* * TailQ initialization values. */ #define TAILQ_INITIALIZER { NULL, NULL } /* * Mutex definitions. */ union pthread_mutex_data { void *m_ptr; int m_count; }; struct pthread_mutex { enum pthread_mutextype m_type; int m_protocol; TAILQ_HEAD(mutex_head, pthread) m_queue; struct pthread *m_owner; union pthread_mutex_data m_data; long m_flags; int m_refcount; /* * Used for priority inheritence and protection. * * m_prio - For priority inheritence, the highest active * priority (threads locking the mutex inherit * this priority). For priority protection, the * ceiling priority of this mutex. * m_saved_prio - mutex owners inherited priority before * taking the mutex, restored when the owner * unlocks the mutex. */ int m_prio; int m_saved_prio; /* * Link for list of all mutexes a thread currently owns. */ TAILQ_ENTRY(pthread_mutex) m_qe; /* * Lock for accesses to this structure. */ spinlock_t lock; }; /* * Flags for mutexes. */ #define MUTEX_FLAGS_PRIVATE 0x01 #define MUTEX_FLAGS_INITED 0x02 #define MUTEX_FLAGS_BUSY 0x04 /* * Static mutex initialization values. */ #define PTHREAD_MUTEX_STATIC_INITIALIZER \ { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, TAILQ_INITIALIZER, \ NULL, { NULL }, 0, 0, 0, 0, TAILQ_INITIALIZER, \ _SPINLOCK_INITIALIZER } struct pthread_mutex_attr { enum pthread_mutextype m_type; int m_protocol; int m_ceiling; long m_flags; }; /* * Condition variable definitions. */ enum pthread_cond_type { COND_TYPE_FAST, COND_TYPE_MAX }; struct pthread_cond { enum pthread_cond_type c_type; TAILQ_HEAD(cond_head, pthread) c_queue; pthread_mutex_t c_mutex; void *c_data; long c_flags; /* * Lock for accesses to this structure. */ spinlock_t lock; }; struct pthread_cond_attr { enum pthread_cond_type c_type; long c_flags; }; /* * Flags for condition variables. */ #define COND_FLAGS_PRIVATE 0x01 #define COND_FLAGS_INITED 0x02 #define COND_FLAGS_BUSY 0x04 /* * Static cond initialization values. */ #define PTHREAD_COND_STATIC_INITIALIZER \ { COND_TYPE_FAST, TAILQ_INITIALIZER, NULL, NULL, \ 0, _SPINLOCK_INITIALIZER } /* * Cleanup definitions. */ struct pthread_cleanup { struct pthread_cleanup *next; void (*routine) (); void *routine_arg; }; struct pthread_attr { int sched_policy; int sched_inherit; int sched_interval; int prio; int suspend; int flags; void *arg_attr; void (*cleanup_attr) (); void *stackaddr_attr; size_t stacksize_attr; }; /* * Thread creation state attributes. */ #define PTHREAD_CREATE_RUNNING 0 #define PTHREAD_CREATE_SUSPENDED 1 /* * Miscellaneous definitions. */ #define PTHREAD_STACK_DEFAULT 65536 -#ifdef _PTHREAD_GSTACK /* Size of red zone at the end of each stack. */ #define PTHREAD_STACK_GUARD 4096 /* Maximum size of initial thread's stack. This perhaps deserves to be larger - * than the stacks of other threads, since legacy applications are likely to run + * than the stacks of other threads, since many applications are likely to run * almost entirely on this stack. */ #define PTHREAD_STACK_INITIAL 0x100000 /* Address immediately beyond the beginning of the initial thread stack. */ #if defined(__FreeBSD__) -# if defined(__alpha__) -# define PTHREAD_STACK_TOP 0x160022000 -# else -# define PTHREAD_STACK_TOP 0xbfbde000 -# endif +# if defined(__i386__) +# define PTHREAD_STACK_TOP 0xbfbde000 +# elif defined(__alpha__) +# define PTHREAD_STACK_TOP 0x160022000 +# else +# error "Don't recognize this architecture!" +# endif #else -# error "Don't recognize this operating system!" +# error "Don't recognize this operating system!" #endif -#endif #define PTHREAD_DEFAULT_PRIORITY 64 #define PTHREAD_MAX_PRIORITY 126 #define PTHREAD_MIN_PRIORITY 0 #define _POSIX_THREAD_ATTR_STACKSIZE /* * Clock resolution in nanoseconds. */ #define CLOCK_RES_NSEC 10000000 /* * Time slice period in microseconds. */ #define TIMESLICE_USEC 100000 struct pthread_key { spinlock_t lock; volatile int allocated; volatile int count; void (*destructor) (); }; struct pthread_rwlockattr { int pshared; }; struct pthread_rwlock { pthread_mutex_t lock; /* monitor lock */ int state; /* 0 = idle >0 = # of readers -1 = writer */ pthread_cond_t read_signal; pthread_cond_t write_signal; int blocked_writers; }; /* * Thread states. */ enum pthread_state { PS_RUNNING, PS_SIGTHREAD, PS_MUTEX_WAIT, PS_COND_WAIT, PS_FDLR_WAIT, PS_FDLW_WAIT, PS_FDR_WAIT, PS_FDW_WAIT, PS_FILE_WAIT, PS_POLL_WAIT, PS_SELECT_WAIT, PS_SLEEP_WAIT, PS_WAIT_WAIT, PS_SIGSUSPEND, PS_SIGWAIT, PS_SPINBLOCK, PS_JOIN, PS_SUSPENDED, PS_DEAD, PS_DEADLOCK, PS_STATE_MAX }; /* * File descriptor locking definitions. */ #define FD_READ 0x1 #define FD_WRITE 0x2 #define FD_RDWR (FD_READ | FD_WRITE) /* * File descriptor table structure. */ struct fd_table_entry { /* * Lock for accesses to this file descriptor table * entry. This is passed to _spinlock() to provide atomic * access to this structure. It does *not* represent the * state of the lock on the file descriptor. */ spinlock_t lock; TAILQ_HEAD(, pthread) r_queue; /* Read queue. */ TAILQ_HEAD(, pthread) w_queue; /* Write queue. */ struct pthread *r_owner; /* Ptr to thread owning read lock. */ struct pthread *w_owner; /* Ptr to thread owning write lock. */ char *r_fname; /* Ptr to read lock source file name */ int r_lineno; /* Read lock source line number. */ char *w_fname; /* Ptr to write lock source file name */ int w_lineno; /* Write lock source line number. */ int r_lockcount; /* Count for FILE read locks. */ int w_lockcount; /* Count for FILE write locks. */ int flags; /* Flags used in open. */ }; struct pthread_poll_data { int nfds; struct pollfd *fds; }; union pthread_wait_data { pthread_mutex_t mutex; pthread_cond_t cond; const sigset_t *sigwait; /* Waiting on a signal in sigwait */ struct { short fd; /* Used when thread waiting on fd */ short branch; /* Line number, for debugging. */ char *fname; /* Source file name for debugging.*/ } fd; struct pthread_poll_data * poll_data; spinlock_t *spinlock; }; /* * Thread structure. */ struct pthread { /* * Magic value to help recognize a valid thread structure * from an invalid one: */ #define PTHREAD_MAGIC ((u_int32_t) 0xd09ba115) u_int32_t magic; char *name; /* * Lock for accesses to this thread structure. */ spinlock_t lock; /* Queue entry for list of all threads: */ TAILQ_ENTRY(pthread) tle; /* Queue entry for list of dead threads: */ TAILQ_ENTRY(pthread) dle; /* * Thread start routine, argument, stack pointer and thread * attributes. */ void *(*start_routine)(void *); void *arg; void *stack; struct pthread_attr attr; #if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(__i386__) /* * Saved floating point registers on systems where they are not * saved in the signal context. */ char saved_fp[108]; #endif /* * Saved signal context used in call to sigreturn by * _thread_kern_sched if sig_saved is TRUE. */ struct sigcontext saved_sigcontext; /* * Saved jump buffer used in call to longjmp by _thread_kern_sched * if sig_saved is FALSE. */ jmp_buf saved_jmp_buf; /* * TRUE if the last state saved was a signal context. FALSE if the * last state saved was a jump buffer. */ int sig_saved; /* * Current signal mask and pending signals. */ sigset_t sigmask; sigset_t sigpend; /* Thread state: */ enum pthread_state state; /* Time that this thread was last made active. */ struct timeval last_active; /* Time that this thread was last made inactive. */ struct timeval last_inactive; /* * Number of microseconds accumulated by this thread when * time slicing is active. */ long slice_usec; /* * Incremental priority accumulated by thread while it is ready to * run but is denied being run. */ int inc_prio; /* * Time to wake up thread. This is used for sleeping threads and * for any operation which may time out (such as select). */ struct timespec wakeup_time; /* TRUE if operation has timed out. */ int timeout; /* * Error variable used instead of errno. The function __error() * returns a pointer to this. */ int error; /* Join queue head and link for waiting threads: */ TAILQ_HEAD(join_head, pthread) join_queue; /* * The current thread can belong to only one scheduling queue at * a time (ready or waiting queue). It can also belong to (only) * one of: * * o A queue of threads waiting for a mutex * o A queue of threads waiting for a condition variable * o A queue of threads waiting for another thread to terminate * (the join queue above) * o A queue of threads waiting for a file descriptor lock * o A queue of threads needing work done by the kernel thread * (waiting for a spinlock or file I/O) * * Use pqe for the scheduling queue link (both ready and waiting), * and qe for other links. */ /* Priority queue entry for this thread: */ TAILQ_ENTRY(pthread) pqe; /* Queue entry for this thread: */ TAILQ_ENTRY(pthread) qe; /* Wait data. */ union pthread_wait_data data; /* * Allocated for converting select into poll. */ struct pthread_poll_data poll_data; /* * Set to TRUE if a blocking operation was * interrupted by a signal: */ int interrupted; /* Signal number when in state PS_SIGWAIT: */ int signo; /* * Set to non-zero when this thread has deferred signals. * We allow for recursive deferral. */ int sig_defer_count; /* * Set to TRUE if this thread should yield after undeferring * signals. */ int yield_on_sig_undefer; /* Miscellaneous data. */ int flags; #define PTHREAD_FLAGS_PRIVATE 0x0001 #define PTHREAD_EXITING 0x0002 #define PTHREAD_FLAGS_IN_CONDQ 0x0004 /* in condition queue using qe link*/ #define PTHREAD_FLAGS_IN_WORKQ 0x0008 /* in work queue using qe link */ #define PTHREAD_FLAGS_IN_WAITQ 0x0010 /* in waiting queue using pqe link*/ #define PTHREAD_FLAGS_IN_PRIOQ 0x0020 /* in priority queue using pqe link*/ #define PTHREAD_FLAGS_TRACE 0x0040 /* for debugging purposes */ /* * Base priority is the user setable and retrievable priority * of the thread. It is only affected by explicit calls to * set thread priority and upon thread creation via a thread * attribute or default priority. */ char base_priority; /* * Inherited priority is the priority a thread inherits by * taking a priority inheritence or protection mutex. It * is not affected by base priority changes. Inherited * priority defaults to and remains 0 until a mutex is taken * that is being waited on by any other thread whose priority * is non-zero. */ char inherited_priority; /* * Active priority is always the maximum of the threads base * priority and inherited priority. When there is a change * in either the base or inherited priority, the active * priority must be recalculated. */ char active_priority; /* Number of priority ceiling or protection mutexes owned. */ int priority_mutex_count; /* * Queue of currently owned mutexes. */ TAILQ_HEAD(, pthread_mutex) mutexq; void *ret; const void **specific_data; int specific_data_count; /* Cleanup handlers Link List */ struct pthread_cleanup *cleanup; char *fname; /* Ptr to source file name */ int lineno; /* Source line number. */ }; -#ifdef _PTHREAD_GSTACK /* Spare thread stack. */ struct stack { SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */ }; -#endif /* * Global variables for the uthread kernel. */ /* Kernel thread structure used when there are no running threads: */ SCLASS struct pthread _thread_kern_thread; /* Ptr to the thread structure for the running thread: */ SCLASS struct pthread * volatile _thread_run #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* Ptr to the thread structure for the last user thread to run: */ SCLASS struct pthread * volatile _last_user_thread #ifdef GLOBAL_PTHREAD_PRIVATE = &_thread_kern_thread; #else ; #endif /* * Ptr to the thread running in single-threaded mode or NULL if * running multi-threaded (default POSIX behaviour). */ SCLASS struct pthread * volatile _thread_single #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* List of all threads: */ SCLASS TAILQ_HEAD(, pthread) _thread_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_thread_list); #else ; #endif /* * Array of kernel pipe file descriptors that are used to ensure that * no signals are missed in calls to _select. */ SCLASS int _thread_kern_pipe[2] #ifdef GLOBAL_PTHREAD_PRIVATE = { -1, -1 }; #else ; #endif SCLASS int volatile _queue_signals #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif SCLASS int _thread_kern_in_sched #ifdef GLOBAL_PTHREAD_PRIVATE = 0; #else ; #endif /* Last time that an incremental priority update was performed: */ SCLASS struct timeval kern_inc_prio_time #ifdef GLOBAL_PTHREAD_PRIVATE = { 0, 0 }; #else ; #endif /* Dead threads: */ SCLASS TAILQ_HEAD(, pthread) _dead_list #ifdef GLOBAL_PTHREAD_PRIVATE = TAILQ_HEAD_INITIALIZER(_dead_list); #else ; #endif /* Initial thread: */ SCLASS struct pthread *_thread_initial #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Default thread attributes: */ SCLASS struct pthread_attr pthread_attr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING, PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, PTHREAD_STACK_DEFAULT }; #else ; #endif /* Default mutex attributes: */ SCLASS struct pthread_mutex_attr pthread_mutexattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, 0 }; #else ; #endif /* Default condition variable attributes: */ SCLASS struct pthread_cond_attr pthread_condattr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { COND_TYPE_FAST, 0 }; #else ; #endif /* * Standard I/O file descriptors need special flag treatment since * setting one to non-blocking does all on *BSD. Sigh. This array * is used to store the initial flag settings. */ SCLASS int _pthread_stdio_flags[3]; /* File table information: */ SCLASS struct fd_table_entry **_thread_fd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif /* Table for polling file descriptors: */ SCLASS struct pollfd *_thread_pfd_table #ifdef GLOBAL_PTHREAD_PRIVATE = NULL; #else ; #endif SCLASS const int dtablecount #ifdef GLOBAL_PTHREAD_PRIVATE = 4096/sizeof(struct fd_table_entry); #else ; #endif SCLASS int _thread_dtablesize /* Descriptor table size. */ #ifdef GLOBAL_PTHREAD_PRIVATE = 1024; #else ; #endif SCLASS int _clock_res_nsec /* Clock resolution in nsec. */ #ifdef GLOBAL_PTHREAD_PRIVATE = CLOCK_RES_NSEC; #else ; #endif /* Garbage collector mutex and condition variable. */ SCLASS pthread_mutex_t _gc_mutex #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; SCLASS pthread_cond_t _gc_cond #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; /* * Array of signal actions for this process. */ struct sigaction _thread_sigact[NSIG]; /* * Scheduling queues: */ SCLASS pq_queue_t _readyq; SCLASS TAILQ_HEAD(, pthread) _waitingq; /* * Work queue: */ SCLASS TAILQ_HEAD(, pthread) _workq; /* Tracks the number of threads blocked while waiting for a spinlock. */ SCLASS volatile int _spinblock_count #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Indicates that the signal queue needs to be checked. */ SCLASS volatile int _sigq_check_reqd #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Thread switch hook. */ SCLASS pthread_switch_routine_t _sched_switch_hook #ifdef GLOBAL_PTHREAD_PRIVATE = NULL #endif ; -#ifdef _PTHREAD_GSTACK /* Spare stack queue. Stacks of default size are cached in order to reduce * thread creation time. Spare stacks are used in LIFO order to increase cache * locality. */ SCLASS SLIST_HEAD(, stack) _stackq; /* Base address of next unallocated default-size stack. Stacks are allocated * contiguously, starting below the beginning of the main stack. When a new * stack is created, a guard page is created just above it in order to (usually) * detect attempts by the adjacent stack to trounce the next thread stack. */ SCLASS void * _next_stack #ifdef GLOBAL_PTHREAD_PRIVATE /* main stack top - main stack size - stack size - (red zone + main stack red zone) */ = (void *) PTHREAD_STACK_TOP - PTHREAD_STACK_INITIAL - PTHREAD_STACK_DEFAULT - (2 * PTHREAD_STACK_GUARD) #endif ; -#endif /* Used for _PTHREADS_INVARIANTS checking. */ SCLASS int _thread_kern_new_state #ifdef GLOBAL_PTHREAD_PRIVATE = 0 #endif ; /* Undefine the storage class specifier: */ #undef SCLASS #ifdef _LOCK_DEBUG #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock_debug(_fd, _type, \ _ts, __FILE__, __LINE__) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock_debug(_fd, _type, \ __FILE__, __LINE__) #else #define _FD_LOCK(_fd,_type,_ts) _thread_fd_lock(_fd, _type, _ts) #define _FD_UNLOCK(_fd,_type) _thread_fd_unlock(_fd, _type) #endif /* * Function prototype definitions. */ __BEGIN_DECLS char *__ttyname_basic(int); char *__ttyname_r_basic(int, char *, size_t); char *ttyname_r(int, char *, size_t); int _find_dead_thread(pthread_t); int _find_thread(pthread_t); int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t); int _thread_fd_lock(int, int, struct timespec *); int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno); void _dispatch_signals(void); void _thread_signal(pthread_t, int); int _mutex_cv_lock(pthread_mutex_t *); int _mutex_cv_unlock(pthread_mutex_t *); int _mutex_reinit(pthread_mutex_t *); void _mutex_notify_priochange(struct pthread *); int _cond_reinit(pthread_cond_t *); int _pq_alloc(struct pq_queue *, int, int); int _pq_init(struct pq_queue *); void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); #if defined(_PTHREADS_INVARIANTS) void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); void _waitq_setactive(void); void _waitq_clearactive(void); #endif void _thread_exit(char *, int, char *); void _thread_fd_unlock(int, int); void _thread_fd_unlock_debug(int, int, char *, int); void *_thread_cleanup(pthread_t); void _thread_cleanupspecific(void); void _thread_dump_info(void); void _thread_init(void); void _thread_kern_sched(struct sigcontext *); void _thread_kern_sched_state(enum pthread_state,char *fname,int lineno); void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno); void _thread_kern_set_timeout(struct timespec *); void _thread_kern_sig_defer(void); void _thread_kern_sig_undefer(void); void _thread_sig_handler(int, int, struct sigcontext *); void _thread_sig_handle(int, struct sigcontext *); void _thread_sig_init(void); void _thread_start(void); void _thread_start_sig_handler(void); void _thread_seterrno(pthread_t,int); int _thread_fd_table_init(int fd); pthread_addr_t _thread_gc(pthread_addr_t); /* #include */ int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); int _thread_sys_sigpending(sigset_t *); int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); int _thread_sys_sigsuspend(const sigset_t *); int _thread_sys_siginterrupt(int, int); int _thread_sys_sigpause(int); int _thread_sys_sigreturn(struct sigcontext *); int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); void _thread_sys_psignal(unsigned int, const char *); void (*_thread_sys_signal(int, void (*)(int)))(int); /* #include */ #ifdef _SYS_STAT_H_ int _thread_sys_fchmod(int, mode_t); int _thread_sys_fstat(int, struct stat *); int _thread_sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ int _thread_sys_fstatfs(int, struct statfs *); #endif int _thread_sys_pipe(int *); /* #include */ #ifdef _SYS_SOCKET_H_ int _thread_sys_accept(int, struct sockaddr *, int *); int _thread_sys_bind(int, const struct sockaddr *, int); int _thread_sys_connect(int, const struct sockaddr *, int); int _thread_sys_getpeername(int, struct sockaddr *, int *); int _thread_sys_getsockname(int, struct sockaddr *, int *); int _thread_sys_getsockopt(int, int, int, void *, int *); int _thread_sys_listen(int, int); int _thread_sys_setsockopt(int, int, int, const void *, int); int _thread_sys_shutdown(int, int); int _thread_sys_socket(int, int, int); int _thread_sys_socketpair(int, int, int, int *); ssize_t _thread_sys_recv(int, void *, size_t, int); ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); ssize_t _thread_sys_send(int, const void *, size_t, int); ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _STDIO_H_ FILE *_thread_sys_fdopen(int, const char *); FILE *_thread_sys_fopen(const char *, const char *); FILE *_thread_sys_freopen(const char *, const char *, FILE *); FILE *_thread_sys_popen(const char *, const char *); FILE *_thread_sys_tmpfile(void); char *_thread_sys_ctermid(char *); char *_thread_sys_cuserid(char *); char *_thread_sys_fgetln(FILE *, size_t *); char *_thread_sys_fgets(char *, int, FILE *); char *_thread_sys_gets(char *); char *_thread_sys_tempnam(const char *, const char *); char *_thread_sys_tmpnam(char *); int _thread_sys_fclose(FILE *); int _thread_sys_feof(FILE *); int _thread_sys_ferror(FILE *); int _thread_sys_fflush(FILE *); int _thread_sys_fgetc(FILE *); int _thread_sys_fgetpos(FILE *, fpos_t *); int _thread_sys_fileno(FILE *); int _thread_sys_fprintf(FILE *, const char *, ...); int _thread_sys_fpurge(FILE *); int _thread_sys_fputc(int, FILE *); int _thread_sys_fputs(const char *, FILE *); int _thread_sys_fscanf(FILE *, const char *, ...); int _thread_sys_fseek(FILE *, long, int); int _thread_sys_fsetpos(FILE *, const fpos_t *); int _thread_sys_getc(FILE *); int _thread_sys_getchar(void); int _thread_sys_getw(FILE *); int _thread_sys_pclose(FILE *); int _thread_sys_printf(const char *, ...); int _thread_sys_putc(int, FILE *); int _thread_sys_putchar(int); int _thread_sys_puts(const char *); int _thread_sys_putw(int, FILE *); int _thread_sys_remove(const char *); int _thread_sys_rename (const char *, const char *); int _thread_sys_scanf(const char *, ...); int _thread_sys_setlinebuf(FILE *); int _thread_sys_setvbuf(FILE *, char *, int, size_t); int _thread_sys_snprintf(char *, size_t, const char *, ...); int _thread_sys_sprintf(char *, const char *, ...); int _thread_sys_sscanf(const char *, const char *, ...); int _thread_sys_ungetc(int, FILE *); int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); long _thread_sys_ftell(FILE *); size_t _thread_sys_fread(void *, size_t, size_t, FILE *); size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); void _thread_sys_clearerr(FILE *); void _thread_sys_perror(const char *); void _thread_sys_rewind(FILE *); void _thread_sys_setbuf(FILE *, char *); void _thread_sys_setbuffer(FILE *, char *, int); #endif /* #include */ #ifdef _UNISTD_H_ char *_thread_sys_ttyname(int); int _thread_sys_close(int); int _thread_sys_dup(int); int _thread_sys_dup2(int, int); int _thread_sys_exect(const char *, char * const *, char * const *); int _thread_sys_execve(const char *, char * const *, char * const *); int _thread_sys_fchdir(int); int _thread_sys_fchown(int, uid_t, gid_t); int _thread_sys_fsync(int); int _thread_sys_ftruncate(int, off_t); int _thread_sys_pause(void); int _thread_sys_pipe(int *); int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); off_t _thread_sys_lseek(int, off_t, int); pid_t _thread_sys_fork(void); pid_t _thread_sys_tcgetpgrp(int); ssize_t _thread_sys_read(int, void *, size_t); ssize_t _thread_sys_write(int, const void *, size_t); void _thread_sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ int _thread_sys_creat(const char *, mode_t); int _thread_sys_fcntl(int, int, ...); int _thread_sys_flock(int, int); int _thread_sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ int _thread_sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ DIR *___thread_sys_opendir2(const char *, int); DIR *_thread_sys_opendir(const char *); int _thread_sys_alphasort(const void *, const void *); int _thread_sys_scandir(const char *, struct dirent ***, int (*)(struct dirent *), int (*)(const void *, const void *)); int _thread_sys_closedir(DIR *); int _thread_sys_getdirentries(int, char *, int, long *); long _thread_sys_telldir(const DIR *); struct dirent *_thread_sys_readdir(DIR *); void _thread_sys_rewinddir(DIR *); void _thread_sys_seekdir(DIR *, long); #endif /* #include */ #ifdef _SYS_UIO_H_ ssize_t _thread_sys_readv(int, const struct iovec *, int); ssize_t _thread_sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG pid_t _thread_sys_wait(int *); pid_t _thread_sys_waitpid(pid_t, int *, int); pid_t _thread_sys_wait3(int *, int, struct rusage *); pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ int _thread_sys_poll(struct pollfd *, unsigned, int); #endif __END_DECLS #endif /* !_PTHREAD_PRIVATE_H */