1996-01-22 00:23:58 +00:00
|
|
|
/*
|
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>
|
|
|
|
#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>
|
1999-07-12 16:09:30 +00:00
|
|
|
#include <sys/user.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>
|
|
|
|
#include <poll.h>
|
1996-01-22 00:23:58 +00:00
|
|
|
#include <pthread.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"
|
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
#include "pthread_private.h"
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/*
|
|
|
|
* All weak references used within libc should be in this table.
|
|
|
|
* This will is so that static libraries will work.
|
|
|
|
*/
|
|
|
|
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,
|
|
|
|
&_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
|
|
|
};
|
|
|
|
|
1998-02-13 01:27:34 +00:00
|
|
|
|
1997-02-05 23:26:09 +00:00
|
|
|
/*
|
|
|
|
* Threaded process initialization
|
|
|
|
*/
|
1996-01-22 00:23:58 +00:00
|
|
|
void
|
|
|
|
_thread_init(void)
|
|
|
|
{
|
1998-08-10 01:24:22 +00:00
|
|
|
int fd;
|
1996-01-22 00:23:58 +00:00
|
|
|
int flags;
|
|
|
|
int i;
|
1999-06-23 15:01:22 +00:00
|
|
|
size_t len;
|
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
|
|
|
int mib[2];
|
|
|
|
struct clockinfo clockinfo;
|
1996-01-22 00:23:58 +00:00
|
|
|
struct sigaction act;
|
1997-02-05 23:26:09 +00:00
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Check if this function has already been called: */
|
1997-02-05 23:26:09 +00:00
|
|
|
if (_thread_initial)
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Only initialise 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
|
|
|
|
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:
|
|
|
|
*/
|
|
|
|
if (getpid() == 1) {
|
|
|
|
/*
|
|
|
|
* 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");
|
2001-01-24 13:03:38 +00:00
|
|
|
if (__sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't set controlling terminal");
|
2001-01-24 13:03:38 +00:00
|
|
|
if (__sys_dup2(fd,0) == -1 ||
|
|
|
|
__sys_dup2(fd,1) == -1 ||
|
|
|
|
__sys_dup2(fd,2) == -1)
|
1998-08-10 01:24:22 +00:00
|
|
|
PANIC("Can't dup2");
|
|
|
|
}
|
|
|
|
|
1997-02-05 23:26:09 +00:00
|
|
|
/* Get the standard I/O flags before messing with them : */
|
|
|
|
for (i = 0; i < 3; i++)
|
1999-08-05 12:08:10 +00:00
|
|
|
if (((_pthread_stdio_flags[i] =
|
2001-01-24 13:03:38 +00:00
|
|
|
__sys_fcntl(i,F_GETFL, NULL)) == -1) &&
|
1999-08-05 12:08:10 +00:00
|
|
|
(errno != EBADF))
|
1997-02-05 23:26:09 +00:00
|
|
|
PANIC("Cannot get stdio flags");
|
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/*
|
|
|
|
* Create a pipe that is written to by the signal handler to prevent
|
2000-11-09 05:08:26 +00:00
|
|
|
* signals being missed in calls to _select:
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
if (__sys_pipe(_thread_kern_pipe) != 0) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Cannot create pipe, so abort: */
|
|
|
|
PANIC("Cannot create kernel pipe");
|
|
|
|
}
|
|
|
|
/* Get the flags for the read pipe: */
|
2001-01-24 13:03:38 +00:00
|
|
|
else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Abort this application: */
|
|
|
|
PANIC("Cannot get kernel read pipe flags");
|
|
|
|
}
|
|
|
|
/* Make the read pipe non-blocking: */
|
2001-01-24 13:03:38 +00:00
|
|
|
else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Abort this application: */
|
|
|
|
PANIC("Cannot make kernel read pipe non-blocking");
|
|
|
|
}
|
|
|
|
/* Get the flags for the write pipe: */
|
2001-01-24 13:03:38 +00:00
|
|
|
else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Abort this application: */
|
|
|
|
PANIC("Cannot get kernel write pipe flags");
|
|
|
|
}
|
|
|
|
/* Make the write pipe non-blocking: */
|
2001-01-24 13:03:38 +00:00
|
|
|
else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Abort this application: */
|
|
|
|
PANIC("Cannot get kernel write pipe flags");
|
|
|
|
}
|
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
|
|
|
/* Allocate and initialize the ready queue: */
|
2000-10-13 22:12:32 +00:00
|
|
|
else if (_pq_alloc(&_readyq, PTHREAD_MIN_PRIORITY, PTHREAD_LAST_PRIORITY) != 0) {
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Abort this application: */
|
|
|
|
PANIC("Cannot allocate priority ready queue.");
|
|
|
|
}
|
1996-01-22 00:23:58 +00:00
|
|
|
/* 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
|
2000-11-09 05:08:26 +00:00
|
|
|
* abort:
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
PANIC("Cannot allocate memory for initial thread");
|
2000-10-13 22:12:32 +00:00
|
|
|
}
|
|
|
|
/* Allocate memory for the scheduler stack: */
|
2000-11-09 05:08:26 +00:00
|
|
|
else if ((_thread_kern_sched_stack = malloc(SCHED_STACK_SIZE)) == NULL)
|
2000-10-13 22:12:32 +00:00
|
|
|
PANIC("Failed to allocate stack for scheduler");
|
|
|
|
else {
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Zero the global kernel thread structure: */
|
|
|
|
memset(&_thread_kern_thread, 0, sizeof(struct pthread));
|
1999-03-23 05:07:56 +00:00
|
|
|
_thread_kern_thread.flags = PTHREAD_FLAGS_PRIVATE;
|
1996-01-22 00:23:58 +00:00
|
|
|
memset(_thread_initial, 0, sizeof(struct pthread));
|
|
|
|
|
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
|
|
|
/* Initialize the waiting and work queues: */
|
1999-03-23 05:07:56 +00:00
|
|
|
TAILQ_INIT(&_waitingq);
|
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
|
|
|
TAILQ_INIT(&_workq);
|
1999-03-23 05:07:56 +00:00
|
|
|
|
|
|
|
/* Initialize the scheduling switch hook routine: */
|
|
|
|
_sched_switch_hook = NULL;
|
|
|
|
|
2000-01-19 07:04:50 +00:00
|
|
|
/* Give this thread default attributes: */
|
|
|
|
memcpy((void *) &_thread_initial->attr, &pthread_attr_default,
|
|
|
|
sizeof(struct pthread_attr));
|
|
|
|
|
1999-07-05 00:35:19 +00:00
|
|
|
/* Initialize the thread stack cache: */
|
|
|
|
SLIST_INIT(&_stackq);
|
|
|
|
|
1999-12-29 15:44:59 +00:00
|
|
|
/*
|
|
|
|
* Create a red zone below the main stack. All other stacks are
|
|
|
|
* constrained to a maximum size by the paramters 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.
|
|
|
|
*/
|
1999-12-28 18:12:07 +00:00
|
|
|
if (mmap((void *) USRSTACK - PTHREAD_STACK_INITIAL -
|
|
|
|
PTHREAD_STACK_GUARD, PTHREAD_STACK_GUARD, 0, MAP_ANON,
|
|
|
|
-1, 0) == MAP_FAILED)
|
1999-07-05 00:35:19 +00:00
|
|
|
PANIC("Cannot allocate red zone for initial thread");
|
1999-12-28 18:12:07 +00:00
|
|
|
|
2000-01-19 07:04:50 +00:00
|
|
|
/* Set the main thread stack pointer. */
|
|
|
|
_thread_initial->stack = (void *) USRSTACK -
|
|
|
|
PTHREAD_STACK_INITIAL;
|
|
|
|
|
|
|
|
/* Set the stack attributes: */
|
|
|
|
_thread_initial->attr.stackaddr_attr = _thread_initial->stack;
|
|
|
|
_thread_initial->attr.stacksize_attr = PTHREAD_STACK_INITIAL;
|
|
|
|
|
2000-10-13 22:12:32 +00:00
|
|
|
/* Setup the context for the scheduler: */
|
|
|
|
_setjmp(_thread_kern_sched_jb);
|
2000-11-09 05:08:26 +00:00
|
|
|
SET_STACK_JB(_thread_kern_sched_jb, _thread_kern_sched_stack +
|
|
|
|
SCHED_STACK_SIZE - sizeof(double));
|
2000-10-13 22:12:32 +00:00
|
|
|
SET_RETURN_ADDR_JB(_thread_kern_sched_jb, _thread_kern_scheduler);
|
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/*
|
|
|
|
* Write a magic value to the thread structure
|
|
|
|
* to help identify valid ones:
|
|
|
|
*/
|
|
|
|
_thread_initial->magic = PTHREAD_MAGIC;
|
|
|
|
|
1999-11-28 05:38:13 +00:00
|
|
|
/* Set the initial cancel state */
|
|
|
|
_thread_initial->cancelflags = PTHREAD_CANCEL_ENABLE |
|
|
|
|
PTHREAD_CANCEL_DEFERRED;
|
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Default the priority of the initial thread: */
|
1999-03-23 05:07:56 +00:00
|
|
|
_thread_initial->base_priority = PTHREAD_DEFAULT_PRIORITY;
|
|
|
|
_thread_initial->active_priority = PTHREAD_DEFAULT_PRIORITY;
|
|
|
|
_thread_initial->inherited_priority = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
|
|
|
|
/* Initialise the state of the initial thread: */
|
|
|
|
_thread_initial->state = PS_RUNNING;
|
|
|
|
|
2001-01-24 13:03:38 +00:00
|
|
|
/* Set the name of the thread: */
|
|
|
|
_thread_initial->name = strdup("_thread_initial");
|
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Initialise the queue: */
|
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
|
|
|
TAILQ_INIT(&(_thread_initial->join_queue));
|
1996-01-22 00:23:58 +00:00
|
|
|
|
1999-03-23 05:07:56 +00:00
|
|
|
/* Initialize the owned mutex queue and count: */
|
|
|
|
TAILQ_INIT(&(_thread_initial->mutexq));
|
|
|
|
_thread_initial->priority_mutex_count = 0;
|
|
|
|
|
2000-10-13 22:12:32 +00:00
|
|
|
/* Initialize the global scheduling time: */
|
|
|
|
_sched_ticks = 0;
|
|
|
|
gettimeofday((struct timeval *) &_sched_tod, NULL);
|
|
|
|
|
|
|
|
/* Initialize last active: */
|
|
|
|
_thread_initial->last_active = (long) _sched_ticks;
|
|
|
|
|
2000-11-09 05:08:26 +00:00
|
|
|
/* Initialize the initial context: */
|
|
|
|
_thread_initial->curframe = NULL;
|
|
|
|
_thread_initial->ctxtype = CTX_JB_NOSIG;
|
2000-01-18 11:35:25 +00:00
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Initialise the rest of the fields: */
|
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
|
|
|
_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;
|
1996-01-22 00:23:58 +00:00
|
|
|
_thread_initial->specific_data = NULL;
|
|
|
|
_thread_initial->cleanup = NULL;
|
|
|
|
_thread_initial->flags = 0;
|
|
|
|
_thread_initial->error = 0;
|
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
|
|
|
TAILQ_INIT(&_thread_list);
|
|
|
|
TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle);
|
2001-01-24 13:03:38 +00:00
|
|
|
_set_curthread(_thread_initial);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
|
|
|
/* Initialise the global signal action structure: */
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
act.sa_handler = (void (*) ()) _thread_sig_handler;
|
2000-11-09 05:08:26 +00:00
|
|
|
act.sa_flags = SA_SIGINFO | SA_ONSTACK;
|
2000-10-13 22:12:32 +00:00
|
|
|
|
|
|
|
/* Clear pending signals for the process: */
|
|
|
|
sigemptyset(&_process_sigpending);
|
1996-01-22 00:23:58 +00:00
|
|
|
|
2000-10-13 22:12:32 +00:00
|
|
|
/* Clear the signal queue: */
|
|
|
|
memset(_thread_sigq, 0, sizeof(_thread_sigq));
|
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
|
|
|
|
2000-11-14 20:00:19 +00:00
|
|
|
/*
|
|
|
|
* Create and install an alternate signal stack of
|
|
|
|
* the recommended size:
|
|
|
|
*/
|
|
|
|
_thread_sigstack.ss_sp = malloc(SIGSTKSZ);
|
|
|
|
_thread_sigstack.ss_size = SIGSTKSZ;
|
|
|
|
_thread_sigstack.ss_flags = 0;
|
|
|
|
if ((_thread_sigstack.ss_sp == NULL) ||
|
2001-01-24 13:03:38 +00:00
|
|
|
(__sys_sigaltstack(&_thread_sigstack, NULL) != 0))
|
2000-11-09 05:08:26 +00:00
|
|
|
PANIC("Unable to install alternate signal stack");
|
|
|
|
|
1998-04-29 09:59:34 +00:00
|
|
|
/* Enter a loop to get the existing signal status: */
|
1996-01-22 00:23:58 +00:00
|
|
|
for (i = 1; i < NSIG; i++) {
|
|
|
|
/* Check for signals which cannot be trapped: */
|
|
|
|
if (i == SIGKILL || i == SIGSTOP) {
|
|
|
|
}
|
1998-04-29 09:59:34 +00:00
|
|
|
|
|
|
|
/* Get the signal handler details: */
|
2001-01-24 13:03:38 +00:00
|
|
|
else if (__sys_sigaction(i, NULL,
|
1999-12-28 18:12:07 +00:00
|
|
|
&_thread_sigact[i - 1]) != 0) {
|
1996-01-22 00:23:58 +00:00
|
|
|
/*
|
|
|
|
* Abort this process if signal
|
2000-11-09 05:08:26 +00:00
|
|
|
* initialisation fails:
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
1998-04-29 09:59:34 +00:00
|
|
|
PANIC("Cannot read signal handler info");
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
2000-06-27 21:30:16 +00:00
|
|
|
|
|
|
|
/* Initialize the SIG_DFL dummy handler count. */
|
|
|
|
_thread_dfl_count[i] = 0;
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
|
1998-04-29 09:59:34 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-01-24 13:03:38 +00:00
|
|
|
if (__sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 ||
|
|
|
|
__sys_sigaction(SIGINFO, &act, NULL) != 0 ||
|
|
|
|
__sys_sigaction(SIGCHLD, &act, NULL) != 0) {
|
1998-04-29 09:59:34 +00:00
|
|
|
/*
|
2000-11-09 05:08:26 +00:00
|
|
|
* Abort this process if signal initialisation fails:
|
1998-04-29 09:59:34 +00:00
|
|
|
*/
|
|
|
|
PANIC("Cannot initialise signal handler");
|
|
|
|
}
|
2000-10-13 22:12:32 +00:00
|
|
|
_thread_sigact[_SCHED_SIGNAL - 1].sa_flags = SA_SIGINFO;
|
|
|
|
_thread_sigact[SIGINFO - 1].sa_flags = SA_SIGINFO;
|
|
|
|
_thread_sigact[SIGCHLD - 1].sa_flags = SA_SIGINFO;
|
|
|
|
|
|
|
|
/* Get the process signal mask: */
|
2001-01-24 13:03:38 +00:00
|
|
|
__sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask);
|
1998-04-29 09:59:34 +00:00
|
|
|
|
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
|
|
|
/* 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)
|
2000-10-13 22:12:32 +00:00
|
|
|
_clock_res_usec = clockinfo.tick;
|
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
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/* Get the table size: */
|
|
|
|
if ((_thread_dtablesize = getdtablesize()) < 0) {
|
|
|
|
/*
|
|
|
|
* Cannot get the system defined table size, so abort
|
2000-11-09 05:08:26 +00:00
|
|
|
* this process.
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
PANIC("Cannot get dtablesize");
|
|
|
|
}
|
|
|
|
/* Allocate memory for the file descriptor table: */
|
1997-05-13 23:54:22 +00:00
|
|
|
if ((_thread_fd_table = (struct fd_table_entry **) malloc(sizeof(struct fd_table_entry *) * _thread_dtablesize)) == NULL) {
|
1999-08-05 12:08:10 +00:00
|
|
|
/* Avoid accesses to file descriptor table on exit: */
|
|
|
|
_thread_dtablesize = 0;
|
|
|
|
|
1996-01-22 00:23:58 +00:00
|
|
|
/*
|
|
|
|
* Cannot allocate memory for the file descriptor
|
2000-11-09 05:08:26 +00:00
|
|
|
* table, so abort this process.
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
PANIC("Cannot allocate memory for file descriptor table");
|
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
|
|
|
}
|
|
|
|
/* 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
|
2000-11-09 05:08:26 +00:00
|
|
|
* table, so abort this process.
|
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
|
|
|
*/
|
|
|
|
PANIC("Cannot allocate memory for pollfd table");
|
1996-01-22 00:23:58 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Enter a loop to initialise the file descriptor
|
2000-11-09 05:08:26 +00:00
|
|
|
* table:
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
for (i = 0; i < _thread_dtablesize; i++) {
|
|
|
|
/* Initialise the file descriptor table: */
|
|
|
|
_thread_fd_table[i] = NULL;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize stdio file descriptor table entries: */
|
1999-08-05 12:08:10 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if ((_thread_fd_table_init(i) != 0) &&
|
|
|
|
(errno != EBADF))
|
|
|
|
PANIC("Cannot initialize stdio file "
|
|
|
|
"descriptor table entry");
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-02-13 01:27:34 +00:00
|
|
|
|
1998-09-30 06:36:56 +00:00
|
|
|
/* Initialise the garbage collector mutex and condition variable. */
|
2001-01-24 13:03:38 +00:00
|
|
|
if (_pthread_mutex_init(&_gc_mutex,NULL) != 0 ||
|
1998-09-30 06:36:56 +00:00
|
|
|
pthread_cond_init(&_gc_cond,NULL) != 0)
|
|
|
|
PANIC("Failed to initialise garbage collector mutex or condvar");
|
1996-01-22 00:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-11-09 05:08:26 +00:00
|
|
|
* Special start up code for NetBSD/Alpha
|
1996-01-22 00:23:58 +00:00
|
|
|
*/
|
|
|
|
#if defined(__NetBSD__) && defined(__alpha__)
|
2000-11-09 05:08:26 +00:00
|
|
|
int
|
1996-01-22 00:23:58 +00:00
|
|
|
main(int argc, char *argv[], char *env);
|
|
|
|
|
|
|
|
int
|
|
|
|
_thread_main(int argc, char *argv[], char *env)
|
|
|
|
{
|
|
|
|
_thread_init();
|
|
|
|
return (main(argc, argv, env));
|
|
|
|
}
|
|
|
|
#endif
|