summaryrefslogtreecommitdiff
path: root/lib/libc_r/uthread
diff options
context:
space:
mode:
authorDavid Leonard <d@cvs.openbsd.org>1998-11-09 03:13:22 +0000
committerDavid Leonard <d@cvs.openbsd.org>1998-11-09 03:13:22 +0000
commitff47464de5e68e9c49fe312c953f54b2e90f546c (patch)
tree4ec0e7f13d619e1b5bf6abfd7125a4ec17569568 /lib/libc_r/uthread
parent1b2b2c2386c6cc5ebffc769b8a39b80d47e4e0d0 (diff)
sync with FreeBSD (rwlock, gc thread, man pages)
add (broken) mips md stuff fix some const warnings add sigaltstack() stub another hash at getting shlib auto-init to work (mips/elf and i386/a.out)
Diffstat (limited to 'lib/libc_r/uthread')
-rw-r--r--lib/libc_r/uthread/Makefile.inc9
-rw-r--r--lib/libc_r/uthread/pthread_private.h52
-rw-r--r--lib/libc_r/uthread/uthread_close.c1
-rw-r--r--lib/libc_r/uthread/uthread_cond.c4
-rw-r--r--lib/libc_r/uthread/uthread_create.c21
-rw-r--r--lib/libc_r/uthread/uthread_detach.c1
-rw-r--r--lib/libc_r/uthread/uthread_dup2.c17
-rw-r--r--lib/libc_r/uthread/uthread_execve.c2
-rw-r--r--lib/libc_r/uthread/uthread_exit.c68
-rw-r--r--lib/libc_r/uthread/uthread_fcntl.c1
-rw-r--r--lib/libc_r/uthread/uthread_fd.c9
-rw-r--r--lib/libc_r/uthread/uthread_file.c29
-rw-r--r--lib/libc_r/uthread/uthread_find_thread.c15
-rw-r--r--lib/libc_r/uthread/uthread_fork.c8
-rw-r--r--lib/libc_r/uthread/uthread_gc.c278
-rw-r--r--lib/libc_r/uthread/uthread_info.c40
-rw-r--r--lib/libc_r/uthread/uthread_init.c35
-rw-r--r--lib/libc_r/uthread/uthread_kern.c104
-rw-r--r--lib/libc_r/uthread/uthread_kill.c44
-rw-r--r--lib/libc_r/uthread/uthread_rwlock.c336
-rw-r--r--lib/libc_r/uthread/uthread_rwlockattr.c99
-rw-r--r--lib/libc_r/uthread/uthread_sig.c61
-rw-r--r--lib/libc_r/uthread/uthread_sigaction.c2
-rw-r--r--lib/libc_r/uthread/uthread_sigaltstack.c21
-rw-r--r--lib/libc_r/uthread/uthread_signal.c2
-rw-r--r--lib/libc_r/uthread/uthread_sigsuspend.c8
-rw-r--r--lib/libc_r/uthread/uthread_sigwait.c46
-rw-r--r--lib/libc_r/uthread/uthread_spec.c2
-rw-r--r--lib/libc_r/uthread/uthread_spinlock.c8
-rw-r--r--lib/libc_r/uthread/uthread_vfork.c12
30 files changed, 1040 insertions, 295 deletions
diff --git a/lib/libc_r/uthread/Makefile.inc b/lib/libc_r/uthread/Makefile.inc
index 0e285297a0b..196cddca170 100644
--- a/lib/libc_r/uthread/Makefile.inc
+++ b/lib/libc_r/uthread/Makefile.inc
@@ -1,5 +1,5 @@
-# $Id: Makefile.inc,v 1.1 1998/08/27 09:00:49 d Exp $
-# $OpenBSD: Makefile.inc,v 1.1 1998/08/27 09:00:49 d Exp $
+# $Id: Makefile.inc,v 1.2 1998/11/09 03:13:18 d Exp $
+# $OpenBSD: Makefile.inc,v 1.2 1998/11/09 03:13:18 d Exp $
# uthread sources
.PATH: ${.CURDIR}/uthread
@@ -42,6 +42,7 @@ SRCS+= \
uthread_fstat.c \
uthread_fstatfs.c \
uthread_fsync.c \
+ uthread_gc.c \
uthread_getdirentries.c \
uthread_getpeername.c \
uthread_getprio.c \
@@ -69,6 +70,8 @@ SRCS+= \
uthread_recvfrom.c \
uthread_recvmsg.c \
uthread_resume_np.c \
+ uthread_rwlock.c \
+ uthread_rwlockattr.c \
uthread_select.c \
uthread_self.c \
uthread_sendmsg.c \
@@ -79,6 +82,7 @@ SRCS+= \
uthread_shutdown.c \
uthread_sig.c \
uthread_sigaction.c \
+ uthread_sigaltstack.c \
uthread_sigblock.c \
uthread_sigmask.c \
uthread_sigprocmask.c \
@@ -91,6 +95,7 @@ SRCS+= \
uthread_spec.c \
uthread_spinlock.c \
uthread_suspend_np.c \
+ uthread_vfork.c \
uthread_wait4.c \
uthread_write.c \
uthread_writev.c \
diff --git a/lib/libc_r/uthread/pthread_private.h b/lib/libc_r/uthread/pthread_private.h
index ce7da3da5c4..2197bb709f6 100644
--- a/lib/libc_r/uthread/pthread_private.h
+++ b/lib/libc_r/uthread/pthread_private.h
@@ -56,8 +56,8 @@
#define PANIC(string) _thread_exit(__FILE__,__LINE__,string)
/* Output debug messages like this: */
-#define stdout_debug(_x) _write(1,_x,strlen(_x));
-#define stderr_debug(_x) _write(2,_x,strlen(_x));
+#define stdout_debug(_x) _thread_sys_write(1,_x,strlen(_x));
+#define stderr_debug(_x) _thread_sys_write(2,_x,strlen(_x));
/*
* State change macro:
@@ -218,6 +218,18 @@ struct pthread_key {
void (*destructor) ();
};
+struct pthread_rwlockattr {
+ int pshared;
+};
+
+struct pthread_rwlock {
+ pthread_mutex_t lock; /* monitor lock */
+ int state; /* 0 = idle >0 = # of readers -1 = writer */
+ pthread_cond_t read_signal;
+ pthread_cond_t write_signal;
+ int blocked_writers;
+};
+
/*
* Thread states.
*/
@@ -234,6 +246,7 @@ enum pthread_state {
PS_SELECT_WAIT,
PS_SLEEP_WAIT,
PS_WAIT_WAIT,
+ PS_SIGSUSPEND,
PS_SIGWAIT,
PS_JOIN,
PS_SUSPENDED,
@@ -264,9 +277,9 @@ struct fd_table_entry {
struct pthread_queue w_queue; /* Write queue. */
struct pthread *r_owner; /* Ptr to thread owning read lock. */
struct pthread *w_owner; /* Ptr to thread owning write lock. */
- char *r_fname; /* Ptr to read lock source file name */
+ const char *r_fname; /* Ptr to read lock source file name */
int r_lineno; /* Read lock source line number. */
- char *w_fname; /* Ptr to write lock source file name */
+ const char *w_fname; /* Ptr to write lock source file name */
int w_lineno; /* Write lock source line number. */
int r_lockcount; /* Count for FILE read locks. */
int w_lockcount; /* Count for FILE write locks. */
@@ -287,7 +300,7 @@ union pthread_wait_data {
struct {
short fd; /* Used when thread waiting on fd */
short branch; /* Line number, for debugging. */
- char *fname; /* Source file name for debugging.*/
+ const char *fname; /* Source file name for debugging.*/
} fd;
struct pthread_select_data * select_data;
};
@@ -315,6 +328,11 @@ struct pthread {
struct pthread *nxt;
/*
+ * Pointer to the next thread in the dead thread linked list.
+ */
+ struct pthread *nxt_dead;
+
+ /*
* Thread start routine, argument, stack pointer and thread
* attributes.
*/
@@ -426,7 +444,7 @@ struct pthread {
/* Cleanup handlers Link List */
struct pthread_cleanup *cleanup;
- char *fname; /* Ptr to source file name */
+ const char *fname; /* Ptr to source file name */
int lineno; /* Source line number. */
};
@@ -486,6 +504,10 @@ extern int _pthread_stdio_flags[3];
extern struct fd_table_entry **_thread_fd_table;
extern int _thread_dtablesize;
+/* Garbage collector mutex and condition variable. */
+extern pthread_mutex_t _gc_mutex;
+extern pthread_cond_t _gc_cond;
+
/*
* Array of signal actions for this process.
*/
@@ -514,24 +536,23 @@ int _find_dead_thread(pthread_t);
int _find_thread(pthread_t);
int _thread_create(pthread_t *,const pthread_attr_t *,void *(*start_routine)(void *),void *,pthread_t);
int _thread_fd_lock(int, int, struct timespec *);
-int _thread_fd_lock_debug(int, int, struct timespec *,char *fname,int lineno);
+int _thread_fd_lock_debug(int, int, struct timespec *,const char *fname,int lineno);
void _dispatch_signals(void);
void _thread_signal(pthread_t, int);
-void _lock_dead_thread_list(void);
void _lock_thread(void);
void _lock_thread_list(void);
-void _unlock_dead_thread_list(void);
void _unlock_thread(void);
void _unlock_thread_list(void);
-void _thread_exit(char *, int, char *);
+void _thread_exit(char *, int, char *)
+ __attribute__((noreturn));
void _thread_fd_unlock(int, int);
void _thread_fd_unlock_debug(int, int, char *, int);
void *_thread_cleanup(pthread_t);
void _thread_cleanupspecific(void);
void _thread_dump_info(void);
-void _thread_init(void) __attribute__((constructor));
+void _thread_init(void) /* __attribute__((constructor)) */;
void _thread_kern_sched(struct sigcontext *);
-void _thread_kern_sched_state(enum pthread_state,char *fname,int lineno);
+void _thread_kern_sched_state(enum pthread_state,const char *fname,int lineno);
void _thread_kern_set_timeout(struct timespec *);
void _thread_sig_handler(int, int, struct sigcontext *);
void _thread_start(void);
@@ -543,6 +564,8 @@ int _thread_queue_remove(struct pthread_queue *, struct pthread *);
int _thread_fd_table_init(int fd);
struct pthread *_thread_queue_get(struct pthread_queue *);
struct pthread *_thread_queue_deq(struct pthread_queue *);
+pthread_addr_t _thread_gc(pthread_addr_t);
+
/* #include <signal.h> */
#ifdef _USER_SIGNAL_H
@@ -553,7 +576,7 @@ int _thread_sys_sigsuspend(const sigset_t *);
int _thread_sys_siginterrupt(int, int);
int _thread_sys_sigpause(int);
int _thread_sys_sigreturn(struct sigcontext *);
-int _thread_sys_sigstack(const struct sigstack *, struct sigstack *);
+int _thread_sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *);
int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *);
void _thread_sys_psignal(unsigned int, const char *);
void (*_thread_sys_signal(int, void (*)(int)))(int);
@@ -675,7 +698,8 @@ pid_t _thread_sys_fork(void);
pid_t _thread_sys_tcgetpgrp(int);
ssize_t _thread_sys_read(int, void *, size_t);
ssize_t _thread_sys_write(int, const void *, size_t);
-void _thread_sys__exit(int);
+void _thread_sys__exit(int)
+ __attribute__((noreturn));
#endif
/* #include <fcntl.h> */
diff --git a/lib/libc_r/uthread/uthread_close.c b/lib/libc_r/uthread/uthread_close.c
index 7e21853223e..4bc4999ebda 100644
--- a/lib/libc_r/uthread/uthread_close.c
+++ b/lib/libc_r/uthread/uthread_close.c
@@ -43,7 +43,6 @@ close(int fd)
{
int flags;
int ret;
- int status;
struct stat sb;
/* Lock the file descriptor while the file is closed: */
diff --git a/lib/libc_r/uthread/uthread_cond.c b/lib/libc_r/uthread/uthread_cond.c
index fae12ebb68b..aedc36b4b31 100644
--- a/lib/libc_r/uthread/uthread_cond.c
+++ b/lib/libc_r/uthread/uthread_cond.c
@@ -126,7 +126,6 @@ int
pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex)
{
int rval = 0;
- int status;
if (cond == NULL)
rval = EINVAL;
@@ -190,7 +189,6 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
const struct timespec * abstime)
{
int rval = 0;
- int status;
if (cond == NULL)
rval = EINVAL;
@@ -270,7 +268,6 @@ int
pthread_cond_signal(pthread_cond_t * cond)
{
int rval = 0;
- int status;
pthread_t pthread;
if (cond == NULL || *cond == NULL)
@@ -309,7 +306,6 @@ int
pthread_cond_broadcast(pthread_cond_t * cond)
{
int rval = 0;
- int status;
pthread_t pthread;
if (cond == NULL || *cond == NULL)
diff --git a/lib/libc_r/uthread/uthread_create.c b/lib/libc_r/uthread/uthread_create.c
index 53759d8f440..68db603cac2 100644
--- a/lib/libc_r/uthread/uthread_create.c
+++ b/lib/libc_r/uthread/uthread_create.c
@@ -44,9 +44,9 @@ int
pthread_create(pthread_t * thread, const pthread_attr_t * attr,
void *(*start_routine) (void *), void *arg)
{
- int i;
+ int f_gc = 0;
int ret = 0;
- int status;
+ pthread_t gc_thread;
pthread_t new_thread;
pthread_attr_t pattr;
void *stack;
@@ -149,6 +149,12 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
/* Lock the thread list: */
_lock_thread_list();
+ /*
+ * Check if the garbage collector thread
+ * needs to be started.
+ */
+ f_gc = (_thread_link_list == _thread_initial);
+
/* Add the thread to the linked list of all threads: */
new_thread->nxt = _thread_link_list;
_thread_link_list = new_thread;
@@ -161,6 +167,14 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
/* Schedule the new user thread: */
_thread_kern_sched(NULL);
+
+ /*
+ * Start a garbage collector thread
+ * if necessary.
+ */
+ if (f_gc && pthread_create(&gc_thread,NULL,
+ _thread_gc,NULL) != 0)
+ PANIC("Can't create gc thread");
}
}
@@ -171,6 +185,9 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
void
_thread_start(void)
{
+ /* We just left the scheduler via longjmp: */
+ _thread_kern_in_sched = 0;
+
/* Run the current thread's start routine with argument: */
pthread_exit(_thread_run->start_routine(_thread_run->arg));
diff --git a/lib/libc_r/uthread/uthread_detach.c b/lib/libc_r/uthread/uthread_detach.c
index da456bfbab9..d471da6f068 100644
--- a/lib/libc_r/uthread/uthread_detach.c
+++ b/lib/libc_r/uthread/uthread_detach.c
@@ -39,7 +39,6 @@ int
pthread_detach(pthread_t pthread)
{
int rval = 0;
- int status;
pthread_t next_thread;
/* Check for invalid calling parameters: */
diff --git a/lib/libc_r/uthread/uthread_dup2.c b/lib/libc_r/uthread/uthread_dup2.c
index e9edddd35c1..3e4ed27d359 100644
--- a/lib/libc_r/uthread/uthread_dup2.c
+++ b/lib/libc_r/uthread/uthread_dup2.c
@@ -30,6 +30,7 @@
* SUCH DAMAGE.
*
*/
+#include <errno.h>
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@@ -39,11 +40,20 @@ int
dup2(int fd, int newfd)
{
int ret;
+ int newfd_opened;
+
+ /* Check if the file descriptor is out of range: */
+ if (newfd < 0 || newfd >= _thread_dtablesize) {
+ /* Return a bad file descriptor error: */
+ errno = EBADF;
+ ret = -1;
+ }
/* Lock the file descriptor: */
- if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
+ else if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Lock the file descriptor: */
- if ((ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
+ if (!(newfd_opened = (_thread_fd_table[newfd] != NULL)) ||
+ (ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
/* Perform the 'dup2' syscall: */
if ((ret = _thread_sys_dup2(fd, newfd)) < 0) {
}
@@ -63,7 +73,8 @@ dup2(int fd, int newfd)
}
/* Unlock the file descriptor: */
- _FD_UNLOCK(newfd, FD_RDWR);
+ if (newfd_opened)
+ _FD_UNLOCK(newfd, FD_RDWR);
}
/* Unlock the file descriptor: */
_FD_UNLOCK(fd, FD_RDWR);
diff --git a/lib/libc_r/uthread/uthread_execve.c b/lib/libc_r/uthread/uthread_execve.c
index 5f40e1c560e..0d289a44ab9 100644
--- a/lib/libc_r/uthread/uthread_execve.c
+++ b/lib/libc_r/uthread/uthread_execve.c
@@ -97,7 +97,7 @@ execve(const char *name, char *const * argv, char *const * envp)
}
}
- /* Execute the process: */
+ /* Set the signal mask: */
_thread_sys_sigprocmask(SIG_SETMASK, &_thread_run->sigmask, NULL);
/* Execute the process: */
diff --git a/lib/libc_r/uthread/uthread_exit.c b/lib/libc_r/uthread/uthread_exit.c
index 0083f2b0169..bc061e0fa6a 100644
--- a/lib/libc_r/uthread/uthread_exit.c
+++ b/lib/libc_r/uthread/uthread_exit.c
@@ -34,10 +34,13 @@
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
+void _exit(int) __attribute__((noreturn));
void _exit(int status)
{
int flags;
@@ -98,14 +101,12 @@ _thread_exit(char *fname, int lineno, char *string)
void
pthread_exit(void *status)
{
- int sig;
- long l;
pthread_t pthread;
/* Check if this thread is already in the process of exiting: */
if ((_thread_run->flags & PTHREAD_EXITING) != 0) {
char msg[128];
- snprintf(msg,"Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",_thread_run);
+ snprintf(msg,sizeof msg,"Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",_thread_run);
PANIC(msg);
}
@@ -133,56 +134,29 @@ pthread_exit(void *status)
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
}
- /* Lock the thread list: */
- _lock_thread_list();
-
- /* Check if the running thread is at the head of the linked list: */
- if (_thread_link_list == _thread_run) {
- /* There is no previous thread: */
- _thread_link_list = _thread_run->nxt;
- } else {
- /* Point to the first thread in the list: */
- pthread = _thread_link_list;
-
- /*
- * Enter a loop to find the thread in the linked list before
- * the running thread:
- */
- while (pthread != NULL && pthread->nxt != _thread_run) {
- /* Point to the next thread: */
- pthread = pthread->nxt;
- }
-
- /* Check that a previous thread was found: */
- if (pthread != NULL) {
- /*
- * Point the previous thread to the one after the
- * running thread:
- */
- pthread->nxt = _thread_run->nxt;
- }
- }
-
- /* Unlock the thread list: */
- _unlock_thread_list();
-
- /* Lock the dead thread list: */
- _lock_dead_thread_list();
-
/*
- * This thread will never run again. Add it to the list of dead
- * threads:
+ * Lock the garbage collector mutex to ensure that the garbage
+ * collector is not using the dead thread list.
*/
- _thread_run->nxt = _thread_dead;
- _thread_dead = _thread_run;
+ if (pthread_mutex_lock(&_gc_mutex) != 0)
+ PANIC("Cannot lock gc mutex");
- /* Unlock the dead thread list: */
- _unlock_dead_thread_list();
+ /* Add this thread to the list of dead threads. */
+ _thread_run->nxt_dead = _thread_dead;
+ _thread_dead = _thread_run;
/*
- * The running thread is no longer in the thread link list so it will
- * now die:
+ * Signal the garbage collector thread that there is something
+ * to clean up.
*/
+ if (pthread_cond_signal(&_gc_cond) != 0)
+ PANIC("Cannot signal gc cond");
+
+ /* Unlock the garbage collector mutex: */
+ if (pthread_mutex_unlock(&_gc_mutex) != 0)
+ PANIC("Cannot lock gc mutex");
+
+ /* This this thread will never be re-scheduled. */
_thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__);
/* This point should not be reached. */
diff --git a/lib/libc_r/uthread/uthread_fcntl.c b/lib/libc_r/uthread/uthread_fcntl.c
index eecda60d8ae..6ea013afe5d 100644
--- a/lib/libc_r/uthread/uthread_fcntl.c
+++ b/lib/libc_r/uthread/uthread_fcntl.c
@@ -44,7 +44,6 @@ fcntl(int fd, int cmd,...)
int nonblock;
int oldfd;
int ret;
- int status;
va_list ap;
/* Lock the file descriptor: */
diff --git a/lib/libc_r/uthread/uthread_fd.c b/lib/libc_r/uthread/uthread_fd.c
index 1e4495e6ef9..0c2b10a7d8a 100644
--- a/lib/libc_r/uthread/uthread_fd.c
+++ b/lib/libc_r/uthread/uthread_fd.c
@@ -29,8 +29,8 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uthread_fd.c,v 1.1 1998/08/27 09:01:01 d Exp $
- * $OpenBSD: uthread_fd.c,v 1.1 1998/08/27 09:01:01 d Exp $
+ * $Id: uthread_fd.c,v 1.2 1998/11/09 03:13:19 d Exp $
+ * $OpenBSD: uthread_fd.c,v 1.2 1998/11/09 03:13:19 d Exp $
*
*/
#include <errno.h>
@@ -56,6 +56,7 @@ _thread_fd_table_init(int fd)
{
int ret = 0;
struct fd_table_entry *entry;
+ int saved_errno;
/* Check if the file descriptor is out of range: */
if (fd < 0 || fd >= _thread_dtablesize) {
@@ -117,8 +118,10 @@ _thread_fd_table_init(int fd)
* not support non-blocking calls, or if the
* driver is naturally non-blocking.
*/
+ saved_errno = errno;
_thread_sys_fcntl(fd, F_SETFL,
entry->flags | O_NONBLOCK);
+ errno = saved_errno;
/* Lock the file descriptor table: */
_SPINLOCK(&fd_table_lock);
@@ -531,7 +534,7 @@ _thread_fd_unlock_debug(int fd, int lock_type, char *fname, int lineno)
int
_thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout,
- char *fname, int lineno)
+ const char *fname, int lineno)
{
int ret;
diff --git a/lib/libc_r/uthread/uthread_file.c b/lib/libc_r/uthread/uthread_file.c
index 71fecfea217..26f0835ec1c 100644
--- a/lib/libc_r/uthread/uthread_file.c
+++ b/lib/libc_r/uthread/uthread_file.c
@@ -29,8 +29,8 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uthread_file.c,v 1.1 1998/08/27 09:01:02 d Exp $
- * $OpenBSD: uthread_file.c,v 1.1 1998/08/27 09:01:02 d Exp $
+ * $Id: uthread_file.c,v 1.2 1998/11/09 03:13:19 d Exp $
+ * $OpenBSD: uthread_file.c,v 1.2 1998/11/09 03:13:19 d Exp $
*
* POSIX stdio FILE locking functions. These assume that the locking
* is only required at FILE structure level, not at file descriptor
@@ -46,13 +46,6 @@
#include "pthread_private.h"
/*
- * Weak symbols for externally visible functions in this file:
- */
-#pragma weak flockfile=_flockfile
-#pragma weak ftrylockfile=_ftrylockfile
-#pragma weak funlockfile=_funlockfile
-
-/*
* The FILE lock structure. The FILE *fp is locked if the owner is
* not NULL. If not locked, the file lock structure can be
* reassigned to a different file by setting fp.
@@ -83,7 +76,7 @@ struct file_lock {
* structures. If there is a collision, a linear search of the
* dynamic list of locks linked to each static lock is perfomed.
*/
-#define file_idx(_p) ((((long) _p) >> sizeof(void *)) % NUM_HEADS)
+#define file_idx(_p) ((((u_long) _p) >> sizeof(void *)) % NUM_HEADS)
/*
* Global array of file locks. The first lock for each hash bucket is
@@ -123,7 +116,7 @@ find_lock(int idx, FILE *fp)
* Loop through the dynamic locks looking for the
* target file:
*/
- while (p != NULL && p->fp != fp && p->owner != NULL)
+ while (p != NULL && (p->fp != fp || p->owner == NULL))
/* Not this file, try the next: */
p = p->entry.le_next;
}
@@ -180,10 +173,8 @@ do_lock(int idx, FILE *fp)
}
void
-_flockfile_debug(FILE * fp, char *fname, int lineno)
+_flockfile_debug(FILE * fp, const char *fname, int lineno)
{
- int fd, flags;
- int status;
int idx = file_idx(fp);
struct file_lock *p;
@@ -246,17 +237,16 @@ _flockfile_debug(FILE * fp, char *fname, int lineno)
}
void
-_flockfile(FILE * fp)
+flockfile(FILE * fp)
{
- _flockfile_debug(fp, __FILE__, __LINE__);
+ _flockfile_debug(fp, "?", 1);
return;
}
int
-_ftrylockfile(FILE * fp)
+ftrylockfile(FILE * fp)
{
int ret = -1;
- int status;
int idx = file_idx(fp);
struct file_lock *p;
@@ -306,9 +296,8 @@ _ftrylockfile(FILE * fp)
}
void
-_funlockfile(FILE * fp)
+funlockfile(FILE * fp)
{
- int status;
int idx = file_idx(fp);
struct file_lock *p;
diff --git a/lib/libc_r/uthread/uthread_find_thread.c b/lib/libc_r/uthread/uthread_find_thread.c
index 99e302306d2..e4a59a0d83f 100644
--- a/lib/libc_r/uthread/uthread_find_thread.c
+++ b/lib/libc_r/uthread/uthread_find_thread.c
@@ -76,8 +76,12 @@ _find_dead_thread(pthread_t pthread)
/* Invalid thread: */
return(EINVAL);
- /* Lock the dead thread list: */
- _lock_dead_thread_list();
+ /*
+ * Lock the garbage collector mutex to ensure that the garbage
+ * collector is not using the dead thread list.
+ */
+ if (pthread_mutex_lock(&_gc_mutex) != 0)
+ PANIC("Cannot lock gc mutex");
/* Point to the first thread in the list: */
pthread1 = _thread_dead;
@@ -85,11 +89,12 @@ _find_dead_thread(pthread_t pthread)
/* Search for the thread to join to: */
while (pthread1 != NULL && pthread1 != pthread) {
/* Point to the next thread: */
- pthread1 = pthread1->nxt;
+ pthread1 = pthread1->nxt_dead;
}
- /* Unlock the dead thread list: */
- _unlock_dead_thread_list();
+ /* Unlock the garbage collector mutex: */
+ if (pthread_mutex_unlock(&_gc_mutex) != 0)
+ PANIC("Cannot lock gc mutex");
/* Return zero if the thread exists: */
return ((pthread1 != NULL) ? 0:ESRCH);
diff --git a/lib/libc_r/uthread/uthread_fork.c b/lib/libc_r/uthread/uthread_fork.c
index b62990a1a50..c9b5cee5f90 100644
--- a/lib/libc_r/uthread/uthread_fork.c
+++ b/lib/libc_r/uthread/uthread_fork.c
@@ -33,8 +33,8 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
-#include <stdlib.h>
#include <fcntl.h>
+#include <stdlib.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"
@@ -43,7 +43,6 @@ pid_t
fork(void)
{
int flags;
- int status;
pid_t ret;
pthread_t pthread;
pthread_t pthread_next;
@@ -105,15 +104,16 @@ fork(void)
pthread->nxt = NULL;
} else {
if (pthread->attr.stackaddr_attr ==
- NULL && pthread->stack != NULL) {
+ NULL && pthread->stack != NULL)
/*
* Free the stack of the
* dead thread:
*/
free(pthread->stack);
- }
+
if (pthread->specific_data != NULL)
free(pthread->specific_data);
+
free(pthread);
}
diff --git a/lib/libc_r/uthread/uthread_gc.c b/lib/libc_r/uthread/uthread_gc.c
new file mode 100644
index 00000000000..32d79b5dfac
--- /dev/null
+++ b/lib/libc_r/uthread/uthread_gc.c
@@ -0,0 +1,278 @@
+/*
+ * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by John Birrell.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: uthread_gc.c,v 1.1 1998/11/09 03:13:19 d Exp $
+ * $OpenBSD: uthread_gc.c,v 1.1 1998/11/09 03:13:19 d Exp $
+ *
+ * Garbage collector thread. Frees memory allocated for dead threads.
+ *
+ */
+#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include <pthread_np.h>
+#include "pthread_private.h"
+
+pthread_addr_t
+_thread_gc(pthread_addr_t arg)
+{
+ int f_debug;
+ int f_done = 0;
+ int ret;
+ pthread_t pthread;
+ pthread_t pthread_cln;
+ pthread_t pthread_nxt;
+ pthread_t pthread_prv;
+ struct timespec abstime;
+ void *p_stack;
+
+ /* Set a debug flag based on an environment variable. */
+ f_debug = (getenv("LIBC_R_DEBUG") != NULL);
+
+ /* Set the name of this thread. */
+ pthread_set_name_np(_thread_run,"GC");
+
+ while (!f_done) {
+ /* Check if debugging this application. */
+ if (f_debug)
+ /* Dump thread info to file. */
+ _thread_dump_info();
+
+ /* Lock the thread list: */
+ _lock_thread_list();
+
+ /* Check if this is the last running thread: */
+ if (_thread_link_list == _thread_run &&
+ _thread_link_list->nxt == NULL)
+ /*
+ * This is the last thread, so it can exit
+ * now.
+ */
+ f_done = 1;
+
+ /* Unlock the thread list: */
+ _unlock_thread_list();
+
+ /*
+ * Lock the garbage collector mutex which ensures that
+ * this thread sees another thread exit:
+ */
+ if (pthread_mutex_lock(&_gc_mutex) != 0)
+ PANIC("Cannot lock gc mutex");
+
+ /* No stack of thread structure to free yet: */
+ p_stack = NULL;
+ pthread_cln = NULL;
+
+ /* Point to the first dead thread (if there are any): */
+ pthread = _thread_dead;
+
+ /* There is no previous dead thread: */
+ pthread_prv = NULL;
+
+ /*
+ * Enter a loop to search for the first dead thread that
+ * has memory to free.
+ */
+ while (p_stack == NULL && pthread_cln == NULL &&
+ pthread != NULL) {
+ /* Save a pointer to the next thread: */
+ pthread_nxt = pthread->nxt_dead;
+
+ /* Check if the initial thread: */
+ if (pthread == _thread_initial)
+ /* Don't destroy the initial thread. */
+ pthread_prv = pthread;
+
+ /*
+ * Check if this thread has detached:
+ */
+ else if ((pthread->attr.flags &
+ PTHREAD_DETACHED) != 0) {
+ /*
+ * Check if there is no previous dead
+ * thread:
+ */
+ if (pthread_prv == NULL)
+ /*
+ * The dead thread is at the head
+ * of the list:
+ */
+ _thread_dead = pthread_nxt;
+ else
+ /*
+ * The dead thread is not at the
+ * head of the list:
+ */
+ pthread_prv->nxt_dead =
+ pthread->nxt_dead;
+
+ /*
+ * Check if the stack was not specified by
+ * the caller to pthread_create and has not
+ * been destroyed yet:
+ */
+ if (pthread->attr.stackaddr_attr == NULL &&
+ pthread->stack != NULL) {
+ /*
+ * Point to the stack that must
+ * be freed outside the locks:
+ */
+ p_stack = pthread->stack;
+ }
+
+ /*
+ * Point to the thread structure that must
+ * be freed outside the locks:
+ */
+ pthread_cln = pthread;
+ } else {
+ /*
+ * This thread has not detached, so do
+ * not destroy it:
+ */
+ pthread_prv = pthread;
+
+ /*
+ * Check if the stack was not specified by
+ * the caller to pthread_create and has not
+ * been destroyed yet:
+ */
+ if (pthread->attr.stackaddr_attr == NULL &&
+ pthread->stack != NULL) {
+ /*
+ * Point to the stack that must
+ * be freed outside the locks:
+ */
+ p_stack = pthread->stack;
+
+ /*
+ * NULL the stack pointer now
+ * that the memory has been freed:
+ */
+ pthread->stack = NULL;
+ }
+ }
+
+ /* Point to the next thread: */
+ pthread = pthread_nxt;
+ }
+
+ /*
+ * Check if this is not the last thread and there is no
+ * memory to free this time around.
+ */
+ if (!f_done && p_stack == NULL && pthread_cln == NULL) {
+ /* Get the current time. */
+ if (clock_gettime(CLOCK_REALTIME,&abstime) != 0)
+ PANIC("gc cannot get time");
+
+ /*
+ * Do a backup poll in 10 seconds if no threads
+ * die before then.
+ */
+ abstime.tv_sec += 10;
+
+ /*
+ * Wait for a signal from a dying thread or a
+ * timeout (for a backup poll).
+ */
+ if ((ret = pthread_cond_timedwait(&_gc_cond,
+ &_gc_mutex, &abstime)) != 0 && ret != ETIMEDOUT)
+ PANIC("gc cannot wait for a signal");
+ }
+
+ /* Unlock the garbage collector mutex: */
+ if (pthread_mutex_unlock(&_gc_mutex) != 0)
+ PANIC("Cannot unlock gc mutex");
+
+ /*
+ * If there is memory to free, do it now. The call to
+ * free() might block, so this must be done outside the
+ * locks.
+ */
+ if (p_stack != NULL)
+ free(p_stack);
+ if (pthread_cln != NULL) {
+ /* Lock the thread list: */
+ _lock_thread_list();
+
+ /*
+ * Check if the thread is at the head of the
+ * linked list.
+ */
+ if (_thread_link_list == pthread_cln)
+ /* There is no previous thread: */
+ _thread_link_list = pthread_cln->nxt;
+ else {
+ /* Point to the first thread in the list: */
+ pthread = _thread_link_list;
+
+ /*
+ * Enter a loop to find the thread in the
+ * linked list before the thread that is
+ * about to be freed.
+ */
+ while (pthread != NULL &&
+ pthread->nxt != pthread_cln)
+ /* Point to the next thread: */
+ pthread = pthread->nxt;
+
+ /* Check that a previous thread was found: */
+ if (pthread != NULL) {
+ /*
+ * Point the previous thread to
+ * the one after the thread being
+ * freed:
+ */
+ pthread->nxt = pthread_cln->nxt;
+ }
+ }
+
+ /* Unlock the thread list: */
+ _unlock_thread_list();
+
+ /*
+ * Free the memory allocated for the thread
+ * structure.
+ */
+ free(pthread_cln);
+ }
+
+ }
+ return (NULL);
+}
+#endif /* _THREAD_SAFE */
diff --git a/lib/libc_r/uthread/uthread_info.c b/lib/libc_r/uthread/uthread_info.c
index bd829e8be55..9db574e7521 100644
--- a/lib/libc_r/uthread/uthread_info.c
+++ b/lib/libc_r/uthread/uthread_info.c
@@ -57,6 +57,7 @@ static const struct s_thread_info thread_info[] = {
{PS_SELECT_WAIT , "Waiting on select"},
{PS_SLEEP_WAIT , "Sleeping"},
{PS_WAIT_WAIT , "Waiting process"},
+ {PS_SIGSUSPEND , "Suspended, waiting for a signal"},
{PS_SIGWAIT , "Waiting for a signal"},
{PS_JOIN , "Waiting to join"},
{PS_SUSPENDED , "Suspended"},
@@ -67,7 +68,7 @@ static const struct s_thread_info thread_info[] = {
void
_thread_dump_info(void)
{
- char s[128];
+ char s[512];
int fd;
int i;
int j;
@@ -85,14 +86,20 @@ _thread_dump_info(void)
_thread_sys_write(fd, s, strlen(s));
/* Enter a loop to report each thread in the global list: */
- for (pthread = _thread_link_list; pthread != NULL; pthread = pthread->nxt) {
+ for (pthread = _thread_link_list; pthread != NULL;
+ pthread = pthread->nxt) {
/* Find the state: */
- for (j = 0; j < (sizeof(thread_info) / sizeof(struct s_thread_info)) - 1; j++)
+ for (j = 0; j < (sizeof(thread_info) /
+ sizeof(struct s_thread_info)) - 1; j++)
if (thread_info[j].state == pthread->state)
break;
/* Output a record for the current thread: */
- sprintf(s, "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
- pthread, (pthread->name == NULL) ? "":pthread->name, pthread->pthread_priority, thread_info[j].name,pthread->fname,pthread->lineno);
+ snprintf(s, sizeof(s),
+ "--------------------\nThread %p (%s) prio %3d state %s [%s:%d]\n",
+ pthread, (pthread->name == NULL) ?
+ "":pthread->name, pthread->pthread_priority,
+ thread_info[j].name,
+ pthread->fname,pthread->lineno);
_thread_sys_write(fd, s, strlen(s));
/* Check if this is the running thread: */
@@ -115,13 +122,19 @@ _thread_dump_info(void)
case PS_FDR_WAIT:
case PS_FDW_WAIT:
/* Write the lock details: */
- sprintf(s, "fd %d[%s:%d]", pthread->data.fd.fd, pthread->data.fd.fname, pthread->data.fd.branch);
+ snprintf(s, sizeof(s), "fd %d[%s:%d]",
+ pthread->data.fd.fd,
+ pthread->data.fd.fname,
+ pthread->data.fd.branch);
_thread_sys_write(fd, s, strlen(s));
- sprintf(s, "owner %pr/%pw\n", _thread_fd_table[pthread->data.fd.fd]->r_owner, _thread_fd_table[pthread->data.fd.fd]->w_owner);
+ snprintf(s, sizeof(s), "owner %pr/%pw\n",
+ _thread_fd_table[pthread->data.fd.fd]->r_owner,
+ _thread_fd_table[pthread->data.fd.fd]->w_owner);
_thread_sys_write(fd, s, strlen(s));
break;
case PS_SIGWAIT:
- sprintf(s, "sigmask 0x%08lx\n", (unsigned long)pthread->sigmask);
+ snprintf(s, sizeof(s), "sigmask 0x%08lx\n",
+ (unsigned long)pthread->sigmask);
_thread_sys_write(fd, s, strlen(s));
break;
@@ -149,9 +162,13 @@ _thread_dump_info(void)
* Enter a loop to report each thread in the global
* dead thread list:
*/
- for (pthread = _thread_dead; pthread != NULL; pthread = pthread->nxt) {
+ for (pthread = _thread_dead; pthread != NULL;
+ pthread = pthread->nxt_dead) {
/* Output a record for the current thread: */
- sprintf(s, "Thread %p prio %3d [%s:%d]\n", pthread, pthread->pthread_priority,pthread->fname,pthread->lineno);
+ snprintf(s, sizeof(s),
+ "Thread %p prio %3d [%s:%d]\n",
+ pthread, pthread->pthread_priority,
+ pthread->fname,pthread->lineno);
_thread_sys_write(fd, s, strlen(s));
}
}
@@ -168,7 +185,8 @@ _thread_dump_info(void)
*/
if (_thread_fd_table[i] != NULL) {
/* Report the file descriptor lock status: */
- sprintf(s, "fd[%3d] read owner %p count %d [%s:%d]\n write owner %p count %d [%s:%d]\n",
+ snprintf(s, sizeof(s),
+ "fd[%3d] read owner %p count %d [%s:%d]\n write owner %p count %d [%s:%d]\n",
i,
_thread_fd_table[i]->r_owner,
_thread_fd_table[i]->r_lockcount,
diff --git a/lib/libc_r/uthread/uthread_init.c b/lib/libc_r/uthread/uthread_init.c
index 40922f5b64b..95bc34e2797 100644
--- a/lib/libc_r/uthread/uthread_init.c
+++ b/lib/libc_r/uthread/uthread_init.c
@@ -35,10 +35,12 @@
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
+#include <paths.h>
#include <unistd.h>
#include <sys/time.h>
-#include <paths.h>
+#include <sys/ttycom.h>
#include <sys/param.h>
+#include <sys/ioctl.h>
#include <signal.h>
#ifdef _THREAD_SAFE
#include <machine/reg.h>
@@ -78,6 +80,8 @@ struct pthread_cond_attr pthread_condattr_default = {
int _pthread_stdio_flags[3];
struct fd_table_entry ** _thread_fd_table = NULL;
int _thread_dtablesize = NOFILE_MAX;
+pthread_mutex_t _gc_mutex = NULL;
+pthread_cond_t _gc_cond = NULL;
struct sigaction _thread_sigact[NSIG];
#ifdef GCC_2_8_MADE_THREAD_AWARE
@@ -112,6 +116,7 @@ static void ***dynamic_allocator_handler_fn()
void
_thread_init(void)
{
+ int fd;
int flags;
int i;
struct sigaction act;
@@ -121,13 +126,11 @@ _thread_init(void)
/* Only initialise the threaded application once. */
return;
-#ifdef __FreeBSD__
/*
* Check for the special case of this process running as
* or in place of init as pid = 1:
*/
if (getpid() == 1) {
- int fd;
/*
* Setup a new session for this process which is
* assumed to be running as root.
@@ -147,7 +150,6 @@ _thread_init(void)
_thread_sys_dup2(fd,2) == -1)
PANIC("Can't dup2");
}
-#endif __FreeBSD__
/* Get the standard I/O flags before messing with them : */
for (i = 0; i < 3; i++)
@@ -287,22 +289,29 @@ _thread_init(void)
__set_dynamic_handler_allocator( dynamic_allocator_handler_fn );
#endif /* GCC_2_8_MADE_THREAD_AWARE */
+ /* Initialise the garbage collector mutex and condition variable. */
+ if (pthread_mutex_init(&_gc_mutex,NULL) != 0 ||
+ pthread_cond_init(&_gc_cond,NULL) != 0)
+ PANIC("Failed to initialise garbage collector mutex or condvar");
+
return;
}
-#if 0
/*
- * Special start up code for NetBSD/Alpha
+ * Use the a.out .init symbol to start the thread package going
*/
-int
-main(int argc, char *argv[], char *env);
+extern void __init_threads __P((void)) asm(".init");
+void __init_threads() {
+ _thread_init();
+}
-int
-_thread_main(int argc, char *argv[], char *env)
-{
+/*
+ * Use elf's ld.so _init symbol to start the thread package going
+ */
+extern int _init __P((void));
+int _init() {
_thread_init();
- return (main(argc, argv, env));
+ return 0;
}
-#endif alpha_special_code
#endif _THREAD_SAFE
diff --git a/lib/libc_r/uthread/uthread_kern.c b/lib/libc_r/uthread/uthread_kern.c
index 9a0f7828f7e..f2c54f69a51 100644
--- a/lib/libc_r/uthread/uthread_kern.c
+++ b/lib/libc_r/uthread/uthread_kern.c
@@ -29,8 +29,8 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uthread_kern.c,v 1.1 1998/08/27 09:01:08 d Exp $
- * $OpenBSD: uthread_kern.c,v 1.1 1998/08/27 09:01:08 d Exp $
+ * $Id: uthread_kern.c,v 1.2 1998/11/09 03:13:20 d Exp $
+ * $OpenBSD: uthread_kern.c,v 1.2 1998/11/09 03:13:20 d Exp $
*
*/
#include <errno.h>
@@ -60,8 +60,6 @@ _thread_kern_sched(struct sigcontext * scp)
int prio = -1;
pthread_t pthread;
pthread_t pthread_h = NULL;
- pthread_t pthread_nxt = NULL;
- pthread_t pthread_prv = NULL;
pthread_t pthread_s = NULL;
struct itimerval itimer;
struct timespec ts;
@@ -91,7 +89,7 @@ _thread_kern_sched(struct sigcontext * scp)
_thread_run->sig_saved = 1;
}
/* Save the state of the current thread: */
- else if (setjmp(_thread_run->saved_jmp_buf) != 0) {
+ else if (_thread_machdep_setjmp(_thread_run->saved_jmp_buf) != 0) {
/*
* This point is reached when a longjmp() is called to
* restore the state of a thread.
@@ -106,89 +104,13 @@ _thread_kern_sched(struct sigcontext * scp)
*/
_dispatch_signals();
return;
- } else {
+ } else
/* Flag the jump buffer was the last state saved: */
_thread_run->sig_saved = 0;
- }
/* Save errno. */
_thread_run->error = errno;
- /* Point to the first dead thread (if there are any): */
- pthread = _thread_dead;
-
- /* There is no previous dead thread: */
- pthread_prv = NULL;
-
- /* Enter a loop to cleanup after dead threads: */
- while (pthread != NULL) {
- /* Save a pointer to the next thread: */
- pthread_nxt = pthread->nxt;
-
- /* Check if this thread is one which is running: */
- if (pthread == _thread_run || pthread == _thread_initial) {
- /*
- * Don't destroy the running thread or the initial
- * thread.
- */
- pthread_prv = pthread;
- }
- /*
- * Check if this thread has detached:
- */
- else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) {
- /* Check if there is no previous dead thread: */
- if (pthread_prv == NULL) {
- /*
- * The dead thread is at the head of the
- * list:
- */
- _thread_dead = pthread_nxt;
- } else {
- /*
- * The dead thread is not at the head of the
- * list:
- */
- pthread_prv->nxt = pthread->nxt;
- }
-
- /*
- * Check if the stack was not specified by the caller
- * to pthread_create and has not been destroyed yet:
- */
- if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) {
- /* Free the stack of the dead thread: */
- free(pthread->stack);
- }
- /* Free the memory allocated to the thread structure: */
- free(pthread);
- } else {
- /*
- * This thread has not detached, so do not destroy
- * it:
- */
- pthread_prv = pthread;
-
- /*
- * Check if the stack was not specified by the caller
- * to pthread_create and has not been destroyed yet:
- */
- if (pthread->attr.stackaddr_attr == NULL && pthread->stack != NULL) {
- /* Free the stack of the dead thread: */
- free(pthread->stack);
-
- /*
- * NULL the stack pointer now that the memory
- * has been freed:
- */
- pthread->stack = NULL;
- }
- }
-
- /* Point to the next thread: */
- pthread = pthread_nxt;
- }
-
/*
* Enter a the scheduling loop that finds the next thread that is
* ready to run. This loop completes when there are no more threads
@@ -280,10 +202,12 @@ _thread_kern_sched(struct sigcontext * scp)
* Accumulate the number of microseconds that this
* thread has run for:
*/
- _thread_run->slice_usec += (_thread_run->last_inactive.tv_sec -
- _thread_run->last_active.tv_sec) * 1000000 +
- _thread_run->last_inactive.tv_usec -
- _thread_run->last_active.tv_usec;
+ if (_thread_run->slice_usec != -1) {
+ _thread_run->slice_usec += (_thread_run->last_inactive.tv_sec -
+ _thread_run->last_active.tv_sec) * 1000000 +
+ _thread_run->last_inactive.tv_usec -
+ _thread_run->last_active.tv_usec;
+ }
/*
* Check if this thread has reached its allocated
@@ -316,7 +240,7 @@ _thread_kern_sched(struct sigcontext * scp)
* the last incremental priority check was
* made:
*/
- else if (timercmp(&_thread_run->last_inactive, &kern_inc_prio_time, <)) {
+ else if (timercmp(&pthread->last_inactive, &kern_inc_prio_time, <)) {
/*
* Increment the incremental priority
* for this thread in the hope that
@@ -657,7 +581,7 @@ _thread_kern_sched(struct sigcontext * scp)
* was context switched out (by a longjmp to
* a different thread):
*/
- longjmp(_thread_run->saved_jmp_buf, 1);
+ _thread_machdep_longjmp(_thread_run->saved_jmp_buf, 1);
/* This point should not be reached. */
PANIC("Thread has returned from sigreturn or longjmp");
@@ -669,7 +593,7 @@ _thread_kern_sched(struct sigcontext * scp)
}
void
-_thread_kern_sched_state(enum pthread_state state, char *fname, int lineno)
+_thread_kern_sched_state(enum pthread_state state, const char *fname, int lineno)
{
/* Change the state of the current thread: */
_thread_run->state = state;
@@ -750,6 +674,7 @@ _thread_kern_select(int wait_reqd)
case PS_STATE_MAX:
case PS_WAIT_WAIT:
case PS_SUSPENDED:
+ case PS_SIGSUSPEND:
/* Nothing to do here. */
break;
@@ -1080,6 +1005,7 @@ _thread_kern_select(int wait_reqd)
case PS_SIGTHREAD:
case PS_STATE_MAX:
case PS_SUSPENDED:
+ case PS_SIGSUSPEND:
/* Nothing to do here. */
break;
diff --git a/lib/libc_r/uthread/uthread_kill.c b/lib/libc_r/uthread/uthread_kill.c
index 98b3a2d39b9..292ba5caf8f 100644
--- a/lib/libc_r/uthread/uthread_kill.c
+++ b/lib/libc_r/uthread/uthread_kill.c
@@ -46,18 +46,48 @@ pthread_kill(pthread_t pthread, int sig)
/* Invalid signal: */
ret = EINVAL;
+ /* Ignored signals get dropped on the floor. */
+ else if (_thread_sigact[sig - 1].sa_handler == SIG_IGN)
+ ret = 0;
+
/* Find the thread in the list of active threads: */
else if ((ret = _find_thread(pthread)) == 0) {
- if ((pthread->state == PS_SIGWAIT) &&
- sigismember(&pthread->sigmask, sig)) {
- /* Change the state of the thread to run: */
- PTHREAD_NEW_STATE(pthread,PS_RUNNING);
+ switch (pthread->state) {
+ case PS_SIGSUSPEND:
+ /*
+ * Only wake up the thread if the signal is unblocked
+ * and there is a handler installed for the signal.
+ */
+ if (!sigismember(&pthread->sigmask, sig) &&
+ _thread_sigact[sig - 1].sa_handler != SIG_DFL) {
+ /* Change the state of the thread to run: */
+ PTHREAD_NEW_STATE(pthread,PS_RUNNING);
+
+ /* Return the signal number: */
+ pthread->signo = sig;
+ }
+ /* Increment the pending signal count: */
+ sigaddset(&pthread->sigpend,sig);
+ break;
+
+ case PS_SIGWAIT:
+ /* Wake up the thread if the signal is blocked. */
+ if (sigismember(pthread->data.sigwait, sig)) {
+ /* Change the state of the thread to run: */
+ PTHREAD_NEW_STATE(pthread,PS_RUNNING);
+
+ /* Return the signal number: */
+ pthread->signo = sig;
+ } else
+ /* Increment the pending signal count. */
+ sigaddset(&pthread->sigpend,sig);
+ break;
- /* Return the signal number: */
- pthread->signo = sig;
- } else
+ default:
/* Increment the pending signal count: */
sigaddset(&pthread->sigpend,sig);
+ break;
+ }
}
/* Return the completion status: */
diff --git a/lib/libc_r/uthread/uthread_rwlock.c b/lib/libc_r/uthread/uthread_rwlock.c
new file mode 100644
index 00000000000..bfdd0b61fc1
--- /dev/null
+++ b/lib/libc_r/uthread/uthread_rwlock.c
@@ -0,0 +1,336 @@
+/*-
+ * Copyright (c) 1998 Alex Nash
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $OpenBSD: uthread_rwlock.c,v 1.1 1998/11/09 03:13:20 d Exp $
+ * $Id: uthread_rwlock.c,v 1.1 1998/11/09 03:13:20 d Exp $
+ */
+
+#ifdef _THREAD_SAFE
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+#include "pthread_private.h"
+
+/* maximum number of times a read lock may be obtained */
+#define MAX_READ_LOCKS (INT_MAX - 1)
+
+static int init_static (pthread_rwlock_t *rwlock);
+
+static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER;
+
+static int
+init_static (pthread_rwlock_t *rwlock)
+{
+ int ret;
+
+ _SPINLOCK(&static_init_lock);
+
+ if (*rwlock == NULL)
+ ret = pthread_rwlock_init(rwlock, NULL);
+ else
+ ret = 0;
+
+ _SPINUNLOCK(&static_init_lock);
+
+ return(ret);
+}
+
+int
+pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
+{
+ int ret;
+
+ if (rwlock == NULL)
+ ret = EINVAL;
+ else {
+ pthread_rwlock_t prwlock;
+
+ prwlock = *rwlock;
+
+ pthread_mutex_destroy(&prwlock->lock);
+ pthread_cond_destroy(&prwlock->read_signal);
+ pthread_cond_destroy(&prwlock->write_signal);
+ free(prwlock);
+
+ *rwlock = NULL;
+
+ ret = 0;
+ }
+
+ return(ret);
+}
+
+int
+pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ /* allocate rwlock object */
+ prwlock = (pthread_rwlock_t)malloc(sizeof(struct pthread_rwlock));
+
+ if (prwlock == NULL)
+ return(ENOMEM);
+
+ /* initialize the lock */
+ if ((ret = pthread_mutex_init(&prwlock->lock, NULL)) != 0)
+ free(prwlock);
+ else {
+ /* initialize the read condition signal */
+ ret = pthread_cond_init(&prwlock->read_signal, NULL);
+
+ if (ret != 0) {
+ pthread_mutex_destroy(&prwlock->lock);
+ free(prwlock);
+ } else {
+ /* initialize the write condition signal */
+ ret = pthread_cond_init(&prwlock->write_signal, NULL);
+
+ if (ret != 0) {
+ pthread_cond_destroy(&prwlock->read_signal);
+ pthread_mutex_destroy(&prwlock->lock);
+ free(prwlock);
+ } else {
+ /* success */
+ prwlock->state = 0;
+ prwlock->blocked_writers = 0;
+
+ *rwlock = prwlock;
+ }
+ }
+ }
+
+ return(ret);
+}
+
+int
+pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ if (rwlock == NULL)
+ return(EINVAL);
+
+ prwlock = *rwlock;
+
+ /* check for static initialization */
+ if (prwlock == NULL) {
+ if ((ret = init_static(rwlock)) != 0)
+ return(ret);
+
+ prwlock = *rwlock;
+ }
+
+ /* grab the monitor lock */
+ if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
+ return(ret);
+
+ /* give writers priority over readers */
+ while (prwlock->blocked_writers || prwlock->state < 0) {
+ ret = pthread_cond_wait(&prwlock->read_signal, &prwlock->lock);
+
+ if (ret != 0) {
+ /* can't do a whole lot if this fails */
+ pthread_mutex_unlock(&prwlock->lock);
+ return(ret);
+ }
+ }
+
+ /* check lock count */
+ if (prwlock->state == MAX_READ_LOCKS)
+ ret = EAGAIN;
+ else
+ ++prwlock->state; /* indicate we are locked for reading */
+
+ /*
+ * Something is really wrong if this call fails. Returning
+ * error won't do because we've already obtained the read
+ * lock. Decrementing 'state' is no good because we probably
+ * don't have the monitor lock.
+ */
+ pthread_mutex_unlock(&prwlock->lock);
+
+ return(ret);
+}
+
+int
+pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ if (rwlock == NULL)
+ return(EINVAL);
+
+ prwlock = *rwlock;
+
+ /* check for static initialization */
+ if (prwlock == NULL) {
+ if ((ret = init_static(rwlock)) != 0)
+ return(ret);
+
+ prwlock = *rwlock;
+ }
+
+ /* grab the monitor lock */
+ if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
+ return(ret);
+
+ /* give writers priority over readers */
+ if (prwlock->blocked_writers || prwlock->state < 0)
+ ret = EWOULDBLOCK;
+ else if (prwlock->state == MAX_READ_LOCKS)
+ ret = EAGAIN; /* too many read locks acquired */
+ else
+ ++prwlock->state; /* indicate we are locked for reading */
+
+ /* see the comment on this in pthread_rwlock_rdlock */
+ pthread_mutex_unlock(&prwlock->lock);
+
+ return(ret);
+}
+
+int
+pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ if (rwlock == NULL)
+ return(EINVAL);
+
+ prwlock = *rwlock;
+
+ /* check for static initialization */
+ if (prwlock == NULL) {
+ if ((ret = init_static(rwlock)) != 0)
+ return(ret);
+
+ prwlock = *rwlock;
+ }
+
+ /* grab the monitor lock */
+ if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
+ return(ret);
+
+ if (prwlock->state != 0)
+ ret = EWOULDBLOCK;
+ else
+ /* indicate we are locked for writing */
+ prwlock->state = -1;
+
+ /* see the comment on this in pthread_rwlock_rdlock */
+ pthread_mutex_unlock(&prwlock->lock);
+
+ return(ret);
+}
+
+int
+pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ if (rwlock == NULL)
+ return(EINVAL);
+
+ prwlock = *rwlock;
+
+ if (prwlock == NULL)
+ return(EINVAL);
+
+ /* grab the monitor lock */
+ if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
+ return(ret);
+
+ if (prwlock->state > 0) {
+ if (--prwlock->state == 0 && prwlock->blocked_writers)
+ ret = pthread_cond_signal(&prwlock->write_signal);
+ } else if (prwlock->state < 0) {
+ prwlock->state = 0;
+
+ if (prwlock->blocked_writers)
+ ret = pthread_cond_signal(&prwlock->write_signal);
+ else
+ ret = pthread_cond_broadcast(&prwlock->read_signal);
+ } else
+ ret = EINVAL;
+
+ /* see the comment on this in pthread_rwlock_rdlock */
+ pthread_mutex_unlock(&prwlock->lock);
+
+ return(ret);
+}
+
+int
+pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
+{
+ pthread_rwlock_t prwlock;
+ int ret;
+
+ if (rwlock == NULL)
+ return(EINVAL);
+
+ prwlock = *rwlock;
+
+ /* check for static initialization */
+ if (prwlock == NULL) {
+ if ((ret = init_static(rwlock)) != 0)
+ return(ret);
+
+ prwlock = *rwlock;
+ }
+
+ /* grab the monitor lock */
+ if ((ret = pthread_mutex_lock(&prwlock->lock)) != 0)
+ return(ret);
+
+ while (prwlock->state != 0) {
+ ++prwlock->blocked_writers;
+
+ ret = pthread_cond_wait(&prwlock->write_signal, &prwlock->lock);
+
+ if (ret != 0) {
+ --prwlock->blocked_writers;
+ pthread_mutex_unlock(&prwlock->lock);
+ return(ret);
+ }
+
+ --prwlock->blocked_writers;
+ }
+
+ /* indicate we are locked for writing */
+ prwlock->state = -1;
+
+ /* see the comment on this in pthread_rwlock_rdlock */
+ pthread_mutex_unlock(&prwlock->lock);
+
+ return(ret);
+}
+
+#endif /* _THREAD_SAFE */
diff --git a/lib/libc_r/uthread/uthread_rwlockattr.c b/lib/libc_r/uthread/uthread_rwlockattr.c
new file mode 100644
index 00000000000..3c16b77b8f5
--- /dev/null
+++ b/lib/libc_r/uthread/uthread_rwlockattr.c
@@ -0,0 +1,99 @@
+/*-
+ * Copyright (c) 1998 Alex Nash
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $Id: uthread_rwlockattr.c,v 1.1 1998/11/09 03:13:20 d Exp $
+ * $OpenBSD: uthread_rwlockattr.c,v 1.1 1998/11/09 03:13:20 d Exp $
+ */
+
+#ifdef _THREAD_SAFE
+#include <errno.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+#include "pthread_private.h"
+
+int
+pthread_rwlockattr_destroy (pthread_rwlockattr_t *rwlockattr)
+{
+ pthread_rwlockattr_t prwlockattr;
+
+ if (rwlockattr == NULL)
+ return(EINVAL);
+
+ prwlockattr = *rwlockattr;
+
+ if (prwlockattr == NULL)
+ return(EINVAL);
+
+ free(prwlockattr);
+
+ return(0);
+}
+
+int
+pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *rwlockattr,
+ int *pshared)
+{
+ *pshared = (*rwlockattr)->pshared;
+
+ return(0);
+}
+
+int
+pthread_rwlockattr_init (pthread_rwlockattr_t *rwlockattr)
+{
+ pthread_rwlockattr_t prwlockattr;
+
+ if (rwlockattr == NULL)
+ return(EINVAL);
+
+ prwlockattr = (pthread_rwlockattr_t)
+ malloc(sizeof(struct pthread_rwlockattr));
+
+ if (prwlockattr == NULL)
+ return(ENOMEM);
+
+ prwlockattr->pshared = PTHREAD_PROCESS_PRIVATE;
+ *rwlockattr = prwlockattr;
+
+ return(0);
+}
+
+int
+pthread_rwlockattr_setpshared (pthread_rwlockattr_t *rwlockattr,
+ int *pshared)
+{
+ int ps = *pshared;
+
+ /* only PTHREAD_PROCESS_PRIVATE is supported */
+ if (ps != PTHREAD_PROCESS_PRIVATE)
+ return(EINVAL);
+
+ (*rwlockattr)->pshared = ps;
+
+ return(0);
+}
+
+#endif /* _THREAD_SAFE */
diff --git a/lib/libc_r/uthread/uthread_sig.c b/lib/libc_r/uthread/uthread_sig.c
index 7f13cd6dde1..3e55d6505a3 100644
--- a/lib/libc_r/uthread/uthread_sig.c
+++ b/lib/libc_r/uthread/uthread_sig.c
@@ -39,9 +39,7 @@
#include "pthread_private.h"
/* Static variables: */
-static int volatile yield_on_unlock_dead = 0;
static int volatile yield_on_unlock_thread = 0;
-static spinlock_t thread_dead_lock = _SPINLOCK_INITIALIZER;
static spinlock_t thread_link_list_lock = _SPINLOCK_INITIALIZER;
/* Lock the thread list: */
@@ -52,14 +50,6 @@ _lock_thread_list()
_SPINLOCK(&thread_link_list_lock);
}
-/* Lock the dead thread list: */
-void
-_lock_dead_thread_list()
-{
- /* Lock the dead thread list: */
- _SPINLOCK(&thread_dead_lock);
-}
-
/* Lock the thread list: */
void
_unlock_thread_list()
@@ -80,26 +70,6 @@ _unlock_thread_list()
}
}
-/* Lock the dead thread list: */
-void
-_unlock_dead_thread_list()
-{
- /* Unlock the dead thread list: */
- _SPINUNLOCK(&thread_dead_lock);
-
- /*
- * Check if a scheduler interrupt occurred while the dead
- * thread list was locked:
- */
- if (yield_on_unlock_dead) {
- /* Reset the interrupt flag: */
- yield_on_unlock_dead = 0;
-
- /* This thread has overstayed it's welcome: */
- sched_yield();
- }
-}
-
void
_thread_sig_handler(int sig, int code, struct sigcontext * scp)
{
@@ -144,18 +114,6 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
*/
yield_on_unlock_thread = 1;
- /* Check if the scheduler interrupt has come at an
- * unfortunate time which one of the threads is
- * modifying the dead thread list:
- */
- if (thread_dead_lock.access_lock)
- /*
- * Set a flag so that the thread that has
- * the lock yields when it unlocks the
- * dead thread list:
- */
- yield_on_unlock_dead = 1;
-
/*
* Check if the kernel has not been interrupted while
* executing scheduler code:
@@ -198,7 +156,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
/*
* POSIX says that pending SIGCONT signals are
- * discarded when one of there signals occurs.
+ * discarded when one of these signals occurs.
*/
if (sig == SIGTSTP || sig == SIGTTIN || sig == SIGTTOU) {
/*
@@ -221,7 +179,7 @@ _thread_sig_handler(int sig, int code, struct sigcontext * scp)
for (pthread = _thread_link_list; pthread != NULL;
pthread = pthread->nxt) {
if ((pthread->state == PS_SIGWAIT) &&
- sigismember(&pthread->sigmask, sig)) {
+ sigismember(pthread->data.sigwait, sig)) {
/* Change the state of the thread to run: */
PTHREAD_NEW_STATE(pthread,PS_RUNNING);
@@ -325,6 +283,21 @@ _thread_signal(pthread_t pthread, int sig)
pthread->signo = sig;
}
break;
+
+ case PS_SIGSUSPEND:
+ /*
+ * Only wake up the thread if the signal is unblocked
+ * and there is a handler installed for the signal.
+ */
+ if (!sigismember(&pthread->sigmask, sig) &&
+ _thread_sigact[sig - 1].sa_handler != SIG_DFL) {
+ /* Change the state of the thread to run: */
+ PTHREAD_NEW_STATE(pthread,PS_RUNNING);
+
+ /* Return the signal number: */
+ pthread->signo = sig;
+ }
+ break;
}
}
diff --git a/lib/libc_r/uthread/uthread_sigaction.c b/lib/libc_r/uthread/uthread_sigaction.c
index 3538f276eba..40f3850008e 100644
--- a/lib/libc_r/uthread/uthread_sigaction.c
+++ b/lib/libc_r/uthread/uthread_sigaction.c
@@ -75,7 +75,7 @@ sigaction(int sig, const struct sigaction * act, struct sigaction * oact)
sig != SIGINFO) {
/* Initialise the global signal action structure: */
gact.sa_mask = act->sa_mask;
- gact.sa_flags = act->sa_flags | SA_RESTART;
+ gact.sa_flags = 0;
/*
* Check if the signal handler is being set to
diff --git a/lib/libc_r/uthread/uthread_sigaltstack.c b/lib/libc_r/uthread/uthread_sigaltstack.c
new file mode 100644
index 00000000000..9a1f9614542
--- /dev/null
+++ b/lib/libc_r/uthread/uthread_sigaltstack.c
@@ -0,0 +1,21 @@
+/*
+ * $OpenBSD: uthread_sigaltstack.c,v 1.1 1998/11/09 03:13:20 d Exp $
+ */
+
+#include <signal.h>
+#include <errno.h>
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+
+/*
+ * placeholder for sigaltstack XXX impl to be done
+ */
+
+int
+sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss)
+{
+ errno = EINVAL;
+ return (-1);
+}
+#endif /* _THREAD_SAFE */
diff --git a/lib/libc_r/uthread/uthread_signal.c b/lib/libc_r/uthread/uthread_signal.c
index cef725f7360..c9ece329762 100644
--- a/lib/libc_r/uthread/uthread_signal.c
+++ b/lib/libc_r/uthread/uthread_signal.c
@@ -44,7 +44,7 @@ _thread_sys_signal(int s, sig_t a)
/* Initialise the signal action structure: */
sigemptyset(&sa.sa_mask);
sa.sa_handler = a;
- sa.sa_flags = SA_RESTART;
+ sa.sa_flags = 0;
/* Perform the sigaction syscall: */
if (_thread_sys_sigaction(s, &sa, &osa) < 0) {
diff --git a/lib/libc_r/uthread/uthread_sigsuspend.c b/lib/libc_r/uthread/uthread_sigsuspend.c
index f7a7b123e34..0ae9a4f7b3e 100644
--- a/lib/libc_r/uthread/uthread_sigsuspend.c
+++ b/lib/libc_r/uthread/uthread_sigsuspend.c
@@ -44,14 +44,14 @@ sigsuspend(const sigset_t * set)
/* Check if a new signal set was provided by the caller: */
if (set != NULL) {
- /* Save the current sigmal mask: */
+ /* Save the current signal mask: */
oset = _thread_run->sigmask;
- /* Combine the caller's mask with the current one: */
- _thread_run->sigmask |= *set;
+ /* Change the caller's mask: */
+ _thread_run->sigmask = *set;
/* Wait for a signal: */
- _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__);
+ _thread_kern_sched_state(PS_SIGSUSPEND, __FILE__, __LINE__);
/* Always return an interrupted error: */
errno = EINTR;
diff --git a/lib/libc_r/uthread/uthread_sigwait.c b/lib/libc_r/uthread/uthread_sigwait.c
index f441fa3b731..e8d6d4ae0ca 100644
--- a/lib/libc_r/uthread/uthread_sigwait.c
+++ b/lib/libc_r/uthread/uthread_sigwait.c
@@ -41,7 +41,7 @@ sigwait(const sigset_t * set, int *sig)
{
int ret = 0;
int i;
- sigset_t oset;
+ sigset_t tempset;
struct sigaction act;
/*
@@ -60,31 +60,48 @@ sigwait(const sigset_t * set, int *sig)
sigdelset(&act.sa_mask, SIGCHLD);
sigdelset(&act.sa_mask, SIGINFO);
+ /* Check to see if a pending signal is in the wait mask. */
+ if ((tempset = (_thread_run->sigpend & act.sa_mask))) {
+ /* Enter a loop to find a pending signal: */
+ for (i = 1; i < NSIG; i++) {
+ if (sigismember (&tempset, i))
+ break;
+ }
+
+ /* Clear the pending signal: */
+ sigdelset(&_thread_run->sigpend,i);
+
+ /* Return the signal number to the caller: */
+ *sig = i;
+
+ return (0);
+ }
+
/*
* Enter a loop to find the signals that are SIG_DFL. For
* these signals we must install a dummy signal handler in
* order for the kernel to pass them in to us. POSIX says
* that the application must explicitly install a dummy
* handler for signals that are SIG_IGN in order to sigwait
- * on them, so we ignore SIG_IGN signals.
+ * on them. Note that SIG_IGN signals are left in the
+ * mask because a subsequent sigaction could enable an
+ * ignored signal.
*/
for (i = 1; i < NSIG; i++) {
if (sigismember(&act.sa_mask, i)) {
- if (_thread_sigact[i - 1].sa_handler == SIG_DFL) {
+ if (_thread_sigact[i - 1].sa_handler == SIG_DFL)
if (_thread_sys_sigaction(i,&act,NULL) != 0)
ret = -1;
- }
- else if (_thread_sigact[i - 1].sa_handler == SIG_IGN)
- sigdelset(&act.sa_mask, i);
}
}
if (ret == 0) {
- /* Save the current signal mask: */
- oset = _thread_run->sigmask;
-
- /* Combine the caller's mask with the current one: */
- _thread_run->sigmask |= act.sa_mask;
+ /*
+ * Save the wait signal mask. The wait signal
+ * mask is independent of the threads signal mask
+ * and requires separate storage.
+ */
+ _thread_run->data.sigwait = &act.sa_mask;
/* Wait for a signal: */
_thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__);
@@ -92,8 +109,11 @@ sigwait(const sigset_t * set, int *sig)
/* Return the signal number to the caller: */
*sig = _thread_run->signo;
- /* Restore the signal mask: */
- _thread_run->sigmask = oset;
+ /*
+ * Probably unnecessary, but since it's in a union struct
+ * we don't know how it could be used in the future.
+ */
+ _thread_run->data.sigwait = NULL;
}
/* Restore the sigactions: */
diff --git a/lib/libc_r/uthread/uthread_spec.c b/lib/libc_r/uthread/uthread_spec.c
index 081a000788d..1fe789e6201 100644
--- a/lib/libc_r/uthread/uthread_spec.c
+++ b/lib/libc_r/uthread/uthread_spec.c
@@ -119,10 +119,12 @@ _thread_cleanupspecific(void)
destructor(data);
} else {
free(_thread_run->specific_data);
+ _thread_run->specific_data = NULL;
return;
}
}
}
+ _thread_run->specific_data = NULL;
free(_thread_run->specific_data);
}
diff --git a/lib/libc_r/uthread/uthread_spinlock.c b/lib/libc_r/uthread/uthread_spinlock.c
index 1863bdfef9a..559f2d4c4b4 100644
--- a/lib/libc_r/uthread/uthread_spinlock.c
+++ b/lib/libc_r/uthread/uthread_spinlock.c
@@ -29,16 +29,16 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uthread_spinlock.c,v 1.1 1998/08/27 09:01:25 d Exp $
- * $OpenBSD: uthread_spinlock.c,v 1.1 1998/08/27 09:01:25 d Exp $
+ * $Id: uthread_spinlock.c,v 1.2 1998/11/09 03:13:21 d Exp $
+ * $OpenBSD: uthread_spinlock.c,v 1.2 1998/11/09 03:13:21 d Exp $
*
*/
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
-#include <string.h>
#include <pthread.h>
+#include <string.h>
#include "pthread_private.h"
extern char *__progname;
@@ -80,7 +80,7 @@ _spinlock(spinlock_t *lck)
* returning.
*/
void
-_spinlock_debug(spinlock_t *lck, char *fname, int lineno)
+_spinlock_debug(spinlock_t *lck, const char *fname, int lineno)
{
/*
* Try to grab the lock and loop if another thread grabs
diff --git a/lib/libc_r/uthread/uthread_vfork.c b/lib/libc_r/uthread/uthread_vfork.c
new file mode 100644
index 00000000000..a3b0ee5bb06
--- /dev/null
+++ b/lib/libc_r/uthread/uthread_vfork.c
@@ -0,0 +1,12 @@
+/*
+ * $OpenBSD: uthread_vfork.c,v 1.1 1998/11/09 03:13:21 d Exp $
+ */
+#include <unistd.h>
+#ifdef _THREAD_SAFE
+
+int
+vfork(void)
+{
+ return (fork());
+}
+#endif /* _THREAD_SAFE */