1996-01-22 00:23:58 +00:00
|
|
|
/*
|
2003-04-18 07:09:43 +00:00
|
|
|
* Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
|
1998-04-29 09:59:34 +00:00
|
|
|
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
|
1996-01-22 00:23:58 +00:00
|
|
|
* 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
|
1999-03-23 05:07:56 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1996-01-22 00:23:58 +00:00
|
|
|
* 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.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate space for global thread variables here: */
|
|
|
|
#define GLOBAL_PTHREAD_PRIVATE
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
#include "namespace.h"
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
o Use a daemon thread to monitor signal events in kernel, if pending
signals were changed in kernel, it will retrieve the pending set and
try to find a thread to dispatch the signal. The dispatching process
can be rolled back if the signal is no longer in kernel.
o Create two functions _thr_signal_init() and _thr_signal_deinit(),
all signal action settings are retrieved from kernel when threading
mode is turned on, after a fork(), child process will reset them to
user settings by calling _thr_signal_deinit(). when threading mode
is not turned on, all signal operations are direct past to kernel.
o When a thread generated a synchoronous signals and its context returned
from completed list, UTS will retrieve the signal from its mailbox and try
to deliver the signal to thread.
o Context signal mask is now only used when delivering signals, thread's
current signal mask is always the one in pthread structure.
o Remove have_signals field in pthread structure, replace it with
psf_valid in pthread_signal_frame. when psf_valid is true, in context
switch time, thread will backout itself from some mutex/condition
internal queues, then begin to process signals. when a thread is not
at blocked state and running, check_pending indicates there are signals
for the thread, after preempted and then resumed time, UTS will try to
deliver signals to the thread.
o At signal delivering time, not only pending signals in thread will be
scanned, process's pending signals will be scanned too.
o Change sigwait code a bit, remove field sigwait in pthread_wait_data,
replace it with oldsigmask in pthread structure, when a thread calls
sigwait(), its current signal mask is backuped to oldsigmask, and waitset
is copied to its signal mask and when the thread gets a signal in the
waitset range, its current signal mask is restored from oldsigmask,
these are done in atomic fashion.
o Two additional POSIX APIs are implemented, sigwaitinfo() and sigtimedwait().
o Signal code locking is better than previous, there is fewer race conditions.
o Temporary disable most of code in _kse_single_thread as it is not safe
after fork().
2003-06-28 09:55:02 +00:00
|
|
|
#include <sys/signalvar.h>
|
2001-01-24 13:03:38 +00:00
|
|
|
#include <machine/reg.h>
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
#include <sys/ioctl.h>
|
2001-01-24 13:03:38 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/stat.h>
|
In the words of the author:
o The polling mechanism for I/O readiness was changed from
select() to poll(). In additon, a wrapped version of poll()
is now provided.
o The wrapped select routine now converts each fd_set to a
poll array so that the thread scheduler doesn't have to
perform a bitwise search for selected fds each time file
descriptors are polled for I/O readiness.
o The thread scheduler was modified to use a new queue (_workq)
for threads that need work. Threads waiting for I/O readiness
and spinblocks are added to the work queue in addition to the
waiting queue. This reduces the time spent forming/searching
the array of file descriptors being polled.
o The waiting queue (_waitingq) is now maintained in order of
thread wakeup time. This allows the thread scheduler to
find the nearest wakeup time by looking at the first thread
in the queue instead of searching the entire queue.
o Removed file descriptor locking for select/poll routines. An
application should not rely on the threads library for providing
this locking; if necessary, the application should use mutexes
to protect selecting/polling of file descriptors.
o Retrieve and use the kernel clock rate/resolution at startup
instead of hardcoding the clock resolution to 10 msec (tested
with kernel running at 1000 HZ).
o All queues have been changed to use queue.h macros. These
include the queues of all threads, dead threads, and threads
waiting for file descriptor locks.
o Added reinitialization of the GC mutex and condition variable
after a fork. Also prevented reallocation of the ready queue
after a fork.
o Prevented the wrapped close routine from closing the thread
kernel pipes.
o Initialized file descriptor table for stdio entries at thread
init.
o Provided additional flags to indicate to what queues threads
belong.
o Moved TAILQ initialization for statically allocated mutex and
condition variables to after the spinlock.
o Added dispatching of signals to pthread_kill. Removing the
dispatching of signals from thread activation broke sigsuspend
when pthread_kill was used to send a signal to a thread.
o Temporarily set the state of a thread to PS_SUSPENDED when it
is first created and placed in the list of threads so that it
will not be accidentally scheduled before becoming a member
of one of the scheduling queues.
o Change the signal handler to queue signals to the thread kernel
pipe if the scheduling queues are protected. When scheduling
queues are unprotected, signals are then dequeued and handled.
o Ensured that all installed signal handlers block the scheduling
signal and that the scheduling signal handler blocks all
other signals. This ensures that the signal handler is only
interruptible for and by non-scheduling signals. An atomic
lock is used to decide which instance of the signal handler
will handle pending signals.
o Removed _lock_thread_list and _unlock_thread_list as they are
no longer used to protect the thread list.
o Added missing RCS IDs to modified files.
o Added checks for appropriate queue membership and activity when
adding, removing, and searching the scheduling queues. These
checks add very little overhead and are enabled when compiled
with _PTHREADS_INVARIANTS defined. Suggested and implemented
by Tor Egge with some modification by me.
o Close a race condition in uthread_close. (Tor Egge)
o Protect the scheduling queues while modifying them in
pthread_cond_signal and _thread_fd_unlock. (Tor Egge)
o Ensure that when a thread gets a mutex, the mutex is on that
threads list of owned mutexes. (Tor Egge)
o Set the kernel-in-scheduler flag in _thread_kern_sched_state
and _thread_kern_sched_state_unlock to prevent a scheduling
signal from calling the scheduler again. (Tor Egge)
o Don't use TAILQ_FOREACH macro while searching the waiting
queue for threads in a sigwait state, because a change of
state destroys the TAILQ link. It is actually safe to do
so, though, because once a sigwaiting thread is found, the
loop ends and the function returns. (Tor Egge)
o When dispatching signals to threads, make the thread inherit
the signal deferral flag of the currently running thread.
(Tor Egge)
Submitted by: Daniel Eischen <eischen@vigrid.com> and
Tor Egge <Tor.Egge@fast.no>
1999-06-20 08:28:48 +00:00
|
|
|
#include <sys/sysctl.h>
|
1996-01-22 00:23:58 +00:00
|
|
|
#include <sys/time.h>
|
1998-08-10 01:24:22 +00:00
|
|
|
#include <sys/ttycom.h>
|
2001-01-24 13:03:38 +00:00
|
|
|
#include <sys/wait.h>
|
1999-07-05 00:35:19 +00:00
|
|
|
#include <sys/mman.h>
|
2001-01-24 13:03:38 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
1996-01-22 00:23:58 +00:00
|
|
|
#include <pthread.h>
|
2003-04-18 05:04:16 +00:00
|
|
|
#include <pthread_np.h>
|
2001-01-24 13:03:38 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "un-namespace.h"
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "libc_private.h"
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
2003-04-18 05:04:16 +00:00
|
|
|
|
2006-03-13 00:59:51 +00:00
|
|
|
LT10_COMPAT_PRIVATE(_libkse_debug);
|
|
|
|
LT10_COMPAT_PRIVATE(_thread_activated);
|
|
|
|
LT10_COMPAT_PRIVATE(_thread_active_threads);
|
|
|
|
LT10_COMPAT_PRIVATE(_thread_list);
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
|
|
|
|
int __pthread_mutex_lock(pthread_mutex_t *);
|
|
|
|
int __pthread_mutex_trylock(pthread_mutex_t *);
|
2003-05-16 19:58:30 +00:00
|
|
|
void _thread_init_hack(void);
|
2004-08-21 11:49:19 +00:00
|
|
|
extern int _thread_state_running;
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
static void init_private(void);
|
|
|
|
static void init_main_thread(struct pthread *thread);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/*
|
|
|
|
* All weak references used within libc should be in this table.
|
2003-04-18 05:04:16 +00:00
|
|
|
* This is so that static libraries will work.
|
2001-01-24 13:03:38 +00:00
|
|
|
*/
|
|
|
|
static void *references[] = {
|
|
|
|
&_accept,
|
|
|
|
&_bind,
|
|
|
|
&_close,
|
|
|
|
&_connect,
|
|
|
|
&_dup,
|
|
|
|
&_dup2,
|
|
|
|
&_execve,
|
|
|
|
&_fcntl,
|
|
|
|
&_flock,
|
|
|
|
&_flockfile,
|
|
|
|
&_fstat,
|
|
|
|
&_fstatfs,
|
|
|
|
&_fsync,
|
|
|
|
&_funlockfile,
|
|
|
|
&_getdirentries,
|
|
|
|
&_getlogin,
|
|
|
|
&_getpeername,
|
|
|
|
&_getsockname,
|
|
|
|
&_getsockopt,
|
|
|
|
&_ioctl,
|
|
|
|
&_kevent,
|
|
|
|
&_listen,
|
|
|
|
&_nanosleep,
|
|
|
|
&_open,
|
|
|
|
&_pthread_getspecific,
|
|
|
|
&_pthread_key_create,
|
|
|
|
&_pthread_key_delete,
|
|
|
|
&_pthread_mutex_destroy,
|
|
|
|
&_pthread_mutex_init,
|
|
|
|
&_pthread_mutex_lock,
|
|
|
|
&_pthread_mutex_trylock,
|
|
|
|
&_pthread_mutex_unlock,
|
|
|
|
&_pthread_mutexattr_init,
|
|
|
|
&_pthread_mutexattr_destroy,
|
|
|
|
&_pthread_mutexattr_settype,
|
|
|
|
&_pthread_once,
|
|
|
|
&_pthread_setspecific,
|
|
|
|
&_read,
|
|
|
|
&_readv,
|
|
|
|
&_recvfrom,
|
|
|
|
&_recvmsg,
|
|
|
|
&_select,
|
|
|
|
&_sendmsg,
|
|
|
|
&_sendto,
|
|
|
|
&_setsockopt,
|
|
|
|
&_sigaction,
|
|
|
|
&_sigprocmask,
|
|
|
|
&_sigsuspend,
|
|
|
|
&_socket,
|
|
|
|
&_socketpair,
|
2003-05-16 19:58:30 +00:00
|
|
|
&_thread_init_hack,
|
2001-01-24 13:03:38 +00:00
|
|
|
&_wait4,
|
|
|
|
&_write,
|
|
|
|
&_writev
|
|
|
|
};
|
|
|
|
|
2001-01-06 06:07:52 +00:00
|
|
|
/*
|
|
|
|
* These are needed when linking statically. All references within
|
|
|
|
* libgcc (and in the future libc) to these routines are weak, but
|
|
|
|
* if they are not (strongly) referenced by the application or other
|
|
|
|
* libraries, then the actual functions will not be loaded.
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
static void *libgcc_references[] = {
|
|
|
|
&_pthread_once,
|
|
|
|
&_pthread_key_create,
|
|
|
|
&_pthread_key_delete,
|
|
|
|
&_pthread_getspecific,
|
|
|
|
&_pthread_setspecific,
|
|
|
|
&_pthread_mutex_init,
|
|
|
|
&_pthread_mutex_destroy,
|
|
|
|
&_pthread_mutex_lock,
|
|
|
|
&_pthread_mutex_trylock,
|
|
|
|
&_pthread_mutex_unlock
|
2001-01-06 06:07:52 +00:00
|
|
|
};
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
#define DUAL_ENTRY(entry) \
|
|
|
|
(pthread_func_t)entry, (pthread_func_t)entry
|
|
|
|
|
|
|
|
static pthread_func_t jmp_table[][2] = {
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_atfork)}, /* PJT_ATFORK */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_destroy)}, /* PJT_ATTR_DESTROY */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getdetachstate)}, /* PJT_ATTR_GETDETACHSTATE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getguardsize)}, /* PJT_ATTR_GETGUARDSIZE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getinheritsched)}, /* PJT_ATTR_GETINHERITSCHED */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getschedparam)}, /* PJT_ATTR_GETSCHEDPARAM */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getschedpolicy)}, /* PJT_ATTR_GETSCHEDPOLICY */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getscope)}, /* PJT_ATTR_GETSCOPE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getstackaddr)}, /* PJT_ATTR_GETSTACKADDR */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_getstacksize)}, /* PJT_ATTR_GETSTACKSIZE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_init)}, /* PJT_ATTR_INIT */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setdetachstate)}, /* PJT_ATTR_SETDETACHSTATE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setguardsize)}, /* PJT_ATTR_SETGUARDSIZE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setinheritsched)}, /* PJT_ATTR_SETINHERITSCHED */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setschedparam)}, /* PJT_ATTR_SETSCHEDPARAM */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setschedpolicy)}, /* PJT_ATTR_SETSCHEDPOLICY */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setscope)}, /* PJT_ATTR_SETSCOPE */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setstackaddr)}, /* PJT_ATTR_SETSTACKADDR */
|
|
|
|
{DUAL_ENTRY(_pthread_attr_setstacksize)}, /* PJT_ATTR_SETSTACKSIZE */
|
|
|
|
{DUAL_ENTRY(_pthread_cancel)}, /* PJT_CANCEL */
|
|
|
|
{DUAL_ENTRY(_pthread_cleanup_pop)}, /* PJT_CLEANUP_POP */
|
|
|
|
{DUAL_ENTRY(_pthread_cleanup_push)}, /* PJT_CLEANUP_PUSH */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_cond_broadcast)}, /* PJT_COND_BROADCAST */
|
|
|
|
{DUAL_ENTRY(_pthread_cond_destroy)}, /* PJT_COND_DESTROY */
|
|
|
|
{DUAL_ENTRY(_pthread_cond_init)}, /* PJT_COND_INIT */
|
|
|
|
{DUAL_ENTRY(_pthread_cond_signal)}, /* PJT_COND_SIGNAL */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_cond_timedwait)}, /* PJT_COND_TIMEDWAIT */
|
2003-04-18 05:04:16 +00:00
|
|
|
{(pthread_func_t)__pthread_cond_wait,
|
|
|
|
(pthread_func_t)_pthread_cond_wait}, /* PJT_COND_WAIT */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_detach)}, /* PJT_DETACH */
|
|
|
|
{DUAL_ENTRY(_pthread_equal)}, /* PJT_EQUAL */
|
|
|
|
{DUAL_ENTRY(_pthread_exit)}, /* PJT_EXIT */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_getspecific)}, /* PJT_GETSPECIFIC */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_join)}, /* PJT_JOIN */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_key_create)}, /* PJT_KEY_CREATE */
|
|
|
|
{DUAL_ENTRY(_pthread_key_delete)}, /* PJT_KEY_DELETE*/
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_kill)}, /* PJT_KILL */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_main_np)}, /* PJT_MAIN_NP */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
|
|
|
|
{DUAL_ENTRY(_pthread_mutexattr_init)}, /* PJT_MUTEXATTR_INIT */
|
|
|
|
{DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_mutex_destroy)}, /* PJT_MUTEX_DESTROY */
|
|
|
|
{DUAL_ENTRY(_pthread_mutex_init)}, /* PJT_MUTEX_INIT */
|
|
|
|
{(pthread_func_t)__pthread_mutex_lock,
|
|
|
|
(pthread_func_t)_pthread_mutex_lock}, /* PJT_MUTEX_LOCK */
|
|
|
|
{(pthread_func_t)__pthread_mutex_trylock,
|
|
|
|
(pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_mutex_unlock)}, /* PJT_MUTEX_UNLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_once)}, /* PJT_ONCE */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_destroy)}, /* PJT_RWLOCK_DESTROY */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_init)}, /* PJT_RWLOCK_INIT */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_rdlock)}, /* PJT_RWLOCK_RDLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_unlock)}, /* PJT_RWLOCK_UNLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_rwlock_wrlock)}, /* PJT_RWLOCK_WRLOCK */
|
|
|
|
{DUAL_ENTRY(_pthread_self)}, /* PJT_SELF */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_setcancelstate)}, /* PJT_SETCANCELSTATE */
|
|
|
|
{DUAL_ENTRY(_pthread_setcanceltype)}, /* PJT_SETCANCELTYPE */
|
2003-04-18 05:04:16 +00:00
|
|
|
{DUAL_ENTRY(_pthread_setspecific)}, /* PJT_SETSPECIFIC */
|
2006-03-05 18:10:28 +00:00
|
|
|
{DUAL_ENTRY(_pthread_sigmask)}, /* PJT_SIGMASK */
|
|
|
|
{DUAL_ENTRY(_pthread_testcancel)} /* PJT_TESTCANCEL */
|
2003-04-18 05:04:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int init_once = 0;
|
1998-02-13 01:27:34 +00:00
|
|
|
|
1997-02-05 23:26:09 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Threaded process initialization.
|
|
|
|
*
|
|
|
|
* This is only called under two conditions:
|
|
|
|
*
|
|
|
|
* 1) Some thread routines have detected that the library hasn't yet
|
|
|
|
* been initialized (_thr_initial == NULL && curthread == NULL), or
|
|
|
|
*
|
|
|
|
* 2) An explicit call to reinitialize after a fork (indicated
|
|
|
|
* by curthread != NULL)
|
1997-02-05 23:26:09 +00:00
|
|
|
*/
|
1996-01-22 00:23:58 +00:00
|
|
|
void
|
2003-04-18 05:04:16 +00:00
|
|
|
_libpthread_init(struct pthread *curthread)
|
1996-01-22 00:23:58 +00:00
|
|
|
{
|
2003-04-18 05:04:16 +00:00
|
|
|
int fd;
|
2003-02-17 10:05:18 +00:00
|
|
|
|
|
|
|
/* Check if this function has already been called: */
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((_thr_initial != NULL) && (curthread == NULL))
|
|
|
|
/* Only initialize the threaded application once. */
|
1997-02-05 23:26:09 +00:00
|
|
|
return;
|
|
|
|
|
2001-01-06 06:07:52 +00:00
|
|
|
/*
|
2001-01-24 13:03:38 +00:00
|
|
|
* Make gcc quiescent about {,libgcc_}references not being
|
2001-01-06 06:07:52 +00:00
|
|
|
* referenced:
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
if ((references[0] == NULL) || (libgcc_references[0] == NULL))
|
|
|
|
PANIC("Failed loading mandatory references in _thread_init");
|
2001-01-06 06:07:52 +00:00
|
|
|
|
2004-08-21 11:49:19 +00:00
|
|
|
/* Pull debug symbols in for static binary */
|
|
|
|
_thread_state_running = PS_RUNNING;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Check the size of the jump table to make sure it is preset
|
|
|
|
* with the correct number of entries.
|
|
|
|
*/
|
|
|
|
if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
|
|
|
|
PANIC("Thread jump table not properly initialized");
|
|
|
|
memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
|
|
|
|
|
1998-08-10 01:24:22 +00:00
|
|
|
/*
|
|
|
|
* Check for the special case of this process running as
|
|
|
|
* or in place of init as pid = 1:
|
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
if ((_thr_pid = getpid()) == 1) {
|
1998-08-10 01:24:22 +00:00
|
|
|
/*
|
|
|
|
* Setup a new session for this process which is
|
|
|
|
* assumed to be running as root.
|
|
|
|
*/
|
1999-07-05 00:35:19 +00:00
|
|
|
if (setsid() == -1)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't set session ID");
|
1999-07-05 00:35:19 +00:00
|
|
|
if (revoke(_PATH_CONSOLE) != 0)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't revoke console");
|
2001-01-24 13:03:38 +00:00
|
|
|
if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't open console");
|
1999-07-05 00:35:19 +00:00
|
|
|
if (setlogin("root") == -1)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't set login to root");
|
2002-05-24 04:32:28 +00:00
|
|
|
if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't set controlling terminal");
|
|
|
|
}
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Initialize pthread private data. */
|
|
|
|
init_private();
|
|
|
|
_kse_init();
|
|
|
|
|
|
|
|
/* Initialize the initial kse and kseg. */
|
2004-08-12 12:12:12 +00:00
|
|
|
_kse_initial = _kse_alloc(NULL, _thread_scope_system > 0);
|
2003-04-18 05:04:16 +00:00
|
|
|
if (_kse_initial == NULL)
|
|
|
|
PANIC("Can't allocate initial kse.");
|
|
|
|
_kse_initial->k_kseg = _kseg_alloc(NULL);
|
|
|
|
if (_kse_initial->k_kseg == NULL)
|
|
|
|
PANIC("Can't allocate initial kseg.");
|
2003-07-17 23:02:30 +00:00
|
|
|
_kse_initial->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
|
2003-04-18 05:04:16 +00:00
|
|
|
_kse_initial->k_schedq = &_kse_initial->k_kseg->kg_schedq;
|
|
|
|
|
2003-04-22 20:28:33 +00:00
|
|
|
TAILQ_INSERT_TAIL(&_kse_initial->k_kseg->kg_kseq, _kse_initial, k_kgqe);
|
|
|
|
_kse_initial->k_kseg->kg_ksecount = 1;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Set the initial thread. */
|
|
|
|
if (curthread == NULL) {
|
|
|
|
/* Create and initialize the initial thread. */
|
|
|
|
curthread = _thr_alloc(NULL);
|
|
|
|
if (curthread == NULL)
|
|
|
|
PANIC("Can't allocate initial thread");
|
|
|
|
_thr_initial = curthread;
|
|
|
|
init_main_thread(curthread);
|
|
|
|
} else {
|
1996-01-22 00:23:58 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* The initial thread is the current thread. It is
|
|
|
|
* assumed that the current thread is already initialized
|
|
|
|
* because it is left over from a fork().
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_initial = curthread;
|
|
|
|
}
|
2003-04-18 07:09:43 +00:00
|
|
|
_kse_initial->k_kseg->kg_threadcount = 0;
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_initial->kse = _kse_initial;
|
|
|
|
_thr_initial->kseg = _kse_initial->k_kseg;
|
|
|
|
_thr_initial->active = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the thread to the thread list and to the KSEG's thread
|
|
|
|
* queue.
|
|
|
|
*/
|
|
|
|
THR_LIST_ADD(_thr_initial);
|
2003-04-18 07:09:43 +00:00
|
|
|
KSEG_THRQ_ADD(_kse_initial->k_kseg, _thr_initial);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Setup the KSE/thread specific data for the current KSE/thread. */
|
|
|
|
_thr_initial->kse->k_curthread = _thr_initial;
|
2003-08-05 22:46:00 +00:00
|
|
|
_kcb_set(_thr_initial->kse->k_kcb);
|
|
|
|
_tcb_set(_thr_initial->kse->k_kcb, _thr_initial->tcb);
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_initial->kse->k_flags |= KF_INITIALIZED;
|
2003-08-18 03:58:29 +00:00
|
|
|
|
|
|
|
_thr_signal_init();
|
|
|
|
_kse_critical_leave(&_thr_initial->tcb->tcb_tmbx);
|
2004-07-13 22:51:03 +00:00
|
|
|
/*
|
|
|
|
* activate threaded mode as soon as possible if we are
|
|
|
|
* being debugged
|
|
|
|
*/
|
|
|
|
if (_libkse_debug)
|
|
|
|
_kse_setthreaded(1);
|
2003-04-18 05:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function and pthread_create() do a lot of the same things.
|
|
|
|
* It'd be nice to consolidate the common stuff in one place.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_main_thread(struct pthread *thread)
|
|
|
|
{
|
|
|
|
/* Setup the thread attributes. */
|
|
|
|
thread->attr = _pthread_attr_default;
|
2003-07-17 23:02:30 +00:00
|
|
|
thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Set up the thread stack.
|
|
|
|
*
|
|
|
|
* Create a red zone below the main stack. All other stacks
|
|
|
|
* are constrained to a maximum size by the parameters
|
|
|
|
* passed to mmap(), but this stack is only limited by
|
|
|
|
* resource limits, so this stack needs an explicitly mapped
|
|
|
|
* red zone to protect the thread stack that is just beyond.
|
|
|
|
*/
|
2005-02-13 18:38:06 +00:00
|
|
|
if (mmap((void *)_usrstack - _thr_stack_initial -
|
2003-04-18 05:04:16 +00:00
|
|
|
_thr_guard_default, _thr_guard_default, 0, MAP_ANON,
|
|
|
|
-1, 0) == MAP_FAILED)
|
|
|
|
PANIC("Cannot allocate red zone for initial thread");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark the stack as an application supplied stack so that it
|
|
|
|
* isn't deallocated.
|
|
|
|
*
|
|
|
|
* XXX - I'm not sure it would hurt anything to deallocate
|
|
|
|
* the main thread stack because deallocation doesn't
|
|
|
|
* actually free() it; it just puts it in the free
|
|
|
|
* stack queue for later reuse.
|
|
|
|
*/
|
2005-02-13 18:38:06 +00:00
|
|
|
thread->attr.stackaddr_attr = (void *)_usrstack - _thr_stack_initial;
|
|
|
|
thread->attr.stacksize_attr = _thr_stack_initial;
|
2003-04-18 05:04:16 +00:00
|
|
|
thread->attr.guardsize_attr = _thr_guard_default;
|
|
|
|
thread->attr.flags |= THR_STACK_USER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a magic value to the thread structure
|
|
|
|
* to help identify valid ones:
|
|
|
|
*/
|
|
|
|
thread->magic = THR_MAGIC;
|
|
|
|
|
|
|
|
thread->slice_usec = -1;
|
|
|
|
thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
|
|
|
|
thread->name = strdup("initial thread");
|
|
|
|
|
|
|
|
/* Initialize the thread for signals: */
|
o Use a daemon thread to monitor signal events in kernel, if pending
signals were changed in kernel, it will retrieve the pending set and
try to find a thread to dispatch the signal. The dispatching process
can be rolled back if the signal is no longer in kernel.
o Create two functions _thr_signal_init() and _thr_signal_deinit(),
all signal action settings are retrieved from kernel when threading
mode is turned on, after a fork(), child process will reset them to
user settings by calling _thr_signal_deinit(). when threading mode
is not turned on, all signal operations are direct past to kernel.
o When a thread generated a synchoronous signals and its context returned
from completed list, UTS will retrieve the signal from its mailbox and try
to deliver the signal to thread.
o Context signal mask is now only used when delivering signals, thread's
current signal mask is always the one in pthread structure.
o Remove have_signals field in pthread structure, replace it with
psf_valid in pthread_signal_frame. when psf_valid is true, in context
switch time, thread will backout itself from some mutex/condition
internal queues, then begin to process signals. when a thread is not
at blocked state and running, check_pending indicates there are signals
for the thread, after preempted and then resumed time, UTS will try to
deliver signals to the thread.
o At signal delivering time, not only pending signals in thread will be
scanned, process's pending signals will be scanned too.
o Change sigwait code a bit, remove field sigwait in pthread_wait_data,
replace it with oldsigmask in pthread structure, when a thread calls
sigwait(), its current signal mask is backuped to oldsigmask, and waitset
is copied to its signal mask and when the thread gets a signal in the
waitset range, its current signal mask is restored from oldsigmask,
these are done in atomic fashion.
o Two additional POSIX APIs are implemented, sigwaitinfo() and sigtimedwait().
o Signal code locking is better than previous, there is fewer race conditions.
o Temporary disable most of code in _kse_single_thread as it is not safe
after fork().
2003-06-28 09:55:02 +00:00
|
|
|
SIGEMPTYSET(thread->sigmask);
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the thread mailbox. The threads saved context
|
|
|
|
* is also in the mailbox.
|
|
|
|
*/
|
2003-08-05 22:46:00 +00:00
|
|
|
thread->tcb->tcb_tmbx.tm_udata = thread;
|
|
|
|
thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size =
|
|
|
|
thread->attr.stacksize_attr;
|
|
|
|
thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp =
|
|
|
|
thread->attr.stackaddr_attr;
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Default the priority of the initial thread: */
|
|
|
|
thread->base_priority = THR_DEFAULT_PRIORITY;
|
|
|
|
thread->active_priority = THR_DEFAULT_PRIORITY;
|
|
|
|
thread->inherited_priority = 0;
|
|
|
|
|
|
|
|
/* Initialize the mutex queue: */
|
|
|
|
TAILQ_INIT(&thread->mutexq);
|
|
|
|
|
|
|
|
/* Initialize hooks in the thread structure: */
|
|
|
|
thread->specific = NULL;
|
|
|
|
thread->cleanup = NULL;
|
|
|
|
thread->flags = 0;
|
2004-12-18 18:07:37 +00:00
|
|
|
thread->sigbackout = NULL;
|
2003-04-18 05:04:16 +00:00
|
|
|
thread->continuation = NULL;
|
|
|
|
|
|
|
|
thread->state = PS_RUNNING;
|
|
|
|
thread->uniqueid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_private(void)
|
|
|
|
{
|
|
|
|
struct clockinfo clockinfo;
|
|
|
|
size_t len;
|
|
|
|
int mib[2];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid reinitializing some things if they don't need to be,
|
|
|
|
* e.g. after a fork().
|
|
|
|
*/
|
|
|
|
if (init_once == 0) {
|
2001-10-26 21:19:22 +00:00
|
|
|
/* Find the stack top */
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_USRSTACK;
|
2001-12-18 02:02:59 +00:00
|
|
|
len = sizeof (_usrstack);
|
2001-10-26 21:19:22 +00:00
|
|
|
if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
|
2003-04-18 05:04:16 +00:00
|
|
|
PANIC("Cannot get kern.usrstack from sysctl");
|
|
|
|
/* 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)
|
2005-02-18 16:07:05 +00:00
|
|
|
_clock_res_usec = 1000000 / clockinfo.stathz;
|
2003-04-18 05:04:16 +00:00
|
|
|
else
|
|
|
|
_clock_res_usec = CLOCK_RES_USEC;
|
|
|
|
|
|
|
|
_thr_page_size = getpagesize();
|
|
|
|
_thr_guard_default = _thr_page_size;
|
2005-02-13 18:38:06 +00:00
|
|
|
if (sizeof(void *) == 8) {
|
|
|
|
_thr_stack_default = THR_STACK64_DEFAULT;
|
|
|
|
_thr_stack_initial = THR_STACK64_INITIAL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_thr_stack_default = THR_STACK32_DEFAULT;
|
|
|
|
_thr_stack_initial = THR_STACK32_INITIAL;
|
|
|
|
}
|
2005-02-15 15:02:11 +00:00
|
|
|
_pthread_attr_default.guardsize_attr = _thr_guard_default;
|
|
|
|
_pthread_attr_default.stacksize_attr = _thr_stack_default;
|
2005-09-24 01:19:53 +00:00
|
|
|
TAILQ_INIT(&_thr_atfork_list);
|
2003-04-18 05:04:16 +00:00
|
|
|
init_once = 1; /* Don't do this again. */
|
|
|
|
} else {
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
2003-04-18 05:04:16 +00:00
|
|
|
* Destroy the locks before creating them. We don't
|
|
|
|
* know what state they are in so it is better to just
|
|
|
|
* recreate them.
|
1999-03-23 05:07:56 +00:00
|
|
|
*/
|
2003-04-18 05:04:16 +00:00
|
|
|
_lock_destroy(&_thread_signal_lock);
|
|
|
|
_lock_destroy(&_mutex_static_lock);
|
|
|
|
_lock_destroy(&_rwlock_static_lock);
|
|
|
|
_lock_destroy(&_keytable_lock);
|
|
|
|
}
|
2003-02-17 10:05:18 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/* Initialize everything else. */
|
|
|
|
TAILQ_INIT(&_thread_list);
|
|
|
|
TAILQ_INIT(&_thread_gc_list);
|
2003-11-04 20:04:45 +00:00
|
|
|
_pthread_mutex_init(&_thr_atfork_mutex, NULL);
|
2003-02-17 10:05:18 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* Initialize the lock for temporary installation of signal
|
|
|
|
* handlers (to support sigwait() semantics) and for the
|
|
|
|
* process signal mask and pending signal sets.
|
|
|
|
*/
|
|
|
|
if (_lock_init(&_thread_signal_lock, LCK_ADAPTIVE,
|
2003-04-29 21:03:33 +00:00
|
|
|
_kse_lock_wait, _kse_lock_wakeup) != 0)
|
2003-04-18 05:04:16 +00:00
|
|
|
PANIC("Cannot initialize _thread_signal_lock");
|
|
|
|
if (_lock_init(&_mutex_static_lock, LCK_ADAPTIVE,
|
|
|
|
_thr_lock_wait, _thr_lock_wakeup) != 0)
|
|
|
|
PANIC("Cannot initialize mutex static init lock");
|
|
|
|
if (_lock_init(&_rwlock_static_lock, LCK_ADAPTIVE,
|
|
|
|
_thr_lock_wait, _thr_lock_wakeup) != 0)
|
|
|
|
PANIC("Cannot initialize rwlock static init lock");
|
|
|
|
if (_lock_init(&_keytable_lock, LCK_ADAPTIVE,
|
|
|
|
_thr_lock_wait, _thr_lock_wakeup) != 0)
|
|
|
|
PANIC("Cannot initialize thread specific keytable lock");
|
2003-05-29 17:10:45 +00:00
|
|
|
_thr_spinlock_init();
|
2003-04-18 05:04:16 +00:00
|
|
|
|
|
|
|
/* Clear pending signals and get the process signal mask. */
|
o Use a daemon thread to monitor signal events in kernel, if pending
signals were changed in kernel, it will retrieve the pending set and
try to find a thread to dispatch the signal. The dispatching process
can be rolled back if the signal is no longer in kernel.
o Create two functions _thr_signal_init() and _thr_signal_deinit(),
all signal action settings are retrieved from kernel when threading
mode is turned on, after a fork(), child process will reset them to
user settings by calling _thr_signal_deinit(). when threading mode
is not turned on, all signal operations are direct past to kernel.
o When a thread generated a synchoronous signals and its context returned
from completed list, UTS will retrieve the signal from its mailbox and try
to deliver the signal to thread.
o Context signal mask is now only used when delivering signals, thread's
current signal mask is always the one in pthread structure.
o Remove have_signals field in pthread structure, replace it with
psf_valid in pthread_signal_frame. when psf_valid is true, in context
switch time, thread will backout itself from some mutex/condition
internal queues, then begin to process signals. when a thread is not
at blocked state and running, check_pending indicates there are signals
for the thread, after preempted and then resumed time, UTS will try to
deliver signals to the thread.
o At signal delivering time, not only pending signals in thread will be
scanned, process's pending signals will be scanned too.
o Change sigwait code a bit, remove field sigwait in pthread_wait_data,
replace it with oldsigmask in pthread structure, when a thread calls
sigwait(), its current signal mask is backuped to oldsigmask, and waitset
is copied to its signal mask and when the thread gets a signal in the
waitset range, its current signal mask is restored from oldsigmask,
these are done in atomic fashion.
o Two additional POSIX APIs are implemented, sigwaitinfo() and sigtimedwait().
o Signal code locking is better than previous, there is fewer race conditions.
o Temporary disable most of code in _kse_single_thread as it is not safe
after fork().
2003-06-28 09:55:02 +00:00
|
|
|
SIGEMPTYSET(_thr_proc_sigpending);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2004-08-07 15:15:38 +00:00
|
|
|
/* Are we in M:N mode (default) or 1:1 mode? */
|
|
|
|
#ifdef SYSTEM_SCOPE_ONLY
|
|
|
|
_thread_scope_system = 1;
|
|
|
|
#else
|
|
|
|
if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
|
|
|
|
_thread_scope_system = 1;
|
2004-08-12 12:12:12 +00:00
|
|
|
else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
|
|
|
|
_thread_scope_system = -1;
|
2004-08-07 15:15:38 +00:00
|
|
|
#endif
|
2006-03-06 05:02:28 +00:00
|
|
|
if (getenv("LIBPTHREAD_DEBUG") != NULL)
|
|
|
|
_thr_debug_flags |= DBG_INFO_DUMP;
|
2004-08-07 15:15:38 +00:00
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* _thread_list_lock and _kse_count are initialized
|
|
|
|
* by _kse_init()
|
|
|
|
*/
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|