From ee1de6a067c70fbd8cb73facd64c3b0b275f9f83 Mon Sep 17 00:00:00 2001 From: deischen Date: Wed, 24 Jan 2001 13:03:38 +0000 Subject: Add weak definitions for wrapped system calls. In general: _foo - wrapped system call foo - weak definition to _foo and for cancellation points: _foo - wrapped system call __foo - enter cancellation point, call _foo(), leave cancellation point foo - weak definition to __foo Change use of global _thread_run to call a function to get the currently running thread. Make all pthread_foo functions weak definitions to _pthread_foo, where _pthread_foo is the implementation. This allows an application to provide its own pthread functions. Provide slightly different versions of pthread_mutex_lock and pthread_mutex_init so that we can tell the difference between a libc mutex and an application mutex. Threads holding mutexes internal to libc should never be allowed to exit, call signal handlers, or cancel. Approved by: -arch --- lib/libc_r/Makefile | 17 - lib/libc_r/uthread/pthread_private.h | 221 ++++------- lib/libc_r/uthread/uthread_accept.c | 21 +- lib/libc_r/uthread/uthread_aio_suspend.c | 9 +- lib/libc_r/uthread/uthread_attr_destroy.c | 7 +- lib/libc_r/uthread/uthread_attr_getdetachstate.c | 6 +- lib/libc_r/uthread/uthread_attr_getinheritsched.c | 6 +- lib/libc_r/uthread/uthread_attr_getschedparam.c | 6 +- lib/libc_r/uthread/uthread_attr_getschedpolicy.c | 6 +- lib/libc_r/uthread/uthread_attr_getscope.c | 6 +- lib/libc_r/uthread/uthread_attr_getstackaddr.c | 6 +- lib/libc_r/uthread/uthread_attr_getstacksize.c | 6 +- lib/libc_r/uthread/uthread_attr_init.c | 7 +- .../uthread/uthread_attr_setcreatesuspend_np.c | 6 +- lib/libc_r/uthread/uthread_attr_setdetachstate.c | 6 +- lib/libc_r/uthread/uthread_attr_setinheritsched.c | 6 +- lib/libc_r/uthread/uthread_attr_setprio.c | 6 +- lib/libc_r/uthread/uthread_attr_setschedparam.c | 6 +- lib/libc_r/uthread/uthread_attr_setschedpolicy.c | 6 +- lib/libc_r/uthread/uthread_attr_setscope.c | 6 +- lib/libc_r/uthread/uthread_attr_setstackaddr.c | 6 +- lib/libc_r/uthread/uthread_attr_setstacksize.c | 6 +- lib/libc_r/uthread/uthread_bind.c | 8 +- lib/libc_r/uthread/uthread_cancel.c | 55 ++- lib/libc_r/uthread/uthread_clean.c | 19 +- lib/libc_r/uthread/uthread_close.c | 14 +- lib/libc_r/uthread/uthread_cond.c | 70 ++-- lib/libc_r/uthread/uthread_condattr_destroy.c | 7 +- lib/libc_r/uthread/uthread_condattr_init.c | 6 +- lib/libc_r/uthread/uthread_connect.c | 15 +- lib/libc_r/uthread/uthread_creat.c | 6 +- lib/libc_r/uthread/uthread_create.c | 19 +- lib/libc_r/uthread/uthread_detach.c | 6 +- lib/libc_r/uthread/uthread_dup.c | 10 +- lib/libc_r/uthread/uthread_dup2.c | 10 +- lib/libc_r/uthread/uthread_equal.c | 6 +- lib/libc_r/uthread/uthread_execve.c | 21 +- lib/libc_r/uthread/uthread_exit.c | 64 +-- lib/libc_r/uthread/uthread_fchflags.c | 7 +- lib/libc_r/uthread/uthread_fchmod.c | 8 +- lib/libc_r/uthread/uthread_fchown.c | 8 +- lib/libc_r/uthread/uthread_fcntl.c | 20 +- lib/libc_r/uthread/uthread_fd.c | 110 +++--- lib/libc_r/uthread/uthread_file.c | 34 +- lib/libc_r/uthread/uthread_find_thread.c | 2 - lib/libc_r/uthread/uthread_flock.c | 8 +- lib/libc_r/uthread/uthread_fork.c | 33 +- lib/libc_r/uthread/uthread_fpathconf.c | 7 +- lib/libc_r/uthread/uthread_fstat.c | 7 +- lib/libc_r/uthread/uthread_fstatfs.c | 8 +- lib/libc_r/uthread/uthread_fsync.c | 8 +- lib/libc_r/uthread/uthread_gc.c | 9 +- lib/libc_r/uthread/uthread_getdirentries.c | 8 +- lib/libc_r/uthread/uthread_getpeername.c | 7 +- lib/libc_r/uthread/uthread_getprio.c | 6 +- lib/libc_r/uthread/uthread_getschedparam.c | 6 +- lib/libc_r/uthread/uthread_getsockname.c | 8 +- lib/libc_r/uthread/uthread_getsockopt.c | 8 +- lib/libc_r/uthread/uthread_info.c | 44 +-- lib/libc_r/uthread/uthread_init.c | 210 +++++----- lib/libc_r/uthread/uthread_ioctl.c | 8 +- lib/libc_r/uthread/uthread_jmp.c | 24 +- lib/libc_r/uthread/uthread_join.c | 41 +- lib/libc_r/uthread/uthread_kern.c | 234 ++++++----- lib/libc_r/uthread/uthread_kevent.c | 25 +- lib/libc_r/uthread/uthread_kill.c | 6 +- lib/libc_r/uthread/uthread_listen.c | 7 +- lib/libc_r/uthread/uthread_mattr_init.c | 6 +- lib/libc_r/uthread/uthread_mattr_kind_np.c | 15 +- lib/libc_r/uthread/uthread_msync.c | 8 +- lib/libc_r/uthread/uthread_multi_np.c | 6 +- lib/libc_r/uthread/uthread_mutex.c | 439 +++++++++++++-------- lib/libc_r/uthread/uthread_mutex_prioceiling.c | 19 +- lib/libc_r/uthread/uthread_mutex_protocol.c | 9 +- lib/libc_r/uthread/uthread_mutexattr_destroy.c | 6 +- lib/libc_r/uthread/uthread_nanosleep.c | 21 +- lib/libc_r/uthread/uthread_once.c | 6 +- lib/libc_r/uthread/uthread_open.c | 10 +- lib/libc_r/uthread/uthread_pause.c | 6 +- lib/libc_r/uthread/uthread_pipe.c | 12 +- lib/libc_r/uthread/uthread_poll.c | 14 +- lib/libc_r/uthread/uthread_priority_queue.c | 4 +- lib/libc_r/uthread/uthread_read.c | 15 +- lib/libc_r/uthread/uthread_readv.c | 15 +- lib/libc_r/uthread/uthread_recvfrom.c | 15 +- lib/libc_r/uthread/uthread_recvmsg.c | 15 +- lib/libc_r/uthread/uthread_resume_np.c | 6 +- lib/libc_r/uthread/uthread_rwlock.c | 24 +- lib/libc_r/uthread/uthread_rwlockattr.c | 15 +- lib/libc_r/uthread/uthread_select.c | 27 +- lib/libc_r/uthread/uthread_self.c | 8 +- lib/libc_r/uthread/uthread_sem.c | 31 +- lib/libc_r/uthread/uthread_sendfile.c | 15 +- lib/libc_r/uthread/uthread_sendmsg.c | 15 +- lib/libc_r/uthread/uthread_sendto.c | 15 +- lib/libc_r/uthread/uthread_seterrno.c | 2 - lib/libc_r/uthread/uthread_setprio.c | 6 +- lib/libc_r/uthread/uthread_setschedparam.c | 6 +- lib/libc_r/uthread/uthread_setsockopt.c | 8 +- lib/libc_r/uthread/uthread_shutdown.c | 12 +- lib/libc_r/uthread/uthread_sig.c | 41 +- lib/libc_r/uthread/uthread_sigaction.c | 11 +- lib/libc_r/uthread/uthread_sigblock.c | 6 +- lib/libc_r/uthread/uthread_sigmask.c | 21 +- lib/libc_r/uthread/uthread_signal.c | 6 +- lib/libc_r/uthread/uthread_sigpending.c | 9 +- lib/libc_r/uthread/uthread_sigprocmask.c | 6 +- lib/libc_r/uthread/uthread_sigsetmask.c | 6 +- lib/libc_r/uthread/uthread_sigsuspend.c | 13 +- lib/libc_r/uthread/uthread_sigwait.c | 23 +- lib/libc_r/uthread/uthread_single_np.c | 10 +- lib/libc_r/uthread/uthread_sleep.c | 6 +- lib/libc_r/uthread/uthread_socket.c | 10 +- lib/libc_r/uthread/uthread_socketpair.c | 12 +- lib/libc_r/uthread/uthread_spec.c | 51 +-- lib/libc_r/uthread/uthread_spinlock.c | 15 +- lib/libc_r/uthread/uthread_suspend_np.c | 10 +- lib/libc_r/uthread/uthread_switch_np.c | 9 +- lib/libc_r/uthread/uthread_system.c | 6 +- lib/libc_r/uthread/uthread_tcdrain.c | 6 +- lib/libc_r/uthread/uthread_vfork.c | 9 +- lib/libc_r/uthread/uthread_wait.c | 6 +- lib/libc_r/uthread/uthread_wait4.c | 13 +- lib/libc_r/uthread/uthread_waitpid.c | 6 +- lib/libc_r/uthread/uthread_write.c | 15 +- lib/libc_r/uthread/uthread_writev.c | 15 +- lib/libc_r/uthread/uthread_yield.c | 17 +- lib/libkse/Makefile | 17 - lib/libkse/thread/thr_aio_suspend.c | 9 +- lib/libkse/thread/thr_attr_destroy.c | 7 +- lib/libkse/thread/thr_attr_getdetachstate.c | 6 +- lib/libkse/thread/thr_attr_getinheritsched.c | 6 +- lib/libkse/thread/thr_attr_getschedparam.c | 6 +- lib/libkse/thread/thr_attr_getschedpolicy.c | 6 +- lib/libkse/thread/thr_attr_getscope.c | 6 +- lib/libkse/thread/thr_attr_getstackaddr.c | 6 +- lib/libkse/thread/thr_attr_getstacksize.c | 6 +- lib/libkse/thread/thr_attr_init.c | 7 +- lib/libkse/thread/thr_attr_setcreatesuspend_np.c | 6 +- lib/libkse/thread/thr_attr_setdetachstate.c | 6 +- lib/libkse/thread/thr_attr_setinheritsched.c | 6 +- lib/libkse/thread/thr_attr_setschedparam.c | 6 +- lib/libkse/thread/thr_attr_setschedpolicy.c | 6 +- lib/libkse/thread/thr_attr_setscope.c | 6 +- lib/libkse/thread/thr_attr_setstackaddr.c | 6 +- lib/libkse/thread/thr_attr_setstacksize.c | 6 +- lib/libkse/thread/thr_cancel.c | 55 ++- lib/libkse/thread/thr_clean.c | 19 +- lib/libkse/thread/thr_close.c | 14 +- lib/libkse/thread/thr_cond.c | 70 ++-- lib/libkse/thread/thr_condattr_destroy.c | 7 +- lib/libkse/thread/thr_condattr_init.c | 6 +- lib/libkse/thread/thr_creat.c | 6 +- lib/libkse/thread/thr_create.c | 19 +- lib/libkse/thread/thr_detach.c | 6 +- lib/libkse/thread/thr_equal.c | 6 +- lib/libkse/thread/thr_exit.c | 64 +-- lib/libkse/thread/thr_fcntl.c | 20 +- lib/libkse/thread/thr_find_thread.c | 2 - lib/libkse/thread/thr_fork.c | 33 +- lib/libkse/thread/thr_fsync.c | 8 +- lib/libkse/thread/thr_getprio.c | 6 +- lib/libkse/thread/thr_getschedparam.c | 6 +- lib/libkse/thread/thr_info.c | 44 +-- lib/libkse/thread/thr_init.c | 210 +++++----- lib/libkse/thread/thr_join.c | 41 +- lib/libkse/thread/thr_kern.c | 234 ++++++----- lib/libkse/thread/thr_kill.c | 6 +- lib/libkse/thread/thr_mattr_init.c | 6 +- lib/libkse/thread/thr_mattr_kind_np.c | 15 +- lib/libkse/thread/thr_msync.c | 8 +- lib/libkse/thread/thr_multi_np.c | 6 +- lib/libkse/thread/thr_mutex.c | 439 +++++++++++++-------- lib/libkse/thread/thr_mutex_prioceiling.c | 19 +- lib/libkse/thread/thr_mutex_protocol.c | 9 +- lib/libkse/thread/thr_mutexattr_destroy.c | 6 +- lib/libkse/thread/thr_nanosleep.c | 21 +- lib/libkse/thread/thr_once.c | 6 +- lib/libkse/thread/thr_open.c | 10 +- lib/libkse/thread/thr_pause.c | 6 +- lib/libkse/thread/thr_poll.c | 14 +- lib/libkse/thread/thr_priority_queue.c | 4 +- lib/libkse/thread/thr_private.h | 221 ++++------- lib/libkse/thread/thr_read.c | 15 +- lib/libkse/thread/thr_readv.c | 15 +- lib/libkse/thread/thr_resume_np.c | 6 +- lib/libkse/thread/thr_rwlock.c | 24 +- lib/libkse/thread/thr_rwlockattr.c | 15 +- lib/libkse/thread/thr_select.c | 27 +- lib/libkse/thread/thr_self.c | 8 +- lib/libkse/thread/thr_sem.c | 31 +- lib/libkse/thread/thr_seterrno.c | 2 - lib/libkse/thread/thr_setprio.c | 6 +- lib/libkse/thread/thr_setschedparam.c | 6 +- lib/libkse/thread/thr_sig.c | 41 +- lib/libkse/thread/thr_sigaction.c | 11 +- lib/libkse/thread/thr_sigmask.c | 21 +- lib/libkse/thread/thr_sigpending.c | 9 +- lib/libkse/thread/thr_sigprocmask.c | 6 +- lib/libkse/thread/thr_sigsuspend.c | 13 +- lib/libkse/thread/thr_sigwait.c | 23 +- lib/libkse/thread/thr_single_np.c | 10 +- lib/libkse/thread/thr_sleep.c | 6 +- lib/libkse/thread/thr_spec.c | 51 +-- lib/libkse/thread/thr_spinlock.c | 15 +- lib/libkse/thread/thr_suspend_np.c | 10 +- lib/libkse/thread/thr_switch_np.c | 9 +- lib/libkse/thread/thr_system.c | 6 +- lib/libkse/thread/thr_tcdrain.c | 6 +- lib/libkse/thread/thr_vfork.c | 9 +- lib/libkse/thread/thr_wait.c | 6 +- lib/libkse/thread/thr_wait4.c | 13 +- lib/libkse/thread/thr_waitpid.c | 6 +- lib/libkse/thread/thr_write.c | 15 +- lib/libkse/thread/thr_writev.c | 15 +- lib/libkse/thread/thr_yield.c | 17 +- lib/libpthread/Makefile | 17 - lib/libpthread/thread/thr_aio_suspend.c | 9 +- lib/libpthread/thread/thr_attr_destroy.c | 7 +- lib/libpthread/thread/thr_attr_getdetachstate.c | 6 +- lib/libpthread/thread/thr_attr_getinheritsched.c | 6 +- lib/libpthread/thread/thr_attr_getschedparam.c | 6 +- lib/libpthread/thread/thr_attr_getschedpolicy.c | 6 +- lib/libpthread/thread/thr_attr_getscope.c | 6 +- lib/libpthread/thread/thr_attr_getstackaddr.c | 6 +- lib/libpthread/thread/thr_attr_getstacksize.c | 6 +- lib/libpthread/thread/thr_attr_init.c | 7 +- .../thread/thr_attr_setcreatesuspend_np.c | 6 +- lib/libpthread/thread/thr_attr_setdetachstate.c | 6 +- lib/libpthread/thread/thr_attr_setinheritsched.c | 6 +- lib/libpthread/thread/thr_attr_setschedparam.c | 6 +- lib/libpthread/thread/thr_attr_setschedpolicy.c | 6 +- lib/libpthread/thread/thr_attr_setscope.c | 6 +- lib/libpthread/thread/thr_attr_setstackaddr.c | 6 +- lib/libpthread/thread/thr_attr_setstacksize.c | 6 +- lib/libpthread/thread/thr_cancel.c | 55 ++- lib/libpthread/thread/thr_clean.c | 19 +- lib/libpthread/thread/thr_close.c | 14 +- lib/libpthread/thread/thr_cond.c | 70 ++-- lib/libpthread/thread/thr_condattr_destroy.c | 7 +- lib/libpthread/thread/thr_condattr_init.c | 6 +- lib/libpthread/thread/thr_creat.c | 6 +- lib/libpthread/thread/thr_create.c | 19 +- lib/libpthread/thread/thr_detach.c | 6 +- lib/libpthread/thread/thr_equal.c | 6 +- lib/libpthread/thread/thr_exit.c | 64 +-- lib/libpthread/thread/thr_fcntl.c | 20 +- lib/libpthread/thread/thr_find_thread.c | 2 - lib/libpthread/thread/thr_fork.c | 33 +- lib/libpthread/thread/thr_fsync.c | 8 +- lib/libpthread/thread/thr_gc.c | 9 +- lib/libpthread/thread/thr_getprio.c | 6 +- lib/libpthread/thread/thr_getschedparam.c | 6 +- lib/libpthread/thread/thr_info.c | 44 +-- lib/libpthread/thread/thr_init.c | 210 +++++----- lib/libpthread/thread/thr_join.c | 41 +- lib/libpthread/thread/thr_kern.c | 234 ++++++----- lib/libpthread/thread/thr_kill.c | 6 +- lib/libpthread/thread/thr_mattr_init.c | 6 +- lib/libpthread/thread/thr_mattr_kind_np.c | 15 +- lib/libpthread/thread/thr_msync.c | 8 +- lib/libpthread/thread/thr_multi_np.c | 6 +- lib/libpthread/thread/thr_mutex.c | 439 +++++++++++++-------- lib/libpthread/thread/thr_mutex_prioceiling.c | 19 +- lib/libpthread/thread/thr_mutex_protocol.c | 9 +- lib/libpthread/thread/thr_mutexattr_destroy.c | 6 +- lib/libpthread/thread/thr_nanosleep.c | 21 +- lib/libpthread/thread/thr_once.c | 6 +- lib/libpthread/thread/thr_open.c | 10 +- lib/libpthread/thread/thr_pause.c | 6 +- lib/libpthread/thread/thr_poll.c | 14 +- lib/libpthread/thread/thr_priority_queue.c | 4 +- lib/libpthread/thread/thr_private.h | 221 ++++------- lib/libpthread/thread/thr_read.c | 15 +- lib/libpthread/thread/thr_readv.c | 15 +- lib/libpthread/thread/thr_resume_np.c | 6 +- lib/libpthread/thread/thr_rwlock.c | 24 +- lib/libpthread/thread/thr_rwlockattr.c | 15 +- lib/libpthread/thread/thr_select.c | 27 +- lib/libpthread/thread/thr_self.c | 8 +- lib/libpthread/thread/thr_sem.c | 31 +- lib/libpthread/thread/thr_seterrno.c | 2 - lib/libpthread/thread/thr_setprio.c | 6 +- lib/libpthread/thread/thr_setschedparam.c | 6 +- lib/libpthread/thread/thr_sig.c | 41 +- lib/libpthread/thread/thr_sigaction.c | 11 +- lib/libpthread/thread/thr_sigmask.c | 21 +- lib/libpthread/thread/thr_sigpending.c | 9 +- lib/libpthread/thread/thr_sigprocmask.c | 6 +- lib/libpthread/thread/thr_sigsuspend.c | 13 +- lib/libpthread/thread/thr_sigwait.c | 23 +- lib/libpthread/thread/thr_single_np.c | 10 +- lib/libpthread/thread/thr_sleep.c | 6 +- lib/libpthread/thread/thr_spec.c | 51 +-- lib/libpthread/thread/thr_spinlock.c | 15 +- lib/libpthread/thread/thr_suspend_np.c | 10 +- lib/libpthread/thread/thr_switch_np.c | 9 +- lib/libpthread/thread/thr_system.c | 6 +- lib/libpthread/thread/thr_tcdrain.c | 6 +- lib/libpthread/thread/thr_vfork.c | 9 +- lib/libpthread/thread/thr_wait.c | 6 +- lib/libpthread/thread/thr_wait4.c | 13 +- lib/libpthread/thread/thr_waitpid.c | 6 +- lib/libpthread/thread/thr_write.c | 15 +- lib/libpthread/thread/thr_writev.c | 15 +- lib/libpthread/thread/thr_yield.c | 17 +- 306 files changed, 3895 insertions(+), 3478 deletions(-) (limited to 'lib') diff --git a/lib/libc_r/Makefile b/lib/libc_r/Makefile index 9b20a24..d7b21bc 100644 --- a/lib/libc_r/Makefile +++ b/lib/libc_r/Makefile @@ -22,23 +22,6 @@ CFLAGS+=-D_PTHREADS_INVARIANTS AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes -# -# This is a list of syscalls that are renamed as _thread_sys_{syscall} -# so that libc_r can provide replacement functions. -# -HIDDEN_SYSCALLS= aio_suspend.o accept.o bind.o close.o connect.o dup.o dup2.o \ - execve.o fchflags.o fchmod.o fchown.o fcntl.o \ - flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ - getlogin.o getpeername.o getsockname.o getsockopt.o ioctl.o \ - kevent.o listen.o \ - msync.o nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ - recvmsg.o sched_yield.o select.o sendfile.o sendmsg.o sendto.o \ - setsockopt.o shutdown.o sigaction.o sigaltstack.o \ - signanosleep.o sigpending.o sigprocmask.o sigreturn.o sigsetmask.o \ - sigsuspend.o socket.o \ - socketpair.o wait4.o write.o writev.o - -.include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" diff --git a/lib/libc_r/uthread/pthread_private.h b/lib/libc_r/uthread/pthread_private.h index 2c9a469..688a2b3 100644 --- a/lib/libc_r/uthread/pthread_private.h +++ b/lib/libc_r/uthread/pthread_private.h @@ -313,6 +313,9 @@ struct pthread_mutex_attr { long m_flags; }; +#define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \ + { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE } + /* * Condition variable definitions. */ @@ -1207,6 +1210,8 @@ int _find_dead_thread(pthread_t); int _find_thread(pthread_t); void _flockfile_backout(pthread_t); void _funlock_owned(pthread_t); +struct pthread *_get_curthread(void); +void _set_curthread(struct pthread *); void _join_backout(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 *); @@ -1224,6 +1229,19 @@ void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); +void *_pthread_getspecific(pthread_key_t); +int _pthread_key_create(pthread_key_t *, void (*) (void *)); +int _pthread_key_delete(pthread_key_t); +int _pthread_mutex_destroy(pthread_mutex_t *); +int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int _pthread_mutex_lock(pthread_mutex_t *); +int _pthread_mutex_trylock(pthread_mutex_t *); +int _pthread_mutex_unlock(pthread_mutex_t *); +int _pthread_mutexattr_init(pthread_mutexattr_t *); +int _pthread_mutexattr_destroy(pthread_mutexattr_t *); +int _pthread_mutexattr_settype(pthread_mutexattr_t *, int); +int _pthread_once(pthread_once_t *, void (*) (void)); +int _pthread_setspecific(pthread_key_t, const void *); void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); #if defined(_PTHREADS_INVARIANTS) @@ -1263,190 +1281,113 @@ void _thread_enter_cancellation_point(void); void _thread_leave_cancellation_point(void); void _thread_cancellation_point(void); +/* #include */ +#ifdef _AIO_H_ +int __sys_aio_suspend(const struct aiocb **, int, const struct timespec); +#endif + /* #include */ -int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); -int _thread_sys_sigpending(sigset_t *); -int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); -int _thread_sys_sigsuspend(const sigset_t *); -int _thread_sys_siginterrupt(int, int); -int _thread_sys_sigpause(int); -int _thread_sys_sigreturn(ucontext_t *); -int _thread_sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); -int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); -int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); -void _thread_sys_psignal(unsigned int, const char *); -void (*_thread_sys_signal(int, void (*)(int)))(int); +#ifdef _SIGNAL_H_ +int __sys_sigaction(int, const struct sigaction *, struct sigaction *); +int __sys_sigpending(sigset_t *); +int __sys_sigprocmask(int, const sigset_t *, sigset_t *); +int __sys_sigsuspend(const sigset_t *); +int __sys_sigreturn(ucontext_t *); +int __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); +#endif /* #include */ #ifdef _SYS_STAT_H_ -int _thread_sys_fchmod(int, mode_t); -int _thread_sys_fstat(int, struct stat *); -int _thread_sys_fchflags(int, u_long); +int __sys_fchmod(int, mode_t); +int __sys_fstat(int, struct stat *); +int __sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ -int _thread_sys_fstatfs(int, struct statfs *); +int __sys_fstatfs(int, struct statfs *); #endif -int _thread_sys_pipe(int *); -/* #include */ -#ifdef _SYS_SOCKET_H_ -int _thread_sys_accept(int, struct sockaddr *, int *); -int _thread_sys_bind(int, const struct sockaddr *, int); -int _thread_sys_connect(int, const struct sockaddr *, int); -int _thread_sys_getpeername(int, struct sockaddr *, int *); -int _thread_sys_getsockname(int, struct sockaddr *, int *); -int _thread_sys_getsockopt(int, int, int, void *, int *); -int _thread_sys_listen(int, int); -int _thread_sys_setsockopt(int, int, int, const void *, int); -int _thread_sys_shutdown(int, int); -int _thread_sys_socket(int, int, int); -int _thread_sys_socketpair(int, int, int, int *); -ssize_t _thread_sys_recv(int, void *, size_t, int); -ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); -ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); -ssize_t _thread_sys_send(int, const void *, size_t, int); -ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); -ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); +/* #inclde */ +#ifdef _SYS_EVENT_H_ +int __sys_kevent(int, const struct kevent *, int, struct kevent *, + int, const struct timespec *); #endif -/* #include */ -#ifdef _STDIO_H_ -FILE *_thread_sys_fdopen(int, const char *); -FILE *_thread_sys_fopen(const char *, const char *); -FILE *_thread_sys_freopen(const char *, const char *, FILE *); -FILE *_thread_sys_popen(const char *, const char *); -FILE *_thread_sys_tmpfile(void); -char *_thread_sys_ctermid(char *); -char *_thread_sys_cuserid(char *); -char *_thread_sys_fgetln(FILE *, size_t *); -char *_thread_sys_fgets(char *, int, FILE *); -char *_thread_sys_gets(char *); -char *_thread_sys_tempnam(const char *, const char *); -char *_thread_sys_tmpnam(char *); -int _thread_sys_fclose(FILE *); -int _thread_sys_feof(FILE *); -int _thread_sys_ferror(FILE *); -int _thread_sys_fflush(FILE *); -int _thread_sys_fgetc(FILE *); -int _thread_sys_fgetpos(FILE *, fpos_t *); -int _thread_sys_fileno(FILE *); -int _thread_sys_fprintf(FILE *, const char *, ...); -int _thread_sys_fpurge(FILE *); -int _thread_sys_fputc(int, FILE *); -int _thread_sys_fputs(const char *, FILE *); -int _thread_sys_fscanf(FILE *, const char *, ...); -int _thread_sys_fseek(FILE *, long, int); -int _thread_sys_fsetpos(FILE *, const fpos_t *); -int _thread_sys_getc(FILE *); -int _thread_sys_getchar(void); -int _thread_sys_getw(FILE *); -int _thread_sys_pclose(FILE *); -int _thread_sys_printf(const char *, ...); -int _thread_sys_putc(int, FILE *); -int _thread_sys_putchar(int); -int _thread_sys_puts(const char *); -int _thread_sys_putw(int, FILE *); -int _thread_sys_remove(const char *); -int _thread_sys_rename (const char *, const char *); -int _thread_sys_scanf(const char *, ...); -int _thread_sys_setlinebuf(FILE *); -int _thread_sys_setvbuf(FILE *, char *, int, size_t); -int _thread_sys_snprintf(char *, size_t, const char *, ...); -int _thread_sys_sprintf(char *, const char *, ...); -int _thread_sys_sscanf(const char *, const char *, ...); -int _thread_sys_ungetc(int, FILE *); -int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); -int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); -int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); -int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); -int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); -int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); -long _thread_sys_ftell(FILE *); -size_t _thread_sys_fread(void *, size_t, size_t, FILE *); -size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); -void _thread_sys_clearerr(FILE *); -void _thread_sys_perror(const char *); -void _thread_sys_rewind(FILE *); -void _thread_sys_setbuf(FILE *, char *); -void _thread_sys_setbuffer(FILE *, char *, int); +/* #include */ +#ifdef _SYS_SOCKET_H_ +int __sys_accept(int, struct sockaddr *, int *); +int __sys_bind(int, const struct sockaddr *, int); +int __sys_connect(int, const struct sockaddr *, int); +int __sys_getpeername(int, struct sockaddr *, int *); +int __sys_getsockname(int, struct sockaddr *, int *); +int __sys_getsockopt(int, int, int, void *, int *); +int __sys_listen(int, int); +int __sys_setsockopt(int, int, int, const void *, int); +int __sys_shutdown(int, int); +int __sys_socket(int, int, int); +int __sys_socketpair(int, int, int, int *); +ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); +ssize_t __sys_recvmsg(int, struct msghdr *, int); +ssize_t __sys_send(int, const void *, size_t, int); +int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int); +ssize_t __sys_sendmsg(int, const struct msghdr *, int); +ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _UNISTD_H_ -char *_thread_sys_ttyname(int); -int _thread_sys_close(int); -int _thread_sys_dup(int); -int _thread_sys_dup2(int, int); -int _thread_sys_exect(const char *, char * const *, char * const *); -int _thread_sys_execve(const char *, char * const *, char * const *); -int _thread_sys_fchdir(int); -int _thread_sys_fchown(int, uid_t, gid_t); -int _thread_sys_fsync(int); -int _thread_sys_ftruncate(int, off_t); -int _thread_sys_pause(void); -int _thread_sys_pipe(int *); -int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); -long _thread_sys_fpathconf(int, int); -off_t _thread_sys_lseek(int, off_t, int); -pid_t _thread_sys_fork(void); -pid_t _thread_sys_tcgetpgrp(int); -ssize_t _thread_sys_read(int, void *, size_t); -ssize_t _thread_sys_write(int, const void *, size_t); -void _thread_sys__exit(int); +int __sys_close(int); +int __sys_dup(int); +int __sys_dup2(int, int); +int __sys_execve(const char *, char * const *, char * const *); +int __sys_fchown(int, uid_t, gid_t); +int __sys_fork(void); +int __sys_fsync(int); +int __sys_pipe(int *); +int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); +long __sys_fpathconf(int, int); +ssize_t __sys_read(int, void *, size_t); +ssize_t __sys_write(int, const void *, size_t); +void __sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ -int _thread_sys_creat(const char *, mode_t); -int _thread_sys_fcntl(int, int, ...); -int _thread_sys_flock(int, int); -int _thread_sys_open(const char *, int, ...); +int __sys_fcntl(int, int, ...); +int __sys_flock(int, int); +int __sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ -int _thread_sys_ioctl(int, unsigned long, ...); +int __sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ -DIR *___thread_sys_opendir2(const char *, int); -DIR *_thread_sys_opendir(const char *); -int _thread_sys_alphasort(const void *, const void *); -int _thread_sys_scandir(const char *, struct dirent ***, - int (*)(struct dirent *), int (*)(const void *, const void *)); -int _thread_sys_closedir(DIR *); -int _thread_sys_getdirentries(int, char *, int, long *); -long _thread_sys_telldir(const DIR *); -struct dirent *_thread_sys_readdir(DIR *); -void _thread_sys_rewinddir(DIR *); -void _thread_sys_seekdir(DIR *, long); +int __sys_getdirentries(int, char *, int, long *); #endif /* #include */ #ifdef _SYS_UIO_H_ -ssize_t _thread_sys_readv(int, const struct iovec *, int); -ssize_t _thread_sys_writev(int, const struct iovec *, int); +ssize_t __sys_readv(int, const struct iovec *, int); +ssize_t __sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG -pid_t _thread_sys_wait(int *); -pid_t _thread_sys_waitpid(pid_t, int *, int); -pid_t _thread_sys_wait3(int *, int, struct rusage *); -pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); +pid_t __sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ -int _thread_sys_poll(struct pollfd *, unsigned, int); +int __sys_poll(struct pollfd *, unsigned, int); #endif /* #include */ #ifdef _SYS_MMAN_H_ -int _thread_sys_msync(void *, size_t, int); +int __sys_msync(void *, size_t, int); #endif /* #include */ diff --git a/lib/libc_r/uthread/uthread_accept.c b/lib/libc_r/uthread/uthread_accept.c index 12abd30..a7865fa 100644 --- a/lib/libc_r/uthread/uthread_accept.c +++ b/lib/libc_r/uthread/uthread_accept.c @@ -36,35 +36,37 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak accept=_accept + int _accept(int fd, struct sockaddr * name, socklen_t *namelen) { + struct pthread *curthread = _get_curthread(); int ret; /* Lock the file descriptor: */ if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { /* Enter a loop to wait for a connection request: */ - while ((ret = _thread_sys_accept(fd, name, namelen)) < 0) { + while ((ret = __sys_accept(fd, name, namelen)) < 0) { /* Check if the socket is to block: */ if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { /* Save the socket file descriptor: */ - _thread_run->data.fd.fd = fd; - _thread_run->data.fd.fname = __FILE__; - _thread_run->data.fd.branch = __LINE__; + curthread->data.fd.fd = fd; + curthread->data.fd.fname = __FILE__; + curthread->data.fd.branch = __LINE__; /* Set the timeout: */ _thread_kern_set_timeout(NULL); - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Schedule the next thread: */ _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; @@ -85,7 +87,7 @@ _accept(int fd, struct sockaddr * name, socklen_t *namelen) /* Initialise the file descriptor table for the new socket: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the socket: */ - _thread_sys_close(ret); + __sys_close(ret); /* Return an error: */ ret = -1; @@ -106,6 +108,3 @@ _accept(int fd, struct sockaddr * name, socklen_t *namelen) /* Return the socket file descriptor or -1 on error: */ return (ret); } - -__strong_reference(_accept, accept); -#endif diff --git a/lib/libc_r/uthread/uthread_aio_suspend.c b/lib/libc_r/uthread/uthread_aio_suspend.c index 1253f20..ea77f4e 100644 --- a/lib/libc_r/uthread/uthread_aio_suspend.c +++ b/lib/libc_r/uthread/uthread_aio_suspend.c @@ -30,10 +30,11 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak aio_suspend=_aio_suspend + int _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct timespec *timeout) @@ -41,11 +42,9 @@ _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct int ret; _thread_enter_cancellation_point(); - ret = _aio_suspend(iocbs, niocb, timeout); + ret = __sys_aio_suspend(iocbs, niocb, timeout); _thread_leave_cancellation_point(); - + return ret; } -__strong_reference(_aio_suspend, aio_suspend); -#endif diff --git a/lib/libc_r/uthread/uthread_attr_destroy.c b/lib/libc_r/uthread/uthread_attr_destroy.c index dfe668e..c025072 100644 --- a/lib/libc_r/uthread/uthread_attr_destroy.c +++ b/lib/libc_r/uthread/uthread_attr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_destroy(pthread_attr_t *attr) +#pragma weak pthread_attr_destroy=_pthread_attr_destroy + +int +_pthread_attr_destroy(pthread_attr_t *attr) { int ret; @@ -58,4 +60,3 @@ int pthread_attr_destroy(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getdetachstate.c b/lib/libc_r/uthread/uthread_attr_getdetachstate.c index d120553..581c952 100644 --- a/lib/libc_r/uthread/uthread_attr_getdetachstate.c +++ b/lib/libc_r/uthread/uthread_attr_getdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getdetachstate=_pthread_attr_getdetachstate + int -pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) +_pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) { int ret; @@ -56,4 +57,3 @@ pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getinheritsched.c b/lib/libc_r/uthread/uthread_attr_getinheritsched.c index 7e243ed..65e347b 100644 --- a/lib/libc_r/uthread/uthread_attr_getinheritsched.c +++ b/lib/libc_r/uthread/uthread_attr_getinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getinheritsched=_pthread_attr_getinheritsched + int -pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) +_pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getschedparam.c b/lib/libc_r/uthread/uthread_attr_getschedparam.c index 46586ff..454e1c2 100644 --- a/lib/libc_r/uthread/uthread_attr_getschedparam.c +++ b/lib/libc_r/uthread/uthread_attr_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedparam=_pthread_attr_getschedparam + int -pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) +_pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getschedpolicy.c b/lib/libc_r/uthread/uthread_attr_getschedpolicy.c index 19f835c..8328b3c 100644 --- a/lib/libc_r/uthread/uthread_attr_getschedpolicy.c +++ b/lib/libc_r/uthread/uthread_attr_getschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedpolicy=_pthread_attr_getschedpolicy + int -pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) +_pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getscope.c b/lib/libc_r/uthread/uthread_attr_getscope.c index 176f01b..ca0f4c9 100644 --- a/lib/libc_r/uthread/uthread_attr_getscope.c +++ b/lib/libc_r/uthread/uthread_attr_getscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getscope=_pthread_attr_getscope + int -pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) +_pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) { int ret = 0; @@ -51,4 +52,3 @@ pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getstackaddr.c b/lib/libc_r/uthread/uthread_attr_getstackaddr.c index 442dcc2..dd14176 100644 --- a/lib/libc_r/uthread/uthread_attr_getstackaddr.c +++ b/lib/libc_r/uthread/uthread_attr_getstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstackaddr=_pthread_attr_getstackaddr + int -pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) +_pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_getstacksize.c b/lib/libc_r/uthread/uthread_attr_getstacksize.c index d9aaca4..d66e473 100644 --- a/lib/libc_r/uthread/uthread_attr_getstacksize.c +++ b/lib/libc_r/uthread/uthread_attr_getstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstacksize=_pthread_attr_getstacksize + int -pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) +_pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_init.c b/lib/libc_r/uthread/uthread_attr_init.c index dbe3091..2e560c7 100644 --- a/lib/libc_r/uthread/uthread_attr_init.c +++ b/lib/libc_r/uthread/uthread_attr_init.c @@ -34,11 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_init(pthread_attr_t *attr) +#pragma weak pthread_attr_init=_pthread_attr_init + +int +_pthread_attr_init(pthread_attr_t *attr) { int ret; pthread_attr_t pattr; @@ -57,4 +59,3 @@ int pthread_attr_init(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setcreatesuspend_np.c b/lib/libc_r/uthread/uthread_attr_setcreatesuspend_np.c index d230397..5520368 100644 --- a/lib/libc_r/uthread/uthread_attr_setcreatesuspend_np.c +++ b/lib/libc_r/uthread/uthread_attr_setcreatesuspend_np.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setcreatesuspend_np=_pthread_attr_setcreatesuspend_np + int -pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) +_pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +50,3 @@ pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setdetachstate.c b/lib/libc_r/uthread/uthread_attr_setdetachstate.c index 6be3e4e..8c8a5fc 100644 --- a/lib/libc_r/uthread/uthread_attr_setdetachstate.c +++ b/lib/libc_r/uthread/uthread_attr_setdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setdetachstate=_pthread_attr_setdetachstate + int -pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) +_pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) { int ret; @@ -58,4 +59,3 @@ pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setinheritsched.c b/lib/libc_r/uthread/uthread_attr_setinheritsched.c index eb2384b..e35b4e9 100644 --- a/lib/libc_r/uthread/uthread_attr_setinheritsched.c +++ b/lib/libc_r/uthread/uthread_attr_setinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setinheritsched=_pthread_attr_setinheritsched + int -pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) +_pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setprio.c b/lib/libc_r/uthread/uthread_attr_setprio.c index 98ed029..5e57fc5 100644 --- a/lib/libc_r/uthread/uthread_attr_setprio.c +++ b/lib/libc_r/uthread/uthread_attr_setprio.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setprio=_pthread_attr_setprio + int -pthread_attr_setprio(pthread_attr_t *attr, int priority) +_pthread_attr_setprio(pthread_attr_t *attr, int priority) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +50,3 @@ pthread_attr_setprio(pthread_attr_t *attr, int priority) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setschedparam.c b/lib/libc_r/uthread/uthread_attr_setschedparam.c index 755bb13..a18494b 100644 --- a/lib/libc_r/uthread/uthread_attr_setschedparam.c +++ b/lib/libc_r/uthread/uthread_attr_setschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedparam=_pthread_attr_setschedparam + int -pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) +_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) { int ret = 0; @@ -54,4 +55,3 @@ pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setschedpolicy.c b/lib/libc_r/uthread/uthread_attr_setschedpolicy.c index 2788deb..29de2f4 100644 --- a/lib/libc_r/uthread/uthread_attr_setschedpolicy.c +++ b/lib/libc_r/uthread/uthread_attr_setschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedpolicy=_pthread_attr_setschedpolicy + int -pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) +_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) { int ret = 0; @@ -50,4 +51,3 @@ pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setscope.c b/lib/libc_r/uthread/uthread_attr_setscope.c index de2fe8e..81a09fa 100644 --- a/lib/libc_r/uthread/uthread_attr_setscope.c +++ b/lib/libc_r/uthread/uthread_attr_setscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setscope=_pthread_attr_setscope + int -pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) +_pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) { int ret = 0; @@ -53,4 +54,3 @@ pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setstackaddr.c b/lib/libc_r/uthread/uthread_attr_setstackaddr.c index 46469e4..8552ea4 100644 --- a/lib/libc_r/uthread/uthread_attr_setstackaddr.c +++ b/lib/libc_r/uthread/uthread_attr_setstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstackaddr=_pthread_attr_setstackaddr + int -pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) +_pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_attr_setstacksize.c b/lib/libc_r/uthread/uthread_attr_setstacksize.c index 3897811..4233cb5 100644 --- a/lib/libc_r/uthread/uthread_attr_setstacksize.c +++ b/lib/libc_r/uthread/uthread_attr_setstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstacksize=_pthread_attr_setstacksize + int -pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) +_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_bind.c b/lib/libc_r/uthread/uthread_bind.c index 0a455c0..61d1930 100644 --- a/lib/libc_r/uthread/uthread_bind.c +++ b/lib/libc_r/uthread/uthread_bind.c @@ -33,21 +33,19 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak bind=_bind + int _bind(int fd, const struct sockaddr * name, socklen_t namelen) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_bind(fd, name, namelen); + ret = __sys_bind(fd, name, namelen); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } - -__strong_reference(_bind, bind); -#endif diff --git a/lib/libc_r/uthread/uthread_cancel.c b/lib/libc_r/uthread/uthread_cancel.c index c6df52e..55f94fb 100644 --- a/lib/libc_r/uthread/uthread_cancel.c +++ b/lib/libc_r/uthread/uthread_cancel.c @@ -8,8 +8,13 @@ static void finish_cancellation(void *arg); +#pragma weak pthread_cancel=_pthread_cancel +#pragma weak pthread_setcancelstate=_pthread_setcancelstate +#pragma weak pthread_setcanceltype=_pthread_setcanceltype +#pragma weak pthread_testcancel=_pthread_testcancel + int -pthread_cancel(pthread_t pthread) +_pthread_cancel(pthread_t pthread) { int ret; @@ -111,26 +116,27 @@ pthread_cancel(pthread_t pthread) } int -pthread_setcancelstate(int state, int *oldstate) +_pthread_setcancelstate(int state, int *oldstate) { + struct pthread *curthread = _get_curthread(); int ostate; int ret; - ostate = _thread_run->cancelflags & PTHREAD_CANCEL_DISABLE; + ostate = curthread->cancelflags & PTHREAD_CANCEL_DISABLE; switch (state) { case PTHREAD_CANCEL_ENABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_DISABLE; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) + curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE; + if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DISABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags |= PTHREAD_CANCEL_DISABLE; + curthread->cancelflags |= PTHREAD_CANCEL_DISABLE; ret = 0; break; default: @@ -141,24 +147,25 @@ pthread_setcancelstate(int state, int *oldstate) } int -pthread_setcanceltype(int type, int *oldtype) +_pthread_setcanceltype(int type, int *oldtype) { + struct pthread *curthread = _get_curthread(); int otype; int ret; - otype = _thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; + otype = curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; switch (type) { case PTHREAD_CANCEL_ASYNCHRONOUS: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DEFERRED: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; ret = 0; break; default: @@ -169,16 +176,18 @@ pthread_setcanceltype(int type, int *oldtype) } void -pthread_testcancel(void) +_pthread_testcancel(void) { - if (((_thread_run->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCELLING) != 0)) { + struct pthread *curthread = _get_curthread(); + + if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && + ((curthread->cancelflags & PTHREAD_CANCELLING) != 0)) { /* * It is possible for this thread to be swapped out * while performing cancellation; do not allow it * to be cancelled again. */ - _thread_run->cancelflags &= ~PTHREAD_CANCELLING; + curthread->cancelflags &= ~PTHREAD_CANCELLING; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); PANIC("cancel"); @@ -188,15 +197,19 @@ pthread_testcancel(void) void _thread_enter_cancellation_point(void) { + struct pthread *curthread = _get_curthread(); + /* Look for a cancellation before we block: */ pthread_testcancel(); - _thread_run->cancelflags |= PTHREAD_AT_CANCEL_POINT; + curthread->cancelflags |= PTHREAD_AT_CANCEL_POINT; } void _thread_leave_cancellation_point(void) { - _thread_run->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; + struct pthread *curthread = _get_curthread(); + + curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; /* Look for a cancellation after we unblock: */ pthread_testcancel(); } @@ -204,11 +217,13 @@ _thread_leave_cancellation_point(void) static void finish_cancellation(void *arg) { - _thread_run->continuation = NULL; - _thread_run->interrupted = 0; + struct pthread *curthread = _get_curthread(); + + curthread->continuation = NULL; + curthread->interrupted = 0; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { - _thread_run->cancelflags &= ~PTHREAD_CANCEL_NEEDED; + if ((curthread->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { + curthread->cancelflags &= ~PTHREAD_CANCEL_NEEDED; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); } diff --git a/lib/libc_r/uthread/uthread_clean.c b/lib/libc_r/uthread/uthread_clean.c index bba5500..9ce3fc2 100644 --- a/lib/libc_r/uthread/uthread_clean.c +++ b/lib/libc_r/uthread/uthread_clean.c @@ -34,31 +34,35 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_cleanup_push=_pthread_cleanup_push +#pragma weak pthread_cleanup_pop=_pthread_cleanup_pop + void -pthread_cleanup_push(void (*routine) (void *), void *routine_arg) +_pthread_cleanup_push(void (*routine) (void *), void *routine_arg) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *new; if ((new = (struct pthread_cleanup *) malloc(sizeof(struct pthread_cleanup))) != NULL) { new->routine = routine; new->routine_arg = routine_arg; - new->next = _thread_run->cleanup; + new->next = curthread->cleanup; - _thread_run->cleanup = new; + curthread->cleanup = new; } } void -pthread_cleanup_pop(int execute) +_pthread_cleanup_pop(int execute) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *old; - if ((old = _thread_run->cleanup) != NULL) { - _thread_run->cleanup = old->next; + if ((old = curthread->cleanup) != NULL) { + curthread->cleanup = old->next; if (execute) { old->routine(old->routine_arg); } @@ -66,4 +70,3 @@ pthread_cleanup_pop(int execute) } } -#endif diff --git a/lib/libc_r/uthread/uthread_close.c b/lib/libc_r/uthread/uthread_close.c index 9dee2bc..43c3b22 100644 --- a/lib/libc_r/uthread/uthread_close.c +++ b/lib/libc_r/uthread/uthread_close.c @@ -36,10 +36,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak close=__close + int _close(int fd) { @@ -60,7 +61,7 @@ _close(int fd) * the file descriptor status: */ else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) && - ((ret = _thread_sys_fstat(fd, &sb)) == 0)) { + ((ret = __sys_fstat(fd, &sb)) == 0)) { /* * Check if the file should be left as blocking. * @@ -83,9 +84,9 @@ _close(int fd) */ if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) && (_thread_fd_table[fd]->flags & O_NONBLOCK) == 0) { /* Get the current flags: */ - flags = _thread_sys_fcntl(fd, F_GETFL, NULL); + flags = __sys_fcntl(fd, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); } /* XXX: Assumes well behaved threads. */ @@ -95,13 +96,13 @@ _close(int fd) free(entry); /* Close the file descriptor: */ - ret = _thread_sys_close(fd); + ret = __sys_close(fd); } return (ret); } int -close(int fd) +__close(int fd) { int ret; @@ -111,4 +112,3 @@ close(int fd) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_cond.c b/lib/libc_r/uthread/uthread_cond.c index 9b93c1a..6f2c509 100644 --- a/lib/libc_r/uthread/uthread_cond.c +++ b/lib/libc_r/uthread/uthread_cond.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -45,6 +44,14 @@ static inline pthread_t cond_queue_deq(pthread_cond_t); static inline void cond_queue_remove(pthread_cond_t, pthread_t); static inline void cond_queue_enq(pthread_cond_t, pthread_t); +#pragma weak pthread_cond_init=_pthread_cond_init +#pragma weak pthread_cond_destroy=_pthread_cond_destroy +#pragma weak pthread_cond_wait=_pthread_cond_wait +#pragma weak pthread_cond_timedwait=_pthread_cond_timedwait +#pragma weak pthread_cond_signal=_pthread_cond_signal +#pragma weak pthread_cond_broadcast=_pthread_cond_broadcast + + /* Reinitialize a condition variable to defaults. */ int _cond_reinit(pthread_cond_t *cond) @@ -70,7 +77,7 @@ _cond_reinit(pthread_cond_t *cond) } int -pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) +_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { enum pthread_cond_type type; pthread_cond_t pcond; @@ -130,7 +137,7 @@ pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) } int -pthread_cond_destroy(pthread_cond_t *cond) +_pthread_cond_destroy(pthread_cond_t *cond) { int rval = 0; @@ -157,8 +164,9 @@ pthread_cond_destroy(pthread_cond_t *cond) } int -pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -213,21 +221,21 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) rval = EINVAL; } else { /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; seqno = (*cond)->c_seqno; /* Wait forever: */ - _thread_run->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_sec = -1; /* Unlock the mutex: */ if ((unlock_mutex != 0) && @@ -237,7 +245,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == @@ -264,7 +272,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) done = (seqno != (*cond)->c_seqno); - if ((_thread_run->flags & + if ((curthread->flags & PTHREAD_FLAGS_IN_CONDQ) != 0) { /* * Lock the condition variable @@ -273,7 +281,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) _SPINLOCK(&(*cond)->lock); cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -286,7 +294,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * Save the interrupted flag; locking * the mutex will destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Note that even though this thread may have @@ -308,8 +316,8 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -319,9 +327,10 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) } int -pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, +_pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -375,20 +384,20 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); } else { /* Set the wakeup time: */ - _thread_run->wakeup_time.tv_sec = + curthread->wakeup_time.tv_sec = abstime->tv_sec; - _thread_run->wakeup_time.tv_nsec = + curthread->wakeup_time.tv_nsec = abstime->tv_nsec; /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; @@ -402,7 +411,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -432,8 +441,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * interrupted (canceled), or needs to * be resumed after handling a signal. */ - if ((_thread_run->timeout == 0) && - (_thread_run->interrupted == 0) && + if ((curthread->timeout == 0) && + (curthread->interrupted == 0) && (done != 0)) { /* Lock the mutex: */ rval = _mutex_cv_lock(mutex); @@ -447,7 +456,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * variable queue: */ cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -457,14 +466,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); /* Return a timeout error: */ - if (_thread_run->timeout != 0) + if (curthread->timeout != 0) rval = ETIMEDOUT; /* * Save the interrupted flag; * locking the mutex will * destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Lock the mutex and ignore any @@ -490,8 +499,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -501,7 +510,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, } int -pthread_cond_signal(pthread_cond_t * cond) +_pthread_cond_signal(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -569,7 +578,7 @@ pthread_cond_signal(pthread_cond_t * cond) } int -pthread_cond_broadcast(pthread_cond_t * cond) +_pthread_cond_broadcast(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -752,4 +761,3 @@ cond_queue_enq(pthread_cond_t cond, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_CONDQ; pthread->data.cond = cond; } -#endif diff --git a/lib/libc_r/uthread/uthread_condattr_destroy.c b/lib/libc_r/uthread/uthread_condattr_destroy.c index ad91228..578523a 100644 --- a/lib/libc_r/uthread/uthread_condattr_destroy.c +++ b/lib/libc_r/uthread/uthread_condattr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_condattr_destroy(pthread_condattr_t *attr) +#pragma weak pthread_condattr_destroy=_pthread_condattr_destroy + +int +_pthread_condattr_destroy(pthread_condattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +51,3 @@ int pthread_condattr_destroy(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_condattr_init.c b/lib/libc_r/uthread/uthread_condattr_init.c index 3379898..dc8059d 100644 --- a/lib/libc_r/uthread/uthread_condattr_init.c +++ b/lib/libc_r/uthread/uthread_condattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_condattr_init=_pthread_condattr_init + int -pthread_condattr_init(pthread_condattr_t *attr) +_pthread_condattr_init(pthread_condattr_t *attr) { int ret; pthread_condattr_t pattr; @@ -55,4 +56,3 @@ pthread_condattr_init(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_connect.c b/lib/libc_r/uthread/uthread_connect.c index b900618..ddb74ea 100644 --- a/lib/libc_r/uthread/uthread_connect.c +++ b/lib/libc_r/uthread/uthread_connect.c @@ -35,22 +35,24 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak connect=_connect + int _connect(int fd, const struct sockaddr * name, socklen_t namelen) { + struct pthread *curthread = _get_curthread(); struct sockaddr tmpname; int errnolen, ret, tmpnamelen; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - if ((ret = _thread_sys_connect(fd, name, namelen)) < 0) { + if ((ret = __sys_connect(fd, name, namelen)) < 0) { if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EINPROGRESS) || (errno == EALREADY) || (errno == EAGAIN))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); @@ -58,14 +60,14 @@ _connect(int fd, const struct sockaddr * name, socklen_t namelen) tmpnamelen = sizeof(tmpname); /* 0 now lets see if it really worked */ - if (((ret = _thread_sys_getpeername(fd, &tmpname, &tmpnamelen)) < 0) && (errno == ENOTCONN)) { + if (((ret = __sys_getpeername(fd, &tmpname, &tmpnamelen)) < 0) && (errno == ENOTCONN)) { /* * Get the error, this function * should not fail */ errnolen = sizeof(errno); - _thread_sys_getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, &errnolen); + __sys_getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, &errnolen); } } else { ret = -1; @@ -75,6 +77,3 @@ _connect(int fd, const struct sockaddr * name, socklen_t namelen) } return (ret); } - -__strong_reference(_connect, connect); -#endif diff --git a/lib/libc_r/uthread/uthread_creat.c b/lib/libc_r/uthread/uthread_creat.c index 1b51fec..56100dd 100644 --- a/lib/libc_r/uthread/uthread_creat.c +++ b/lib/libc_r/uthread/uthread_creat.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak creat=___creat + int -creat(const char *path, mode_t mode) +___creat(const char *path, mode_t mode) { int ret; @@ -45,4 +46,3 @@ creat(const char *path, mode_t mode) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_create.c b/lib/libc_r/uthread/uthread_create.c index 430869e..a0a51ec 100644 --- a/lib/libc_r/uthread/uthread_create.c +++ b/lib/libc_r/uthread/uthread_create.c @@ -40,7 +40,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -64,10 +63,13 @@ int _thread_CTX_JB_value = CTX_JB; int _thread_CTX_SJB_value = CTX_SJB; int _thread_CTX_UC_value = CTX_UC; +#pragma weak pthread_create=_pthread_create + int -pthread_create(pthread_t * thread, const pthread_attr_t * attr, +_pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { + struct pthread *curthread = _get_curthread(); struct itimerval itimer; int f_gc = 0; int ret = 0; @@ -180,7 +182,7 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ - new_thread->sigmask = _thread_run->sigmask; + new_thread->sigmask = curthread->sigmask; new_thread->sigmask_seqno = 0; /* Initialize the signal frame: */ @@ -214,13 +216,13 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.prio = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.sched_policy = - _thread_run->attr.sched_policy; + curthread->attr.sched_policy; } else { /* * Use just the thread priority, leaving the @@ -315,13 +317,14 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, void _thread_start(void) { + struct pthread *curthread = _get_curthread(); + /* 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)); + pthread_exit(curthread->start_routine(curthread->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } -#endif diff --git a/lib/libc_r/uthread/uthread_detach.c b/lib/libc_r/uthread/uthread_detach.c index 6dd762a..c387926 100644 --- a/lib/libc_r/uthread/uthread_detach.c +++ b/lib/libc_r/uthread/uthread_detach.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_detach=_pthread_detach + int -pthread_detach(pthread_t pthread) +_pthread_detach(pthread_t pthread) { int rval = 0; pthread_t next_thread; @@ -85,4 +86,3 @@ pthread_detach(pthread_t pthread) /* Return the completion status: */ return (rval); } -#endif diff --git a/lib/libc_r/uthread/uthread_dup.c b/lib/libc_r/uthread/uthread_dup.c index 30af80b..ed1699d 100644 --- a/lib/libc_r/uthread/uthread_dup.c +++ b/lib/libc_r/uthread/uthread_dup.c @@ -32,10 +32,11 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak dup=_dup + int _dup(int fd) { @@ -44,12 +45,12 @@ _dup(int fd) /* Lock the file descriptor: */ if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { /* Perform the 'dup' syscall: */ - if ((ret = _thread_sys_dup(fd)) < 0) { + if ((ret = __sys_dup(fd)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the file: */ - _thread_sys_close(ret); + __sys_close(ret); /* Reset the file descriptor: */ ret = -1; @@ -67,6 +68,3 @@ _dup(int fd) /* Return the completion status: */ return (ret); } - -__strong_reference(_dup, dup); -#endif diff --git a/lib/libc_r/uthread/uthread_dup2.c b/lib/libc_r/uthread/uthread_dup2.c index b1e8b5e..4057a6f 100644 --- a/lib/libc_r/uthread/uthread_dup2.c +++ b/lib/libc_r/uthread/uthread_dup2.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak dup2=_dup2 + int _dup2(int fd, int newfd) { @@ -56,12 +57,12 @@ _dup2(int fd, int newfd) 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) { + if ((ret = __sys_dup2(fd, newfd)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the file: */ - _thread_sys_close(ret); + __sys_close(ret); /* Reset the file descriptor: */ ret = -1; @@ -83,6 +84,3 @@ _dup2(int fd, int newfd) /* Return the completion status: */ return (ret); } - -__strong_reference(_dup2, dup2); -#endif diff --git a/lib/libc_r/uthread/uthread_equal.c b/lib/libc_r/uthread/uthread_equal.c index e03ba4a..334d023 100644 --- a/lib/libc_r/uthread/uthread_equal.c +++ b/lib/libc_r/uthread/uthread_equal.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_equal=_pthread_equal + int -pthread_equal(pthread_t t1, pthread_t t2) +_pthread_equal(pthread_t t1, pthread_t t2) { /* Compare the two thread pointers: */ return (t1 == t2); } -#endif diff --git a/lib/libc_r/uthread/uthread_execve.c b/lib/libc_r/uthread/uthread_execve.c index ed1906c..090b2b5 100644 --- a/lib/libc_r/uthread/uthread_execve.c +++ b/lib/libc_r/uthread/uthread_execve.c @@ -34,13 +34,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak execve=_execve + int _execve(const char *name, char *const * argv, char *const * envp) { + struct pthread *curthread = _get_curthread(); int flags; int i; int ret; @@ -56,8 +58,8 @@ _execve(const char *name, char *const * argv, char *const * envp) setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL); /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* * Enter a loop to set all file descriptors to blocking @@ -68,9 +70,9 @@ _execve(const char *name, char *const * argv, char *const * envp) if (_thread_fd_table[i] != NULL && !(_thread_fd_table[i]->flags & O_NONBLOCK)) { /* Get the current flags: */ - flags = _thread_sys_fcntl(i, F_GETFL, NULL); + flags = __sys_fcntl(i, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); } } @@ -94,19 +96,16 @@ _execve(const char *name, char *const * argv, char *const * envp) act.sa_flags = _thread_sigact[i - 1].sa_flags; /* Change the signal action for the process: */ - _thread_sys_sigaction(i, &act, &oact); + __sys_sigaction(i, &act, &oact); } } /* Set the signal mask: */ - _thread_sys_sigprocmask(SIG_SETMASK, &_thread_run->sigmask, NULL); + __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL); /* Execute the process: */ - ret = _thread_sys_execve(name, argv, envp); + ret = __sys_execve(name, argv, envp); /* Return the completion status: */ return (ret); } - -__strong_reference(_execve, execve); -#endif diff --git a/lib/libc_r/uthread/uthread_exit.c b/lib/libc_r/uthread/uthread_exit.c index aef12fe..42bf025 100644 --- a/lib/libc_r/uthread/uthread_exit.c +++ b/lib/libc_r/uthread/uthread_exit.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" #define FLAGS_IN_SCHEDQ \ (PTHREAD_FLAGS_IN_PRIOQ|PTHREAD_FLAGS_IN_WAITQ|PTHREAD_FLAGS_IN_WORKQ) +#pragma weak _exit=__exit +#pragma weak pthread_exit=_pthread_exit + void __exit(int status) { int flags; @@ -58,8 +60,8 @@ void __exit(int status) setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL); /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* * Enter a loop to set all file descriptors to blocking @@ -70,18 +72,16 @@ void __exit(int status) if (_thread_fd_table[i] != NULL && !(_thread_fd_table[i]->flags & O_NONBLOCK)) { /* Get the current flags: */ - flags = _thread_sys_fcntl(i, F_GETFL, NULL); + flags = __sys_fcntl(i, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); } } /* Call the _exit syscall: */ - _thread_sys__exit(status); + __sys__exit(status); } -__strong_reference(__exit, _exit); - void _thread_exit(char *fname, int lineno, char *string) { @@ -97,7 +97,7 @@ _thread_exit(char *fname, int lineno, char *string) strcat(s, ")\n"); /* Write the string to the standard error file descriptor: */ - _thread_sys_write(2, s, strlen(s)); + __sys_write(2, s, strlen(s)); /* Force this process to exit: */ /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */ @@ -116,6 +116,8 @@ _thread_exit(char *fname, int lineno, char *string) void _thread_exit_cleanup(void) { + struct pthread *curthread = _get_curthread(); + /* * POSIX states that cancellation/termination of a thread should * not release any visible resources (such as mutexes) and that @@ -124,13 +126,13 @@ _thread_exit_cleanup(void) * are not visible to the application and need to be released. */ /* Unlock all owned fd locks: */ - _thread_fd_unlock_owned(_thread_run); + _thread_fd_unlock_owned(curthread); /* Unlock all owned file locks: */ - _funlock_owned(_thread_run); + _funlock_owned(curthread); /* Unlock all private mutexes: */ - _mutex_unlock_private(_thread_run); + _mutex_unlock_private(curthread); /* * This still isn't quite correct because we don't account @@ -139,39 +141,40 @@ _thread_exit_cleanup(void) } void -pthread_exit(void *status) +_pthread_exit(void *status) { + struct pthread *curthread = _get_curthread(); pthread_t pthread; /* Check if this thread is already in the process of exiting: */ - if ((_thread_run->flags & PTHREAD_EXITING) != 0) { + if ((curthread->flags & PTHREAD_EXITING) != 0) { char msg[128]; - 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); + 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!",curthread); PANIC(msg); } /* Flag this thread as exiting: */ - _thread_run->flags |= PTHREAD_EXITING; + curthread->flags |= PTHREAD_EXITING; /* Save the return value: */ - _thread_run->ret = status; + curthread->ret = status; - while (_thread_run->cleanup != NULL) { + while (curthread->cleanup != NULL) { pthread_cleanup_pop(1); } - if (_thread_run->attr.cleanup_attr != NULL) { - _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr); + if (curthread->attr.cleanup_attr != NULL) { + curthread->attr.cleanup_attr(curthread->attr.arg_attr); } /* Check if there is thread specific data: */ - if (_thread_run->specific_data != NULL) { + if (curthread->specific_data != NULL) { /* Run the thread-specific data destructors: */ _thread_cleanupspecific(); } /* Free thread-specific poll_data structure, if allocated: */ - if (_thread_run->poll_data.fds != NULL) { - free(_thread_run->poll_data.fds); - _thread_run->poll_data.fds = NULL; + if (curthread->poll_data.fds != NULL) { + free(curthread->poll_data.fds); + curthread->poll_data.fds = NULL; } /* @@ -182,7 +185,7 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Add this thread to the list of dead threads. */ - TAILQ_INSERT_HEAD(&_dead_list, _thread_run, dle); + TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); /* * Signal the garbage collector thread that there is something @@ -203,9 +206,9 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Check if there are any threads joined to this one: */ - while ((pthread = TAILQ_FIRST(&(_thread_run->join_queue))) != NULL) { + while ((pthread = TAILQ_FIRST(&(curthread->join_queue))) != NULL) { /* Remove the thread from the queue: */ - TAILQ_REMOVE(&_thread_run->join_queue, pthread, sqe); + TAILQ_REMOVE(&curthread->join_queue, pthread, sqe); pthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; /* @@ -217,16 +220,16 @@ pthread_exit(void *status) /* * Set the return value for the woken thread: */ - if ((_thread_run->attr.flags & PTHREAD_DETACHED) != 0) + if ((curthread->attr.flags & PTHREAD_DETACHED) != 0) pthread->error = ESRCH; else { - pthread->ret = _thread_run->ret; + pthread->ret = curthread->ret; pthread->error = 0; } } /* Remove this thread from the thread list: */ - TAILQ_REMOVE(&_thread_list, _thread_run, tle); + TAILQ_REMOVE(&_thread_list, curthread, tle); /* This thread will never be re-scheduled. */ _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__); @@ -234,4 +237,3 @@ pthread_exit(void *status) /* This point should not be reached. */ PANIC("Dead thread has resumed"); } -#endif diff --git a/lib/libc_r/uthread/uthread_fchflags.c b/lib/libc_r/uthread/uthread_fchflags.c index 0cd37e3..f43c13d 100644 --- a/lib/libc_r/uthread/uthread_fchflags.c +++ b/lib/libc_r/uthread/uthread_fchflags.c @@ -7,21 +7,18 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fchflags=_fchflags int _fchflags(int fd, u_long flags) { int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - ret = _thread_sys_fchflags(fd, flags); + ret = __sys_fchflags(fd, flags); _FD_UNLOCK(fd, FD_WRITE); } return (ret); } - -__strong_reference(_fchflags, fchflags); -#endif diff --git a/lib/libc_r/uthread/uthread_fchmod.c b/lib/libc_r/uthread/uthread_fchmod.c index b9a5224..0558714 100644 --- a/lib/libc_r/uthread/uthread_fchmod.c +++ b/lib/libc_r/uthread/uthread_fchmod.c @@ -33,21 +33,19 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fchmod=_fchmod + int _fchmod(int fd, mode_t mode) { int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - ret = _thread_sys_fchmod(fd, mode); + ret = __sys_fchmod(fd, mode); _FD_UNLOCK(fd, FD_WRITE); } return (ret); } - -__strong_reference(_fchmod, fchmod); -#endif diff --git a/lib/libc_r/uthread/uthread_fchown.c b/lib/libc_r/uthread/uthread_fchown.c index 06bfbcb..39ee79d 100644 --- a/lib/libc_r/uthread/uthread_fchown.c +++ b/lib/libc_r/uthread/uthread_fchown.c @@ -34,21 +34,19 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fchown=_fchown + int _fchown(int fd, uid_t owner, gid_t group) { int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - ret = _thread_sys_fchown(fd, owner, group); + ret = __sys_fchown(fd, owner, group); _FD_UNLOCK(fd, FD_WRITE); } return (ret); } - -__strong_reference(_fchown, fchown); -#endif diff --git a/lib/libc_r/uthread/uthread_fcntl.c b/lib/libc_r/uthread/uthread_fcntl.c index f5dd064..84f4678 100644 --- a/lib/libc_r/uthread/uthread_fcntl.c +++ b/lib/libc_r/uthread/uthread_fcntl.c @@ -34,10 +34,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fcntl=__fcntl + int _fcntl(int fd, int cmd,...) { @@ -63,12 +64,12 @@ _fcntl(int fd, int cmd,...) oldfd = va_arg(ap, int); /* Initialise the file descriptor table entry: */ - if ((ret = _thread_sys_fcntl(fd, cmd, oldfd)) < 0) { + if ((ret = __sys_fcntl(fd, cmd, oldfd)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the file: */ - _thread_sys_close(ret); + __sys_close(ret); /* Reset the file descriptor: */ ret = -1; @@ -82,10 +83,10 @@ _fcntl(int fd, int cmd,...) break; case F_SETFD: flags = va_arg(ap, int); - ret = _thread_sys_fcntl(fd, cmd, flags); + ret = __sys_fcntl(fd, cmd, flags); break; case F_GETFD: - ret = _thread_sys_fcntl(fd, cmd, 0); + ret = __sys_fcntl(fd, cmd, 0); break; case F_GETFL: ret = _thread_fd_table[fd]->flags; @@ -104,10 +105,10 @@ _fcntl(int fd, int cmd,...) nonblock = flags & O_NONBLOCK; /* Set the file descriptor flags: */ - if ((ret = _thread_sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { + if ((ret = __sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { /* Get the flags so that we behave like the kernel: */ - } else if ((flags = _thread_sys_fcntl(fd, + } else if ((flags = __sys_fcntl(fd, F_GETFL, 0)) == -1) { /* Error getting flags: */ ret = -1; @@ -125,7 +126,7 @@ _fcntl(int fd, int cmd,...) break; default: /* Might want to make va_arg use a union */ - ret = _thread_sys_fcntl(fd, cmd, va_arg(ap, void *)); + ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); break; } @@ -140,7 +141,7 @@ _fcntl(int fd, int cmd,...) } int -fcntl(int fd, int cmd,...) +__fcntl(int fd, int cmd,...) { int ret; va_list ap; @@ -167,4 +168,3 @@ fcntl(int fd, int cmd,...) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_fd.c b/lib/libc_r/uthread/uthread_fd.c index f609ae0..b5b37c3 100644 --- a/lib/libc_r/uthread/uthread_fd.c +++ b/lib/libc_r/uthread/uthread_fd.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -115,7 +114,7 @@ _thread_fd_table_init(int fd) /* Get the flags for the file: */ if (((fd >= 3) || (_pthread_stdio_flags[fd] == -1)) && - (entry->flags = _thread_sys_fcntl(fd, F_GETFL, 0)) == -1) { + (entry->flags = __sys_fcntl(fd, F_GETFL, 0)) == -1) { ret = -1; } else { @@ -138,7 +137,7 @@ _thread_fd_table_init(int fd) * driver is naturally non-blocking. */ saved_errno = errno; - _thread_sys_fcntl(fd, F_SETFL, + __sys_fcntl(fd, F_SETFL, entry->flags | O_NONBLOCK); errno = saved_errno; @@ -181,6 +180,7 @@ _thread_fd_table_init(int fd) void _thread_fd_unlock(int fd, int lock_type) { + struct pthread *curthread = _get_curthread(); int ret; /* @@ -202,7 +202,7 @@ _thread_fd_unlock(int fd, int lock_type) _SPINLOCK(&_thread_fd_table[fd]->lock); /* Check if the running thread owns the read lock: */ - if (_thread_fd_table[fd]->r_owner == _thread_run) { + if (_thread_fd_table[fd]->r_owner == curthread) { /* Check the file descriptor and lock types: */ if (lock_type == FD_READ || lock_type == FD_RDWR) { /* @@ -244,7 +244,7 @@ _thread_fd_unlock(int fd, int lock_type) } } /* Check if the running thread owns the write lock: */ - if (_thread_fd_table[fd]->w_owner == _thread_run) { + if (_thread_fd_table[fd]->w_owner == curthread) { /* Check the file descriptor and lock types: */ if (lock_type == FD_WRITE || lock_type == FD_RDWR) { /* @@ -300,6 +300,7 @@ _thread_fd_unlock(int fd, int lock_type) int _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) { + struct pthread *curthread = _get_curthread(); int ret; /* @@ -308,7 +309,7 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) */ if ((ret = _thread_fd_table_init(fd)) == 0) { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Lock the file descriptor table entry to prevent @@ -323,8 +324,8 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) * Wait for the file descriptor to be locked * for read for the current thread: */ - while ((_thread_fd_table[fd]->r_owner != _thread_run) && - (_thread_run->interrupted == 0)) { + while ((_thread_fd_table[fd]->r_owner != curthread) && + (curthread->interrupted == 0)) { /* * Check if the file descriptor is locked by * another thread: @@ -336,14 +337,14 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) * queue of threads waiting for a * read lock on this file descriptor: */ - FDQ_INSERT(&_thread_fd_table[fd]->r_queue, _thread_run); + FDQ_INSERT(&_thread_fd_table[fd]->r_queue, curthread); /* * Save the file descriptor details * in the thread structure for the * running thread: */ - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(timeout); @@ -371,16 +372,16 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) */ _SPINLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { FDQ_REMOVE(&_thread_fd_table[fd]->r_queue, - _thread_run); + curthread); } } else { /* * The running thread now owns the * read lock on this file descriptor: */ - _thread_fd_table[fd]->r_owner = _thread_run; + _thread_fd_table[fd]->r_owner = curthread; /* * Reset the number of read locks for @@ -390,20 +391,20 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) } } - if (_thread_fd_table[fd]->r_owner == _thread_run) + if (_thread_fd_table[fd]->r_owner == curthread) /* Increment the read lock count: */ _thread_fd_table[fd]->r_lockcount++; } /* Check the file descriptor and lock types: */ - if (_thread_run->interrupted == 0 && + if (curthread->interrupted == 0 && (lock_type == FD_WRITE || lock_type == FD_RDWR)) { /* * Wait for the file descriptor to be locked * for write for the current thread: */ - while ((_thread_fd_table[fd]->w_owner != _thread_run) && - (_thread_run->interrupted == 0)) { + while ((_thread_fd_table[fd]->w_owner != curthread) && + (curthread->interrupted == 0)) { /* * Check if the file descriptor is locked by * another thread: @@ -416,14 +417,14 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) * write lock on this file * descriptor: */ - FDQ_INSERT(&_thread_fd_table[fd]->w_queue, _thread_run); + FDQ_INSERT(&_thread_fd_table[fd]->w_queue, curthread); /* * Save the file descriptor details * in the thread structure for the * running thread: */ - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(timeout); @@ -450,9 +451,9 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) */ _SPINLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { FDQ_REMOVE(&_thread_fd_table[fd]->w_queue, - _thread_run); + curthread); } } else { /* @@ -460,7 +461,7 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) * write lock on this file * descriptor: */ - _thread_fd_table[fd]->w_owner = _thread_run; + _thread_fd_table[fd]->w_owner = curthread; /* * Reset the number of write locks @@ -470,7 +471,7 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) } } - if (_thread_fd_table[fd]->w_owner == _thread_run) + if (_thread_fd_table[fd]->w_owner == curthread) /* Increment the write lock count: */ _thread_fd_table[fd]->w_lockcount++; } @@ -478,11 +479,11 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) /* Unlock the file descriptor table entry: */ _SPINUNLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { ret = -1; errno = EINTR; - if (_thread_run->continuation != NULL) - _thread_run->continuation((void *)_thread_run); + if (curthread->continuation != NULL) + curthread->continuation((void *)curthread); } } @@ -493,6 +494,7 @@ _thread_fd_lock(int fd, int lock_type, struct timespec * timeout) void _thread_fd_unlock_debug(int fd, int lock_type, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int ret; /* @@ -514,7 +516,7 @@ _thread_fd_unlock_debug(int fd, int lock_type, char *fname, int lineno) _spinlock_debug(&_thread_fd_table[fd]->lock, fname, lineno); /* Check if the running thread owns the read lock: */ - if (_thread_fd_table[fd]->r_owner == _thread_run) { + if (_thread_fd_table[fd]->r_owner == curthread) { /* Check the file descriptor and lock types: */ if (lock_type == FD_READ || lock_type == FD_RDWR) { /* @@ -556,7 +558,7 @@ _thread_fd_unlock_debug(int fd, int lock_type, char *fname, int lineno) } } /* Check if the running thread owns the write lock: */ - if (_thread_fd_table[fd]->w_owner == _thread_run) { + if (_thread_fd_table[fd]->w_owner == curthread) { /* Check the file descriptor and lock types: */ if (lock_type == FD_WRITE || lock_type == FD_RDWR) { /* @@ -613,6 +615,7 @@ int _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int ret; /* @@ -621,7 +624,7 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, */ if ((ret = _thread_fd_table_init(fd)) == 0) { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Lock the file descriptor table entry to prevent @@ -636,8 +639,8 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, * Wait for the file descriptor to be locked * for read for the current thread: */ - while ((_thread_fd_table[fd]->r_owner != _thread_run) && - (_thread_run->interrupted == 0)) { + while ((_thread_fd_table[fd]->r_owner != curthread) && + (curthread->interrupted == 0)) { /* * Check if the file descriptor is locked by * another thread: @@ -649,16 +652,16 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, * queue of threads waiting for a * read lock on this file descriptor: */ - FDQ_INSERT(&_thread_fd_table[fd]->r_queue, _thread_run); + FDQ_INSERT(&_thread_fd_table[fd]->r_queue, curthread); /* * Save the file descriptor details * in the thread structure for the * running thread: */ - _thread_run->data.fd.fd = fd; - _thread_run->data.fd.branch = lineno; - _thread_run->data.fd.fname = fname; + curthread->data.fd.fd = fd; + curthread->data.fd.branch = lineno; + curthread->data.fd.fname = fname; /* Set the timeout: */ _thread_kern_set_timeout(timeout); @@ -686,16 +689,16 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, */ _SPINLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { FDQ_REMOVE(&_thread_fd_table[fd]->r_queue, - _thread_run); + curthread); } } else { /* * The running thread now owns the * read lock on this file descriptor: */ - _thread_fd_table[fd]->r_owner = _thread_run; + _thread_fd_table[fd]->r_owner = curthread; /* * Reset the number of read locks for @@ -712,20 +715,20 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, } } - if (_thread_fd_table[fd]->r_owner == _thread_run) + if (_thread_fd_table[fd]->r_owner == curthread) /* Increment the read lock count: */ _thread_fd_table[fd]->r_lockcount++; } /* Check the file descriptor and lock types: */ - if (_thread_run->interrupted == 0 && + if (curthread->interrupted == 0 && (lock_type == FD_WRITE || lock_type == FD_RDWR)) { /* * Wait for the file descriptor to be locked * for write for the current thread: */ - while ((_thread_fd_table[fd]->w_owner != _thread_run) && - (_thread_run->interrupted == 0)) { + while ((_thread_fd_table[fd]->w_owner != curthread) && + (curthread->interrupted == 0)) { /* * Check if the file descriptor is locked by * another thread: @@ -738,16 +741,16 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, * write lock on this file * descriptor: */ - FDQ_INSERT(&_thread_fd_table[fd]->w_queue, _thread_run); + FDQ_INSERT(&_thread_fd_table[fd]->w_queue, curthread); /* * Save the file descriptor details * in the thread structure for the * running thread: */ - _thread_run->data.fd.fd = fd; - _thread_run->data.fd.branch = lineno; - _thread_run->data.fd.fname = fname; + curthread->data.fd.fd = fd; + curthread->data.fd.branch = lineno; + curthread->data.fd.fname = fname; /* Set the timeout: */ _thread_kern_set_timeout(timeout); @@ -774,9 +777,9 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, */ _SPINLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { FDQ_REMOVE(&_thread_fd_table[fd]->w_queue, - _thread_run); + curthread); } } else { /* @@ -784,7 +787,7 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, * write lock on this file * descriptor: */ - _thread_fd_table[fd]->w_owner = _thread_run; + _thread_fd_table[fd]->w_owner = curthread; /* * Reset the number of write locks @@ -801,7 +804,7 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, } } - if (_thread_fd_table[fd]->w_owner == _thread_run) + if (_thread_fd_table[fd]->w_owner == curthread) /* Increment the write lock count: */ _thread_fd_table[fd]->w_lockcount++; } @@ -809,11 +812,11 @@ _thread_fd_lock_debug(int fd, int lock_type, struct timespec * timeout, /* Unlock the file descriptor table entry: */ _SPINUNLOCK(&_thread_fd_table[fd]->lock); - if (_thread_run->interrupted != 0) { + if (curthread->interrupted != 0) { ret = -1; errno = EINTR; - if (_thread_run->continuation != NULL) - _thread_run->continuation((void *)_thread_run); + if (curthread->continuation != NULL) + curthread->continuation((void *)curthread); } } @@ -985,4 +988,3 @@ fd_next_writer(int fd) return (pthread); } -#endif diff --git a/lib/libc_r/uthread/uthread_file.c b/lib/libc_r/uthread/uthread_file.c index 04c6526..e5a8484 100644 --- a/lib/libc_r/uthread/uthread_file.c +++ b/lib/libc_r/uthread/uthread_file.c @@ -40,7 +40,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -137,6 +136,7 @@ static struct file_lock * do_lock(int idx, FILE *fp) { + struct pthread *curthread = _get_curthread(); struct file_lock *p; /* Check if the static structure is not being used: */ @@ -171,7 +171,7 @@ do_lock(int idx, FILE *fp) if (p != NULL) { /* Acquire the lock for the running thread: */ p->fp = fp; - p->owner = _thread_run; + p->owner = curthread; p->count = 1; TAILQ_INIT(&p->l_head); } @@ -181,6 +181,7 @@ do_lock(int idx, FILE *fp) void _flockfile_debug(FILE * fp, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int idx = file_idx(fp); struct file_lock *p; @@ -213,7 +214,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno) * The file is already locked, so check if the * running thread is the owner: */ - } else if (p->owner == _thread_run) { + } else if (p->owner == curthread) { /* * The running thread is already the * owner, so increment the count of @@ -226,7 +227,7 @@ _flockfile_debug(FILE * fp, char *fname, int lineno) _SPINUNLOCK(&hash_lock); } else { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Prevent being context switched out while @@ -239,27 +240,27 @@ _flockfile_debug(FILE * fp, char *fname, int lineno) * Append this thread to the queue of * threads waiting on the lock. */ - TAILQ_INSERT_TAIL(&p->l_head,_thread_run,qe); - _thread_run->flags |= PTHREAD_FLAGS_IN_FILEQ; + TAILQ_INSERT_TAIL(&p->l_head,curthread,qe); + curthread->flags |= PTHREAD_FLAGS_IN_FILEQ; /* Unlock the hash table: */ _SPINUNLOCK(&hash_lock); - _thread_run->data.fp = fp; + curthread->data.fp = fp; /* Wait on the FILE lock: */ _thread_kern_sched_state(PS_FILE_WAIT, fname, lineno); - if ((_thread_run->flags & PTHREAD_FLAGS_IN_FILEQ) != 0) { - TAILQ_REMOVE(&p->l_head,_thread_run,qe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_FILEQ; + if ((curthread->flags & PTHREAD_FLAGS_IN_FILEQ) != 0) { + TAILQ_REMOVE(&p->l_head,curthread,qe); + curthread->flags &= ~PTHREAD_FLAGS_IN_FILEQ; } _thread_kern_sig_undefer(); - if (_thread_run->interrupted != 0 && - _thread_run->continuation != NULL) - _thread_run->continuation((void *)_thread_run); + if (curthread->interrupted != 0 && + curthread->continuation != NULL) + curthread->continuation((void *)curthread); } } } @@ -273,6 +274,7 @@ _flockfile(FILE * fp) int _ftrylockfile(FILE * fp) { + struct pthread *curthread = _get_curthread(); int ret = -1; int idx = file_idx(fp); struct file_lock *p; @@ -294,7 +296,7 @@ _ftrylockfile(FILE * fp) * The file is already locked, so check if the * running thread is the owner: */ - } else if (p->owner == _thread_run) { + } else if (p->owner == curthread) { /* * The running thread is already the * owner, so increment the count of @@ -325,6 +327,7 @@ _ftrylockfile(FILE * fp) void _funlockfile(FILE * fp) { + struct pthread *curthread = _get_curthread(); int idx = file_idx(fp); struct file_lock *p; @@ -344,7 +347,7 @@ _funlockfile(FILE * fp) * the running thread is the one with the lock: */ if ((p = find_lock(idx, fp)) != NULL && - p->owner == _thread_run) { + p->owner == curthread) { /* * Check if this thread has locked the FILE * more than once: @@ -503,4 +506,3 @@ _flockfile_backout(pthread_t pthread) _thread_kern_sig_undefer(); } -#endif diff --git a/lib/libc_r/uthread/uthread_find_thread.c b/lib/libc_r/uthread/uthread_find_thread.c index d4a3bbd..861c4cb 100644 --- a/lib/libc_r/uthread/uthread_find_thread.c +++ b/lib/libc_r/uthread/uthread_find_thread.c @@ -32,7 +32,6 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -97,4 +96,3 @@ _find_dead_thread(pthread_t pthread) /* Return zero if the thread exists: */ return ((pthread1 != NULL) ? 0:ESRCH); } -#endif diff --git a/lib/libc_r/uthread/uthread_flock.c b/lib/libc_r/uthread/uthread_flock.c index 3a4df6c..2d68776 100644 --- a/lib/libc_r/uthread/uthread_flock.c +++ b/lib/libc_r/uthread/uthread_flock.c @@ -32,21 +32,19 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak flock=_flock + int _flock(int fd, int operation) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_flock(fd, operation); + ret = __sys_flock(fd, operation); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } - -__strong_reference(_flock, flock); -#endif diff --git a/lib/libc_r/uthread/uthread_fork.c b/lib/libc_r/uthread/uthread_fork.c index 97039b3..34d640c 100644 --- a/lib/libc_r/uthread/uthread_fork.c +++ b/lib/libc_r/uthread/uthread_fork.c @@ -36,13 +36,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fork=_fork + pid_t _fork(void) { + struct pthread *curthread = _get_curthread(); int i, flags; pid_t ret; pthread_t pthread; @@ -55,42 +57,42 @@ _fork(void) _thread_kern_sig_defer(); /* Fork a new process: */ - if ((ret = _thread_sys_fork()) != 0) { + if ((ret = __sys_fork()) != 0) { /* Parent process or error. Nothing to do here. */ } else { /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* Reset signals pending for the running thread: */ - sigemptyset(&_thread_run->sigpend); + sigemptyset(&curthread->sigpend); /* * Create a pipe that is written to by the signal handler to * prevent signals being missed in calls to - * _thread_sys_select: + * __sys_select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create pthread kernel pipe for forked process"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } @@ -125,7 +127,7 @@ _fork(void) pthread = TAILQ_NEXT(pthread, dle); /* Make sure this isn't the running thread: */ - if (pthread_save != _thread_run) { + if (pthread_save != curthread) { /* Remove this thread from the list: */ TAILQ_REMOVE(&_thread_list, pthread_save, tle); @@ -165,7 +167,7 @@ _fork(void) } /* Treat the current thread as the initial thread: */ - _thread_initial = _thread_run; + _thread_initial = curthread; /* Re-init the dead thread list: */ TAILQ_INIT(&_dead_list); @@ -175,7 +177,7 @@ _fork(void) TAILQ_INIT(&_workq); /* Re-init the threads mutex queue: */ - TAILQ_INIT(&_thread_run->mutexq); + TAILQ_INIT(&curthread->mutexq); /* No spinlocks yet: */ _spinblock_count = 0; @@ -217,6 +219,3 @@ _fork(void) /* Return the process ID: */ return (ret); } - -__strong_reference(_fork, fork); -#endif diff --git a/lib/libc_r/uthread/uthread_fpathconf.c b/lib/libc_r/uthread/uthread_fpathconf.c index 04fae27..8327b1c 100644 --- a/lib/libc_r/uthread/uthread_fpathconf.c +++ b/lib/libc_r/uthread/uthread_fpathconf.c @@ -26,21 +26,20 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fpathconf=_fpathconf + long _fpathconf(int fd, int name) { long ret; if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { - ret = _thread_sys_fpathconf(fd, name); + ret = __sys_fpathconf(fd, name); _FD_UNLOCK(fd, FD_READ); } return ret; } -__strong_reference(_fpathconf, fpathconf); -#endif diff --git a/lib/libc_r/uthread/uthread_fstat.c b/lib/libc_r/uthread/uthread_fstat.c index 7fc64e4..e0698e1 100644 --- a/lib/libc_r/uthread/uthread_fstat.c +++ b/lib/libc_r/uthread/uthread_fstat.c @@ -37,10 +37,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fstat=_fstat + int _fstat(int fd, struct stat * buf) { @@ -49,12 +50,10 @@ _fstat(int fd, struct stat * buf) /* Lock the file descriptor for read: */ if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { /* Get the file status: */ - ret = _thread_sys_fstat(fd, buf); + ret = __sys_fstat(fd, buf); /* Unlock the file descriptor: */ _FD_UNLOCK(fd, FD_READ); } return (ret); } -__strong_reference(_fstat, fstat); -#endif diff --git a/lib/libc_r/uthread/uthread_fstatfs.c b/lib/libc_r/uthread/uthread_fstatfs.c index eac447a..a6a1884 100644 --- a/lib/libc_r/uthread/uthread_fstatfs.c +++ b/lib/libc_r/uthread/uthread_fstatfs.c @@ -37,10 +37,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fstatfs=_fstatfs + int _fstatfs(int fd, struct statfs * buf) { @@ -49,12 +50,9 @@ _fstatfs(int fd, struct statfs * buf) /* Lock the file descriptor for read: */ if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { /* Get the file system status: */ - ret = _thread_sys_fstatfs(fd, buf); + ret = __sys_fstatfs(fd, buf); /* Unlock the file descriptor: */ _FD_UNLOCK(fd, FD_READ); } return (ret); } - -__strong_reference(_fstatfs, fstatfs); -#endif diff --git a/lib/libc_r/uthread/uthread_fsync.c b/lib/libc_r/uthread/uthread_fsync.c index 0d88320..b90d896 100644 --- a/lib/libc_r/uthread/uthread_fsync.c +++ b/lib/libc_r/uthread/uthread_fsync.c @@ -32,24 +32,25 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fsync=__fsync + int _fsync(int fd) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_fsync(fd); + ret = __sys_fsync(fd); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } int -fsync(int fd) +__fsync(int fd) { int ret; @@ -59,4 +60,3 @@ fsync(int fd) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_gc.c b/lib/libc_r/uthread/uthread_gc.c index a81ea91..cdc9681 100644 --- a/lib/libc_r/uthread/uthread_gc.c +++ b/lib/libc_r/uthread/uthread_gc.c @@ -47,6 +47,7 @@ pthread_addr_t _thread_gc(pthread_addr_t arg) { + struct pthread *curthread = _get_curthread(); int f_debug; int f_done = 0; int ret; @@ -61,13 +62,13 @@ _thread_gc(pthread_addr_t arg) pthread_sigmask(SIG_BLOCK, &mask, NULL); /* Mark this thread as a library thread (not a user thread). */ - _thread_run->flags |= PTHREAD_FLAGS_PRIVATE; + curthread->flags |= PTHREAD_FLAGS_PRIVATE; /* Set a debug flag based on an environment variable. */ f_debug = (getenv("LIBC_R_DEBUG") != NULL); /* Set the name of this thread. */ - pthread_set_name_np(_thread_run,"GC"); + pthread_set_name_np(curthread,"GC"); while (!f_done) { /* Check if debugging this application. */ @@ -82,8 +83,8 @@ _thread_gc(pthread_addr_t arg) _thread_kern_sig_defer(); /* Check if this is the last running thread: */ - if (TAILQ_FIRST(&_thread_list) == _thread_run && - TAILQ_NEXT(_thread_run, tle) == NULL) + if (TAILQ_FIRST(&_thread_list) == curthread && + TAILQ_NEXT(curthread, tle) == NULL) /* * This is the last thread, so it can exit * now. diff --git a/lib/libc_r/uthread/uthread_getdirentries.c b/lib/libc_r/uthread/uthread_getdirentries.c index 3cd5450..cda3f5d 100644 --- a/lib/libc_r/uthread/uthread_getdirentries.c +++ b/lib/libc_r/uthread/uthread_getdirentries.c @@ -33,21 +33,19 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak getdirentries=_getdirentries + int _getdirentries(int fd, char *buf, int nbytes, long *basep) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_getdirentries(fd, buf, nbytes, basep); + ret = __sys_getdirentries(fd, buf, nbytes, basep); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } - -__strong_reference(_getdirentries, getdirentries); -#endif diff --git a/lib/libc_r/uthread/uthread_getpeername.c b/lib/libc_r/uthread/uthread_getpeername.c index b258836..4a1d7a1 100644 --- a/lib/libc_r/uthread/uthread_getpeername.c +++ b/lib/libc_r/uthread/uthread_getpeername.c @@ -33,21 +33,20 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak getpeername=_getpeername + int _getpeername(int fd, struct sockaddr * peer, socklen_t *paddrlen) { int ret; if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { - ret = _thread_sys_getpeername(fd, peer, paddrlen); + ret = __sys_getpeername(fd, peer, paddrlen); _FD_UNLOCK(fd, FD_READ); } return ret; } -__strong_reference(_getpeername, getpeername); -#endif diff --git a/lib/libc_r/uthread/uthread_getprio.c b/lib/libc_r/uthread/uthread_getprio.c index 2f94b86..35eeb5b 100644 --- a/lib/libc_r/uthread/uthread_getprio.c +++ b/lib/libc_r/uthread/uthread_getprio.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getprio=_pthread_getprio + int -pthread_getprio(pthread_t pthread) +_pthread_getprio(pthread_t pthread) { int policy, ret; struct sched_param param; @@ -53,4 +54,3 @@ pthread_getprio(pthread_t pthread) /* Return the thread priority or an error status: */ return (ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_getschedparam.c b/lib/libc_r/uthread/uthread_getschedparam.c index 5dbd1f0..02f27c2 100644 --- a/lib/libc_r/uthread/uthread_getschedparam.c +++ b/lib/libc_r/uthread/uthread_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getschedparam=_pthread_getschedparam + int -pthread_getschedparam(pthread_t pthread, int *policy, +_pthread_getschedparam(pthread_t pthread, int *policy, struct sched_param *param) { int ret; @@ -56,4 +57,3 @@ pthread_getschedparam(pthread_t pthread, int *policy, return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_getsockname.c b/lib/libc_r/uthread/uthread_getsockname.c index f4d6420..b09dd2e 100644 --- a/lib/libc_r/uthread/uthread_getsockname.c +++ b/lib/libc_r/uthread/uthread_getsockname.c @@ -33,21 +33,19 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak getsockname=_getsockname + int _getsockname(int s, struct sockaddr * name, socklen_t *namelen) { int ret; if ((ret = _FD_LOCK(s, FD_READ, NULL)) == 0) { - ret = _thread_sys_getsockname(s, name, namelen); + ret = __sys_getsockname(s, name, namelen); _FD_UNLOCK(s, FD_READ); } return ret; } - -__strong_reference(_getsockname, getsockname); -#endif diff --git a/lib/libc_r/uthread/uthread_getsockopt.c b/lib/libc_r/uthread/uthread_getsockopt.c index 3222dbd..cc90ae1 100644 --- a/lib/libc_r/uthread/uthread_getsockopt.c +++ b/lib/libc_r/uthread/uthread_getsockopt.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak getsockopt=_getsockopt + int _getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen) @@ -44,11 +45,8 @@ _getsockopt(int fd, int level, int optname, void *optval, socklen_t int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_getsockopt(fd, level, optname, optval, optlen); + ret = __sys_getsockopt(fd, level, optname, optval, optlen); _FD_UNLOCK(fd, FD_RDWR); } return ret; } - -__strong_reference(_getsockopt, getsockopt); -#endif diff --git a/lib/libc_r/uthread/uthread_info.c b/lib/libc_r/uthread/uthread_info.c index 956ae7c..9c85e84 100644 --- a/lib/libc_r/uthread/uthread_info.c +++ b/lib/libc_r/uthread/uthread_info.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -47,6 +46,7 @@ static void dump_thread(int fd, pthread_t pthread, int long_version); +#pragma weak pthread_set_name_np=_pthread_set_name_np struct s_thread_info { enum pthread_state state; @@ -92,7 +92,7 @@ _thread_dump_info(void) snprintf(tmpfile, sizeof(tmpfile), "/tmp/uthread.dump.%u.%i", getpid(), i); /* Open the dump file for append and create it if necessary: */ - if ((fd = _thread_sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, + if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) { /* Can't open the dump file. */ if (errno == EEXIST) @@ -114,7 +114,7 @@ _thread_dump_info(void) } else { /* Output a header for active threads: */ strcpy(s, "\n\n=============\nACTIVE THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the global list: */ TAILQ_FOREACH(pthread, &_thread_list, tle) { @@ -123,7 +123,7 @@ _thread_dump_info(void) /* Output a header for ready threads: */ strcpy(s, "\n\n=============\nREADY THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the ready queue: */ TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) { @@ -134,7 +134,7 @@ _thread_dump_info(void) /* Output a header for waiting threads: */ strcpy(s, "\n\n=============\nWAITING THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_waitingq, pqe) { @@ -143,7 +143,7 @@ _thread_dump_info(void) /* Output a header for threads in the work queue: */ strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_workq, qe) { @@ -154,11 +154,11 @@ _thread_dump_info(void) if (TAILQ_FIRST(&_dead_list) == NULL) { /* Output a record: */ strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } else { /* Output a header for dead threads: */ strcpy(s, "\n\nDEAD THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* * Enter a loop to report each thread in the global @@ -172,7 +172,7 @@ _thread_dump_info(void) /* Output a header for file descriptors: */ snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR " "TABLE (table size %d)\n\n", _thread_dtablesize); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report file descriptor lock usage: */ for (i = 0; i < _thread_dtablesize; i++) { @@ -193,18 +193,19 @@ _thread_dump_info(void) _thread_fd_table[i]->w_lockcount, _thread_fd_table[i]->w_fname, _thread_fd_table[i]->w_lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } } /* Close the dump file: */ - _thread_sys_close(fd); + __sys_close(fd); } } static void dump_thread(int fd, pthread_t pthread, int long_version) { + struct pthread *curthread = _get_curthread(); char s[512]; int i; @@ -219,20 +220,20 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread, (pthread->name == NULL) ? "" : pthread->name, pthread->active_priority, thread_info[i].name, pthread->fname, pthread->lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); if (long_version != 0) { /* Check if this is the running thread: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Output a record for the running thread: */ strcpy(s, "This is the running thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Check if this is the initial thread: */ if (pthread == _thread_initial) { /* Output a record for the initial thread: */ strcpy(s, "This is the initial thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Process according to thread state: */ switch (pthread->state) { @@ -246,22 +247,22 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread->data.fd.fd, pthread->data.fd.fname, pthread->data.fd.branch); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); 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)); + __sys_write(fd, s, strlen(s)); break; case PS_SIGWAIT: snprintf(s, sizeof(s), "sigmask (hi)"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); for (i = _SIG_WORDS - 1; i >= 0; i--) { snprintf(s, sizeof(s), "%08x\n", pthread->sigmask.__bits[i]); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } snprintf(s, sizeof(s), "(lo)\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); break; /* * Trap other states that are not explicitly @@ -276,7 +277,7 @@ dump_thread(int fd, pthread_t pthread, int long_version) /* Set the thread name for debug: */ void -pthread_set_name_np(pthread_t thread, char *name) +_pthread_set_name_np(pthread_t thread, char *name) { /* Check if the caller has specified a valid thread: */ if (thread != NULL && thread->magic == PTHREAD_MAGIC) { @@ -287,4 +288,3 @@ pthread_set_name_np(pthread_t thread, char *name) thread->name = strdup(name); } } -#endif diff --git a/lib/libc_r/uthread/uthread_init.c b/lib/libc_r/uthread/uthread_init.c index 93d7849..c92efe3 100644 --- a/lib/libc_r/uthread/uthread_init.c +++ b/lib/libc_r/uthread/uthread_init.c @@ -35,74 +35,117 @@ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE -#include -#include -#include -#include -#include -#include -#include +#include "namespace.h" +#include +#include +#include + #include +#include +#include +#include +#include +#include #include #include #include -#include #include +#include #include -#ifdef _THREAD_SAFE -#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include "un-namespace.h" + #include "pthread_private.h" /* + * 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 +}; + +/* * 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. */ -static void *thread_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, - &pthread_cond_init, - &pthread_cond_destroy, - &pthread_cond_wait, - &pthread_cond_timedwait, - &pthread_cond_signal, - &pthread_cond_broadcast +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 }; -#ifdef GCC_2_8_MADE_THREAD_AWARE -typedef void *** (*dynamic_handler_allocator)(); -extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); - -static pthread_key_t except_head_key; - -typedef struct { - void **__dynamic_handler_chain; - void *top_elt[2]; -} except_struct; - -static void ***dynamic_allocator_handler_fn() -{ - except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); - - if(dh == NULL) { - dh = (except_struct *)malloc( sizeof(except_struct) ); - memset(dh, '\0', sizeof(except_struct)); - dh->__dynamic_handler_chain= dh->top_elt; - pthread_setspecific(except_head_key, (void *)dh); - } - return &dh->__dynamic_handler_chain; -} -#endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization @@ -124,11 +167,11 @@ _thread_init(void) return; /* - * Make gcc quiescent about thread_references not being + * Make gcc quiescent about {,libgcc_}references not being * referenced: */ - if (thread_references[0] == NULL) - PANIC("Mandatory pthread_* functions not loaded"); + if ((references[0] == NULL) || (libgcc_references[0] == NULL)) + PANIC("Failed loading mandatory references in _thread_init"); /* * Check for the special case of this process running as @@ -143,22 +186,22 @@ _thread_init(void) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); - if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) + if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); - if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) + if (__sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); - if (_thread_sys_dup2(fd,0) == -1 || - _thread_sys_dup2(fd,1) == -1 || - _thread_sys_dup2(fd,2) == -1) + if (__sys_dup2(fd,0) == -1 || + __sys_dup2(fd,1) == -1 || + __sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if (((_pthread_stdio_flags[i] = - _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) && + __sys_fcntl(i,F_GETFL, NULL)) == -1) && (errno != EBADF)) PANIC("Cannot get stdio flags"); @@ -166,27 +209,27 @@ _thread_init(void) * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } @@ -270,6 +313,9 @@ _thread_init(void) /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; + /* Set the name of the thread: */ + _thread_initial->name = strdup("_thread_initial"); + /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); @@ -299,7 +345,7 @@ _thread_init(void) _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); - _thread_run = _thread_initial; + _set_curthread(_thread_initial); /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); @@ -320,7 +366,7 @@ _thread_init(void) _thread_sigstack.ss_size = SIGSTKSZ; _thread_sigstack.ss_flags = 0; if ((_thread_sigstack.ss_sp == NULL) || - (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0)) + (__sys_sigaltstack(&_thread_sigstack, NULL) != 0)) PANIC("Unable to install alternate signal stack"); /* Enter a loop to get the existing signal status: */ @@ -330,7 +376,7 @@ _thread_init(void) } /* Get the signal handler details: */ - else if (_thread_sys_sigaction(i, NULL, + else if (__sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal @@ -348,9 +394,9 @@ _thread_init(void) * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ - if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || - _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || - _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { + if (__sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || + __sys_sigaction(SIGINFO, &act, NULL) != 0 || + __sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ @@ -361,7 +407,7 @@ _thread_init(void) _thread_sigact[SIGCHLD - 1].sa_flags = SA_SIGINFO; /* Get the process signal mask: */ - _thread_sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); + __sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); /* Get the kernel clockrate: */ mib[0] = CTL_KERN; @@ -416,17 +462,8 @@ _thread_init(void) } } -#ifdef GCC_2_8_MADE_THREAD_AWARE - /* Create the thread-specific data for the exception linked list. */ - if(pthread_key_create(&except_head_key, NULL) != 0) - PANIC("Failed to create thread specific execption head"); - - /* Setup the gcc exception handler per thread. */ - __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); -#endif /* GCC_2_8_MADE_THREAD_AWARE */ - /* Initialise the garbage collector mutex and condition variable. */ - if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || + if (_pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); } @@ -445,12 +482,3 @@ _thread_main(int argc, char *argv[], char *env) return (main(argc, argv, env)); } #endif -#else -/* - * A stub for non-threaded programs. - */ -void -_thread_init(void) -{ -} -#endif diff --git a/lib/libc_r/uthread/uthread_ioctl.c b/lib/libc_r/uthread/uthread_ioctl.c index e72615a..c4babb2 100644 --- a/lib/libc_r/uthread/uthread_ioctl.c +++ b/lib/libc_r/uthread/uthread_ioctl.c @@ -33,11 +33,12 @@ */ #include #include -#ifdef _THREAD_SAFE #include /* O_NONBLOCK*/ #include #include "pthread_private.h" +#pragma weak ioctl=_ioctl + int _ioctl(int fd, unsigned long request,...) { @@ -62,7 +63,7 @@ _ioctl(int fd, unsigned long request,...) ret = 0; break; default: - ret = _thread_sys_ioctl(fd, request, va_arg(ap, char *)); + ret = __sys_ioctl(fd, request, va_arg(ap, char *)); break; } @@ -76,6 +77,3 @@ _ioctl(int fd, unsigned long request,...) /* Return the completion status: */ return (ret); } - -__strong_reference(_ioctl, ioctl); -#endif diff --git a/lib/libc_r/uthread/uthread_jmp.c b/lib/libc_r/uthread/uthread_jmp.c index db19ebd..a350280 100644 --- a/lib/libc_r/uthread/uthread_jmp.c +++ b/lib/libc_r/uthread/uthread_jmp.c @@ -35,7 +35,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -43,10 +42,16 @@ /* Prototypes: */ static inline int check_stack(pthread_t thread, void *stackp); +#pragma weak siglongjmp=_thread_siglongjmp +#pragma weak longjmp=_thread_longjmp +#pragma weak _longjmp=__thread_longjmp + void -siglongjmp(sigjmp_buf env, int savemask) +_thread_siglongjmp(sigjmp_buf env, int savemask) { - if (check_stack(_thread_run, (void *) GET_STACK_SJB(env))) + struct pthread *curthread = _get_curthread(); + + if (check_stack(curthread, (void *) GET_STACK_SJB(env))) PANIC("siglongjmp()ing between thread contexts is undefined by " "POSIX 1003.1"); @@ -58,9 +63,11 @@ siglongjmp(sigjmp_buf env, int savemask) } void -longjmp(jmp_buf env, int val) +_thread_longjmp(jmp_buf env, int val) { - if (check_stack(_thread_run, (void *) GET_STACK_JB(env))) + struct pthread *curthread = _get_curthread(); + + if (check_stack(curthread, (void *) GET_STACK_JB(env))) PANIC("longjmp()ing between thread contexts is undefined by " "POSIX 1003.1"); @@ -72,9 +79,11 @@ longjmp(jmp_buf env, int val) } void -_longjmp(jmp_buf env, int val) +__thread_longjmp(jmp_buf env, int val) { - if (check_stack(_thread_run, (void *) GET_STACK_JB(env))) + struct pthread *curthread = _get_curthread(); + + if (check_stack(curthread, (void *) GET_STACK_JB(env))) PANIC("_longjmp()ing between thread contexts is undefined by " "POSIX 1003.1"); @@ -108,4 +117,3 @@ check_stack(pthread_t thread, void *stackp) else return (0); } -#endif diff --git a/lib/libc_r/uthread/uthread_join.c b/lib/libc_r/uthread/uthread_join.c index b4a7c61..2615f73 100644 --- a/lib/libc_r/uthread/uthread_join.c +++ b/lib/libc_r/uthread/uthread_join.c @@ -32,13 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_join=_pthread_join + int -pthread_join(pthread_t pthread, void **thread_return) +_pthread_join(pthread_t pthread, void **thread_return) { + struct pthread *curthread = _get_curthread(); int ret = 0; _thread_enter_cancellation_point(); @@ -51,7 +53,7 @@ pthread_join(pthread_t pthread, void **thread_return) } /* Check if the caller has specified itself: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Avoid a deadlock condition: */ _thread_leave_cancellation_point(); return(EDEADLK); @@ -72,7 +74,7 @@ pthread_join(pthread_t pthread, void **thread_return) /* Check if the thread is not dead: */ else if (pthread->state != PS_DEAD) { - PTHREAD_ASSERT_NOT_IN_SYNCQ(_thread_run); + PTHREAD_ASSERT_NOT_IN_SYNCQ(curthread); /* * Enter a loop in case this thread is woken prematurely @@ -80,7 +82,7 @@ pthread_join(pthread_t pthread, void **thread_return) */ for (;;) { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Protect against being context switched out while @@ -90,25 +92,25 @@ pthread_join(pthread_t pthread, void **thread_return) /* Add the running thread to the join queue: */ TAILQ_INSERT_TAIL(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags |= PTHREAD_FLAGS_IN_JOINQ; - _thread_run->data.thread = pthread; + curthread, sqe); + curthread->flags |= PTHREAD_FLAGS_IN_JOINQ; + curthread->data.thread = pthread; /* Schedule the next thread: */ _thread_kern_sched_state(PS_JOIN, __FILE__, __LINE__); - if ((_thread_run->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { + if ((curthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread, sqe); + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } - _thread_run->data.thread = NULL; + curthread->data.thread = NULL; _thread_kern_sig_undefer(); - if (_thread_run->interrupted != 0) { - if (_thread_run->continuation != NULL) - _thread_run->continuation(_thread_run); + if (curthread->interrupted != 0) { + if (curthread->continuation != NULL) + curthread->continuation(curthread); /* * This thread was interrupted, probably to * invoke a signal handler. Make sure the @@ -134,9 +136,9 @@ pthread_join(pthread_t pthread, void **thread_return) * by the thread we're joining to when it * exits or detaches: */ - ret = _thread_run->error; + ret = curthread->error; if ((ret == 0) && (thread_return != NULL)) - *thread_return = _thread_run->ret; + *thread_return = curthread->ret; /* We're done; break out of the loop. */ break; @@ -156,11 +158,12 @@ pthread_join(pthread_t pthread, void **thread_return) void _join_backout(pthread_t pthread) { + struct pthread *curthread = _get_curthread(); + _thread_kern_sig_defer(); if ((pthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&pthread->data.thread->join_queue, pthread, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } _thread_kern_sig_undefer(); } -#endif diff --git a/lib/libc_r/uthread/uthread_kern.c b/lib/libc_r/uthread/uthread_kern.c index 6f4bb6e..8a32253 100644 --- a/lib/libc_r/uthread/uthread_kern.c +++ b/lib/libc_r/uthread/uthread_kern.c @@ -48,7 +48,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -80,6 +79,8 @@ static int called_from_handler = 0; void _thread_kern_sched_frame(struct pthread_signal_frame *psf) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a signal from interrupting this execution and @@ -88,10 +89,10 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) _thread_kern_in_sched = 1; /* Restore the signal frame: */ - _thread_sigframe_restore(_thread_run, psf); + _thread_sigframe_restore(curthread, psf); /* The signal mask was restored; check for any pending signals: */ - _thread_run->check_pending = 1; + curthread->check_pending = 1; /* Switch to the thread scheduler: */ ___longjmp(_thread_kern_sched_jb, 1); @@ -101,6 +102,8 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) void _thread_kern_sched(ucontext_t *scp) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -114,13 +117,13 @@ _thread_kern_sched(ucontext_t *scp) DBG_MSG("Entering scheduler due to signal\n"); } else { /* Save the state of the current thread: */ - if (_setjmp(_thread_run->ctx.jb) == 0) { + if (_setjmp(curthread->ctx.jb) == 0) { /* Flag the jump buffer was the last state saved: */ - _thread_run->ctxtype = CTX_JB_NOSIG; - _thread_run->longjmp_val = 1; + curthread->ctxtype = CTX_JB_NOSIG; + curthread->longjmp_val = 1; } else { DBG_MSG("Returned from ___longjmp, thread %p\n", - _thread_run); + curthread); /* * This point is reached when a longjmp() is called * to restore the state of a thread. @@ -129,10 +132,10 @@ _thread_kern_sched(ucontext_t *scp) */ _thread_kern_in_sched = 0; - if (_thread_run->sig_defer_count == 0) { - if (((_thread_run->cancelflags & + if (curthread->sig_defer_count == 0) { + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) /* * Cancellations override signals. @@ -150,7 +153,7 @@ _thread_kern_sched(ucontext_t *scp) if (_sched_switch_hook != NULL) { /* Run the installed switch hook: */ thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } return; } @@ -162,7 +165,9 @@ _thread_kern_sched(ucontext_t *scp) void _thread_kern_sched_sig(void) { - _thread_run->check_pending = 1; + struct pthread *curthread = _get_curthread(); + + curthread->check_pending = 1; _thread_kern_sched(NULL); } @@ -172,13 +177,14 @@ _thread_kern_scheduler(void) { struct timespec ts; struct timeval tv; + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; unsigned int current_tick; int add_to_prioq; /* If the currently running thread is a user thread, save it: */ - if ((_thread_run->flags & PTHREAD_FLAGS_PRIVATE) == 0) - _last_user_thread = _thread_run; + if ((curthread->flags & PTHREAD_FLAGS_PRIVATE) == 0) + _last_user_thread = curthread; if (called_from_handler != 0) { called_from_handler = 0; @@ -188,7 +194,7 @@ _thread_kern_scheduler(void) * the current thread. Restore the process signal * mask. */ - if (_thread_sys_sigprocmask(SIG_SETMASK, + if (__sys_sigprocmask(SIG_SETMASK, &_process_sigmask, NULL) != 0) PANIC("Unable to restore process mask after signal"); @@ -196,14 +202,14 @@ _thread_kern_scheduler(void) * Since the signal handler didn't return normally, we * have to tell the kernel to reuse the signal stack. */ - if (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0) + if (__sys_sigaltstack(&_thread_sigstack, NULL) != 0) PANIC("Unable to restore alternate signal stack"); } /* Are there pending signals for this thread? */ - if (_thread_run->check_pending != 0) { - _thread_run->check_pending = 0; - _thread_sig_check_pending(_thread_run); + if (curthread->check_pending != 0) { + curthread->check_pending = 0; + _thread_sig_check_pending(curthread); } /* @@ -226,22 +232,22 @@ _thread_kern_scheduler(void) _queue_signals = 1; add_to_prioq = 0; - if (_thread_run != &_thread_kern_thread) { + if (curthread != &_thread_kern_thread) { /* * This thread no longer needs to yield the CPU. */ - _thread_run->yield_on_sig_undefer = 0; + curthread->yield_on_sig_undefer = 0; - if (_thread_run->state != PS_RUNNING) { + if (curthread->state != PS_RUNNING) { /* * Save the current time as the time that the * thread became inactive: */ - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ + curthread->last_inactive =+ UINT_MAX + 1; } } @@ -250,7 +256,7 @@ _thread_kern_scheduler(void) * Place the currently running thread into the * appropriate queue(s). */ - switch (_thread_run->state) { + switch (curthread->state) { case PS_DEAD: case PS_STATE_MAX: /* to silence -Wall */ case PS_SUSPENDED: @@ -285,31 +291,31 @@ _thread_kern_scheduler(void) case PS_SIGWAIT: case PS_WAIT_WAIT: /* No timeouts for these states: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States which can timeout: */ case PS_COND_WAIT: case PS_SLEEP_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States that require periodic work: */ case PS_SPINBLOCK: /* No timeouts for this state: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Increment spinblock count: */ _spinblock_count++; @@ -320,13 +326,13 @@ _thread_kern_scheduler(void) case PS_POLL_WAIT: case PS_SELECT_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); /* Insert into the work queue: */ - PTHREAD_WORKQ_INSERT(_thread_run); + PTHREAD_WORKQ_INSERT(curthread); break; } } @@ -342,7 +348,7 @@ _thread_kern_scheduler(void) * has occurred or if we have no more runnable threads. */ else if (((current_tick = _sched_ticks) != last_tick) || - ((_thread_run->state != PS_RUNNING) && + ((curthread->state != PS_RUNNING) && (PTHREAD_PRIOQ_FIRST() == NULL))) { /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -407,43 +413,43 @@ _thread_kern_scheduler(void) * thread became inactive: */ current_tick = _sched_ticks; - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ UINT_MAX + 1; + curthread->last_inactive =+ UINT_MAX + 1; } - if ((_thread_run->slice_usec != -1) && - (_thread_run->attr.sched_policy != SCHED_FIFO)) { + if ((curthread->slice_usec != -1) && + (curthread->attr.sched_policy != SCHED_FIFO)) { /* * Accumulate the number of microseconds for * which the current thread has run: */ - _thread_run->slice_usec += - (_thread_run->last_inactive - - _thread_run->last_active) * + curthread->slice_usec += + (curthread->last_inactive - + curthread->last_active) * (long)_clock_res_usec; /* Check for time quantum exceeded: */ - if (_thread_run->slice_usec > TIMESLICE_USEC) - _thread_run->slice_usec = -1; + if (curthread->slice_usec > TIMESLICE_USEC) + curthread->slice_usec = -1; } - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* * The thread exceeded its time * quantum or it yielded the CPU; * place it at the tail of the * queue for its priority. */ - PTHREAD_PRIOQ_INSERT_TAIL(_thread_run); + PTHREAD_PRIOQ_INSERT_TAIL(curthread); } else { /* * The thread hasn't exceeded its * interval. Place it at the head * of the queue for its priority. */ - PTHREAD_PRIOQ_INSERT_HEAD(_thread_run); + PTHREAD_PRIOQ_INSERT_HEAD(curthread); } } @@ -459,9 +465,11 @@ _thread_kern_scheduler(void) * the running thread to point to the global kernel * thread structure: */ - _thread_run = &_thread_kern_thread; + _set_curthread(&_thread_kern_thread); + curthread = &_thread_kern_thread; + DBG_MSG("No runnable threads, using kernel thread %p\n", - _thread_run); + curthread); /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -532,23 +540,24 @@ _thread_kern_scheduler(void) } /* Make the selected thread the current thread: */ - _thread_run = pthread_h; + _set_curthread(pthread_h); + curthread = pthread_h; /* * Save the current time as the time that the thread * became active: */ current_tick = _sched_ticks; - _thread_run->last_active = (long) current_tick; + curthread->last_active = (long) current_tick; /* * Check if this thread is running for the first time * or running again after using its full time slice * allocation: */ - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* Reset the accumulated time slice period: */ - _thread_run->slice_usec = 0; + curthread->slice_usec = 0; } /* @@ -556,29 +565,29 @@ _thread_kern_scheduler(void) * installed switch hooks. */ if ((_sched_switch_hook != NULL) && - (_last_user_thread != _thread_run)) { + (_last_user_thread != curthread)) { thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } /* * Continue the thread at its current frame: */ - switch(_thread_run->ctxtype) { + switch(curthread->ctxtype) { case CTX_JB_NOSIG: - ___longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + ___longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_JB: - __longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + __longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_SJB: - __siglongjmp(_thread_run->ctx.sigjb, - _thread_run->longjmp_val); + __siglongjmp(curthread->ctx.sigjb, + curthread->longjmp_val); break; case CTX_UC: /* XXX - Restore FP regsisters? */ - FP_RESTORE_UC(&_thread_run->ctx.uc); + FP_RESTORE_UC(&curthread->ctx.uc); /* * Do a sigreturn to restart the thread that @@ -587,15 +596,15 @@ _thread_kern_scheduler(void) _thread_kern_in_sched = 0; #if NOT_YET - _setcontext(&_thread_run->ctx.uc); + _setcontext(&curthread->ctx.uc); #else /* * Ensure the process signal mask is set * correctly: */ - _thread_run->ctx.uc.uc_sigmask = + curthread->ctx.uc.uc_sigmask = _process_sigmask; - _thread_sys_sigreturn(&_thread_run->ctx.uc); + __sys_sigreturn(&curthread->ctx.uc); #endif break; } @@ -611,6 +620,8 @@ _thread_kern_scheduler(void) void _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -625,9 +636,9 @@ _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; /* Schedule the next thread that is ready: */ _thread_kern_sched(NULL); @@ -637,6 +648,8 @@ void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -652,9 +665,9 @@ _thread_kern_sched_state_unlock(enum pthread_state state, _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; _SPINUNLOCK(lock); @@ -817,7 +830,7 @@ thread_kern_poll(int wait_reqd) * Wait for a file descriptor to be ready for read, write, or * an exception, or a timeout to occur: */ - count = _thread_sys_poll(_thread_pfd_table, nfds, timeout_ms); + count = __sys_poll(_thread_pfd_table, nfds, timeout_ms); if (kern_pipe_added != 0) /* @@ -991,11 +1004,12 @@ thread_kern_poll(int wait_reqd) void _thread_kern_set_timeout(const struct timespec * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec current_time; struct timeval tv; /* Reset the timeout flag for the running thread: */ - _thread_run->timeout = 0; + curthread->timeout = 0; /* Check if the thread is to wait forever: */ if (timeout == NULL) { @@ -1003,28 +1017,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) * Set the wakeup time to something that can be recognised as * different to an actual time of day: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; } /* Check if no waiting is required: */ else if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) { /* Set the wake up time to 'immediately': */ - _thread_run->wakeup_time.tv_sec = 0; - _thread_run->wakeup_time.tv_nsec = 0; + curthread->wakeup_time.tv_sec = 0; + curthread->wakeup_time.tv_nsec = 0; } else { /* Get the current time: */ GET_CURRENT_TOD(tv); TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; /* Check if the nanosecond field needs to wrap: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } } } @@ -1032,24 +1046,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) void _thread_kern_sig_defer(void) { + struct pthread *curthread = _get_curthread(); + /* Allow signal deferral to be recursive. */ - _thread_run->sig_defer_count++; + curthread->sig_defer_count++; } void _thread_kern_sig_undefer(void) { + struct pthread *curthread = _get_curthread(); + /* * Perform checks to yield only if we are about to undefer * signals. */ - if (_thread_run->sig_defer_count > 1) { + if (curthread->sig_defer_count > 1) { /* Decrement the signal deferral count. */ - _thread_run->sig_defer_count--; + curthread->sig_defer_count--; } - else if (_thread_run->sig_defer_count == 1) { + else if (curthread->sig_defer_count == 1) { /* Reenable signals: */ - _thread_run->sig_defer_count = 0; + curthread->sig_defer_count = 0; /* * Check if there are queued signals: @@ -1061,8 +1079,8 @@ _thread_kern_sig_undefer(void) * Check for asynchronous cancellation before delivering any * pending signals: */ - if (((_thread_run->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) pthread_testcancel(); /* @@ -1071,9 +1089,9 @@ _thread_kern_sig_undefer(void) * * XXX - Come back and revisit the pending signal problem */ - if ((_thread_run->yield_on_sig_undefer != 0) || - SIGNOTEMPTY(_thread_run->sigpend)) { - _thread_run->yield_on_sig_undefer = 0; + if ((curthread->yield_on_sig_undefer != 0) || + SIGNOTEMPTY(curthread->sigpend)) { + curthread->yield_on_sig_undefer = 0; _thread_kern_sched(NULL); } } @@ -1088,7 +1106,7 @@ dequeue_signals(void) /* * Enter a loop to clear the pthread kernel pipe: */ - while (((num = _thread_sys_read(_thread_kern_pipe[0], bufr, + while (((num = __sys_read(_thread_kern_pipe[0], bufr, sizeof(bufr))) > 0) || (num == -1 && errno == EINTR)) { } if ((num < 0) && (errno != EAGAIN)) { @@ -1120,4 +1138,18 @@ thread_run_switch_hook(pthread_t thread_out, pthread_t thread_in) _sched_switch_hook(tid_out, tid_in); } } -#endif + +struct pthread * +_get_curthread(void) +{ + if (_thread_initial == NULL) + _thread_init(); + + return (_thread_run); +} + +void +_set_curthread(struct pthread *newthread) +{ + _thread_run = newthread; +} diff --git a/lib/libc_r/uthread/uthread_kevent.c b/lib/libc_r/uthread/uthread_kevent.c index 290094d..0d52f13 100644 --- a/lib/libc_r/uthread/uthread_kevent.c +++ b/lib/libc_r/uthread/uthread_kevent.c @@ -31,46 +31,47 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak keven=_kevent + int -kevent(int kq, const struct kevent *changelist, int nchanges, +_kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout) { + struct pthread *curthread = _get_curthread(); struct timespec nullts = { 0, 0 }; int rc; /* Set the wake up time */ _thread_kern_set_timeout(timeout); - rc = _thread_sys_kevent(kq, changelist, nchanges, + rc = __sys_kevent(kq, changelist, nchanges, eventlist, nevents, &nullts); if (rc == 0 && (timeout == NULL || timeout->tv_sec != 0 || timeout->tv_nsec != 0)) { /* Save the socket file descriptor: */ - _thread_run->data.fd.fd = kq; - _thread_run->data.fd.fname = __FILE__; - _thread_run->data.fd.branch = __LINE__; + curthread->data.fd.fd = kq; + curthread->data.fd.fname = __FILE__; + curthread->data.fd.branch = __LINE__; do { /* Reset the interrupted and timeout flags: */ - _thread_run->interrupted = 0; - _thread_run->timeout = 0; + curthread->interrupted = 0; + curthread->timeout = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; rc = -1; break; } - rc = _thread_sys_kevent(kq, NULL, 0, + rc = __sys_kevent(kq, NULL, 0, eventlist, nevents, &nullts); - } while (rc == 0 && _thread_run->timeout == 0); + } while (rc == 0 && curthread->timeout == 0); } return (rc); } -#endif diff --git a/lib/libc_r/uthread/uthread_kill.c b/lib/libc_r/uthread/uthread_kill.c index 4bf1761..7a24ffb 100644 --- a/lib/libc_r/uthread/uthread_kill.c +++ b/lib/libc_r/uthread/uthread_kill.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_kill=_pthread_kill + int -pthread_kill(pthread_t pthread, int sig) +_pthread_kill(pthread_t pthread, int sig) { int ret; @@ -71,4 +72,3 @@ pthread_kill(pthread_t pthread, int sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_listen.c b/lib/libc_r/uthread/uthread_listen.c index 14a6e6d..11b55bf 100644 --- a/lib/libc_r/uthread/uthread_listen.c +++ b/lib/libc_r/uthread/uthread_listen.c @@ -33,21 +33,20 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak listen=_listen + int _listen(int fd, int backlog) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_listen(fd, backlog); + ret = __sys_listen(fd, backlog); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } -__strong_reference(_listen, listen); -#endif diff --git a/lib/libc_r/uthread/uthread_mattr_init.c b/lib/libc_r/uthread/uthread_mattr_init.c index 63d4401..4859915 100644 --- a/lib/libc_r/uthread/uthread_mattr_init.c +++ b/lib/libc_r/uthread/uthread_mattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_init=_pthread_mutexattr_init + int -pthread_mutexattr_init(pthread_mutexattr_t *attr) +_pthread_mutexattr_init(pthread_mutexattr_t *attr) { int ret; pthread_mutexattr_t pattr; @@ -55,4 +56,3 @@ pthread_mutexattr_init(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_mattr_kind_np.c b/lib/libc_r/uthread/uthread_mattr_kind_np.c index 1a0832f..5ad7928 100644 --- a/lib/libc_r/uthread/uthread_mattr_kind_np.c +++ b/lib/libc_r/uthread/uthread_mattr_kind_np.c @@ -32,12 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_setkind_np=_pthread_mutexattr_setkind_np +#pragma weak pthread_mutexattr_getkind_np=_pthread_mutexattr_getkind_np +#pragma weak pthread_mutexattr_gettype=_pthread_mutexattr_gettype +#pragma weak pthread_mutexattr_settype=_pthread_mutexattr_settype + int -pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) +_pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) { int ret; if (attr == NULL || *attr == NULL) { @@ -51,7 +55,7 @@ pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) } int -pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) +_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) { int ret; if (attr == NULL) { @@ -64,7 +68,7 @@ pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) } int -pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) +_pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { int ret; if (attr == NULL || *attr == NULL || type >= MUTEX_TYPE_MAX) { @@ -78,7 +82,7 @@ pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) } int -pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) +_pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) { int ret; @@ -91,4 +95,3 @@ pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) } return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_msync.c b/lib/libc_r/uthread/uthread_msync.c index 2ae6ac7..2952da8 100644 --- a/lib/libc_r/uthread/uthread_msync.c +++ b/lib/libc_r/uthread/uthread_msync.c @@ -8,22 +8,23 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak msync=__msync + int _msync(void *addr, size_t len, int flags) { int ret; - ret = _thread_sys_msync(addr, len, flags); + ret = __sys_msync(addr, len, flags); return (ret); } int -msync(void *addr, size_t len, int flags) +__msync(void *addr, size_t len, int flags) { int ret; @@ -39,4 +40,3 @@ msync(void *addr, size_t len, int flags) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_multi_np.c b/lib/libc_r/uthread/uthread_multi_np.c index 9a2f7c1..cf58bb4 100644 --- a/lib/libc_r/uthread/uthread_multi_np.c +++ b/lib/libc_r/uthread/uthread_multi_np.c @@ -32,15 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_multi_np=_pthread_multi_np + int -pthread_multi_np() +_pthread_multi_np() { /* Return to multi-threaded scheduling mode: */ _thread_single = NULL; return(0); } -#endif diff --git a/lib/libc_r/uthread/uthread_mutex.c b/lib/libc_r/uthread/uthread_mutex.c index e0ad7da..1c72b7b 100644 --- a/lib/libc_r/uthread/uthread_mutex.c +++ b/lib/libc_r/uthread/uthread_mutex.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -75,6 +74,20 @@ static inline void mutex_queue_enq(pthread_mutex_t, pthread_t); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; +static struct pthread_mutex_attr static_mutex_attr = + PTHREAD_MUTEXATTR_STATIC_INITIALIZER; +static pthread_mutexattr_t static_mattr = &static_mutex_attr; + +/* Single underscore versions provided for libc internal usage: */ +#pragma weak pthread_mutex_trylock=__pthread_mutex_trylock +#pragma weak pthread_mutex_lock=__pthread_mutex_lock + +/* No difference between libc and application usage of these: */ +#pragma weak pthread_mutex_init=_pthread_mutex_init +#pragma weak pthread_mutex_destroy=_pthread_mutex_destroy +#pragma weak pthread_mutex_unlock=_pthread_mutex_unlock + + /* Reinitialize a mutex to defaults. */ int _mutex_reinit(pthread_mutex_t * mutex) @@ -106,7 +119,7 @@ _mutex_reinit(pthread_mutex_t * mutex) } int -pthread_mutex_init(pthread_mutex_t * mutex, +_pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutex_attr) { enum pthread_mutextype type; @@ -201,7 +214,7 @@ pthread_mutex_init(pthread_mutex_t * mutex, } int -pthread_mutex_destroy(pthread_mutex_t * mutex) +_pthread_mutex_destroy(pthread_mutex_t * mutex) { int ret = 0; @@ -259,165 +272,203 @@ init_static(pthread_mutex_t *mutex) return(ret); } -int -pthread_mutex_trylock(pthread_mutex_t * mutex) +static int +init_static_private(pthread_mutex_t *mutex) { + int ret; + + _SPINLOCK(&static_init_lock); + + if (*mutex == NULL) + ret = pthread_mutex_init(mutex, &static_mattr); + else + ret = 0; + + _SPINUNLOCK(&static_init_lock); + + return(ret); +} + +static int +mutex_trylock_common(pthread_mutex_t *mutex) +{ + struct pthread *curthread = _get_curthread(); int ret = 0; - if (mutex == NULL) - ret = EINVAL; + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* - * If the mutex is statically initialized, perform the dynamic - * initialization: + * Defer signals to protect the scheduling queues from + * access by the signal handler: */ - else if (*mutex != NULL || (ret = init_static(mutex)) == 0) { - /* - * Defer signals to protect the scheduling queues from - * access by the signal handler: - */ - _thread_kern_sig_defer(); + _thread_kern_sig_defer(); - /* Lock the mutex structure: */ - _SPINLOCK(&(*mutex)->lock); + /* Lock the mutex structure: */ + _SPINLOCK(&(*mutex)->lock); - /* - * If the mutex was statically allocated, properly - * initialize the tail queue. - */ - if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { - TAILQ_INIT(&(*mutex)->m_queue); - _MUTEX_INIT_LINK(*mutex); - (*mutex)->m_flags |= MUTEX_FLAGS_INITED; - } + /* + * If the mutex was statically allocated, properly + * initialize the tail queue. + */ + if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { + TAILQ_INIT(&(*mutex)->m_queue); + _MUTEX_INIT_LINK(*mutex); + (*mutex)->m_flags |= MUTEX_FLAGS_INITED; + } - /* Process according to mutex type: */ - switch ((*mutex)->m_protocol) { - /* Default POSIX mutex: */ - case PTHREAD_PRIO_NONE: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Process according to mutex type: */ + switch ((*mutex)->m_protocol) { + /* Default POSIX mutex: */ + case PTHREAD_PRIO_NONE: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority inheritence mutex: */ - case PTHREAD_PRIO_INHERIT: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* POSIX priority inheritence mutex: */ + case PTHREAD_PRIO_INHERIT: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The mutex takes on the attributes of the - * running thread when there are no waiters. - */ - (*mutex)->m_prio = _thread_run->active_priority; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; + /* + * The mutex takes on the attributes of the + * running thread when there are no waiters. + */ + (*mutex)->m_prio = curthread->active_priority; + (*mutex)->m_saved_prio = + curthread->inherited_priority; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority protection mutex: */ - case PTHREAD_PRIO_PROTECT: - /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) - ret = EINVAL; + /* POSIX priority protection mutex: */ + case PTHREAD_PRIO_PROTECT: + /* Check for a priority ceiling violation: */ + if (curthread->active_priority > (*mutex)->m_prio) + ret = EINVAL; - /* Check if this mutex is not locked: */ - else if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Check if this mutex is not locked: */ + else if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The running thread inherits the ceiling - * priority of the mutex and executes at that - * priority. - */ - _thread_run->active_priority = (*mutex)->m_prio; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = - (*mutex)->m_prio; + /* + * The running thread inherits the ceiling + * priority of the mutex and executes at that + * priority. + */ + curthread->active_priority = (*mutex)->m_prio; + (*mutex)->m_saved_prio = + curthread->inherited_priority; + curthread->inherited_priority = + (*mutex)->m_prio; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* Trap invalid mutex types: */ - default: - /* Return an invalid argument error: */ - ret = EINVAL; - break; - } + /* Trap invalid mutex types: */ + default: + /* Return an invalid argument error: */ + ret = EINVAL; + break; + } - /* Unlock the mutex structure: */ - _SPINUNLOCK(&(*mutex)->lock); + /* Unlock the mutex structure: */ + _SPINUNLOCK(&(*mutex)->lock); - /* - * Undefer and handle pending signals, yielding if - * necessary: - */ - _thread_kern_sig_undefer(); - } + /* + * Undefer and handle pending signals, yielding if + * necessary: + */ + _thread_kern_sig_undefer(); /* Return the completion status: */ return (ret); } int -pthread_mutex_lock(pthread_mutex_t * mutex) +__pthread_mutex_trylock(pthread_mutex_t *mutex) { int ret = 0; - if (_thread_initial == NULL) - _thread_init(); - if (mutex == NULL) - return (EINVAL); + ret = EINVAL; /* * If the mutex is statically initialized, perform the dynamic * initialization: */ - if ((*mutex == NULL) && - ((ret = init_static(mutex)) != 0)) - return (ret); + else if ((*mutex != NULL) || (ret = init_static(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking the mutex private (delete safe): + */ + else if ((*mutex != NULL) || (ret = init_static_private(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +static int +mutex_lock_common(pthread_mutex_t * mutex) +{ + struct pthread *curthread = _get_curthread(); + int ret = 0; + + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* Reset the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Enter a loop waiting to become the mutex owner. We need a @@ -453,27 +504,27 @@ pthread_mutex_lock(pthread_mutex_t * mutex) case PTHREAD_PRIO_NONE: if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* * Unlock the mutex structure and schedule the @@ -492,42 +543,42 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* Check if this mutex is not locked: */ if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The mutex takes on attributes of the * running thread when there are no waiters. */ - (*mutex)->m_prio = _thread_run->active_priority; + (*mutex)->m_prio = curthread->active_priority; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; - if (_thread_run->active_priority > + if (curthread->active_priority > (*mutex)->m_prio) /* Adjust priorities: */ mutex_priority_adjust(*mutex); @@ -547,7 +598,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* POSIX priority protection mutex: */ case PTHREAD_PRIO_PROTECT: /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) + if (curthread->active_priority > (*mutex)->m_prio) ret = EINVAL; /* Check if this mutex is not locked: */ @@ -556,43 +607,43 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Lock the mutex for the running * thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The running thread inherits the ceiling * priority of the mutex and executes at that * priority: */ - _thread_run->active_priority = (*mutex)->m_prio; + curthread->active_priority = (*mutex)->m_prio; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* Clear any previous error: */ - _thread_run->error = 0; + curthread->error = 0; /* * Unlock the mutex structure and schedule the @@ -609,8 +660,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * waiting for the mutex causing a ceiling * violation. */ - ret = _thread_run->error; - _thread_run->error = 0; + ret = curthread->error; + curthread->error = 0; } break; @@ -625,8 +676,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Check to see if this thread was interrupted and * is still in the mutex queue of waiting threads: */ - if (_thread_run->interrupted != 0) - mutex_queue_remove(*mutex, _thread_run); + if (curthread->interrupted != 0) + mutex_queue_remove(*mutex, curthread); /* Unlock the mutex structure: */ _SPINUNLOCK(&(*mutex)->lock); @@ -636,19 +687,61 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * necessary: */ _thread_kern_sig_undefer(); - } while (((*mutex)->m_owner != _thread_run) && (ret == 0) && - (_thread_run->interrupted == 0)); + } while (((*mutex)->m_owner != curthread) && (ret == 0) && + (curthread->interrupted == 0)); - if (_thread_run->interrupted != 0 && - _thread_run->continuation != NULL) - _thread_run->continuation((void *) _thread_run); + if (curthread->interrupted != 0 && + curthread->continuation != NULL) + curthread->continuation((void *) curthread); /* Return the completion status: */ return (ret); } int -pthread_mutex_unlock(pthread_mutex_t * mutex) +__pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization: + */ + else if ((*mutex != NULL) || ((ret = init_static(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking it private (delete safe): + */ + else if ((*mutex != NULL) || ((ret = init_static_private(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_unlock(pthread_mutex_t * mutex) { return (mutex_unlock_common(mutex, /* add reference */ 0)); } @@ -738,6 +831,7 @@ mutex_self_lock(pthread_mutex_t mutex) static inline int mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) { + struct pthread *curthread = _get_curthread(); int ret = 0; if (mutex == NULL || *mutex == NULL) { @@ -760,7 +854,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -828,7 +922,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -852,16 +946,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -946,7 +1040,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -970,16 +1064,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -1477,4 +1571,3 @@ mutex_queue_enq(pthread_mutex_t mutex, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_MUTEXQ; } -#endif diff --git a/lib/libc_r/uthread/uthread_mutex_prioceiling.c b/lib/libc_r/uthread/uthread_mutex_prioceiling.c index c193c82..d19f521 100644 --- a/lib/libc_r/uthread/uthread_mutex_prioceiling.c +++ b/lib/libc_r/uthread/uthread_mutex_prioceiling.c @@ -34,12 +34,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprioceiling=_pthread_mutexattr_getprioceiling +#pragma weak pthread_mutexattr_setprioceiling=_pthread_mutexattr_setprioceiling +#pragma weak pthread_mutex_getprioceiling=_pthread_mutex_getprioceiling +#pragma weak pthread_mutex_setprioceiling=_pthread_mutex_setprioceiling + int -pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) +_pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) { int ret = 0; @@ -54,7 +58,7 @@ pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) } int -pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) +_pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) { int ret = 0; @@ -69,8 +73,8 @@ pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) } int -pthread_mutex_getprioceiling(pthread_mutex_t *mutex, - int *prioceiling) +_pthread_mutex_getprioceiling(pthread_mutex_t *mutex, + int *prioceiling) { int ret; @@ -85,8 +89,8 @@ pthread_mutex_getprioceiling(pthread_mutex_t *mutex, } int -pthread_mutex_setprioceiling(pthread_mutex_t *mutex, - int prioceiling, int *old_ceiling) +_pthread_mutex_setprioceiling(pthread_mutex_t *mutex, + int prioceiling, int *old_ceiling) { int ret = 0; @@ -107,4 +111,3 @@ pthread_mutex_setprioceiling(pthread_mutex_t *mutex, } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_mutex_protocol.c b/lib/libc_r/uthread/uthread_mutex_protocol.c index 9847ae5..1ad9b15 100644 --- a/lib/libc_r/uthread/uthread_mutex_protocol.c +++ b/lib/libc_r/uthread/uthread_mutex_protocol.c @@ -34,12 +34,14 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprotocol=_pthread_mutexattr_getprotocol +#pragma weak pthread_mutexattr_setprotocol=_pthread_mutexattr_setprotocol + int -pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) +_pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) { int ret = 0; @@ -52,7 +54,7 @@ pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) } int -pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) +_pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) { int ret = 0; @@ -66,4 +68,3 @@ pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_mutexattr_destroy.c b/lib/libc_r/uthread/uthread_mutexattr_destroy.c index 6eed2a2..cc766f0 100644 --- a/lib/libc_r/uthread/uthread_mutexattr_destroy.c +++ b/lib/libc_r/uthread/uthread_mutexattr_destroy.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_destroy=_pthread_mutexattr_destroy + int -pthread_mutexattr_destroy(pthread_mutexattr_t *attr) +_pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -50,4 +51,3 @@ pthread_mutexattr_destroy(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_nanosleep.c b/lib/libc_r/uthread/uthread_nanosleep.c index 67dd08d..54349d4 100644 --- a/lib/libc_r/uthread/uthread_nanosleep.c +++ b/lib/libc_r/uthread/uthread_nanosleep.c @@ -33,14 +33,16 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak nanosleep=__nanosleep + int _nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { + struct pthread *curthread = _get_curthread(); int ret = 0; struct timespec current_time; struct timespec current_time1; @@ -63,16 +65,16 @@ _nanosleep(const struct timespec * time_to_sleep, TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; /* Check if the nanosecond field has overflowed: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Reschedule the current thread to sleep: */ _thread_kern_sched_state(PS_SLEEP_WAIT, __FILE__, __LINE__); @@ -118,7 +120,7 @@ _nanosleep(const struct timespec * time_to_sleep, } /* Check if the sleep was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an EINTR error : */ errno = EINTR; ret = -1; @@ -128,7 +130,7 @@ _nanosleep(const struct timespec * time_to_sleep, } int -nanosleep(const struct timespec * time_to_sleep, struct timespec * +__nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { int ret; @@ -139,4 +141,3 @@ nanosleep(const struct timespec * time_to_sleep, struct timespec * return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_once.c b/lib/libc_r/uthread/uthread_once.c index d7bb494..dde5474 100644 --- a/lib/libc_r/uthread/uthread_once.c +++ b/lib/libc_r/uthread/uthread_once.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_once=_pthread_once + int -pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) +_pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) { if (once_control->state == PTHREAD_NEEDS_INIT) { if (_thread_initial == NULL) @@ -50,4 +51,3 @@ pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) } return (0); } -#endif diff --git a/lib/libc_r/uthread/uthread_open.c b/lib/libc_r/uthread/uthread_open.c index e7fef91..37bd43d 100644 --- a/lib/libc_r/uthread/uthread_open.c +++ b/lib/libc_r/uthread/uthread_open.c @@ -37,10 +37,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak open=_open + int _open(const char *path, int flags,...) { @@ -56,12 +57,12 @@ _open(const char *path, int flags,...) va_end(ap); } /* Open the file: */ - if ((fd = _thread_sys_open(path, flags, mode)) < 0) { + if ((fd = __sys_open(path, flags, mode)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(fd) != 0) { /* Quietly close the file: */ - _thread_sys_close(fd); + __sys_close(fd); /* Reset the file descriptor: */ fd = -1; @@ -72,7 +73,7 @@ _open(const char *path, int flags,...) } int -open(const char *path, int flags,...) +__open(const char *path, int flags,...) { int ret; int mode = 0; @@ -93,4 +94,3 @@ open(const char *path, int flags,...) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_pause.c b/lib/libc_r/uthread/uthread_pause.c index 09e4c17..19501df 100644 --- a/lib/libc_r/uthread/uthread_pause.c +++ b/lib/libc_r/uthread/uthread_pause.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pause=_pause + int -pause(void) +_pause(void) { int ret; @@ -45,4 +46,3 @@ pause(void) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_pipe.c b/lib/libc_r/uthread/uthread_pipe.c index 9767742..c6274cb 100644 --- a/lib/libc_r/uthread/uthread_pipe.c +++ b/lib/libc_r/uthread/uthread_pipe.c @@ -33,24 +33,22 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pipe=_pipe + int _pipe(int fds[2]) { int ret; - if ((ret = _thread_sys_pipe(fds)) >= 0) { + if ((ret = __sys_pipe(fds)) >= 0) { if (_thread_fd_table_init(fds[0]) != 0 || _thread_fd_table_init(fds[1]) != 0) { - _thread_sys_close(fds[0]); - _thread_sys_close(fds[1]); + __sys_close(fds[0]); + __sys_close(fds[1]); ret = -1; } } return (ret); } - -__strong_reference(_pipe, pipe); -#endif diff --git a/lib/libc_r/uthread/uthread_poll.c b/lib/libc_r/uthread/uthread_poll.c index 3717273..d3f631a 100644 --- a/lib/libc_r/uthread/uthread_poll.c +++ b/lib/libc_r/uthread/uthread_poll.c @@ -38,14 +38,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak poll=_poll int _poll(struct pollfd *fds, unsigned int nfds, int timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int numfds = nfds; int i, ret = 0; @@ -71,7 +72,7 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (-1); } - if (((ret = _thread_sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { + if (((ret = __sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { data.nfds = numfds; data.fds = fds; @@ -83,10 +84,10 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) fds[i].revents = 0; } - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_POLL_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; } else { @@ -96,6 +97,3 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (ret); } - -__strong_reference(_poll, poll); -#endif diff --git a/lib/libc_r/uthread/uthread_priority_queue.c b/lib/libc_r/uthread/uthread_priority_queue.c index 84c3065..55d742b 100644 --- a/lib/libc_r/uthread/uthread_priority_queue.c +++ b/lib/libc_r/uthread/uthread_priority_queue.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -71,7 +70,7 @@ static int _pq_active = 0; } while (0) #define _PQ_ASSERT_PROTECTED(msg) \ PTHREAD_ASSERT((_thread_kern_in_sched != 0) || \ - (_thread_run->sig_defer_count > 0) || \ + ((_get_curthread())->sig_defer_count > 0) ||\ (_sig_in_handler != 0), msg); #else @@ -336,4 +335,3 @@ _waitq_clearactive(void) _PQ_ASSERT_ACTIVE("_waitq_clearactive: ! pq_active"); _PQ_CLEAR_ACTIVE(); } -#endif diff --git a/lib/libc_r/uthread/uthread_read.c b/lib/libc_r/uthread/uthread_read.c index b4d9694..5f5475d 100644 --- a/lib/libc_r/uthread/uthread_read.c +++ b/lib/libc_r/uthread/uthread_read.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak read=__read + ssize_t _read(int fd, void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -66,14 +68,14 @@ _read(int fd, void *buf, size_t nbytes) } /* Perform a non-blocking read syscall: */ - while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) { + while ((ret = __sys_read(fd, buf, nbytes)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -82,7 +84,7 @@ _read(int fd, void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -97,7 +99,7 @@ _read(int fd, void *buf, size_t nbytes) } ssize_t -read(int fd, void *buf, size_t nbytes) +__read(int fd, void *buf, size_t nbytes) { ssize_t ret; @@ -107,4 +109,3 @@ read(int fd, void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_readv.c b/lib/libc_r/uthread/uthread_readv.c index d8e02c0..2e14a9f 100644 --- a/lib/libc_r/uthread/uthread_readv.c +++ b/lib/libc_r/uthread/uthread_readv.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak readv=_readv + ssize_t _readv(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -61,14 +63,14 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } /* Perform a non-blocking readv syscall: */ - while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) { + while ((ret = __sys_readv(fd, iov, iovcnt)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -77,7 +79,7 @@ _readv(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -90,6 +92,3 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } return (ret); } - -__strong_reference(_readv, readv); -#endif diff --git a/lib/libc_r/uthread/uthread_recvfrom.c b/lib/libc_r/uthread/uthread_recvfrom.c index 8fcf7a6..6e2f40d 100644 --- a/lib/libc_r/uthread/uthread_recvfrom.c +++ b/lib/libc_r/uthread/uthread_recvfrom.c @@ -35,28 +35,30 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak recvfrom=_recvfrom + ssize_t _recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, socklen_t *from_len) { + struct pthread *curthread = _get_curthread(); int ret; if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { - while ((ret = _thread_sys_recvfrom(fd, buf, len, flags, from, from_len)) < 0) { + while ((ret = __sys_recvfrom(fd, buf, len, flags, from, from_len)) < 0) { if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; @@ -71,6 +73,3 @@ _recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, } return (ret); } - -__strong_reference(_recvfrom, recvfrom); -#endif diff --git a/lib/libc_r/uthread/uthread_recvmsg.c b/lib/libc_r/uthread/uthread_recvmsg.c index da15d99..930671f 100644 --- a/lib/libc_r/uthread/uthread_recvmsg.c +++ b/lib/libc_r/uthread/uthread_recvmsg.c @@ -35,27 +35,29 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak recvmsg=_recvmsg + ssize_t _recvmsg(int fd, struct msghdr *msg, int flags) { + struct pthread *curthread = _get_curthread(); int ret; if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { - while ((ret = _thread_sys_recvmsg(fd, msg, flags)) < 0) { + while ((ret = __sys_recvmsg(fd, msg, flags)) < 0) { if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; @@ -70,6 +72,3 @@ _recvmsg(int fd, struct msghdr *msg, int flags) } return (ret); } - -__strong_reference(_recvmsg, recvmsg); -#endif diff --git a/lib/libc_r/uthread/uthread_resume_np.c b/lib/libc_r/uthread/uthread_resume_np.c index 32b3d05..0d4738d 100644 --- a/lib/libc_r/uthread/uthread_resume_np.c +++ b/lib/libc_r/uthread/uthread_resume_np.c @@ -32,13 +32,14 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_resume_np=_pthread_resume_np + /* Resume a thread: */ int -pthread_resume_np(pthread_t thread) +_pthread_resume_np(pthread_t thread) { int ret; enum pthread_susp old_suspended; @@ -89,4 +90,3 @@ pthread_resume_np(pthread_t thread) } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_rwlock.c b/lib/libc_r/uthread/uthread_rwlock.c index 648e8a3..9c87e44 100644 --- a/lib/libc_r/uthread/uthread_rwlock.c +++ b/lib/libc_r/uthread/uthread_rwlock.c @@ -26,7 +26,6 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include @@ -37,6 +36,14 @@ /* maximum number of times a read lock may be obtained */ #define MAX_READ_LOCKS (INT_MAX - 1) +#pragma weak pthread_rwlock_destroy=_pthread_rwlock_destroy +#pragma weak pthread_rwlock_init=_pthread_rwlock_init +#pragma weak pthread_rwlock_rdlock=_pthread_rwlock_rdlock +#pragma weak pthread_rwlock_tryrdlock=_pthread_rwlock_tryrdlock +#pragma weak pthread_rwlock_trywrlock=_pthread_rwlock_trywrlock +#pragma weak pthread_rwlock_unlock=_pthread_rwlock_unlock +#pragma weak pthread_rwlock_wrlock=_pthread_rwlock_wrlock + static int init_static (pthread_rwlock_t *rwlock); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; @@ -59,7 +66,7 @@ init_static (pthread_rwlock_t *rwlock) } int -pthread_rwlock_destroy (pthread_rwlock_t *rwlock) +_pthread_rwlock_destroy (pthread_rwlock_t *rwlock) { int ret; @@ -84,7 +91,7 @@ pthread_rwlock_destroy (pthread_rwlock_t *rwlock) } int -pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) +_pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { pthread_rwlock_t prwlock; int ret; @@ -127,7 +134,7 @@ pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) } int -pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -178,7 +185,7 @@ pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -215,7 +222,7 @@ pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -250,7 +257,7 @@ pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_unlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_unlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -287,7 +294,7 @@ pthread_rwlock_unlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -332,4 +339,3 @@ pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) return(ret); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libc_r/uthread/uthread_rwlockattr.c b/lib/libc_r/uthread/uthread_rwlockattr.c index 37eb3ea..7dcaaeb 100644 --- a/lib/libc_r/uthread/uthread_rwlockattr.c +++ b/lib/libc_r/uthread/uthread_rwlockattr.c @@ -26,15 +26,19 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include #include "pthread_private.h" +#pragma weak pthread_rwlockattr_destroy=_pthread_rwlockattr_destroy +#pragma weak pthread_rwlockattr_getpshared=_pthread_rwlockattr_getpshared +#pragma weak pthread_rwlockattr_init=_pthread_rwlockattr_init +#pragma weak pthread_rwlockattr_setpshared=_pthread_rwlockattr_setpshared + int -pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -52,7 +56,7 @@ pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, +_pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, int *pshared) { *pshared = (*rwlockattr)->pshared; @@ -61,7 +65,7 @@ pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, } int -pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -81,7 +85,7 @@ pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) +_pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) { /* Only PTHREAD_PROCESS_PRIVATE is supported. */ if (pshared != PTHREAD_PROCESS_PRIVATE) @@ -92,4 +96,3 @@ pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) return(0); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libc_r/uthread/uthread_select.c b/lib/libc_r/uthread/uthread_select.c index 0a7c0d4..547046a 100644 --- a/lib/libc_r/uthread/uthread_select.c +++ b/lib/libc_r/uthread/uthread_select.c @@ -40,14 +40,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak select=_select + int _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int i, ret = 0, f_wait = 1; int pfd_index, got_one = 0, fd_count = 0; @@ -91,9 +93,9 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * Allocate memory for poll data if it hasn't already been * allocated or if previously allocated memory is insufficient. */ - if ((_thread_run->poll_data.fds == NULL) || - (_thread_run->poll_data.nfds < fd_count)) { - data.fds = (struct pollfd *) realloc(_thread_run->poll_data.fds, + if ((curthread->poll_data.fds == NULL) || + (curthread->poll_data.nfds < fd_count)) { + data.fds = (struct pollfd *) realloc(curthread->poll_data.fds, sizeof(struct pollfd) * MAX(128, fd_count)); if (data.fds == NULL) { errno = ENOMEM; @@ -105,13 +107,13 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * indicates what is allocated, not what is * currently being polled. */ - _thread_run->poll_data.fds = data.fds; - _thread_run->poll_data.nfds = MAX(128, fd_count); + curthread->poll_data.fds = data.fds; + curthread->poll_data.nfds = MAX(128, fd_count); } } if (ret == 0) { /* Setup the wait data. */ - data.fds = _thread_run->poll_data.fds; + data.fds = curthread->poll_data.fds; data.nfds = fd_count; /* @@ -142,12 +144,12 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, pfd_index--; } } - if (((ret = _thread_sys_poll(data.fds, data.nfds, 0)) == 0) && + if (((ret = __sys_poll(data.fds, data.nfds, 0)) == 0) && (f_wait != 0)) { - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_SELECT_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; data.nfds = 0; ret = -1; @@ -203,6 +205,3 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, return (ret); } - -__strong_reference(_select, select); -#endif diff --git a/lib/libc_r/uthread/uthread_self.c b/lib/libc_r/uthread/uthread_self.c index 81ec427..3c92daa 100644 --- a/lib/libc_r/uthread/uthread_self.c +++ b/lib/libc_r/uthread/uthread_self.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_self=_pthread_self + pthread_t -pthread_self(void) +_pthread_self(void) { /* Return the running thread pointer: */ - return (_thread_run); + return (_get_curthread()); } -#endif diff --git a/lib/libc_r/uthread/uthread_sem.c b/lib/libc_r/uthread/uthread_sem.c index eb42919..926f498 100644 --- a/lib/libc_r/uthread/uthread_sem.c +++ b/lib/libc_r/uthread/uthread_sem.c @@ -31,7 +31,6 @@ #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -43,8 +42,19 @@ goto RETURN; \ } +#pragma weak sem_init=_sem_init +#pragma weak sem_destroy=_sem_destroy +#pragma weak sem_open=_sem_open +#pragma weak sem_close=_sem_close +#pragma weak sem_unlink=_sem_unlink +#pragma weak sem_wait=_sem_wait +#pragma weak sem_trywait=_sem_trywait +#pragma weak sem_post=_sem_post +#pragma weak sem_getvalue=_sem_getvalue + + int -sem_init(sem_t *sem, int pshared, unsigned int value) +_sem_init(sem_t *sem, int pshared, unsigned int value) { int retval; @@ -103,7 +113,7 @@ sem_init(sem_t *sem, int pshared, unsigned int value) } int -sem_destroy(sem_t *sem) +_sem_destroy(sem_t *sem) { int retval; @@ -131,28 +141,28 @@ sem_destroy(sem_t *sem) } sem_t * -sem_open(const char *name, int oflag, ...) +_sem_open(const char *name, int oflag, ...) { errno = ENOSYS; return SEM_FAILED; } int -sem_close(sem_t *sem) +_sem_close(sem_t *sem) { errno = ENOSYS; return -1; } int -sem_unlink(const char *name) +_sem_unlink(const char *name) { errno = ENOSYS; return -1; } int -sem_wait(sem_t *sem) +_sem_wait(sem_t *sem) { int retval; @@ -178,7 +188,7 @@ sem_wait(sem_t *sem) } int -sem_trywait(sem_t *sem) +_sem_trywait(sem_t *sem) { int retval; @@ -201,7 +211,7 @@ sem_trywait(sem_t *sem) } int -sem_post(sem_t *sem) +_sem_post(sem_t *sem) { int retval; @@ -235,7 +245,7 @@ sem_post(sem_t *sem) } int -sem_getvalue(sem_t *sem, int *sval) +_sem_getvalue(sem_t *sem, int *sval) { int retval; @@ -249,4 +259,3 @@ sem_getvalue(sem_t *sem, int *sval) RETURN: return retval; } -#endif diff --git a/lib/libc_r/uthread/uthread_sendfile.c b/lib/libc_r/uthread/uthread_sendfile.c index 51f4247..cb6f5d7 100644 --- a/lib/libc_r/uthread/uthread_sendfile.c +++ b/lib/libc_r/uthread/uthread_sendfile.c @@ -34,14 +34,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sendfile=_sendfile + int -sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, +_sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, off_t *sbytes, int flags) { + struct pthread *curthread = _get_curthread(); int ret, type, blocking; ssize_t wvret, num = 0; off_t n, nwritten = 0; @@ -92,7 +94,7 @@ sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, */ for (;;) { /* Perform a non-blocking sendfile syscall. */ - ret = _thread_sys_sendfile(fd, s, offset + num, nbytes - num, + ret = __sys_sendfile(fd, s, offset + num, nbytes - num, NULL, &n, flags); if (ret == 0) { @@ -108,16 +110,16 @@ sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, /* Update the count of bytes written. */ num += n; - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag. */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Interrupted by a signal. Return an error. */ break; } @@ -151,4 +153,3 @@ sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, } return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_sendmsg.c b/lib/libc_r/uthread/uthread_sendmsg.c index 13da2eb..cb3970c 100644 --- a/lib/libc_r/uthread/uthread_sendmsg.c +++ b/lib/libc_r/uthread/uthread_sendmsg.c @@ -35,27 +35,29 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sendmsg=_sendmsg + ssize_t _sendmsg(int fd, const struct msghdr *msg, int flags) { + struct pthread *curthread = _get_curthread(); int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - while ((ret = _thread_sys_sendmsg(fd, msg, flags)) < 0) { + while ((ret = __sys_sendmsg(fd, msg, flags)) < 0) { if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); /* Check if the operation was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -69,6 +71,3 @@ _sendmsg(int fd, const struct msghdr *msg, int flags) } return (ret); } - -__strong_reference(_sendmsg, sendmsg); -#endif diff --git a/lib/libc_r/uthread/uthread_sendto.c b/lib/libc_r/uthread/uthread_sendto.c index f830c5a..c1420f1 100644 --- a/lib/libc_r/uthread/uthread_sendto.c +++ b/lib/libc_r/uthread/uthread_sendto.c @@ -35,28 +35,30 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sendto=_sendto + ssize_t _sendto(int fd, const void *msg, size_t len, int flags, const struct sockaddr * to, socklen_t to_len) { + struct pthread *curthread = _get_curthread(); int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - while ((ret = _thread_sys_sendto(fd, msg, len, flags, to, to_len)) < 0) { + while ((ret = __sys_sendto(fd, msg, len, flags, to, to_len)) < 0) { if (!(_thread_fd_table[fd]->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); /* Check if the operation was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -70,6 +72,3 @@ _sendto(int fd, const void *msg, size_t len, int flags, const struct } return (ret); } - -__strong_reference(_sendto, sendto); -#endif diff --git a/lib/libc_r/uthread/uthread_seterrno.c b/lib/libc_r/uthread/uthread_seterrno.c index 1934aac..0d9474f 100644 --- a/lib/libc_r/uthread/uthread_seterrno.c +++ b/lib/libc_r/uthread/uthread_seterrno.c @@ -31,7 +31,6 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -58,4 +57,3 @@ _thread_seterrno(pthread_t thread, int error) */ thread->error = error; } -#endif diff --git a/lib/libc_r/uthread/uthread_setprio.c b/lib/libc_r/uthread/uthread_setprio.c index c630db5..eee05dc 100644 --- a/lib/libc_r/uthread/uthread_setprio.c +++ b/lib/libc_r/uthread/uthread_setprio.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setprio=_pthread_setprio + int -pthread_setprio(pthread_t pthread, int prio) +_pthread_setprio(pthread_t pthread, int prio) { int ret, policy; struct sched_param param; @@ -49,4 +50,3 @@ pthread_setprio(pthread_t pthread, int prio) /* Return the error status: */ return (ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_setschedparam.c b/lib/libc_r/uthread/uthread_setschedparam.c index 9a44ad7..a03f036 100644 --- a/lib/libc_r/uthread/uthread_setschedparam.c +++ b/lib/libc_r/uthread/uthread_setschedparam.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setschedparam=_pthread_setschedparam + int -pthread_setschedparam(pthread_t pthread, int policy, +_pthread_setschedparam(pthread_t pthread, int policy, const struct sched_param *param) { int old_prio, in_readyq = 0, ret = 0; @@ -116,4 +117,3 @@ pthread_setschedparam(pthread_t pthread, int policy, } return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_setsockopt.c b/lib/libc_r/uthread/uthread_setsockopt.c index f50a708..f2e1a13 100644 --- a/lib/libc_r/uthread/uthread_setsockopt.c +++ b/lib/libc_r/uthread/uthread_setsockopt.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak setsockopt=_setsockopt + int _setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) @@ -44,11 +45,8 @@ _setsockopt(int fd, int level, int optname, const void *optval, socklen_t int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_setsockopt(fd, level, optname, optval, optlen); + ret = __sys_setsockopt(fd, level, optname, optval, optlen); _FD_UNLOCK(fd, FD_RDWR); } return ret; } - -__strong_reference(_setsockopt, setsockopt); -#endif diff --git a/lib/libc_r/uthread/uthread_shutdown.c b/lib/libc_r/uthread/uthread_shutdown.c index c4b08ff7..7efd4f0 100644 --- a/lib/libc_r/uthread/uthread_shutdown.c +++ b/lib/libc_r/uthread/uthread_shutdown.c @@ -34,10 +34,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak shutdown=_shutdown + int _shutdown(int fd, int how) { @@ -46,19 +47,19 @@ _shutdown(int fd, int how) switch (how) { case 0: if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { - ret = _thread_sys_shutdown(fd, how); + ret = __sys_shutdown(fd, how); _FD_UNLOCK(fd, FD_READ); } break; case 1: if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { - ret = _thread_sys_shutdown(fd, how); + ret = __sys_shutdown(fd, how); _FD_UNLOCK(fd, FD_WRITE); } break; case 2: if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_shutdown(fd, how); + ret = __sys_shutdown(fd, how); _FD_UNLOCK(fd, FD_RDWR); } break; @@ -69,6 +70,3 @@ _shutdown(int fd, int how) } return (ret); } - -__strong_reference(_shutdown, shutdown); -#endif diff --git a/lib/libc_r/uthread/uthread_sig.c b/lib/libc_r/uthread/uthread_sig.c index a855a61..9559bb7 100644 --- a/lib/libc_r/uthread/uthread_sig.c +++ b/lib/libc_r/uthread/uthread_sig.c @@ -39,7 +39,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -70,6 +69,7 @@ static void thread_sigframe_save(pthread_t thread, struct pthread_signal_frame * void _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) { + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; void *stackp; int in_sched = 0; @@ -77,7 +77,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) if (ucp == NULL) PANIC("Thread signal handler received null context"); - DBG_MSG("Got signal %d, current thread %p\n", sig, _thread_run); + DBG_MSG("Got signal %d, current thread %p\n", sig, curthread); if (_thread_kern_in_sched != 0) in_sched = 1; @@ -104,13 +104,13 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * the currently running thread has deferred thread * signals. */ - else if (_thread_run->sig_defer_count > 0) - _thread_run->yield_on_sig_undefer = 1; + else if (curthread->sig_defer_count > 0) + curthread->yield_on_sig_undefer = 1; else { /* * Save the context of the currently running thread: */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* * Schedule the next thread. This function is not @@ -131,7 +131,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * is accessing the scheduling queues or if there is a currently * running thread that has deferred signals. */ - else if ((in_sched != 0) || (_thread_run->sig_defer_count > 0)) { + else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) { /* Cast the signal number to a character variable: */ c = sig; @@ -140,7 +140,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * be ready to read when this signal handler returns. */ if (_queue_signals != 0) { - _thread_sys_write(_thread_kern_pipe[1], &c, 1); + __sys_write(_thread_kern_pipe[1], &c, 1); DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig); } if (_thread_sigq[sig - 1].blocked == 0) { @@ -221,7 +221,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * This also applies if the current thread is the * thread to be signaled. */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* Setup the target thread to receive the signal: */ thread_sig_add(pthread, sig, /*has_args*/ 1); @@ -241,8 +241,8 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * signal and the currently running thread is not in a * signal handler. */ - if ((pthread == _thread_run) || ((pthread_h != NULL) && - (pthread_h->active_priority > _thread_run->active_priority))) { + if ((pthread == curthread) || ((pthread_h != NULL) && + (pthread_h->active_priority > curthread->active_priority))) { /* Enter the kernel scheduler: */ _thread_kern_sched(ucp); } @@ -272,6 +272,7 @@ thread_sig_savecontext(pthread_t pthread, ucontext_t *ucp) pthread_t thread_sig_find(int sig) { + struct pthread *curthread = _get_curthread(); int handler_installed; pthread_t pthread, pthread_next; pthread_t suspended_thread, signaled_thread; @@ -302,9 +303,9 @@ thread_sig_find(int sig) * installed, the signal only affects threads in sigwait. */ suspended_thread = NULL; - if ((_thread_run != &_thread_kern_thread) && - !sigismember(&_thread_run->sigmask, sig)) - signaled_thread = _thread_run; + if ((curthread != &_thread_kern_thread) && + !sigismember(&curthread->sigmask, sig)) + signaled_thread = curthread; else signaled_thread = NULL; if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) || @@ -508,7 +509,7 @@ thread_sig_handle_special(int sig) /* * Set the file descriptor to non-blocking: */ - _thread_sys_fcntl(i, F_SETFL, + __sys_fcntl(i, F_SETFL, _thread_fd_table[i]->flags | O_NONBLOCK); } } @@ -568,6 +569,7 @@ thread_sig_handle_special(int sig) static void thread_sig_add(pthread_t pthread, int sig, int has_args) { + struct pthread *curthread = _get_curthread(); int restart; int suppress_handler = 0; @@ -751,7 +753,7 @@ thread_sig_add(pthread_t pthread, int sig, int has_args) * the thread in the run queue. */ pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY; - if (pthread != _thread_run) + if (pthread != curthread) PTHREAD_PRIOQ_INSERT_TAIL(pthread); } } @@ -849,6 +851,8 @@ thread_sig_check_state(pthread_t pthread, int sig) void _thread_sig_send(pthread_t pthread, int sig) { + struct pthread *curthread = _get_curthread(); + /* Check for signals whose actions are SIG_DFL: */ if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) { /* @@ -878,7 +882,7 @@ _thread_sig_send(pthread_t pthread, int sig) /* Return the signal number: */ pthread->signo = sig; - } else if (pthread == _thread_run) { + } else if (pthread == curthread) { /* Add the signal to the pending set: */ sigaddset(&pthread->sigpend, sig); if (!sigismember(&pthread->sigmask, sig)) { @@ -915,9 +919,7 @@ _thread_sig_wrapper(void) { void (*sigfunc)(int, siginfo_t *, void *); struct pthread_signal_frame *psf; - pthread_t thread; - - thread = _thread_run; + struct pthread *thread = _get_curthread(); /* Get the current frame and state: */ psf = thread->curframe; @@ -1117,4 +1119,3 @@ thread_sigframe_save(pthread_t thread, struct pthread_signal_frame *psf) psf->saved_state.psd_sig_defer_count = thread->sig_defer_count; } -#endif diff --git a/lib/libc_r/uthread/uthread_sigaction.c b/lib/libc_r/uthread/uthread_sigaction.c index 4d13819..dba2f7e 100644 --- a/lib/libc_r/uthread/uthread_sigaction.c +++ b/lib/libc_r/uthread/uthread_sigaction.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigaction=_sigaction + int _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) { @@ -49,6 +50,9 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) errno = EINVAL; ret = -1; } else { + if (_thread_initial == NULL) + _thread_init(); + /* * Check if the existing signal action structure contents are * to be returned: @@ -98,7 +102,7 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) gact.sa_handler = (void (*) ()) _thread_sig_handler; /* Change the signal action in the kernel: */ - if (_thread_sys_sigaction(sig,&gact,NULL) != 0) + if (__sys_sigaction(sig,&gact,NULL) != 0) ret = -1; } } @@ -106,6 +110,3 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) /* Return the completion status: */ return (ret); } - -__strong_reference(_sigaction, sigaction); -#endif diff --git a/lib/libc_r/uthread/uthread_sigblock.c b/lib/libc_r/uthread/uthread_sigblock.c index 986379b..b1adf782 100644 --- a/lib/libc_r/uthread/uthread_sigblock.c +++ b/lib/libc_r/uthread/uthread_sigblock.c @@ -32,18 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" int -_thread_sys_sigblock(int mask) +__sys_sigblock(int mask) { int omask, n; - n = _thread_sys_sigprocmask(SIG_BLOCK, (sigset_t *) & mask, (sigset_t *) & omask); + n = __sys_sigprocmask(SIG_BLOCK, (sigset_t *) & mask, (sigset_t *) & omask); if (n) return (n); return (omask); } -#endif diff --git a/lib/libc_r/uthread/uthread_sigmask.c b/lib/libc_r/uthread/uthread_sigmask.c index 53d0774..c16d66b 100644 --- a/lib/libc_r/uthread/uthread_sigmask.c +++ b/lib/libc_r/uthread/uthread_sigmask.c @@ -36,20 +36,22 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_sigmask=_pthread_sigmask + int -pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) +_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { + struct pthread *curthread = _get_curthread(); sigset_t sigset; int ret = 0; /* Check if the existing signal process mask is to be returned: */ if (oset != NULL) { /* Return the current mask: */ - *oset = _thread_run->sigmask; + *oset = curthread->sigmask; } /* Check if a new signal set was provided by the caller: */ if (set != NULL) { @@ -58,19 +60,19 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Block signals: */ case SIG_BLOCK: /* Add signals to the existing mask: */ - SIGSETOR(_thread_run->sigmask, *set); + SIGSETOR(curthread->sigmask, *set); break; /* Unblock signals: */ case SIG_UNBLOCK: /* Clear signals from the existing mask: */ - SIGSETNAND(_thread_run->sigmask, *set); + SIGSETNAND(curthread->sigmask, *set); break; /* Set the signal process mask: */ case SIG_SETMASK: /* Set the new mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; break; /* Trap invalid actions: */ @@ -82,15 +84,15 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) } /* Increment the sequence number: */ - _thread_run->sigmask_seqno++; + curthread->sigmask_seqno++; /* * Check if there are pending signals for the running * thread or process that aren't blocked: */ - sigset = _thread_run->sigpend; + sigset = curthread->sigpend; SIGSETOR(sigset, _process_sigpending); - SIGSETNAND(sigset, _thread_run->sigmask); + SIGSETNAND(sigset, curthread->sigmask); if (SIGNOTEMPTY(sigset)) /* * Call the kernel scheduler which will safely @@ -102,4 +104,3 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_signal.c b/lib/libc_r/uthread/uthread_signal.c index ff4ce88..32f572a 100644 --- a/lib/libc_r/uthread/uthread_signal.c +++ b/lib/libc_r/uthread/uthread_signal.c @@ -32,12 +32,11 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" sig_t -_thread_sys_signal(int s, sig_t a) +__sys_signal(int s, sig_t a) { struct sigaction sa; struct sigaction osa; @@ -48,11 +47,10 @@ _thread_sys_signal(int s, sig_t a) sa.sa_flags = SA_SIGINFO; /* Perform the sigaction syscall: */ - if (_thread_sys_sigaction(s, &sa, &osa) < 0) { + if (__sys_sigaction(s, &sa, &osa) < 0) { /* Return an error: */ return (SIG_ERR); } /* Return a pointer to the old signal handler: */ return (osa.sa_handler); } -#endif diff --git a/lib/libc_r/uthread/uthread_sigpending.c b/lib/libc_r/uthread/uthread_sigpending.c index a118347..98b7d26 100644 --- a/lib/libc_r/uthread/uthread_sigpending.c +++ b/lib/libc_r/uthread/uthread_sigpending.c @@ -33,13 +33,15 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigpending=_sigpending + int _sigpending(sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = 0; /* Check for a null signal set pointer: */ @@ -48,11 +50,8 @@ _sigpending(sigset_t * set) ret = EINVAL; } else { - *set = _thread_run->sigpend; + *set = curthread->sigpend; } /* Return the completion status: */ return (ret); } - -__strong_reference(_sigpending, sigpending); -#endif diff --git a/lib/libc_r/uthread/uthread_sigprocmask.c b/lib/libc_r/uthread/uthread_sigprocmask.c index f8ca031..f93d463 100644 --- a/lib/libc_r/uthread/uthread_sigprocmask.c +++ b/lib/libc_r/uthread/uthread_sigprocmask.c @@ -36,15 +36,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigprocmask=_sigprocmask + int _sigprocmask(int how, const sigset_t *set, sigset_t *oset) { return (pthread_sigmask(how, set, oset)); } - -__strong_reference(_sigprocmask, sigprocmask); -#endif diff --git a/lib/libc_r/uthread/uthread_sigsetmask.c b/lib/libc_r/uthread/uthread_sigsetmask.c index b02196b..4134e4c 100644 --- a/lib/libc_r/uthread/uthread_sigsetmask.c +++ b/lib/libc_r/uthread/uthread_sigsetmask.c @@ -32,18 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" int -_thread_sys_sigsetmask(int mask) +__sys_sigsetmask(int mask) { int omask, n; - n = _thread_sys_sigprocmask(SIG_SETMASK, (sigset_t *) & mask, (sigset_t *) & omask); + n = __sys_sigprocmask(SIG_SETMASK, (sigset_t *) & mask, (sigset_t *) & omask); if (n) return (n); return (omask); } -#endif diff --git a/lib/libc_r/uthread/uthread_sigsuspend.c b/lib/libc_r/uthread/uthread_sigsuspend.c index 28959c8..f0a08e2 100644 --- a/lib/libc_r/uthread/uthread_sigsuspend.c +++ b/lib/libc_r/uthread/uthread_sigsuspend.c @@ -33,23 +33,25 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigsuspend=__sigsuspend + int _sigsuspend(const sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = -1; sigset_t oset; /* Check if a new signal set was provided by the caller: */ if (set != NULL) { /* Save the current signal mask: */ - oset = _thread_run->sigmask; + oset = curthread->sigmask; /* Change the caller's mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGSUSPEND, __FILE__, __LINE__); @@ -58,7 +60,7 @@ _sigsuspend(const sigset_t * set) errno = EINTR; /* Restore the signal mask: */ - _thread_run->sigmask = oset; + curthread->sigmask = oset; } else { /* Return an invalid argument error: */ errno = EINVAL; @@ -69,7 +71,7 @@ _sigsuspend(const sigset_t * set) } int -sigsuspend(const sigset_t * set) +__sigsuspend(const sigset_t * set) { int ret; @@ -79,4 +81,3 @@ sigsuspend(const sigset_t * set) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_sigwait.c b/lib/libc_r/uthread/uthread_sigwait.c index f2c6ea1..71fe007 100644 --- a/lib/libc_r/uthread/uthread_sigwait.c +++ b/lib/libc_r/uthread/uthread_sigwait.c @@ -35,13 +35,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigwait=_sigwait + int -sigwait(const sigset_t *set, int *sig) +_sigwait(const sigset_t *set, int *sig) { + struct pthread *curthread = _get_curthread(); int ret = 0; int i; sigset_t tempset, waitset; @@ -69,7 +71,7 @@ sigwait(const sigset_t *set, int *sig) sigdelset(&waitset, SIGINFO); /* Check to see if a pending signal is in the wait mask. */ - tempset = _thread_run->sigpend; + tempset = curthread->sigpend; SIGSETOR(tempset, _process_sigpending); SIGSETAND(tempset, waitset); if (SIGNOTEMPTY(tempset)) { @@ -80,8 +82,8 @@ sigwait(const sigset_t *set, int *sig) } /* Clear the pending signal: */ - if (sigismember(&_thread_run->sigpend,i)) - sigdelset(&_thread_run->sigpend,i); + if (sigismember(&curthread->sigpend,i)) + sigdelset(&curthread->sigpend,i); else sigdelset(&_process_sigpending,i); @@ -115,7 +117,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]++; sigaddset(&tempset, i); if (_thread_dfl_count[i] == 1) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -129,19 +131,19 @@ sigwait(const sigset_t *set, int *sig) * mask is independent of the threads signal mask * and requires separate storage. */ - _thread_run->data.sigwait = &waitset; + curthread->data.sigwait = &waitset; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__); /* Return the signal number to the caller: */ - *sig = _thread_run->signo; + *sig = curthread->signo; /* * 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; + curthread->data.sigwait = NULL; } /* @@ -157,7 +159,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]--; if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) && (_thread_dfl_count[i] == 0)) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -170,4 +172,3 @@ sigwait(const sigset_t *set, int *sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_single_np.c b/lib/libc_r/uthread/uthread_single_np.c index d6ecb48..52cd6ed 100644 --- a/lib/libc_r/uthread/uthread_single_np.c +++ b/lib/libc_r/uthread/uthread_single_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_single_np() +#pragma weak pthread_single_np=_pthread_single_np + +int _pthread_single_np() { + struct pthread *curthread = _get_curthread(); + /* Enter single-threaded (non-POSIX) scheduling mode: */ - _thread_single = _thread_run; + _thread_single = curthread; return(0); } -#endif diff --git a/lib/libc_r/uthread/uthread_sleep.c b/lib/libc_r/uthread/uthread_sleep.c index fdfeeec..acc6e55 100644 --- a/lib/libc_r/uthread/uthread_sleep.c +++ b/lib/libc_r/uthread/uthread_sleep.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sleep=_sleep + unsigned int -sleep(unsigned int seconds) +_sleep(unsigned int seconds) { unsigned int ret; @@ -45,4 +46,3 @@ sleep(unsigned int seconds) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_socket.c b/lib/libc_r/uthread/uthread_socket.c index 5cd8f75..cbbbe82 100644 --- a/lib/libc_r/uthread/uthread_socket.c +++ b/lib/libc_r/uthread/uthread_socket.c @@ -35,26 +35,24 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak socket=_socket + int _socket(int af, int type, int protocol) { int fd; /* Create a socket: */ - if ((fd = _thread_sys_socket(af, type, protocol)) < 0) { + if ((fd = __sys_socket(af, type, protocol)) < 0) { /* Error creating socket. */ /* Initialise the entry in the file descriptor table: */ } else if (_thread_fd_table_init(fd) != 0) { - _thread_sys_close(fd); + __sys_close(fd); fd = -1; } return (fd); } - -__strong_reference(_socket, socket); -#endif diff --git a/lib/libc_r/uthread/uthread_socketpair.c b/lib/libc_r/uthread/uthread_socketpair.c index f54256d..f10a1c6 100644 --- a/lib/libc_r/uthread/uthread_socketpair.c +++ b/lib/libc_r/uthread/uthread_socketpair.c @@ -36,23 +36,21 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak socketpair=_socketpair + int _socketpair(int af, int type, int protocol, int pair[2]) { int ret; - if (!((ret = _thread_sys_socketpair(af, type, protocol, pair)) < 0)) + if (!((ret = __sys_socketpair(af, type, protocol, pair)) < 0)) if (_thread_fd_table_init(pair[0]) != 0 || _thread_fd_table_init(pair[1]) != 0) { - _thread_sys_close(pair[0]); - _thread_sys_close(pair[1]); + __sys_close(pair[0]); + __sys_close(pair[1]); ret = -1; } return (ret); } - -__strong_reference(_socketpair, socketpair); -#endif diff --git a/lib/libc_r/uthread/uthread_spec.c b/lib/libc_r/uthread/uthread_spec.c index 1485cbb..783617f 100644 --- a/lib/libc_r/uthread/uthread_spec.c +++ b/lib/libc_r/uthread/uthread_spec.c @@ -35,15 +35,20 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" /* Static variables: */ static struct pthread_key key_table[PTHREAD_KEYS_MAX]; +#pragma weak pthread_key_create=_pthread_key_create +#pragma weak pthread_key_delete=_pthread_key_delete +#pragma weak pthread_getspecific=_pthread_getspecific +#pragma weak pthread_setspecific=_pthread_setspecific + + int -pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) +_pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) { for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) { /* Lock the key table entry: */ @@ -65,7 +70,7 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) } int -pthread_key_delete(pthread_key_t key) +_pthread_key_delete(pthread_key_t key) { int ret = 0; @@ -88,23 +93,24 @@ pthread_key_delete(pthread_key_t key) void _thread_cleanupspecific(void) { - void *data = NULL; - int key; - int itr; + struct pthread *curthread = _get_curthread(); + void *data = NULL; + int key; + int itr; void (*destructor)( void *); for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) { for (key = 0; key < PTHREAD_KEYS_MAX; key++) { - if (_thread_run->specific_data_count) { + if (curthread->specific_data_count) { /* Lock the key table entry: */ _SPINLOCK(&key_table[key].lock); destructor = NULL; if (key_table[key].allocated) { - if (_thread_run->specific_data[key]) { - data = (void *) _thread_run->specific_data[key]; - _thread_run->specific_data[key] = NULL; - _thread_run->specific_data_count--; + if (curthread->specific_data[key]) { + data = (void *) curthread->specific_data[key]; + curthread->specific_data[key] = NULL; + curthread->specific_data_count--; destructor = key_table[key].destructor; } } @@ -119,14 +125,14 @@ _thread_cleanupspecific(void) if (destructor) destructor(data); } else { - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; return; } } } - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; } static inline const void ** @@ -140,13 +146,13 @@ pthread_key_allocate_data(void) } int -pthread_setspecific(pthread_key_t key, const void *value) +_pthread_setspecific(pthread_key_t key, const void *value) { - pthread_t pthread; - int ret = 0; + struct pthread *pthread; + int ret = 0; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); if ((pthread->specific_data) || (pthread->specific_data = pthread_key_allocate_data())) { @@ -171,13 +177,13 @@ pthread_setspecific(pthread_key_t key, const void *value) } void * -pthread_getspecific(pthread_key_t key) +_pthread_getspecific(pthread_key_t key) { - pthread_t pthread; + struct pthread *pthread; void *data; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); /* Check if there is specific data: */ if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) { @@ -197,4 +203,3 @@ pthread_getspecific(pthread_key_t key) data = NULL; return (data); } -#endif diff --git a/lib/libc_r/uthread/uthread_spinlock.c b/lib/libc_r/uthread/uthread_spinlock.c index e797f90..696174e 100644 --- a/lib/libc_r/uthread/uthread_spinlock.c +++ b/lib/libc_r/uthread/uthread_spinlock.c @@ -51,18 +51,20 @@ extern char *__progname; void _spinlock(spinlock_t *lck) { + struct pthread *curthread = _get_curthread(); + /* * Try to grab the lock and loop if another thread grabs * it before we do. */ while(_atomic_lock(&lck->access_lock)) { /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, __FILE__, __LINE__); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; } /* @@ -78,6 +80,7 @@ _spinlock(spinlock_t *lck) void _spinlock_debug(spinlock_t *lck, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int cnt = 0; /* @@ -88,19 +91,19 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno) cnt++; if (cnt > 100) { char str[256]; - snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, _thread_run, lck, fname, lineno, lck->fname, lck->lineno); - _thread_sys_write(2,str,strlen(str)); + snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno); + __sys_write(2,str,strlen(str)); __sleep(1); cnt = 0; } /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, fname, lineno); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; lck->fname = fname; lck->lineno = lineno; } diff --git a/lib/libc_r/uthread/uthread_suspend_np.c b/lib/libc_r/uthread/uthread_suspend_np.c index 083f552..5c204a2 100644 --- a/lib/libc_r/uthread/uthread_suspend_np.c +++ b/lib/libc_r/uthread/uthread_suspend_np.c @@ -32,15 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" static void finish_suspension(void *arg); +#pragma weak pthread_suspend_np=_pthread_suspend_np + /* Suspend a thread: */ int -pthread_suspend_np(pthread_t thread) +_pthread_suspend_np(pthread_t thread) { int ret; @@ -144,9 +145,10 @@ pthread_suspend_np(pthread_t thread) static void finish_suspension(void *arg) { - if (_thread_run->suspended != SUSP_NO) + struct pthread *curthread = _get_curthread(); + + if (curthread->suspended != SUSP_NO) _thread_kern_sched_state(PS_SUSPENDED, __FILE__, __LINE__); } -#endif diff --git a/lib/libc_r/uthread/uthread_switch_np.c b/lib/libc_r/uthread/uthread_switch_np.c index 9b83545..e999259 100644 --- a/lib/libc_r/uthread/uthread_switch_np.c +++ b/lib/libc_r/uthread/uthread_switch_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" +#pragma weak pthread_switch_add_np=_pthread_switch_add_np +#pragma weak pthread_switch_delete_np=_pthread_switch_delete_np + int -pthread_switch_add_np(pthread_switch_routine_t routine) +_pthread_switch_add_np(pthread_switch_routine_t routine) { int ret = 0; @@ -54,7 +56,7 @@ pthread_switch_add_np(pthread_switch_routine_t routine) } int -pthread_switch_delete_np(pthread_switch_routine_t routine) +_pthread_switch_delete_np(pthread_switch_routine_t routine) { int ret = 0; @@ -67,4 +69,3 @@ pthread_switch_delete_np(pthread_switch_routine_t routine) return(ret); } -#endif diff --git a/lib/libc_r/uthread/uthread_system.c b/lib/libc_r/uthread/uthread_system.c index 47a572d..d3a16cb 100644 --- a/lib/libc_r/uthread/uthread_system.c +++ b/lib/libc_r/uthread/uthread_system.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak system=_system + int -system(const char *string) +_system(const char *string) { int ret; @@ -45,4 +46,3 @@ system(const char *string) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_tcdrain.c b/lib/libc_r/uthread/uthread_tcdrain.c index 6470882..7700791 100644 --- a/lib/libc_r/uthread/uthread_tcdrain.c +++ b/lib/libc_r/uthread/uthread_tcdrain.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak tcdrain=_tcdrain + int -tcdrain(int fd) +_tcdrain(int fd) { int ret; @@ -45,4 +46,3 @@ tcdrain(int fd) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_vfork.c b/lib/libc_r/uthread/uthread_vfork.c index bbfcf00..78d9f01 100644 --- a/lib/libc_r/uthread/uthread_vfork.c +++ b/lib/libc_r/uthread/uthread_vfork.c @@ -1,9 +1,12 @@ +/* + * $FreeBSD$ + */ #include -#ifdef _THREAD_SAFE + +#pragma weak vfork=_vfork int -vfork(void) +_vfork(void) { return (fork()); } -#endif diff --git a/lib/libc_r/uthread/uthread_wait.c b/lib/libc_r/uthread/uthread_wait.c index c34d997..9482586 100644 --- a/lib/libc_r/uthread/uthread_wait.c +++ b/lib/libc_r/uthread/uthread_wait.c @@ -29,12 +29,13 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait=_wait + pid_t -wait(int *istat) +_wait(int *istat) { pid_t ret; @@ -44,4 +45,3 @@ wait(int *istat) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_wait4.c b/lib/libc_r/uthread/uthread_wait4.c index da69a4f..8b44366 100644 --- a/lib/libc_r/uthread/uthread_wait4.c +++ b/lib/libc_r/uthread/uthread_wait4.c @@ -35,27 +35,29 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait4=_wait4 + pid_t _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) { + struct pthread *curthread = _get_curthread(); pid_t ret; _thread_kern_sig_defer(); /* Perform a non-blocking wait4 syscall: */ - while ((ret = _thread_sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { + while ((ret = __sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Schedule the next thread while this one waits: */ _thread_kern_sched_state(PS_WAIT_WAIT, __FILE__, __LINE__); /* Check if this call was interrupted by a signal: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -66,6 +68,3 @@ _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) return (ret); } - -__strong_reference(_wait4, wait4); -#endif diff --git a/lib/libc_r/uthread/uthread_waitpid.c b/lib/libc_r/uthread/uthread_waitpid.c index 28fcd78..672183d 100644 --- a/lib/libc_r/uthread/uthread_waitpid.c +++ b/lib/libc_r/uthread/uthread_waitpid.c @@ -31,12 +31,13 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak waitpid=_waitpid + pid_t -waitpid(pid_t wpid, int *status, int options) +_waitpid(pid_t wpid, int *status, int options) { pid_t ret; @@ -46,4 +47,3 @@ waitpid(pid_t wpid, int *status, int options) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_write.c b/lib/libc_r/uthread/uthread_write.c index 5d9ef35..b841726 100644 --- a/lib/libc_r/uthread/uthread_write.c +++ b/lib/libc_r/uthread/uthread_write.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak write=__write + ssize_t _write(int fd, const void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int blocking; int type; ssize_t n; @@ -76,7 +78,7 @@ _write(int fd, const void *buf, size_t nbytes) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_write(fd, buf + num, nbytes - num); + n = __sys_write(fd, buf + num, nbytes - num); /* Check if one or more bytes were written: */ if (n > 0) @@ -94,11 +96,11 @@ _write(int fd, const void *buf, size_t nbytes) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && num < nbytes))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -107,7 +109,7 @@ _write(int fd, const void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -133,7 +135,7 @@ _write(int fd, const void *buf, size_t nbytes) } ssize_t -write(int fd, const void *buf, size_t nbytes) +__write(int fd, const void *buf, size_t nbytes) { ssize_t ret; @@ -143,4 +145,3 @@ write(int fd, const void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libc_r/uthread/uthread_writev.c b/lib/libc_r/uthread/uthread_writev.c index 5f31468..66b451f 100644 --- a/lib/libc_r/uthread/uthread_writev.c +++ b/lib/libc_r/uthread/uthread_writev.c @@ -39,13 +39,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak writev=_writev + ssize_t _writev(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int blocking; int idx = 0; int type; @@ -92,7 +94,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx); + n = __sys_writev(fd, &p_iov[idx], iovcnt - idx); /* Check if one or more bytes were written: */ if (n > 0) { @@ -158,11 +160,11 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -171,7 +173,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -200,6 +202,3 @@ _writev(int fd, const struct iovec * iov, int iovcnt) return (ret); } - -__strong_reference(_writev, writev); -#endif diff --git a/lib/libc_r/uthread/uthread_yield.c b/lib/libc_r/uthread/uthread_yield.c index 7d64283..67ca501 100644 --- a/lib/libc_r/uthread/uthread_yield.c +++ b/lib/libc_r/uthread/uthread_yield.c @@ -31,15 +31,19 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sched_yield=_sched_yield +#pragma weak pthread_yield=_pthread_yield + int -sched_yield(void) +_sched_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); @@ -50,12 +54,13 @@ sched_yield(void) /* Draft 4 yield */ void -pthread_yield(void) +_pthread_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); } -#endif diff --git a/lib/libkse/Makefile b/lib/libkse/Makefile index 9b20a24..d7b21bc 100644 --- a/lib/libkse/Makefile +++ b/lib/libkse/Makefile @@ -22,23 +22,6 @@ CFLAGS+=-D_PTHREADS_INVARIANTS AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes -# -# This is a list of syscalls that are renamed as _thread_sys_{syscall} -# so that libc_r can provide replacement functions. -# -HIDDEN_SYSCALLS= aio_suspend.o accept.o bind.o close.o connect.o dup.o dup2.o \ - execve.o fchflags.o fchmod.o fchown.o fcntl.o \ - flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ - getlogin.o getpeername.o getsockname.o getsockopt.o ioctl.o \ - kevent.o listen.o \ - msync.o nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ - recvmsg.o sched_yield.o select.o sendfile.o sendmsg.o sendto.o \ - setsockopt.o shutdown.o sigaction.o sigaltstack.o \ - signanosleep.o sigpending.o sigprocmask.o sigreturn.o sigsetmask.o \ - sigsuspend.o socket.o \ - socketpair.o wait4.o write.o writev.o - -.include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" diff --git a/lib/libkse/thread/thr_aio_suspend.c b/lib/libkse/thread/thr_aio_suspend.c index 1253f20..ea77f4e 100644 --- a/lib/libkse/thread/thr_aio_suspend.c +++ b/lib/libkse/thread/thr_aio_suspend.c @@ -30,10 +30,11 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak aio_suspend=_aio_suspend + int _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct timespec *timeout) @@ -41,11 +42,9 @@ _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct int ret; _thread_enter_cancellation_point(); - ret = _aio_suspend(iocbs, niocb, timeout); + ret = __sys_aio_suspend(iocbs, niocb, timeout); _thread_leave_cancellation_point(); - + return ret; } -__strong_reference(_aio_suspend, aio_suspend); -#endif diff --git a/lib/libkse/thread/thr_attr_destroy.c b/lib/libkse/thread/thr_attr_destroy.c index dfe668e..c025072 100644 --- a/lib/libkse/thread/thr_attr_destroy.c +++ b/lib/libkse/thread/thr_attr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_destroy(pthread_attr_t *attr) +#pragma weak pthread_attr_destroy=_pthread_attr_destroy + +int +_pthread_attr_destroy(pthread_attr_t *attr) { int ret; @@ -58,4 +60,3 @@ int pthread_attr_destroy(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getdetachstate.c b/lib/libkse/thread/thr_attr_getdetachstate.c index d120553..581c952 100644 --- a/lib/libkse/thread/thr_attr_getdetachstate.c +++ b/lib/libkse/thread/thr_attr_getdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getdetachstate=_pthread_attr_getdetachstate + int -pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) +_pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) { int ret; @@ -56,4 +57,3 @@ pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getinheritsched.c b/lib/libkse/thread/thr_attr_getinheritsched.c index 7e243ed..65e347b 100644 --- a/lib/libkse/thread/thr_attr_getinheritsched.c +++ b/lib/libkse/thread/thr_attr_getinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getinheritsched=_pthread_attr_getinheritsched + int -pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) +_pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getschedparam.c b/lib/libkse/thread/thr_attr_getschedparam.c index 46586ff..454e1c2 100644 --- a/lib/libkse/thread/thr_attr_getschedparam.c +++ b/lib/libkse/thread/thr_attr_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedparam=_pthread_attr_getschedparam + int -pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) +_pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getschedpolicy.c b/lib/libkse/thread/thr_attr_getschedpolicy.c index 19f835c..8328b3c 100644 --- a/lib/libkse/thread/thr_attr_getschedpolicy.c +++ b/lib/libkse/thread/thr_attr_getschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedpolicy=_pthread_attr_getschedpolicy + int -pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) +_pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getscope.c b/lib/libkse/thread/thr_attr_getscope.c index 176f01b..ca0f4c9 100644 --- a/lib/libkse/thread/thr_attr_getscope.c +++ b/lib/libkse/thread/thr_attr_getscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getscope=_pthread_attr_getscope + int -pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) +_pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) { int ret = 0; @@ -51,4 +52,3 @@ pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getstackaddr.c b/lib/libkse/thread/thr_attr_getstackaddr.c index 442dcc2..dd14176 100644 --- a/lib/libkse/thread/thr_attr_getstackaddr.c +++ b/lib/libkse/thread/thr_attr_getstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstackaddr=_pthread_attr_getstackaddr + int -pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) +_pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_getstacksize.c b/lib/libkse/thread/thr_attr_getstacksize.c index d9aaca4..d66e473 100644 --- a/lib/libkse/thread/thr_attr_getstacksize.c +++ b/lib/libkse/thread/thr_attr_getstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstacksize=_pthread_attr_getstacksize + int -pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) +_pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_init.c b/lib/libkse/thread/thr_attr_init.c index dbe3091..2e560c7 100644 --- a/lib/libkse/thread/thr_attr_init.c +++ b/lib/libkse/thread/thr_attr_init.c @@ -34,11 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_init(pthread_attr_t *attr) +#pragma weak pthread_attr_init=_pthread_attr_init + +int +_pthread_attr_init(pthread_attr_t *attr) { int ret; pthread_attr_t pattr; @@ -57,4 +59,3 @@ int pthread_attr_init(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setcreatesuspend_np.c b/lib/libkse/thread/thr_attr_setcreatesuspend_np.c index d230397..5520368 100644 --- a/lib/libkse/thread/thr_attr_setcreatesuspend_np.c +++ b/lib/libkse/thread/thr_attr_setcreatesuspend_np.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setcreatesuspend_np=_pthread_attr_setcreatesuspend_np + int -pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) +_pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +50,3 @@ pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setdetachstate.c b/lib/libkse/thread/thr_attr_setdetachstate.c index 6be3e4e..8c8a5fc 100644 --- a/lib/libkse/thread/thr_attr_setdetachstate.c +++ b/lib/libkse/thread/thr_attr_setdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setdetachstate=_pthread_attr_setdetachstate + int -pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) +_pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) { int ret; @@ -58,4 +59,3 @@ pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setinheritsched.c b/lib/libkse/thread/thr_attr_setinheritsched.c index eb2384b..e35b4e9 100644 --- a/lib/libkse/thread/thr_attr_setinheritsched.c +++ b/lib/libkse/thread/thr_attr_setinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setinheritsched=_pthread_attr_setinheritsched + int -pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) +_pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setschedparam.c b/lib/libkse/thread/thr_attr_setschedparam.c index 755bb13..a18494b 100644 --- a/lib/libkse/thread/thr_attr_setschedparam.c +++ b/lib/libkse/thread/thr_attr_setschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedparam=_pthread_attr_setschedparam + int -pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) +_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) { int ret = 0; @@ -54,4 +55,3 @@ pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setschedpolicy.c b/lib/libkse/thread/thr_attr_setschedpolicy.c index 2788deb..29de2f4 100644 --- a/lib/libkse/thread/thr_attr_setschedpolicy.c +++ b/lib/libkse/thread/thr_attr_setschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedpolicy=_pthread_attr_setschedpolicy + int -pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) +_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) { int ret = 0; @@ -50,4 +51,3 @@ pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setscope.c b/lib/libkse/thread/thr_attr_setscope.c index de2fe8e..81a09fa 100644 --- a/lib/libkse/thread/thr_attr_setscope.c +++ b/lib/libkse/thread/thr_attr_setscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setscope=_pthread_attr_setscope + int -pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) +_pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) { int ret = 0; @@ -53,4 +54,3 @@ pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setstackaddr.c b/lib/libkse/thread/thr_attr_setstackaddr.c index 46469e4..8552ea4 100644 --- a/lib/libkse/thread/thr_attr_setstackaddr.c +++ b/lib/libkse/thread/thr_attr_setstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstackaddr=_pthread_attr_setstackaddr + int -pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) +_pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_attr_setstacksize.c b/lib/libkse/thread/thr_attr_setstacksize.c index 3897811..4233cb5 100644 --- a/lib/libkse/thread/thr_attr_setstacksize.c +++ b/lib/libkse/thread/thr_attr_setstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstacksize=_pthread_attr_setstacksize + int -pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) +_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_cancel.c b/lib/libkse/thread/thr_cancel.c index c6df52e..55f94fb 100644 --- a/lib/libkse/thread/thr_cancel.c +++ b/lib/libkse/thread/thr_cancel.c @@ -8,8 +8,13 @@ static void finish_cancellation(void *arg); +#pragma weak pthread_cancel=_pthread_cancel +#pragma weak pthread_setcancelstate=_pthread_setcancelstate +#pragma weak pthread_setcanceltype=_pthread_setcanceltype +#pragma weak pthread_testcancel=_pthread_testcancel + int -pthread_cancel(pthread_t pthread) +_pthread_cancel(pthread_t pthread) { int ret; @@ -111,26 +116,27 @@ pthread_cancel(pthread_t pthread) } int -pthread_setcancelstate(int state, int *oldstate) +_pthread_setcancelstate(int state, int *oldstate) { + struct pthread *curthread = _get_curthread(); int ostate; int ret; - ostate = _thread_run->cancelflags & PTHREAD_CANCEL_DISABLE; + ostate = curthread->cancelflags & PTHREAD_CANCEL_DISABLE; switch (state) { case PTHREAD_CANCEL_ENABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_DISABLE; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) + curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE; + if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DISABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags |= PTHREAD_CANCEL_DISABLE; + curthread->cancelflags |= PTHREAD_CANCEL_DISABLE; ret = 0; break; default: @@ -141,24 +147,25 @@ pthread_setcancelstate(int state, int *oldstate) } int -pthread_setcanceltype(int type, int *oldtype) +_pthread_setcanceltype(int type, int *oldtype) { + struct pthread *curthread = _get_curthread(); int otype; int ret; - otype = _thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; + otype = curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; switch (type) { case PTHREAD_CANCEL_ASYNCHRONOUS: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DEFERRED: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; ret = 0; break; default: @@ -169,16 +176,18 @@ pthread_setcanceltype(int type, int *oldtype) } void -pthread_testcancel(void) +_pthread_testcancel(void) { - if (((_thread_run->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCELLING) != 0)) { + struct pthread *curthread = _get_curthread(); + + if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && + ((curthread->cancelflags & PTHREAD_CANCELLING) != 0)) { /* * It is possible for this thread to be swapped out * while performing cancellation; do not allow it * to be cancelled again. */ - _thread_run->cancelflags &= ~PTHREAD_CANCELLING; + curthread->cancelflags &= ~PTHREAD_CANCELLING; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); PANIC("cancel"); @@ -188,15 +197,19 @@ pthread_testcancel(void) void _thread_enter_cancellation_point(void) { + struct pthread *curthread = _get_curthread(); + /* Look for a cancellation before we block: */ pthread_testcancel(); - _thread_run->cancelflags |= PTHREAD_AT_CANCEL_POINT; + curthread->cancelflags |= PTHREAD_AT_CANCEL_POINT; } void _thread_leave_cancellation_point(void) { - _thread_run->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; + struct pthread *curthread = _get_curthread(); + + curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; /* Look for a cancellation after we unblock: */ pthread_testcancel(); } @@ -204,11 +217,13 @@ _thread_leave_cancellation_point(void) static void finish_cancellation(void *arg) { - _thread_run->continuation = NULL; - _thread_run->interrupted = 0; + struct pthread *curthread = _get_curthread(); + + curthread->continuation = NULL; + curthread->interrupted = 0; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { - _thread_run->cancelflags &= ~PTHREAD_CANCEL_NEEDED; + if ((curthread->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { + curthread->cancelflags &= ~PTHREAD_CANCEL_NEEDED; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); } diff --git a/lib/libkse/thread/thr_clean.c b/lib/libkse/thread/thr_clean.c index bba5500..9ce3fc2 100644 --- a/lib/libkse/thread/thr_clean.c +++ b/lib/libkse/thread/thr_clean.c @@ -34,31 +34,35 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_cleanup_push=_pthread_cleanup_push +#pragma weak pthread_cleanup_pop=_pthread_cleanup_pop + void -pthread_cleanup_push(void (*routine) (void *), void *routine_arg) +_pthread_cleanup_push(void (*routine) (void *), void *routine_arg) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *new; if ((new = (struct pthread_cleanup *) malloc(sizeof(struct pthread_cleanup))) != NULL) { new->routine = routine; new->routine_arg = routine_arg; - new->next = _thread_run->cleanup; + new->next = curthread->cleanup; - _thread_run->cleanup = new; + curthread->cleanup = new; } } void -pthread_cleanup_pop(int execute) +_pthread_cleanup_pop(int execute) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *old; - if ((old = _thread_run->cleanup) != NULL) { - _thread_run->cleanup = old->next; + if ((old = curthread->cleanup) != NULL) { + curthread->cleanup = old->next; if (execute) { old->routine(old->routine_arg); } @@ -66,4 +70,3 @@ pthread_cleanup_pop(int execute) } } -#endif diff --git a/lib/libkse/thread/thr_close.c b/lib/libkse/thread/thr_close.c index 9dee2bc..43c3b22 100644 --- a/lib/libkse/thread/thr_close.c +++ b/lib/libkse/thread/thr_close.c @@ -36,10 +36,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak close=__close + int _close(int fd) { @@ -60,7 +61,7 @@ _close(int fd) * the file descriptor status: */ else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) && - ((ret = _thread_sys_fstat(fd, &sb)) == 0)) { + ((ret = __sys_fstat(fd, &sb)) == 0)) { /* * Check if the file should be left as blocking. * @@ -83,9 +84,9 @@ _close(int fd) */ if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) && (_thread_fd_table[fd]->flags & O_NONBLOCK) == 0) { /* Get the current flags: */ - flags = _thread_sys_fcntl(fd, F_GETFL, NULL); + flags = __sys_fcntl(fd, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); } /* XXX: Assumes well behaved threads. */ @@ -95,13 +96,13 @@ _close(int fd) free(entry); /* Close the file descriptor: */ - ret = _thread_sys_close(fd); + ret = __sys_close(fd); } return (ret); } int -close(int fd) +__close(int fd) { int ret; @@ -111,4 +112,3 @@ close(int fd) return ret; } -#endif diff --git a/lib/libkse/thread/thr_cond.c b/lib/libkse/thread/thr_cond.c index 9b93c1a..6f2c509 100644 --- a/lib/libkse/thread/thr_cond.c +++ b/lib/libkse/thread/thr_cond.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -45,6 +44,14 @@ static inline pthread_t cond_queue_deq(pthread_cond_t); static inline void cond_queue_remove(pthread_cond_t, pthread_t); static inline void cond_queue_enq(pthread_cond_t, pthread_t); +#pragma weak pthread_cond_init=_pthread_cond_init +#pragma weak pthread_cond_destroy=_pthread_cond_destroy +#pragma weak pthread_cond_wait=_pthread_cond_wait +#pragma weak pthread_cond_timedwait=_pthread_cond_timedwait +#pragma weak pthread_cond_signal=_pthread_cond_signal +#pragma weak pthread_cond_broadcast=_pthread_cond_broadcast + + /* Reinitialize a condition variable to defaults. */ int _cond_reinit(pthread_cond_t *cond) @@ -70,7 +77,7 @@ _cond_reinit(pthread_cond_t *cond) } int -pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) +_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { enum pthread_cond_type type; pthread_cond_t pcond; @@ -130,7 +137,7 @@ pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) } int -pthread_cond_destroy(pthread_cond_t *cond) +_pthread_cond_destroy(pthread_cond_t *cond) { int rval = 0; @@ -157,8 +164,9 @@ pthread_cond_destroy(pthread_cond_t *cond) } int -pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -213,21 +221,21 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) rval = EINVAL; } else { /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; seqno = (*cond)->c_seqno; /* Wait forever: */ - _thread_run->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_sec = -1; /* Unlock the mutex: */ if ((unlock_mutex != 0) && @@ -237,7 +245,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == @@ -264,7 +272,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) done = (seqno != (*cond)->c_seqno); - if ((_thread_run->flags & + if ((curthread->flags & PTHREAD_FLAGS_IN_CONDQ) != 0) { /* * Lock the condition variable @@ -273,7 +281,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) _SPINLOCK(&(*cond)->lock); cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -286,7 +294,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * Save the interrupted flag; locking * the mutex will destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Note that even though this thread may have @@ -308,8 +316,8 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -319,9 +327,10 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) } int -pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, +_pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -375,20 +384,20 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); } else { /* Set the wakeup time: */ - _thread_run->wakeup_time.tv_sec = + curthread->wakeup_time.tv_sec = abstime->tv_sec; - _thread_run->wakeup_time.tv_nsec = + curthread->wakeup_time.tv_nsec = abstime->tv_nsec; /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; @@ -402,7 +411,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -432,8 +441,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * interrupted (canceled), or needs to * be resumed after handling a signal. */ - if ((_thread_run->timeout == 0) && - (_thread_run->interrupted == 0) && + if ((curthread->timeout == 0) && + (curthread->interrupted == 0) && (done != 0)) { /* Lock the mutex: */ rval = _mutex_cv_lock(mutex); @@ -447,7 +456,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * variable queue: */ cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -457,14 +466,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); /* Return a timeout error: */ - if (_thread_run->timeout != 0) + if (curthread->timeout != 0) rval = ETIMEDOUT; /* * Save the interrupted flag; * locking the mutex will * destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Lock the mutex and ignore any @@ -490,8 +499,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -501,7 +510,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, } int -pthread_cond_signal(pthread_cond_t * cond) +_pthread_cond_signal(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -569,7 +578,7 @@ pthread_cond_signal(pthread_cond_t * cond) } int -pthread_cond_broadcast(pthread_cond_t * cond) +_pthread_cond_broadcast(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -752,4 +761,3 @@ cond_queue_enq(pthread_cond_t cond, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_CONDQ; pthread->data.cond = cond; } -#endif diff --git a/lib/libkse/thread/thr_condattr_destroy.c b/lib/libkse/thread/thr_condattr_destroy.c index ad91228..578523a 100644 --- a/lib/libkse/thread/thr_condattr_destroy.c +++ b/lib/libkse/thread/thr_condattr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_condattr_destroy(pthread_condattr_t *attr) +#pragma weak pthread_condattr_destroy=_pthread_condattr_destroy + +int +_pthread_condattr_destroy(pthread_condattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +51,3 @@ int pthread_condattr_destroy(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_condattr_init.c b/lib/libkse/thread/thr_condattr_init.c index 3379898..dc8059d 100644 --- a/lib/libkse/thread/thr_condattr_init.c +++ b/lib/libkse/thread/thr_condattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_condattr_init=_pthread_condattr_init + int -pthread_condattr_init(pthread_condattr_t *attr) +_pthread_condattr_init(pthread_condattr_t *attr) { int ret; pthread_condattr_t pattr; @@ -55,4 +56,3 @@ pthread_condattr_init(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_creat.c b/lib/libkse/thread/thr_creat.c index 1b51fec..56100dd 100644 --- a/lib/libkse/thread/thr_creat.c +++ b/lib/libkse/thread/thr_creat.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak creat=___creat + int -creat(const char *path, mode_t mode) +___creat(const char *path, mode_t mode) { int ret; @@ -45,4 +46,3 @@ creat(const char *path, mode_t mode) return ret; } -#endif diff --git a/lib/libkse/thread/thr_create.c b/lib/libkse/thread/thr_create.c index 430869e..a0a51ec 100644 --- a/lib/libkse/thread/thr_create.c +++ b/lib/libkse/thread/thr_create.c @@ -40,7 +40,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -64,10 +63,13 @@ int _thread_CTX_JB_value = CTX_JB; int _thread_CTX_SJB_value = CTX_SJB; int _thread_CTX_UC_value = CTX_UC; +#pragma weak pthread_create=_pthread_create + int -pthread_create(pthread_t * thread, const pthread_attr_t * attr, +_pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { + struct pthread *curthread = _get_curthread(); struct itimerval itimer; int f_gc = 0; int ret = 0; @@ -180,7 +182,7 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ - new_thread->sigmask = _thread_run->sigmask; + new_thread->sigmask = curthread->sigmask; new_thread->sigmask_seqno = 0; /* Initialize the signal frame: */ @@ -214,13 +216,13 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.prio = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.sched_policy = - _thread_run->attr.sched_policy; + curthread->attr.sched_policy; } else { /* * Use just the thread priority, leaving the @@ -315,13 +317,14 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, void _thread_start(void) { + struct pthread *curthread = _get_curthread(); + /* 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)); + pthread_exit(curthread->start_routine(curthread->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } -#endif diff --git a/lib/libkse/thread/thr_detach.c b/lib/libkse/thread/thr_detach.c index 6dd762a..c387926 100644 --- a/lib/libkse/thread/thr_detach.c +++ b/lib/libkse/thread/thr_detach.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_detach=_pthread_detach + int -pthread_detach(pthread_t pthread) +_pthread_detach(pthread_t pthread) { int rval = 0; pthread_t next_thread; @@ -85,4 +86,3 @@ pthread_detach(pthread_t pthread) /* Return the completion status: */ return (rval); } -#endif diff --git a/lib/libkse/thread/thr_equal.c b/lib/libkse/thread/thr_equal.c index e03ba4a..334d023 100644 --- a/lib/libkse/thread/thr_equal.c +++ b/lib/libkse/thread/thr_equal.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_equal=_pthread_equal + int -pthread_equal(pthread_t t1, pthread_t t2) +_pthread_equal(pthread_t t1, pthread_t t2) { /* Compare the two thread pointers: */ return (t1 == t2); } -#endif diff --git a/lib/libkse/thread/thr_exit.c b/lib/libkse/thread/thr_exit.c index aef12fe..42bf025 100644 --- a/lib/libkse/thread/thr_exit.c +++ b/lib/libkse/thread/thr_exit.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" #define FLAGS_IN_SCHEDQ \ (PTHREAD_FLAGS_IN_PRIOQ|PTHREAD_FLAGS_IN_WAITQ|PTHREAD_FLAGS_IN_WORKQ) +#pragma weak _exit=__exit +#pragma weak pthread_exit=_pthread_exit + void __exit(int status) { int flags; @@ -58,8 +60,8 @@ void __exit(int status) setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL); /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* * Enter a loop to set all file descriptors to blocking @@ -70,18 +72,16 @@ void __exit(int status) if (_thread_fd_table[i] != NULL && !(_thread_fd_table[i]->flags & O_NONBLOCK)) { /* Get the current flags: */ - flags = _thread_sys_fcntl(i, F_GETFL, NULL); + flags = __sys_fcntl(i, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); } } /* Call the _exit syscall: */ - _thread_sys__exit(status); + __sys__exit(status); } -__strong_reference(__exit, _exit); - void _thread_exit(char *fname, int lineno, char *string) { @@ -97,7 +97,7 @@ _thread_exit(char *fname, int lineno, char *string) strcat(s, ")\n"); /* Write the string to the standard error file descriptor: */ - _thread_sys_write(2, s, strlen(s)); + __sys_write(2, s, strlen(s)); /* Force this process to exit: */ /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */ @@ -116,6 +116,8 @@ _thread_exit(char *fname, int lineno, char *string) void _thread_exit_cleanup(void) { + struct pthread *curthread = _get_curthread(); + /* * POSIX states that cancellation/termination of a thread should * not release any visible resources (such as mutexes) and that @@ -124,13 +126,13 @@ _thread_exit_cleanup(void) * are not visible to the application and need to be released. */ /* Unlock all owned fd locks: */ - _thread_fd_unlock_owned(_thread_run); + _thread_fd_unlock_owned(curthread); /* Unlock all owned file locks: */ - _funlock_owned(_thread_run); + _funlock_owned(curthread); /* Unlock all private mutexes: */ - _mutex_unlock_private(_thread_run); + _mutex_unlock_private(curthread); /* * This still isn't quite correct because we don't account @@ -139,39 +141,40 @@ _thread_exit_cleanup(void) } void -pthread_exit(void *status) +_pthread_exit(void *status) { + struct pthread *curthread = _get_curthread(); pthread_t pthread; /* Check if this thread is already in the process of exiting: */ - if ((_thread_run->flags & PTHREAD_EXITING) != 0) { + if ((curthread->flags & PTHREAD_EXITING) != 0) { char msg[128]; - 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); + 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!",curthread); PANIC(msg); } /* Flag this thread as exiting: */ - _thread_run->flags |= PTHREAD_EXITING; + curthread->flags |= PTHREAD_EXITING; /* Save the return value: */ - _thread_run->ret = status; + curthread->ret = status; - while (_thread_run->cleanup != NULL) { + while (curthread->cleanup != NULL) { pthread_cleanup_pop(1); } - if (_thread_run->attr.cleanup_attr != NULL) { - _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr); + if (curthread->attr.cleanup_attr != NULL) { + curthread->attr.cleanup_attr(curthread->attr.arg_attr); } /* Check if there is thread specific data: */ - if (_thread_run->specific_data != NULL) { + if (curthread->specific_data != NULL) { /* Run the thread-specific data destructors: */ _thread_cleanupspecific(); } /* Free thread-specific poll_data structure, if allocated: */ - if (_thread_run->poll_data.fds != NULL) { - free(_thread_run->poll_data.fds); - _thread_run->poll_data.fds = NULL; + if (curthread->poll_data.fds != NULL) { + free(curthread->poll_data.fds); + curthread->poll_data.fds = NULL; } /* @@ -182,7 +185,7 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Add this thread to the list of dead threads. */ - TAILQ_INSERT_HEAD(&_dead_list, _thread_run, dle); + TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); /* * Signal the garbage collector thread that there is something @@ -203,9 +206,9 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Check if there are any threads joined to this one: */ - while ((pthread = TAILQ_FIRST(&(_thread_run->join_queue))) != NULL) { + while ((pthread = TAILQ_FIRST(&(curthread->join_queue))) != NULL) { /* Remove the thread from the queue: */ - TAILQ_REMOVE(&_thread_run->join_queue, pthread, sqe); + TAILQ_REMOVE(&curthread->join_queue, pthread, sqe); pthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; /* @@ -217,16 +220,16 @@ pthread_exit(void *status) /* * Set the return value for the woken thread: */ - if ((_thread_run->attr.flags & PTHREAD_DETACHED) != 0) + if ((curthread->attr.flags & PTHREAD_DETACHED) != 0) pthread->error = ESRCH; else { - pthread->ret = _thread_run->ret; + pthread->ret = curthread->ret; pthread->error = 0; } } /* Remove this thread from the thread list: */ - TAILQ_REMOVE(&_thread_list, _thread_run, tle); + TAILQ_REMOVE(&_thread_list, curthread, tle); /* This thread will never be re-scheduled. */ _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__); @@ -234,4 +237,3 @@ pthread_exit(void *status) /* This point should not be reached. */ PANIC("Dead thread has resumed"); } -#endif diff --git a/lib/libkse/thread/thr_fcntl.c b/lib/libkse/thread/thr_fcntl.c index f5dd064..84f4678 100644 --- a/lib/libkse/thread/thr_fcntl.c +++ b/lib/libkse/thread/thr_fcntl.c @@ -34,10 +34,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fcntl=__fcntl + int _fcntl(int fd, int cmd,...) { @@ -63,12 +64,12 @@ _fcntl(int fd, int cmd,...) oldfd = va_arg(ap, int); /* Initialise the file descriptor table entry: */ - if ((ret = _thread_sys_fcntl(fd, cmd, oldfd)) < 0) { + if ((ret = __sys_fcntl(fd, cmd, oldfd)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the file: */ - _thread_sys_close(ret); + __sys_close(ret); /* Reset the file descriptor: */ ret = -1; @@ -82,10 +83,10 @@ _fcntl(int fd, int cmd,...) break; case F_SETFD: flags = va_arg(ap, int); - ret = _thread_sys_fcntl(fd, cmd, flags); + ret = __sys_fcntl(fd, cmd, flags); break; case F_GETFD: - ret = _thread_sys_fcntl(fd, cmd, 0); + ret = __sys_fcntl(fd, cmd, 0); break; case F_GETFL: ret = _thread_fd_table[fd]->flags; @@ -104,10 +105,10 @@ _fcntl(int fd, int cmd,...) nonblock = flags & O_NONBLOCK; /* Set the file descriptor flags: */ - if ((ret = _thread_sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { + if ((ret = __sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { /* Get the flags so that we behave like the kernel: */ - } else if ((flags = _thread_sys_fcntl(fd, + } else if ((flags = __sys_fcntl(fd, F_GETFL, 0)) == -1) { /* Error getting flags: */ ret = -1; @@ -125,7 +126,7 @@ _fcntl(int fd, int cmd,...) break; default: /* Might want to make va_arg use a union */ - ret = _thread_sys_fcntl(fd, cmd, va_arg(ap, void *)); + ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); break; } @@ -140,7 +141,7 @@ _fcntl(int fd, int cmd,...) } int -fcntl(int fd, int cmd,...) +__fcntl(int fd, int cmd,...) { int ret; va_list ap; @@ -167,4 +168,3 @@ fcntl(int fd, int cmd,...) return ret; } -#endif diff --git a/lib/libkse/thread/thr_find_thread.c b/lib/libkse/thread/thr_find_thread.c index d4a3bbd..861c4cb 100644 --- a/lib/libkse/thread/thr_find_thread.c +++ b/lib/libkse/thread/thr_find_thread.c @@ -32,7 +32,6 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -97,4 +96,3 @@ _find_dead_thread(pthread_t pthread) /* Return zero if the thread exists: */ return ((pthread1 != NULL) ? 0:ESRCH); } -#endif diff --git a/lib/libkse/thread/thr_fork.c b/lib/libkse/thread/thr_fork.c index 97039b3..34d640c 100644 --- a/lib/libkse/thread/thr_fork.c +++ b/lib/libkse/thread/thr_fork.c @@ -36,13 +36,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fork=_fork + pid_t _fork(void) { + struct pthread *curthread = _get_curthread(); int i, flags; pid_t ret; pthread_t pthread; @@ -55,42 +57,42 @@ _fork(void) _thread_kern_sig_defer(); /* Fork a new process: */ - if ((ret = _thread_sys_fork()) != 0) { + if ((ret = __sys_fork()) != 0) { /* Parent process or error. Nothing to do here. */ } else { /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* Reset signals pending for the running thread: */ - sigemptyset(&_thread_run->sigpend); + sigemptyset(&curthread->sigpend); /* * Create a pipe that is written to by the signal handler to * prevent signals being missed in calls to - * _thread_sys_select: + * __sys_select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create pthread kernel pipe for forked process"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } @@ -125,7 +127,7 @@ _fork(void) pthread = TAILQ_NEXT(pthread, dle); /* Make sure this isn't the running thread: */ - if (pthread_save != _thread_run) { + if (pthread_save != curthread) { /* Remove this thread from the list: */ TAILQ_REMOVE(&_thread_list, pthread_save, tle); @@ -165,7 +167,7 @@ _fork(void) } /* Treat the current thread as the initial thread: */ - _thread_initial = _thread_run; + _thread_initial = curthread; /* Re-init the dead thread list: */ TAILQ_INIT(&_dead_list); @@ -175,7 +177,7 @@ _fork(void) TAILQ_INIT(&_workq); /* Re-init the threads mutex queue: */ - TAILQ_INIT(&_thread_run->mutexq); + TAILQ_INIT(&curthread->mutexq); /* No spinlocks yet: */ _spinblock_count = 0; @@ -217,6 +219,3 @@ _fork(void) /* Return the process ID: */ return (ret); } - -__strong_reference(_fork, fork); -#endif diff --git a/lib/libkse/thread/thr_fsync.c b/lib/libkse/thread/thr_fsync.c index 0d88320..b90d896 100644 --- a/lib/libkse/thread/thr_fsync.c +++ b/lib/libkse/thread/thr_fsync.c @@ -32,24 +32,25 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fsync=__fsync + int _fsync(int fd) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_fsync(fd); + ret = __sys_fsync(fd); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } int -fsync(int fd) +__fsync(int fd) { int ret; @@ -59,4 +60,3 @@ fsync(int fd) return ret; } -#endif diff --git a/lib/libkse/thread/thr_getprio.c b/lib/libkse/thread/thr_getprio.c index 2f94b86..35eeb5b 100644 --- a/lib/libkse/thread/thr_getprio.c +++ b/lib/libkse/thread/thr_getprio.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getprio=_pthread_getprio + int -pthread_getprio(pthread_t pthread) +_pthread_getprio(pthread_t pthread) { int policy, ret; struct sched_param param; @@ -53,4 +54,3 @@ pthread_getprio(pthread_t pthread) /* Return the thread priority or an error status: */ return (ret); } -#endif diff --git a/lib/libkse/thread/thr_getschedparam.c b/lib/libkse/thread/thr_getschedparam.c index 5dbd1f0..02f27c2 100644 --- a/lib/libkse/thread/thr_getschedparam.c +++ b/lib/libkse/thread/thr_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getschedparam=_pthread_getschedparam + int -pthread_getschedparam(pthread_t pthread, int *policy, +_pthread_getschedparam(pthread_t pthread, int *policy, struct sched_param *param) { int ret; @@ -56,4 +57,3 @@ pthread_getschedparam(pthread_t pthread, int *policy, return(ret); } -#endif diff --git a/lib/libkse/thread/thr_info.c b/lib/libkse/thread/thr_info.c index 956ae7c..9c85e84 100644 --- a/lib/libkse/thread/thr_info.c +++ b/lib/libkse/thread/thr_info.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -47,6 +46,7 @@ static void dump_thread(int fd, pthread_t pthread, int long_version); +#pragma weak pthread_set_name_np=_pthread_set_name_np struct s_thread_info { enum pthread_state state; @@ -92,7 +92,7 @@ _thread_dump_info(void) snprintf(tmpfile, sizeof(tmpfile), "/tmp/uthread.dump.%u.%i", getpid(), i); /* Open the dump file for append and create it if necessary: */ - if ((fd = _thread_sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, + if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) { /* Can't open the dump file. */ if (errno == EEXIST) @@ -114,7 +114,7 @@ _thread_dump_info(void) } else { /* Output a header for active threads: */ strcpy(s, "\n\n=============\nACTIVE THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the global list: */ TAILQ_FOREACH(pthread, &_thread_list, tle) { @@ -123,7 +123,7 @@ _thread_dump_info(void) /* Output a header for ready threads: */ strcpy(s, "\n\n=============\nREADY THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the ready queue: */ TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) { @@ -134,7 +134,7 @@ _thread_dump_info(void) /* Output a header for waiting threads: */ strcpy(s, "\n\n=============\nWAITING THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_waitingq, pqe) { @@ -143,7 +143,7 @@ _thread_dump_info(void) /* Output a header for threads in the work queue: */ strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_workq, qe) { @@ -154,11 +154,11 @@ _thread_dump_info(void) if (TAILQ_FIRST(&_dead_list) == NULL) { /* Output a record: */ strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } else { /* Output a header for dead threads: */ strcpy(s, "\n\nDEAD THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* * Enter a loop to report each thread in the global @@ -172,7 +172,7 @@ _thread_dump_info(void) /* Output a header for file descriptors: */ snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR " "TABLE (table size %d)\n\n", _thread_dtablesize); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report file descriptor lock usage: */ for (i = 0; i < _thread_dtablesize; i++) { @@ -193,18 +193,19 @@ _thread_dump_info(void) _thread_fd_table[i]->w_lockcount, _thread_fd_table[i]->w_fname, _thread_fd_table[i]->w_lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } } /* Close the dump file: */ - _thread_sys_close(fd); + __sys_close(fd); } } static void dump_thread(int fd, pthread_t pthread, int long_version) { + struct pthread *curthread = _get_curthread(); char s[512]; int i; @@ -219,20 +220,20 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread, (pthread->name == NULL) ? "" : pthread->name, pthread->active_priority, thread_info[i].name, pthread->fname, pthread->lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); if (long_version != 0) { /* Check if this is the running thread: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Output a record for the running thread: */ strcpy(s, "This is the running thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Check if this is the initial thread: */ if (pthread == _thread_initial) { /* Output a record for the initial thread: */ strcpy(s, "This is the initial thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Process according to thread state: */ switch (pthread->state) { @@ -246,22 +247,22 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread->data.fd.fd, pthread->data.fd.fname, pthread->data.fd.branch); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); 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)); + __sys_write(fd, s, strlen(s)); break; case PS_SIGWAIT: snprintf(s, sizeof(s), "sigmask (hi)"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); for (i = _SIG_WORDS - 1; i >= 0; i--) { snprintf(s, sizeof(s), "%08x\n", pthread->sigmask.__bits[i]); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } snprintf(s, sizeof(s), "(lo)\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); break; /* * Trap other states that are not explicitly @@ -276,7 +277,7 @@ dump_thread(int fd, pthread_t pthread, int long_version) /* Set the thread name for debug: */ void -pthread_set_name_np(pthread_t thread, char *name) +_pthread_set_name_np(pthread_t thread, char *name) { /* Check if the caller has specified a valid thread: */ if (thread != NULL && thread->magic == PTHREAD_MAGIC) { @@ -287,4 +288,3 @@ pthread_set_name_np(pthread_t thread, char *name) thread->name = strdup(name); } } -#endif diff --git a/lib/libkse/thread/thr_init.c b/lib/libkse/thread/thr_init.c index 93d7849..c92efe3 100644 --- a/lib/libkse/thread/thr_init.c +++ b/lib/libkse/thread/thr_init.c @@ -35,74 +35,117 @@ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE -#include -#include -#include -#include -#include -#include -#include +#include "namespace.h" +#include +#include +#include + #include +#include +#include +#include +#include +#include #include #include #include -#include #include +#include #include -#ifdef _THREAD_SAFE -#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include "un-namespace.h" + #include "pthread_private.h" /* + * 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 +}; + +/* * 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. */ -static void *thread_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, - &pthread_cond_init, - &pthread_cond_destroy, - &pthread_cond_wait, - &pthread_cond_timedwait, - &pthread_cond_signal, - &pthread_cond_broadcast +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 }; -#ifdef GCC_2_8_MADE_THREAD_AWARE -typedef void *** (*dynamic_handler_allocator)(); -extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); - -static pthread_key_t except_head_key; - -typedef struct { - void **__dynamic_handler_chain; - void *top_elt[2]; -} except_struct; - -static void ***dynamic_allocator_handler_fn() -{ - except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); - - if(dh == NULL) { - dh = (except_struct *)malloc( sizeof(except_struct) ); - memset(dh, '\0', sizeof(except_struct)); - dh->__dynamic_handler_chain= dh->top_elt; - pthread_setspecific(except_head_key, (void *)dh); - } - return &dh->__dynamic_handler_chain; -} -#endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization @@ -124,11 +167,11 @@ _thread_init(void) return; /* - * Make gcc quiescent about thread_references not being + * Make gcc quiescent about {,libgcc_}references not being * referenced: */ - if (thread_references[0] == NULL) - PANIC("Mandatory pthread_* functions not loaded"); + if ((references[0] == NULL) || (libgcc_references[0] == NULL)) + PANIC("Failed loading mandatory references in _thread_init"); /* * Check for the special case of this process running as @@ -143,22 +186,22 @@ _thread_init(void) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); - if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) + if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); - if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) + if (__sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); - if (_thread_sys_dup2(fd,0) == -1 || - _thread_sys_dup2(fd,1) == -1 || - _thread_sys_dup2(fd,2) == -1) + if (__sys_dup2(fd,0) == -1 || + __sys_dup2(fd,1) == -1 || + __sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if (((_pthread_stdio_flags[i] = - _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) && + __sys_fcntl(i,F_GETFL, NULL)) == -1) && (errno != EBADF)) PANIC("Cannot get stdio flags"); @@ -166,27 +209,27 @@ _thread_init(void) * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } @@ -270,6 +313,9 @@ _thread_init(void) /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; + /* Set the name of the thread: */ + _thread_initial->name = strdup("_thread_initial"); + /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); @@ -299,7 +345,7 @@ _thread_init(void) _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); - _thread_run = _thread_initial; + _set_curthread(_thread_initial); /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); @@ -320,7 +366,7 @@ _thread_init(void) _thread_sigstack.ss_size = SIGSTKSZ; _thread_sigstack.ss_flags = 0; if ((_thread_sigstack.ss_sp == NULL) || - (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0)) + (__sys_sigaltstack(&_thread_sigstack, NULL) != 0)) PANIC("Unable to install alternate signal stack"); /* Enter a loop to get the existing signal status: */ @@ -330,7 +376,7 @@ _thread_init(void) } /* Get the signal handler details: */ - else if (_thread_sys_sigaction(i, NULL, + else if (__sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal @@ -348,9 +394,9 @@ _thread_init(void) * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ - if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || - _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || - _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { + if (__sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || + __sys_sigaction(SIGINFO, &act, NULL) != 0 || + __sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ @@ -361,7 +407,7 @@ _thread_init(void) _thread_sigact[SIGCHLD - 1].sa_flags = SA_SIGINFO; /* Get the process signal mask: */ - _thread_sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); + __sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); /* Get the kernel clockrate: */ mib[0] = CTL_KERN; @@ -416,17 +462,8 @@ _thread_init(void) } } -#ifdef GCC_2_8_MADE_THREAD_AWARE - /* Create the thread-specific data for the exception linked list. */ - if(pthread_key_create(&except_head_key, NULL) != 0) - PANIC("Failed to create thread specific execption head"); - - /* Setup the gcc exception handler per thread. */ - __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); -#endif /* GCC_2_8_MADE_THREAD_AWARE */ - /* Initialise the garbage collector mutex and condition variable. */ - if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || + if (_pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); } @@ -445,12 +482,3 @@ _thread_main(int argc, char *argv[], char *env) return (main(argc, argv, env)); } #endif -#else -/* - * A stub for non-threaded programs. - */ -void -_thread_init(void) -{ -} -#endif diff --git a/lib/libkse/thread/thr_join.c b/lib/libkse/thread/thr_join.c index b4a7c61..2615f73 100644 --- a/lib/libkse/thread/thr_join.c +++ b/lib/libkse/thread/thr_join.c @@ -32,13 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_join=_pthread_join + int -pthread_join(pthread_t pthread, void **thread_return) +_pthread_join(pthread_t pthread, void **thread_return) { + struct pthread *curthread = _get_curthread(); int ret = 0; _thread_enter_cancellation_point(); @@ -51,7 +53,7 @@ pthread_join(pthread_t pthread, void **thread_return) } /* Check if the caller has specified itself: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Avoid a deadlock condition: */ _thread_leave_cancellation_point(); return(EDEADLK); @@ -72,7 +74,7 @@ pthread_join(pthread_t pthread, void **thread_return) /* Check if the thread is not dead: */ else if (pthread->state != PS_DEAD) { - PTHREAD_ASSERT_NOT_IN_SYNCQ(_thread_run); + PTHREAD_ASSERT_NOT_IN_SYNCQ(curthread); /* * Enter a loop in case this thread is woken prematurely @@ -80,7 +82,7 @@ pthread_join(pthread_t pthread, void **thread_return) */ for (;;) { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Protect against being context switched out while @@ -90,25 +92,25 @@ pthread_join(pthread_t pthread, void **thread_return) /* Add the running thread to the join queue: */ TAILQ_INSERT_TAIL(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags |= PTHREAD_FLAGS_IN_JOINQ; - _thread_run->data.thread = pthread; + curthread, sqe); + curthread->flags |= PTHREAD_FLAGS_IN_JOINQ; + curthread->data.thread = pthread; /* Schedule the next thread: */ _thread_kern_sched_state(PS_JOIN, __FILE__, __LINE__); - if ((_thread_run->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { + if ((curthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread, sqe); + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } - _thread_run->data.thread = NULL; + curthread->data.thread = NULL; _thread_kern_sig_undefer(); - if (_thread_run->interrupted != 0) { - if (_thread_run->continuation != NULL) - _thread_run->continuation(_thread_run); + if (curthread->interrupted != 0) { + if (curthread->continuation != NULL) + curthread->continuation(curthread); /* * This thread was interrupted, probably to * invoke a signal handler. Make sure the @@ -134,9 +136,9 @@ pthread_join(pthread_t pthread, void **thread_return) * by the thread we're joining to when it * exits or detaches: */ - ret = _thread_run->error; + ret = curthread->error; if ((ret == 0) && (thread_return != NULL)) - *thread_return = _thread_run->ret; + *thread_return = curthread->ret; /* We're done; break out of the loop. */ break; @@ -156,11 +158,12 @@ pthread_join(pthread_t pthread, void **thread_return) void _join_backout(pthread_t pthread) { + struct pthread *curthread = _get_curthread(); + _thread_kern_sig_defer(); if ((pthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&pthread->data.thread->join_queue, pthread, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } _thread_kern_sig_undefer(); } -#endif diff --git a/lib/libkse/thread/thr_kern.c b/lib/libkse/thread/thr_kern.c index 6f4bb6e..8a32253 100644 --- a/lib/libkse/thread/thr_kern.c +++ b/lib/libkse/thread/thr_kern.c @@ -48,7 +48,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -80,6 +79,8 @@ static int called_from_handler = 0; void _thread_kern_sched_frame(struct pthread_signal_frame *psf) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a signal from interrupting this execution and @@ -88,10 +89,10 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) _thread_kern_in_sched = 1; /* Restore the signal frame: */ - _thread_sigframe_restore(_thread_run, psf); + _thread_sigframe_restore(curthread, psf); /* The signal mask was restored; check for any pending signals: */ - _thread_run->check_pending = 1; + curthread->check_pending = 1; /* Switch to the thread scheduler: */ ___longjmp(_thread_kern_sched_jb, 1); @@ -101,6 +102,8 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) void _thread_kern_sched(ucontext_t *scp) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -114,13 +117,13 @@ _thread_kern_sched(ucontext_t *scp) DBG_MSG("Entering scheduler due to signal\n"); } else { /* Save the state of the current thread: */ - if (_setjmp(_thread_run->ctx.jb) == 0) { + if (_setjmp(curthread->ctx.jb) == 0) { /* Flag the jump buffer was the last state saved: */ - _thread_run->ctxtype = CTX_JB_NOSIG; - _thread_run->longjmp_val = 1; + curthread->ctxtype = CTX_JB_NOSIG; + curthread->longjmp_val = 1; } else { DBG_MSG("Returned from ___longjmp, thread %p\n", - _thread_run); + curthread); /* * This point is reached when a longjmp() is called * to restore the state of a thread. @@ -129,10 +132,10 @@ _thread_kern_sched(ucontext_t *scp) */ _thread_kern_in_sched = 0; - if (_thread_run->sig_defer_count == 0) { - if (((_thread_run->cancelflags & + if (curthread->sig_defer_count == 0) { + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) /* * Cancellations override signals. @@ -150,7 +153,7 @@ _thread_kern_sched(ucontext_t *scp) if (_sched_switch_hook != NULL) { /* Run the installed switch hook: */ thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } return; } @@ -162,7 +165,9 @@ _thread_kern_sched(ucontext_t *scp) void _thread_kern_sched_sig(void) { - _thread_run->check_pending = 1; + struct pthread *curthread = _get_curthread(); + + curthread->check_pending = 1; _thread_kern_sched(NULL); } @@ -172,13 +177,14 @@ _thread_kern_scheduler(void) { struct timespec ts; struct timeval tv; + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; unsigned int current_tick; int add_to_prioq; /* If the currently running thread is a user thread, save it: */ - if ((_thread_run->flags & PTHREAD_FLAGS_PRIVATE) == 0) - _last_user_thread = _thread_run; + if ((curthread->flags & PTHREAD_FLAGS_PRIVATE) == 0) + _last_user_thread = curthread; if (called_from_handler != 0) { called_from_handler = 0; @@ -188,7 +194,7 @@ _thread_kern_scheduler(void) * the current thread. Restore the process signal * mask. */ - if (_thread_sys_sigprocmask(SIG_SETMASK, + if (__sys_sigprocmask(SIG_SETMASK, &_process_sigmask, NULL) != 0) PANIC("Unable to restore process mask after signal"); @@ -196,14 +202,14 @@ _thread_kern_scheduler(void) * Since the signal handler didn't return normally, we * have to tell the kernel to reuse the signal stack. */ - if (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0) + if (__sys_sigaltstack(&_thread_sigstack, NULL) != 0) PANIC("Unable to restore alternate signal stack"); } /* Are there pending signals for this thread? */ - if (_thread_run->check_pending != 0) { - _thread_run->check_pending = 0; - _thread_sig_check_pending(_thread_run); + if (curthread->check_pending != 0) { + curthread->check_pending = 0; + _thread_sig_check_pending(curthread); } /* @@ -226,22 +232,22 @@ _thread_kern_scheduler(void) _queue_signals = 1; add_to_prioq = 0; - if (_thread_run != &_thread_kern_thread) { + if (curthread != &_thread_kern_thread) { /* * This thread no longer needs to yield the CPU. */ - _thread_run->yield_on_sig_undefer = 0; + curthread->yield_on_sig_undefer = 0; - if (_thread_run->state != PS_RUNNING) { + if (curthread->state != PS_RUNNING) { /* * Save the current time as the time that the * thread became inactive: */ - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ + curthread->last_inactive =+ UINT_MAX + 1; } } @@ -250,7 +256,7 @@ _thread_kern_scheduler(void) * Place the currently running thread into the * appropriate queue(s). */ - switch (_thread_run->state) { + switch (curthread->state) { case PS_DEAD: case PS_STATE_MAX: /* to silence -Wall */ case PS_SUSPENDED: @@ -285,31 +291,31 @@ _thread_kern_scheduler(void) case PS_SIGWAIT: case PS_WAIT_WAIT: /* No timeouts for these states: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States which can timeout: */ case PS_COND_WAIT: case PS_SLEEP_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States that require periodic work: */ case PS_SPINBLOCK: /* No timeouts for this state: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Increment spinblock count: */ _spinblock_count++; @@ -320,13 +326,13 @@ _thread_kern_scheduler(void) case PS_POLL_WAIT: case PS_SELECT_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); /* Insert into the work queue: */ - PTHREAD_WORKQ_INSERT(_thread_run); + PTHREAD_WORKQ_INSERT(curthread); break; } } @@ -342,7 +348,7 @@ _thread_kern_scheduler(void) * has occurred or if we have no more runnable threads. */ else if (((current_tick = _sched_ticks) != last_tick) || - ((_thread_run->state != PS_RUNNING) && + ((curthread->state != PS_RUNNING) && (PTHREAD_PRIOQ_FIRST() == NULL))) { /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -407,43 +413,43 @@ _thread_kern_scheduler(void) * thread became inactive: */ current_tick = _sched_ticks; - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ UINT_MAX + 1; + curthread->last_inactive =+ UINT_MAX + 1; } - if ((_thread_run->slice_usec != -1) && - (_thread_run->attr.sched_policy != SCHED_FIFO)) { + if ((curthread->slice_usec != -1) && + (curthread->attr.sched_policy != SCHED_FIFO)) { /* * Accumulate the number of microseconds for * which the current thread has run: */ - _thread_run->slice_usec += - (_thread_run->last_inactive - - _thread_run->last_active) * + curthread->slice_usec += + (curthread->last_inactive - + curthread->last_active) * (long)_clock_res_usec; /* Check for time quantum exceeded: */ - if (_thread_run->slice_usec > TIMESLICE_USEC) - _thread_run->slice_usec = -1; + if (curthread->slice_usec > TIMESLICE_USEC) + curthread->slice_usec = -1; } - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* * The thread exceeded its time * quantum or it yielded the CPU; * place it at the tail of the * queue for its priority. */ - PTHREAD_PRIOQ_INSERT_TAIL(_thread_run); + PTHREAD_PRIOQ_INSERT_TAIL(curthread); } else { /* * The thread hasn't exceeded its * interval. Place it at the head * of the queue for its priority. */ - PTHREAD_PRIOQ_INSERT_HEAD(_thread_run); + PTHREAD_PRIOQ_INSERT_HEAD(curthread); } } @@ -459,9 +465,11 @@ _thread_kern_scheduler(void) * the running thread to point to the global kernel * thread structure: */ - _thread_run = &_thread_kern_thread; + _set_curthread(&_thread_kern_thread); + curthread = &_thread_kern_thread; + DBG_MSG("No runnable threads, using kernel thread %p\n", - _thread_run); + curthread); /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -532,23 +540,24 @@ _thread_kern_scheduler(void) } /* Make the selected thread the current thread: */ - _thread_run = pthread_h; + _set_curthread(pthread_h); + curthread = pthread_h; /* * Save the current time as the time that the thread * became active: */ current_tick = _sched_ticks; - _thread_run->last_active = (long) current_tick; + curthread->last_active = (long) current_tick; /* * Check if this thread is running for the first time * or running again after using its full time slice * allocation: */ - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* Reset the accumulated time slice period: */ - _thread_run->slice_usec = 0; + curthread->slice_usec = 0; } /* @@ -556,29 +565,29 @@ _thread_kern_scheduler(void) * installed switch hooks. */ if ((_sched_switch_hook != NULL) && - (_last_user_thread != _thread_run)) { + (_last_user_thread != curthread)) { thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } /* * Continue the thread at its current frame: */ - switch(_thread_run->ctxtype) { + switch(curthread->ctxtype) { case CTX_JB_NOSIG: - ___longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + ___longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_JB: - __longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + __longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_SJB: - __siglongjmp(_thread_run->ctx.sigjb, - _thread_run->longjmp_val); + __siglongjmp(curthread->ctx.sigjb, + curthread->longjmp_val); break; case CTX_UC: /* XXX - Restore FP regsisters? */ - FP_RESTORE_UC(&_thread_run->ctx.uc); + FP_RESTORE_UC(&curthread->ctx.uc); /* * Do a sigreturn to restart the thread that @@ -587,15 +596,15 @@ _thread_kern_scheduler(void) _thread_kern_in_sched = 0; #if NOT_YET - _setcontext(&_thread_run->ctx.uc); + _setcontext(&curthread->ctx.uc); #else /* * Ensure the process signal mask is set * correctly: */ - _thread_run->ctx.uc.uc_sigmask = + curthread->ctx.uc.uc_sigmask = _process_sigmask; - _thread_sys_sigreturn(&_thread_run->ctx.uc); + __sys_sigreturn(&curthread->ctx.uc); #endif break; } @@ -611,6 +620,8 @@ _thread_kern_scheduler(void) void _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -625,9 +636,9 @@ _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; /* Schedule the next thread that is ready: */ _thread_kern_sched(NULL); @@ -637,6 +648,8 @@ void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -652,9 +665,9 @@ _thread_kern_sched_state_unlock(enum pthread_state state, _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; _SPINUNLOCK(lock); @@ -817,7 +830,7 @@ thread_kern_poll(int wait_reqd) * Wait for a file descriptor to be ready for read, write, or * an exception, or a timeout to occur: */ - count = _thread_sys_poll(_thread_pfd_table, nfds, timeout_ms); + count = __sys_poll(_thread_pfd_table, nfds, timeout_ms); if (kern_pipe_added != 0) /* @@ -991,11 +1004,12 @@ thread_kern_poll(int wait_reqd) void _thread_kern_set_timeout(const struct timespec * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec current_time; struct timeval tv; /* Reset the timeout flag for the running thread: */ - _thread_run->timeout = 0; + curthread->timeout = 0; /* Check if the thread is to wait forever: */ if (timeout == NULL) { @@ -1003,28 +1017,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) * Set the wakeup time to something that can be recognised as * different to an actual time of day: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; } /* Check if no waiting is required: */ else if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) { /* Set the wake up time to 'immediately': */ - _thread_run->wakeup_time.tv_sec = 0; - _thread_run->wakeup_time.tv_nsec = 0; + curthread->wakeup_time.tv_sec = 0; + curthread->wakeup_time.tv_nsec = 0; } else { /* Get the current time: */ GET_CURRENT_TOD(tv); TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; /* Check if the nanosecond field needs to wrap: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } } } @@ -1032,24 +1046,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) void _thread_kern_sig_defer(void) { + struct pthread *curthread = _get_curthread(); + /* Allow signal deferral to be recursive. */ - _thread_run->sig_defer_count++; + curthread->sig_defer_count++; } void _thread_kern_sig_undefer(void) { + struct pthread *curthread = _get_curthread(); + /* * Perform checks to yield only if we are about to undefer * signals. */ - if (_thread_run->sig_defer_count > 1) { + if (curthread->sig_defer_count > 1) { /* Decrement the signal deferral count. */ - _thread_run->sig_defer_count--; + curthread->sig_defer_count--; } - else if (_thread_run->sig_defer_count == 1) { + else if (curthread->sig_defer_count == 1) { /* Reenable signals: */ - _thread_run->sig_defer_count = 0; + curthread->sig_defer_count = 0; /* * Check if there are queued signals: @@ -1061,8 +1079,8 @@ _thread_kern_sig_undefer(void) * Check for asynchronous cancellation before delivering any * pending signals: */ - if (((_thread_run->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) pthread_testcancel(); /* @@ -1071,9 +1089,9 @@ _thread_kern_sig_undefer(void) * * XXX - Come back and revisit the pending signal problem */ - if ((_thread_run->yield_on_sig_undefer != 0) || - SIGNOTEMPTY(_thread_run->sigpend)) { - _thread_run->yield_on_sig_undefer = 0; + if ((curthread->yield_on_sig_undefer != 0) || + SIGNOTEMPTY(curthread->sigpend)) { + curthread->yield_on_sig_undefer = 0; _thread_kern_sched(NULL); } } @@ -1088,7 +1106,7 @@ dequeue_signals(void) /* * Enter a loop to clear the pthread kernel pipe: */ - while (((num = _thread_sys_read(_thread_kern_pipe[0], bufr, + while (((num = __sys_read(_thread_kern_pipe[0], bufr, sizeof(bufr))) > 0) || (num == -1 && errno == EINTR)) { } if ((num < 0) && (errno != EAGAIN)) { @@ -1120,4 +1138,18 @@ thread_run_switch_hook(pthread_t thread_out, pthread_t thread_in) _sched_switch_hook(tid_out, tid_in); } } -#endif + +struct pthread * +_get_curthread(void) +{ + if (_thread_initial == NULL) + _thread_init(); + + return (_thread_run); +} + +void +_set_curthread(struct pthread *newthread) +{ + _thread_run = newthread; +} diff --git a/lib/libkse/thread/thr_kill.c b/lib/libkse/thread/thr_kill.c index 4bf1761..7a24ffb 100644 --- a/lib/libkse/thread/thr_kill.c +++ b/lib/libkse/thread/thr_kill.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_kill=_pthread_kill + int -pthread_kill(pthread_t pthread, int sig) +_pthread_kill(pthread_t pthread, int sig) { int ret; @@ -71,4 +72,3 @@ pthread_kill(pthread_t pthread, int sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libkse/thread/thr_mattr_init.c b/lib/libkse/thread/thr_mattr_init.c index 63d4401..4859915 100644 --- a/lib/libkse/thread/thr_mattr_init.c +++ b/lib/libkse/thread/thr_mattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_init=_pthread_mutexattr_init + int -pthread_mutexattr_init(pthread_mutexattr_t *attr) +_pthread_mutexattr_init(pthread_mutexattr_t *attr) { int ret; pthread_mutexattr_t pattr; @@ -55,4 +56,3 @@ pthread_mutexattr_init(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_mattr_kind_np.c b/lib/libkse/thread/thr_mattr_kind_np.c index 1a0832f..5ad7928 100644 --- a/lib/libkse/thread/thr_mattr_kind_np.c +++ b/lib/libkse/thread/thr_mattr_kind_np.c @@ -32,12 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_setkind_np=_pthread_mutexattr_setkind_np +#pragma weak pthread_mutexattr_getkind_np=_pthread_mutexattr_getkind_np +#pragma weak pthread_mutexattr_gettype=_pthread_mutexattr_gettype +#pragma weak pthread_mutexattr_settype=_pthread_mutexattr_settype + int -pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) +_pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) { int ret; if (attr == NULL || *attr == NULL) { @@ -51,7 +55,7 @@ pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) } int -pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) +_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) { int ret; if (attr == NULL) { @@ -64,7 +68,7 @@ pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) } int -pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) +_pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { int ret; if (attr == NULL || *attr == NULL || type >= MUTEX_TYPE_MAX) { @@ -78,7 +82,7 @@ pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) } int -pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) +_pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) { int ret; @@ -91,4 +95,3 @@ pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) } return ret; } -#endif diff --git a/lib/libkse/thread/thr_msync.c b/lib/libkse/thread/thr_msync.c index 2ae6ac7..2952da8 100644 --- a/lib/libkse/thread/thr_msync.c +++ b/lib/libkse/thread/thr_msync.c @@ -8,22 +8,23 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak msync=__msync + int _msync(void *addr, size_t len, int flags) { int ret; - ret = _thread_sys_msync(addr, len, flags); + ret = __sys_msync(addr, len, flags); return (ret); } int -msync(void *addr, size_t len, int flags) +__msync(void *addr, size_t len, int flags) { int ret; @@ -39,4 +40,3 @@ msync(void *addr, size_t len, int flags) return ret; } -#endif diff --git a/lib/libkse/thread/thr_multi_np.c b/lib/libkse/thread/thr_multi_np.c index 9a2f7c1..cf58bb4 100644 --- a/lib/libkse/thread/thr_multi_np.c +++ b/lib/libkse/thread/thr_multi_np.c @@ -32,15 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_multi_np=_pthread_multi_np + int -pthread_multi_np() +_pthread_multi_np() { /* Return to multi-threaded scheduling mode: */ _thread_single = NULL; return(0); } -#endif diff --git a/lib/libkse/thread/thr_mutex.c b/lib/libkse/thread/thr_mutex.c index e0ad7da..1c72b7b 100644 --- a/lib/libkse/thread/thr_mutex.c +++ b/lib/libkse/thread/thr_mutex.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -75,6 +74,20 @@ static inline void mutex_queue_enq(pthread_mutex_t, pthread_t); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; +static struct pthread_mutex_attr static_mutex_attr = + PTHREAD_MUTEXATTR_STATIC_INITIALIZER; +static pthread_mutexattr_t static_mattr = &static_mutex_attr; + +/* Single underscore versions provided for libc internal usage: */ +#pragma weak pthread_mutex_trylock=__pthread_mutex_trylock +#pragma weak pthread_mutex_lock=__pthread_mutex_lock + +/* No difference between libc and application usage of these: */ +#pragma weak pthread_mutex_init=_pthread_mutex_init +#pragma weak pthread_mutex_destroy=_pthread_mutex_destroy +#pragma weak pthread_mutex_unlock=_pthread_mutex_unlock + + /* Reinitialize a mutex to defaults. */ int _mutex_reinit(pthread_mutex_t * mutex) @@ -106,7 +119,7 @@ _mutex_reinit(pthread_mutex_t * mutex) } int -pthread_mutex_init(pthread_mutex_t * mutex, +_pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutex_attr) { enum pthread_mutextype type; @@ -201,7 +214,7 @@ pthread_mutex_init(pthread_mutex_t * mutex, } int -pthread_mutex_destroy(pthread_mutex_t * mutex) +_pthread_mutex_destroy(pthread_mutex_t * mutex) { int ret = 0; @@ -259,165 +272,203 @@ init_static(pthread_mutex_t *mutex) return(ret); } -int -pthread_mutex_trylock(pthread_mutex_t * mutex) +static int +init_static_private(pthread_mutex_t *mutex) { + int ret; + + _SPINLOCK(&static_init_lock); + + if (*mutex == NULL) + ret = pthread_mutex_init(mutex, &static_mattr); + else + ret = 0; + + _SPINUNLOCK(&static_init_lock); + + return(ret); +} + +static int +mutex_trylock_common(pthread_mutex_t *mutex) +{ + struct pthread *curthread = _get_curthread(); int ret = 0; - if (mutex == NULL) - ret = EINVAL; + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* - * If the mutex is statically initialized, perform the dynamic - * initialization: + * Defer signals to protect the scheduling queues from + * access by the signal handler: */ - else if (*mutex != NULL || (ret = init_static(mutex)) == 0) { - /* - * Defer signals to protect the scheduling queues from - * access by the signal handler: - */ - _thread_kern_sig_defer(); + _thread_kern_sig_defer(); - /* Lock the mutex structure: */ - _SPINLOCK(&(*mutex)->lock); + /* Lock the mutex structure: */ + _SPINLOCK(&(*mutex)->lock); - /* - * If the mutex was statically allocated, properly - * initialize the tail queue. - */ - if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { - TAILQ_INIT(&(*mutex)->m_queue); - _MUTEX_INIT_LINK(*mutex); - (*mutex)->m_flags |= MUTEX_FLAGS_INITED; - } + /* + * If the mutex was statically allocated, properly + * initialize the tail queue. + */ + if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { + TAILQ_INIT(&(*mutex)->m_queue); + _MUTEX_INIT_LINK(*mutex); + (*mutex)->m_flags |= MUTEX_FLAGS_INITED; + } - /* Process according to mutex type: */ - switch ((*mutex)->m_protocol) { - /* Default POSIX mutex: */ - case PTHREAD_PRIO_NONE: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Process according to mutex type: */ + switch ((*mutex)->m_protocol) { + /* Default POSIX mutex: */ + case PTHREAD_PRIO_NONE: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority inheritence mutex: */ - case PTHREAD_PRIO_INHERIT: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* POSIX priority inheritence mutex: */ + case PTHREAD_PRIO_INHERIT: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The mutex takes on the attributes of the - * running thread when there are no waiters. - */ - (*mutex)->m_prio = _thread_run->active_priority; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; + /* + * The mutex takes on the attributes of the + * running thread when there are no waiters. + */ + (*mutex)->m_prio = curthread->active_priority; + (*mutex)->m_saved_prio = + curthread->inherited_priority; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority protection mutex: */ - case PTHREAD_PRIO_PROTECT: - /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) - ret = EINVAL; + /* POSIX priority protection mutex: */ + case PTHREAD_PRIO_PROTECT: + /* Check for a priority ceiling violation: */ + if (curthread->active_priority > (*mutex)->m_prio) + ret = EINVAL; - /* Check if this mutex is not locked: */ - else if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Check if this mutex is not locked: */ + else if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The running thread inherits the ceiling - * priority of the mutex and executes at that - * priority. - */ - _thread_run->active_priority = (*mutex)->m_prio; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = - (*mutex)->m_prio; + /* + * The running thread inherits the ceiling + * priority of the mutex and executes at that + * priority. + */ + curthread->active_priority = (*mutex)->m_prio; + (*mutex)->m_saved_prio = + curthread->inherited_priority; + curthread->inherited_priority = + (*mutex)->m_prio; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* Trap invalid mutex types: */ - default: - /* Return an invalid argument error: */ - ret = EINVAL; - break; - } + /* Trap invalid mutex types: */ + default: + /* Return an invalid argument error: */ + ret = EINVAL; + break; + } - /* Unlock the mutex structure: */ - _SPINUNLOCK(&(*mutex)->lock); + /* Unlock the mutex structure: */ + _SPINUNLOCK(&(*mutex)->lock); - /* - * Undefer and handle pending signals, yielding if - * necessary: - */ - _thread_kern_sig_undefer(); - } + /* + * Undefer and handle pending signals, yielding if + * necessary: + */ + _thread_kern_sig_undefer(); /* Return the completion status: */ return (ret); } int -pthread_mutex_lock(pthread_mutex_t * mutex) +__pthread_mutex_trylock(pthread_mutex_t *mutex) { int ret = 0; - if (_thread_initial == NULL) - _thread_init(); - if (mutex == NULL) - return (EINVAL); + ret = EINVAL; /* * If the mutex is statically initialized, perform the dynamic * initialization: */ - if ((*mutex == NULL) && - ((ret = init_static(mutex)) != 0)) - return (ret); + else if ((*mutex != NULL) || (ret = init_static(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking the mutex private (delete safe): + */ + else if ((*mutex != NULL) || (ret = init_static_private(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +static int +mutex_lock_common(pthread_mutex_t * mutex) +{ + struct pthread *curthread = _get_curthread(); + int ret = 0; + + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* Reset the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Enter a loop waiting to become the mutex owner. We need a @@ -453,27 +504,27 @@ pthread_mutex_lock(pthread_mutex_t * mutex) case PTHREAD_PRIO_NONE: if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* * Unlock the mutex structure and schedule the @@ -492,42 +543,42 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* Check if this mutex is not locked: */ if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The mutex takes on attributes of the * running thread when there are no waiters. */ - (*mutex)->m_prio = _thread_run->active_priority; + (*mutex)->m_prio = curthread->active_priority; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; - if (_thread_run->active_priority > + if (curthread->active_priority > (*mutex)->m_prio) /* Adjust priorities: */ mutex_priority_adjust(*mutex); @@ -547,7 +598,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* POSIX priority protection mutex: */ case PTHREAD_PRIO_PROTECT: /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) + if (curthread->active_priority > (*mutex)->m_prio) ret = EINVAL; /* Check if this mutex is not locked: */ @@ -556,43 +607,43 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Lock the mutex for the running * thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The running thread inherits the ceiling * priority of the mutex and executes at that * priority: */ - _thread_run->active_priority = (*mutex)->m_prio; + curthread->active_priority = (*mutex)->m_prio; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* Clear any previous error: */ - _thread_run->error = 0; + curthread->error = 0; /* * Unlock the mutex structure and schedule the @@ -609,8 +660,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * waiting for the mutex causing a ceiling * violation. */ - ret = _thread_run->error; - _thread_run->error = 0; + ret = curthread->error; + curthread->error = 0; } break; @@ -625,8 +676,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Check to see if this thread was interrupted and * is still in the mutex queue of waiting threads: */ - if (_thread_run->interrupted != 0) - mutex_queue_remove(*mutex, _thread_run); + if (curthread->interrupted != 0) + mutex_queue_remove(*mutex, curthread); /* Unlock the mutex structure: */ _SPINUNLOCK(&(*mutex)->lock); @@ -636,19 +687,61 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * necessary: */ _thread_kern_sig_undefer(); - } while (((*mutex)->m_owner != _thread_run) && (ret == 0) && - (_thread_run->interrupted == 0)); + } while (((*mutex)->m_owner != curthread) && (ret == 0) && + (curthread->interrupted == 0)); - if (_thread_run->interrupted != 0 && - _thread_run->continuation != NULL) - _thread_run->continuation((void *) _thread_run); + if (curthread->interrupted != 0 && + curthread->continuation != NULL) + curthread->continuation((void *) curthread); /* Return the completion status: */ return (ret); } int -pthread_mutex_unlock(pthread_mutex_t * mutex) +__pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization: + */ + else if ((*mutex != NULL) || ((ret = init_static(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking it private (delete safe): + */ + else if ((*mutex != NULL) || ((ret = init_static_private(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_unlock(pthread_mutex_t * mutex) { return (mutex_unlock_common(mutex, /* add reference */ 0)); } @@ -738,6 +831,7 @@ mutex_self_lock(pthread_mutex_t mutex) static inline int mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) { + struct pthread *curthread = _get_curthread(); int ret = 0; if (mutex == NULL || *mutex == NULL) { @@ -760,7 +854,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -828,7 +922,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -852,16 +946,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -946,7 +1040,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -970,16 +1064,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -1477,4 +1571,3 @@ mutex_queue_enq(pthread_mutex_t mutex, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_MUTEXQ; } -#endif diff --git a/lib/libkse/thread/thr_mutex_prioceiling.c b/lib/libkse/thread/thr_mutex_prioceiling.c index c193c82..d19f521 100644 --- a/lib/libkse/thread/thr_mutex_prioceiling.c +++ b/lib/libkse/thread/thr_mutex_prioceiling.c @@ -34,12 +34,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprioceiling=_pthread_mutexattr_getprioceiling +#pragma weak pthread_mutexattr_setprioceiling=_pthread_mutexattr_setprioceiling +#pragma weak pthread_mutex_getprioceiling=_pthread_mutex_getprioceiling +#pragma weak pthread_mutex_setprioceiling=_pthread_mutex_setprioceiling + int -pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) +_pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) { int ret = 0; @@ -54,7 +58,7 @@ pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) } int -pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) +_pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) { int ret = 0; @@ -69,8 +73,8 @@ pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) } int -pthread_mutex_getprioceiling(pthread_mutex_t *mutex, - int *prioceiling) +_pthread_mutex_getprioceiling(pthread_mutex_t *mutex, + int *prioceiling) { int ret; @@ -85,8 +89,8 @@ pthread_mutex_getprioceiling(pthread_mutex_t *mutex, } int -pthread_mutex_setprioceiling(pthread_mutex_t *mutex, - int prioceiling, int *old_ceiling) +_pthread_mutex_setprioceiling(pthread_mutex_t *mutex, + int prioceiling, int *old_ceiling) { int ret = 0; @@ -107,4 +111,3 @@ pthread_mutex_setprioceiling(pthread_mutex_t *mutex, } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_mutex_protocol.c b/lib/libkse/thread/thr_mutex_protocol.c index 9847ae5..1ad9b15 100644 --- a/lib/libkse/thread/thr_mutex_protocol.c +++ b/lib/libkse/thread/thr_mutex_protocol.c @@ -34,12 +34,14 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprotocol=_pthread_mutexattr_getprotocol +#pragma weak pthread_mutexattr_setprotocol=_pthread_mutexattr_setprotocol + int -pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) +_pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) { int ret = 0; @@ -52,7 +54,7 @@ pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) } int -pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) +_pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) { int ret = 0; @@ -66,4 +68,3 @@ pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_mutexattr_destroy.c b/lib/libkse/thread/thr_mutexattr_destroy.c index 6eed2a2..cc766f0 100644 --- a/lib/libkse/thread/thr_mutexattr_destroy.c +++ b/lib/libkse/thread/thr_mutexattr_destroy.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_destroy=_pthread_mutexattr_destroy + int -pthread_mutexattr_destroy(pthread_mutexattr_t *attr) +_pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -50,4 +51,3 @@ pthread_mutexattr_destroy(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_nanosleep.c b/lib/libkse/thread/thr_nanosleep.c index 67dd08d..54349d4 100644 --- a/lib/libkse/thread/thr_nanosleep.c +++ b/lib/libkse/thread/thr_nanosleep.c @@ -33,14 +33,16 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak nanosleep=__nanosleep + int _nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { + struct pthread *curthread = _get_curthread(); int ret = 0; struct timespec current_time; struct timespec current_time1; @@ -63,16 +65,16 @@ _nanosleep(const struct timespec * time_to_sleep, TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; /* Check if the nanosecond field has overflowed: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Reschedule the current thread to sleep: */ _thread_kern_sched_state(PS_SLEEP_WAIT, __FILE__, __LINE__); @@ -118,7 +120,7 @@ _nanosleep(const struct timespec * time_to_sleep, } /* Check if the sleep was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an EINTR error : */ errno = EINTR; ret = -1; @@ -128,7 +130,7 @@ _nanosleep(const struct timespec * time_to_sleep, } int -nanosleep(const struct timespec * time_to_sleep, struct timespec * +__nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { int ret; @@ -139,4 +141,3 @@ nanosleep(const struct timespec * time_to_sleep, struct timespec * return ret; } -#endif diff --git a/lib/libkse/thread/thr_once.c b/lib/libkse/thread/thr_once.c index d7bb494..dde5474 100644 --- a/lib/libkse/thread/thr_once.c +++ b/lib/libkse/thread/thr_once.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_once=_pthread_once + int -pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) +_pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) { if (once_control->state == PTHREAD_NEEDS_INIT) { if (_thread_initial == NULL) @@ -50,4 +51,3 @@ pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) } return (0); } -#endif diff --git a/lib/libkse/thread/thr_open.c b/lib/libkse/thread/thr_open.c index e7fef91..37bd43d 100644 --- a/lib/libkse/thread/thr_open.c +++ b/lib/libkse/thread/thr_open.c @@ -37,10 +37,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak open=_open + int _open(const char *path, int flags,...) { @@ -56,12 +57,12 @@ _open(const char *path, int flags,...) va_end(ap); } /* Open the file: */ - if ((fd = _thread_sys_open(path, flags, mode)) < 0) { + if ((fd = __sys_open(path, flags, mode)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(fd) != 0) { /* Quietly close the file: */ - _thread_sys_close(fd); + __sys_close(fd); /* Reset the file descriptor: */ fd = -1; @@ -72,7 +73,7 @@ _open(const char *path, int flags,...) } int -open(const char *path, int flags,...) +__open(const char *path, int flags,...) { int ret; int mode = 0; @@ -93,4 +94,3 @@ open(const char *path, int flags,...) return ret; } -#endif diff --git a/lib/libkse/thread/thr_pause.c b/lib/libkse/thread/thr_pause.c index 09e4c17..19501df 100644 --- a/lib/libkse/thread/thr_pause.c +++ b/lib/libkse/thread/thr_pause.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pause=_pause + int -pause(void) +_pause(void) { int ret; @@ -45,4 +46,3 @@ pause(void) return ret; } -#endif diff --git a/lib/libkse/thread/thr_poll.c b/lib/libkse/thread/thr_poll.c index 3717273..d3f631a 100644 --- a/lib/libkse/thread/thr_poll.c +++ b/lib/libkse/thread/thr_poll.c @@ -38,14 +38,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak poll=_poll int _poll(struct pollfd *fds, unsigned int nfds, int timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int numfds = nfds; int i, ret = 0; @@ -71,7 +72,7 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (-1); } - if (((ret = _thread_sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { + if (((ret = __sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { data.nfds = numfds; data.fds = fds; @@ -83,10 +84,10 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) fds[i].revents = 0; } - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_POLL_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; } else { @@ -96,6 +97,3 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (ret); } - -__strong_reference(_poll, poll); -#endif diff --git a/lib/libkse/thread/thr_priority_queue.c b/lib/libkse/thread/thr_priority_queue.c index 84c3065..55d742b 100644 --- a/lib/libkse/thread/thr_priority_queue.c +++ b/lib/libkse/thread/thr_priority_queue.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -71,7 +70,7 @@ static int _pq_active = 0; } while (0) #define _PQ_ASSERT_PROTECTED(msg) \ PTHREAD_ASSERT((_thread_kern_in_sched != 0) || \ - (_thread_run->sig_defer_count > 0) || \ + ((_get_curthread())->sig_defer_count > 0) ||\ (_sig_in_handler != 0), msg); #else @@ -336,4 +335,3 @@ _waitq_clearactive(void) _PQ_ASSERT_ACTIVE("_waitq_clearactive: ! pq_active"); _PQ_CLEAR_ACTIVE(); } -#endif diff --git a/lib/libkse/thread/thr_private.h b/lib/libkse/thread/thr_private.h index 2c9a469..688a2b3 100644 --- a/lib/libkse/thread/thr_private.h +++ b/lib/libkse/thread/thr_private.h @@ -313,6 +313,9 @@ struct pthread_mutex_attr { long m_flags; }; +#define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \ + { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE } + /* * Condition variable definitions. */ @@ -1207,6 +1210,8 @@ int _find_dead_thread(pthread_t); int _find_thread(pthread_t); void _flockfile_backout(pthread_t); void _funlock_owned(pthread_t); +struct pthread *_get_curthread(void); +void _set_curthread(struct pthread *); void _join_backout(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 *); @@ -1224,6 +1229,19 @@ void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); +void *_pthread_getspecific(pthread_key_t); +int _pthread_key_create(pthread_key_t *, void (*) (void *)); +int _pthread_key_delete(pthread_key_t); +int _pthread_mutex_destroy(pthread_mutex_t *); +int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int _pthread_mutex_lock(pthread_mutex_t *); +int _pthread_mutex_trylock(pthread_mutex_t *); +int _pthread_mutex_unlock(pthread_mutex_t *); +int _pthread_mutexattr_init(pthread_mutexattr_t *); +int _pthread_mutexattr_destroy(pthread_mutexattr_t *); +int _pthread_mutexattr_settype(pthread_mutexattr_t *, int); +int _pthread_once(pthread_once_t *, void (*) (void)); +int _pthread_setspecific(pthread_key_t, const void *); void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); #if defined(_PTHREADS_INVARIANTS) @@ -1263,190 +1281,113 @@ void _thread_enter_cancellation_point(void); void _thread_leave_cancellation_point(void); void _thread_cancellation_point(void); +/* #include */ +#ifdef _AIO_H_ +int __sys_aio_suspend(const struct aiocb **, int, const struct timespec); +#endif + /* #include */ -int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); -int _thread_sys_sigpending(sigset_t *); -int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); -int _thread_sys_sigsuspend(const sigset_t *); -int _thread_sys_siginterrupt(int, int); -int _thread_sys_sigpause(int); -int _thread_sys_sigreturn(ucontext_t *); -int _thread_sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); -int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); -int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); -void _thread_sys_psignal(unsigned int, const char *); -void (*_thread_sys_signal(int, void (*)(int)))(int); +#ifdef _SIGNAL_H_ +int __sys_sigaction(int, const struct sigaction *, struct sigaction *); +int __sys_sigpending(sigset_t *); +int __sys_sigprocmask(int, const sigset_t *, sigset_t *); +int __sys_sigsuspend(const sigset_t *); +int __sys_sigreturn(ucontext_t *); +int __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); +#endif /* #include */ #ifdef _SYS_STAT_H_ -int _thread_sys_fchmod(int, mode_t); -int _thread_sys_fstat(int, struct stat *); -int _thread_sys_fchflags(int, u_long); +int __sys_fchmod(int, mode_t); +int __sys_fstat(int, struct stat *); +int __sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ -int _thread_sys_fstatfs(int, struct statfs *); +int __sys_fstatfs(int, struct statfs *); #endif -int _thread_sys_pipe(int *); -/* #include */ -#ifdef _SYS_SOCKET_H_ -int _thread_sys_accept(int, struct sockaddr *, int *); -int _thread_sys_bind(int, const struct sockaddr *, int); -int _thread_sys_connect(int, const struct sockaddr *, int); -int _thread_sys_getpeername(int, struct sockaddr *, int *); -int _thread_sys_getsockname(int, struct sockaddr *, int *); -int _thread_sys_getsockopt(int, int, int, void *, int *); -int _thread_sys_listen(int, int); -int _thread_sys_setsockopt(int, int, int, const void *, int); -int _thread_sys_shutdown(int, int); -int _thread_sys_socket(int, int, int); -int _thread_sys_socketpair(int, int, int, int *); -ssize_t _thread_sys_recv(int, void *, size_t, int); -ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); -ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); -ssize_t _thread_sys_send(int, const void *, size_t, int); -ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); -ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); +/* #inclde */ +#ifdef _SYS_EVENT_H_ +int __sys_kevent(int, const struct kevent *, int, struct kevent *, + int, const struct timespec *); #endif -/* #include */ -#ifdef _STDIO_H_ -FILE *_thread_sys_fdopen(int, const char *); -FILE *_thread_sys_fopen(const char *, const char *); -FILE *_thread_sys_freopen(const char *, const char *, FILE *); -FILE *_thread_sys_popen(const char *, const char *); -FILE *_thread_sys_tmpfile(void); -char *_thread_sys_ctermid(char *); -char *_thread_sys_cuserid(char *); -char *_thread_sys_fgetln(FILE *, size_t *); -char *_thread_sys_fgets(char *, int, FILE *); -char *_thread_sys_gets(char *); -char *_thread_sys_tempnam(const char *, const char *); -char *_thread_sys_tmpnam(char *); -int _thread_sys_fclose(FILE *); -int _thread_sys_feof(FILE *); -int _thread_sys_ferror(FILE *); -int _thread_sys_fflush(FILE *); -int _thread_sys_fgetc(FILE *); -int _thread_sys_fgetpos(FILE *, fpos_t *); -int _thread_sys_fileno(FILE *); -int _thread_sys_fprintf(FILE *, const char *, ...); -int _thread_sys_fpurge(FILE *); -int _thread_sys_fputc(int, FILE *); -int _thread_sys_fputs(const char *, FILE *); -int _thread_sys_fscanf(FILE *, const char *, ...); -int _thread_sys_fseek(FILE *, long, int); -int _thread_sys_fsetpos(FILE *, const fpos_t *); -int _thread_sys_getc(FILE *); -int _thread_sys_getchar(void); -int _thread_sys_getw(FILE *); -int _thread_sys_pclose(FILE *); -int _thread_sys_printf(const char *, ...); -int _thread_sys_putc(int, FILE *); -int _thread_sys_putchar(int); -int _thread_sys_puts(const char *); -int _thread_sys_putw(int, FILE *); -int _thread_sys_remove(const char *); -int _thread_sys_rename (const char *, const char *); -int _thread_sys_scanf(const char *, ...); -int _thread_sys_setlinebuf(FILE *); -int _thread_sys_setvbuf(FILE *, char *, int, size_t); -int _thread_sys_snprintf(char *, size_t, const char *, ...); -int _thread_sys_sprintf(char *, const char *, ...); -int _thread_sys_sscanf(const char *, const char *, ...); -int _thread_sys_ungetc(int, FILE *); -int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); -int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); -int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); -int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); -int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); -int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); -long _thread_sys_ftell(FILE *); -size_t _thread_sys_fread(void *, size_t, size_t, FILE *); -size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); -void _thread_sys_clearerr(FILE *); -void _thread_sys_perror(const char *); -void _thread_sys_rewind(FILE *); -void _thread_sys_setbuf(FILE *, char *); -void _thread_sys_setbuffer(FILE *, char *, int); +/* #include */ +#ifdef _SYS_SOCKET_H_ +int __sys_accept(int, struct sockaddr *, int *); +int __sys_bind(int, const struct sockaddr *, int); +int __sys_connect(int, const struct sockaddr *, int); +int __sys_getpeername(int, struct sockaddr *, int *); +int __sys_getsockname(int, struct sockaddr *, int *); +int __sys_getsockopt(int, int, int, void *, int *); +int __sys_listen(int, int); +int __sys_setsockopt(int, int, int, const void *, int); +int __sys_shutdown(int, int); +int __sys_socket(int, int, int); +int __sys_socketpair(int, int, int, int *); +ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); +ssize_t __sys_recvmsg(int, struct msghdr *, int); +ssize_t __sys_send(int, const void *, size_t, int); +int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int); +ssize_t __sys_sendmsg(int, const struct msghdr *, int); +ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _UNISTD_H_ -char *_thread_sys_ttyname(int); -int _thread_sys_close(int); -int _thread_sys_dup(int); -int _thread_sys_dup2(int, int); -int _thread_sys_exect(const char *, char * const *, char * const *); -int _thread_sys_execve(const char *, char * const *, char * const *); -int _thread_sys_fchdir(int); -int _thread_sys_fchown(int, uid_t, gid_t); -int _thread_sys_fsync(int); -int _thread_sys_ftruncate(int, off_t); -int _thread_sys_pause(void); -int _thread_sys_pipe(int *); -int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); -long _thread_sys_fpathconf(int, int); -off_t _thread_sys_lseek(int, off_t, int); -pid_t _thread_sys_fork(void); -pid_t _thread_sys_tcgetpgrp(int); -ssize_t _thread_sys_read(int, void *, size_t); -ssize_t _thread_sys_write(int, const void *, size_t); -void _thread_sys__exit(int); +int __sys_close(int); +int __sys_dup(int); +int __sys_dup2(int, int); +int __sys_execve(const char *, char * const *, char * const *); +int __sys_fchown(int, uid_t, gid_t); +int __sys_fork(void); +int __sys_fsync(int); +int __sys_pipe(int *); +int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); +long __sys_fpathconf(int, int); +ssize_t __sys_read(int, void *, size_t); +ssize_t __sys_write(int, const void *, size_t); +void __sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ -int _thread_sys_creat(const char *, mode_t); -int _thread_sys_fcntl(int, int, ...); -int _thread_sys_flock(int, int); -int _thread_sys_open(const char *, int, ...); +int __sys_fcntl(int, int, ...); +int __sys_flock(int, int); +int __sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ -int _thread_sys_ioctl(int, unsigned long, ...); +int __sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ -DIR *___thread_sys_opendir2(const char *, int); -DIR *_thread_sys_opendir(const char *); -int _thread_sys_alphasort(const void *, const void *); -int _thread_sys_scandir(const char *, struct dirent ***, - int (*)(struct dirent *), int (*)(const void *, const void *)); -int _thread_sys_closedir(DIR *); -int _thread_sys_getdirentries(int, char *, int, long *); -long _thread_sys_telldir(const DIR *); -struct dirent *_thread_sys_readdir(DIR *); -void _thread_sys_rewinddir(DIR *); -void _thread_sys_seekdir(DIR *, long); +int __sys_getdirentries(int, char *, int, long *); #endif /* #include */ #ifdef _SYS_UIO_H_ -ssize_t _thread_sys_readv(int, const struct iovec *, int); -ssize_t _thread_sys_writev(int, const struct iovec *, int); +ssize_t __sys_readv(int, const struct iovec *, int); +ssize_t __sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG -pid_t _thread_sys_wait(int *); -pid_t _thread_sys_waitpid(pid_t, int *, int); -pid_t _thread_sys_wait3(int *, int, struct rusage *); -pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); +pid_t __sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ -int _thread_sys_poll(struct pollfd *, unsigned, int); +int __sys_poll(struct pollfd *, unsigned, int); #endif /* #include */ #ifdef _SYS_MMAN_H_ -int _thread_sys_msync(void *, size_t, int); +int __sys_msync(void *, size_t, int); #endif /* #include */ diff --git a/lib/libkse/thread/thr_read.c b/lib/libkse/thread/thr_read.c index b4d9694..5f5475d 100644 --- a/lib/libkse/thread/thr_read.c +++ b/lib/libkse/thread/thr_read.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak read=__read + ssize_t _read(int fd, void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -66,14 +68,14 @@ _read(int fd, void *buf, size_t nbytes) } /* Perform a non-blocking read syscall: */ - while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) { + while ((ret = __sys_read(fd, buf, nbytes)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -82,7 +84,7 @@ _read(int fd, void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -97,7 +99,7 @@ _read(int fd, void *buf, size_t nbytes) } ssize_t -read(int fd, void *buf, size_t nbytes) +__read(int fd, void *buf, size_t nbytes) { ssize_t ret; @@ -107,4 +109,3 @@ read(int fd, void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libkse/thread/thr_readv.c b/lib/libkse/thread/thr_readv.c index d8e02c0..2e14a9f 100644 --- a/lib/libkse/thread/thr_readv.c +++ b/lib/libkse/thread/thr_readv.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak readv=_readv + ssize_t _readv(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -61,14 +63,14 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } /* Perform a non-blocking readv syscall: */ - while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) { + while ((ret = __sys_readv(fd, iov, iovcnt)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -77,7 +79,7 @@ _readv(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -90,6 +92,3 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } return (ret); } - -__strong_reference(_readv, readv); -#endif diff --git a/lib/libkse/thread/thr_resume_np.c b/lib/libkse/thread/thr_resume_np.c index 32b3d05..0d4738d 100644 --- a/lib/libkse/thread/thr_resume_np.c +++ b/lib/libkse/thread/thr_resume_np.c @@ -32,13 +32,14 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_resume_np=_pthread_resume_np + /* Resume a thread: */ int -pthread_resume_np(pthread_t thread) +_pthread_resume_np(pthread_t thread) { int ret; enum pthread_susp old_suspended; @@ -89,4 +90,3 @@ pthread_resume_np(pthread_t thread) } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_rwlock.c b/lib/libkse/thread/thr_rwlock.c index 648e8a3..9c87e44 100644 --- a/lib/libkse/thread/thr_rwlock.c +++ b/lib/libkse/thread/thr_rwlock.c @@ -26,7 +26,6 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include @@ -37,6 +36,14 @@ /* maximum number of times a read lock may be obtained */ #define MAX_READ_LOCKS (INT_MAX - 1) +#pragma weak pthread_rwlock_destroy=_pthread_rwlock_destroy +#pragma weak pthread_rwlock_init=_pthread_rwlock_init +#pragma weak pthread_rwlock_rdlock=_pthread_rwlock_rdlock +#pragma weak pthread_rwlock_tryrdlock=_pthread_rwlock_tryrdlock +#pragma weak pthread_rwlock_trywrlock=_pthread_rwlock_trywrlock +#pragma weak pthread_rwlock_unlock=_pthread_rwlock_unlock +#pragma weak pthread_rwlock_wrlock=_pthread_rwlock_wrlock + static int init_static (pthread_rwlock_t *rwlock); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; @@ -59,7 +66,7 @@ init_static (pthread_rwlock_t *rwlock) } int -pthread_rwlock_destroy (pthread_rwlock_t *rwlock) +_pthread_rwlock_destroy (pthread_rwlock_t *rwlock) { int ret; @@ -84,7 +91,7 @@ pthread_rwlock_destroy (pthread_rwlock_t *rwlock) } int -pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) +_pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { pthread_rwlock_t prwlock; int ret; @@ -127,7 +134,7 @@ pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) } int -pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -178,7 +185,7 @@ pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -215,7 +222,7 @@ pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -250,7 +257,7 @@ pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_unlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_unlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -287,7 +294,7 @@ pthread_rwlock_unlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -332,4 +339,3 @@ pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) return(ret); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libkse/thread/thr_rwlockattr.c b/lib/libkse/thread/thr_rwlockattr.c index 37eb3ea..7dcaaeb 100644 --- a/lib/libkse/thread/thr_rwlockattr.c +++ b/lib/libkse/thread/thr_rwlockattr.c @@ -26,15 +26,19 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include #include "pthread_private.h" +#pragma weak pthread_rwlockattr_destroy=_pthread_rwlockattr_destroy +#pragma weak pthread_rwlockattr_getpshared=_pthread_rwlockattr_getpshared +#pragma weak pthread_rwlockattr_init=_pthread_rwlockattr_init +#pragma weak pthread_rwlockattr_setpshared=_pthread_rwlockattr_setpshared + int -pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -52,7 +56,7 @@ pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, +_pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, int *pshared) { *pshared = (*rwlockattr)->pshared; @@ -61,7 +65,7 @@ pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, } int -pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -81,7 +85,7 @@ pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) +_pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) { /* Only PTHREAD_PROCESS_PRIVATE is supported. */ if (pshared != PTHREAD_PROCESS_PRIVATE) @@ -92,4 +96,3 @@ pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) return(0); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libkse/thread/thr_select.c b/lib/libkse/thread/thr_select.c index 0a7c0d4..547046a 100644 --- a/lib/libkse/thread/thr_select.c +++ b/lib/libkse/thread/thr_select.c @@ -40,14 +40,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak select=_select + int _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int i, ret = 0, f_wait = 1; int pfd_index, got_one = 0, fd_count = 0; @@ -91,9 +93,9 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * Allocate memory for poll data if it hasn't already been * allocated or if previously allocated memory is insufficient. */ - if ((_thread_run->poll_data.fds == NULL) || - (_thread_run->poll_data.nfds < fd_count)) { - data.fds = (struct pollfd *) realloc(_thread_run->poll_data.fds, + if ((curthread->poll_data.fds == NULL) || + (curthread->poll_data.nfds < fd_count)) { + data.fds = (struct pollfd *) realloc(curthread->poll_data.fds, sizeof(struct pollfd) * MAX(128, fd_count)); if (data.fds == NULL) { errno = ENOMEM; @@ -105,13 +107,13 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * indicates what is allocated, not what is * currently being polled. */ - _thread_run->poll_data.fds = data.fds; - _thread_run->poll_data.nfds = MAX(128, fd_count); + curthread->poll_data.fds = data.fds; + curthread->poll_data.nfds = MAX(128, fd_count); } } if (ret == 0) { /* Setup the wait data. */ - data.fds = _thread_run->poll_data.fds; + data.fds = curthread->poll_data.fds; data.nfds = fd_count; /* @@ -142,12 +144,12 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, pfd_index--; } } - if (((ret = _thread_sys_poll(data.fds, data.nfds, 0)) == 0) && + if (((ret = __sys_poll(data.fds, data.nfds, 0)) == 0) && (f_wait != 0)) { - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_SELECT_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; data.nfds = 0; ret = -1; @@ -203,6 +205,3 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, return (ret); } - -__strong_reference(_select, select); -#endif diff --git a/lib/libkse/thread/thr_self.c b/lib/libkse/thread/thr_self.c index 81ec427..3c92daa 100644 --- a/lib/libkse/thread/thr_self.c +++ b/lib/libkse/thread/thr_self.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_self=_pthread_self + pthread_t -pthread_self(void) +_pthread_self(void) { /* Return the running thread pointer: */ - return (_thread_run); + return (_get_curthread()); } -#endif diff --git a/lib/libkse/thread/thr_sem.c b/lib/libkse/thread/thr_sem.c index eb42919..926f498 100644 --- a/lib/libkse/thread/thr_sem.c +++ b/lib/libkse/thread/thr_sem.c @@ -31,7 +31,6 @@ #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -43,8 +42,19 @@ goto RETURN; \ } +#pragma weak sem_init=_sem_init +#pragma weak sem_destroy=_sem_destroy +#pragma weak sem_open=_sem_open +#pragma weak sem_close=_sem_close +#pragma weak sem_unlink=_sem_unlink +#pragma weak sem_wait=_sem_wait +#pragma weak sem_trywait=_sem_trywait +#pragma weak sem_post=_sem_post +#pragma weak sem_getvalue=_sem_getvalue + + int -sem_init(sem_t *sem, int pshared, unsigned int value) +_sem_init(sem_t *sem, int pshared, unsigned int value) { int retval; @@ -103,7 +113,7 @@ sem_init(sem_t *sem, int pshared, unsigned int value) } int -sem_destroy(sem_t *sem) +_sem_destroy(sem_t *sem) { int retval; @@ -131,28 +141,28 @@ sem_destroy(sem_t *sem) } sem_t * -sem_open(const char *name, int oflag, ...) +_sem_open(const char *name, int oflag, ...) { errno = ENOSYS; return SEM_FAILED; } int -sem_close(sem_t *sem) +_sem_close(sem_t *sem) { errno = ENOSYS; return -1; } int -sem_unlink(const char *name) +_sem_unlink(const char *name) { errno = ENOSYS; return -1; } int -sem_wait(sem_t *sem) +_sem_wait(sem_t *sem) { int retval; @@ -178,7 +188,7 @@ sem_wait(sem_t *sem) } int -sem_trywait(sem_t *sem) +_sem_trywait(sem_t *sem) { int retval; @@ -201,7 +211,7 @@ sem_trywait(sem_t *sem) } int -sem_post(sem_t *sem) +_sem_post(sem_t *sem) { int retval; @@ -235,7 +245,7 @@ sem_post(sem_t *sem) } int -sem_getvalue(sem_t *sem, int *sval) +_sem_getvalue(sem_t *sem, int *sval) { int retval; @@ -249,4 +259,3 @@ sem_getvalue(sem_t *sem, int *sval) RETURN: return retval; } -#endif diff --git a/lib/libkse/thread/thr_seterrno.c b/lib/libkse/thread/thr_seterrno.c index 1934aac..0d9474f 100644 --- a/lib/libkse/thread/thr_seterrno.c +++ b/lib/libkse/thread/thr_seterrno.c @@ -31,7 +31,6 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -58,4 +57,3 @@ _thread_seterrno(pthread_t thread, int error) */ thread->error = error; } -#endif diff --git a/lib/libkse/thread/thr_setprio.c b/lib/libkse/thread/thr_setprio.c index c630db5..eee05dc 100644 --- a/lib/libkse/thread/thr_setprio.c +++ b/lib/libkse/thread/thr_setprio.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setprio=_pthread_setprio + int -pthread_setprio(pthread_t pthread, int prio) +_pthread_setprio(pthread_t pthread, int prio) { int ret, policy; struct sched_param param; @@ -49,4 +50,3 @@ pthread_setprio(pthread_t pthread, int prio) /* Return the error status: */ return (ret); } -#endif diff --git a/lib/libkse/thread/thr_setschedparam.c b/lib/libkse/thread/thr_setschedparam.c index 9a44ad7..a03f036 100644 --- a/lib/libkse/thread/thr_setschedparam.c +++ b/lib/libkse/thread/thr_setschedparam.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setschedparam=_pthread_setschedparam + int -pthread_setschedparam(pthread_t pthread, int policy, +_pthread_setschedparam(pthread_t pthread, int policy, const struct sched_param *param) { int old_prio, in_readyq = 0, ret = 0; @@ -116,4 +117,3 @@ pthread_setschedparam(pthread_t pthread, int policy, } return(ret); } -#endif diff --git a/lib/libkse/thread/thr_sig.c b/lib/libkse/thread/thr_sig.c index a855a61..9559bb7 100644 --- a/lib/libkse/thread/thr_sig.c +++ b/lib/libkse/thread/thr_sig.c @@ -39,7 +39,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -70,6 +69,7 @@ static void thread_sigframe_save(pthread_t thread, struct pthread_signal_frame * void _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) { + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; void *stackp; int in_sched = 0; @@ -77,7 +77,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) if (ucp == NULL) PANIC("Thread signal handler received null context"); - DBG_MSG("Got signal %d, current thread %p\n", sig, _thread_run); + DBG_MSG("Got signal %d, current thread %p\n", sig, curthread); if (_thread_kern_in_sched != 0) in_sched = 1; @@ -104,13 +104,13 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * the currently running thread has deferred thread * signals. */ - else if (_thread_run->sig_defer_count > 0) - _thread_run->yield_on_sig_undefer = 1; + else if (curthread->sig_defer_count > 0) + curthread->yield_on_sig_undefer = 1; else { /* * Save the context of the currently running thread: */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* * Schedule the next thread. This function is not @@ -131,7 +131,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * is accessing the scheduling queues or if there is a currently * running thread that has deferred signals. */ - else if ((in_sched != 0) || (_thread_run->sig_defer_count > 0)) { + else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) { /* Cast the signal number to a character variable: */ c = sig; @@ -140,7 +140,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * be ready to read when this signal handler returns. */ if (_queue_signals != 0) { - _thread_sys_write(_thread_kern_pipe[1], &c, 1); + __sys_write(_thread_kern_pipe[1], &c, 1); DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig); } if (_thread_sigq[sig - 1].blocked == 0) { @@ -221,7 +221,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * This also applies if the current thread is the * thread to be signaled. */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* Setup the target thread to receive the signal: */ thread_sig_add(pthread, sig, /*has_args*/ 1); @@ -241,8 +241,8 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * signal and the currently running thread is not in a * signal handler. */ - if ((pthread == _thread_run) || ((pthread_h != NULL) && - (pthread_h->active_priority > _thread_run->active_priority))) { + if ((pthread == curthread) || ((pthread_h != NULL) && + (pthread_h->active_priority > curthread->active_priority))) { /* Enter the kernel scheduler: */ _thread_kern_sched(ucp); } @@ -272,6 +272,7 @@ thread_sig_savecontext(pthread_t pthread, ucontext_t *ucp) pthread_t thread_sig_find(int sig) { + struct pthread *curthread = _get_curthread(); int handler_installed; pthread_t pthread, pthread_next; pthread_t suspended_thread, signaled_thread; @@ -302,9 +303,9 @@ thread_sig_find(int sig) * installed, the signal only affects threads in sigwait. */ suspended_thread = NULL; - if ((_thread_run != &_thread_kern_thread) && - !sigismember(&_thread_run->sigmask, sig)) - signaled_thread = _thread_run; + if ((curthread != &_thread_kern_thread) && + !sigismember(&curthread->sigmask, sig)) + signaled_thread = curthread; else signaled_thread = NULL; if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) || @@ -508,7 +509,7 @@ thread_sig_handle_special(int sig) /* * Set the file descriptor to non-blocking: */ - _thread_sys_fcntl(i, F_SETFL, + __sys_fcntl(i, F_SETFL, _thread_fd_table[i]->flags | O_NONBLOCK); } } @@ -568,6 +569,7 @@ thread_sig_handle_special(int sig) static void thread_sig_add(pthread_t pthread, int sig, int has_args) { + struct pthread *curthread = _get_curthread(); int restart; int suppress_handler = 0; @@ -751,7 +753,7 @@ thread_sig_add(pthread_t pthread, int sig, int has_args) * the thread in the run queue. */ pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY; - if (pthread != _thread_run) + if (pthread != curthread) PTHREAD_PRIOQ_INSERT_TAIL(pthread); } } @@ -849,6 +851,8 @@ thread_sig_check_state(pthread_t pthread, int sig) void _thread_sig_send(pthread_t pthread, int sig) { + struct pthread *curthread = _get_curthread(); + /* Check for signals whose actions are SIG_DFL: */ if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) { /* @@ -878,7 +882,7 @@ _thread_sig_send(pthread_t pthread, int sig) /* Return the signal number: */ pthread->signo = sig; - } else if (pthread == _thread_run) { + } else if (pthread == curthread) { /* Add the signal to the pending set: */ sigaddset(&pthread->sigpend, sig); if (!sigismember(&pthread->sigmask, sig)) { @@ -915,9 +919,7 @@ _thread_sig_wrapper(void) { void (*sigfunc)(int, siginfo_t *, void *); struct pthread_signal_frame *psf; - pthread_t thread; - - thread = _thread_run; + struct pthread *thread = _get_curthread(); /* Get the current frame and state: */ psf = thread->curframe; @@ -1117,4 +1119,3 @@ thread_sigframe_save(pthread_t thread, struct pthread_signal_frame *psf) psf->saved_state.psd_sig_defer_count = thread->sig_defer_count; } -#endif diff --git a/lib/libkse/thread/thr_sigaction.c b/lib/libkse/thread/thr_sigaction.c index 4d13819..dba2f7e 100644 --- a/lib/libkse/thread/thr_sigaction.c +++ b/lib/libkse/thread/thr_sigaction.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigaction=_sigaction + int _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) { @@ -49,6 +50,9 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) errno = EINVAL; ret = -1; } else { + if (_thread_initial == NULL) + _thread_init(); + /* * Check if the existing signal action structure contents are * to be returned: @@ -98,7 +102,7 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) gact.sa_handler = (void (*) ()) _thread_sig_handler; /* Change the signal action in the kernel: */ - if (_thread_sys_sigaction(sig,&gact,NULL) != 0) + if (__sys_sigaction(sig,&gact,NULL) != 0) ret = -1; } } @@ -106,6 +110,3 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) /* Return the completion status: */ return (ret); } - -__strong_reference(_sigaction, sigaction); -#endif diff --git a/lib/libkse/thread/thr_sigmask.c b/lib/libkse/thread/thr_sigmask.c index 53d0774..c16d66b 100644 --- a/lib/libkse/thread/thr_sigmask.c +++ b/lib/libkse/thread/thr_sigmask.c @@ -36,20 +36,22 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_sigmask=_pthread_sigmask + int -pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) +_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { + struct pthread *curthread = _get_curthread(); sigset_t sigset; int ret = 0; /* Check if the existing signal process mask is to be returned: */ if (oset != NULL) { /* Return the current mask: */ - *oset = _thread_run->sigmask; + *oset = curthread->sigmask; } /* Check if a new signal set was provided by the caller: */ if (set != NULL) { @@ -58,19 +60,19 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Block signals: */ case SIG_BLOCK: /* Add signals to the existing mask: */ - SIGSETOR(_thread_run->sigmask, *set); + SIGSETOR(curthread->sigmask, *set); break; /* Unblock signals: */ case SIG_UNBLOCK: /* Clear signals from the existing mask: */ - SIGSETNAND(_thread_run->sigmask, *set); + SIGSETNAND(curthread->sigmask, *set); break; /* Set the signal process mask: */ case SIG_SETMASK: /* Set the new mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; break; /* Trap invalid actions: */ @@ -82,15 +84,15 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) } /* Increment the sequence number: */ - _thread_run->sigmask_seqno++; + curthread->sigmask_seqno++; /* * Check if there are pending signals for the running * thread or process that aren't blocked: */ - sigset = _thread_run->sigpend; + sigset = curthread->sigpend; SIGSETOR(sigset, _process_sigpending); - SIGSETNAND(sigset, _thread_run->sigmask); + SIGSETNAND(sigset, curthread->sigmask); if (SIGNOTEMPTY(sigset)) /* * Call the kernel scheduler which will safely @@ -102,4 +104,3 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libkse/thread/thr_sigpending.c b/lib/libkse/thread/thr_sigpending.c index a118347..98b7d26 100644 --- a/lib/libkse/thread/thr_sigpending.c +++ b/lib/libkse/thread/thr_sigpending.c @@ -33,13 +33,15 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigpending=_sigpending + int _sigpending(sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = 0; /* Check for a null signal set pointer: */ @@ -48,11 +50,8 @@ _sigpending(sigset_t * set) ret = EINVAL; } else { - *set = _thread_run->sigpend; + *set = curthread->sigpend; } /* Return the completion status: */ return (ret); } - -__strong_reference(_sigpending, sigpending); -#endif diff --git a/lib/libkse/thread/thr_sigprocmask.c b/lib/libkse/thread/thr_sigprocmask.c index f8ca031..f93d463 100644 --- a/lib/libkse/thread/thr_sigprocmask.c +++ b/lib/libkse/thread/thr_sigprocmask.c @@ -36,15 +36,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigprocmask=_sigprocmask + int _sigprocmask(int how, const sigset_t *set, sigset_t *oset) { return (pthread_sigmask(how, set, oset)); } - -__strong_reference(_sigprocmask, sigprocmask); -#endif diff --git a/lib/libkse/thread/thr_sigsuspend.c b/lib/libkse/thread/thr_sigsuspend.c index 28959c8..f0a08e2 100644 --- a/lib/libkse/thread/thr_sigsuspend.c +++ b/lib/libkse/thread/thr_sigsuspend.c @@ -33,23 +33,25 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigsuspend=__sigsuspend + int _sigsuspend(const sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = -1; sigset_t oset; /* Check if a new signal set was provided by the caller: */ if (set != NULL) { /* Save the current signal mask: */ - oset = _thread_run->sigmask; + oset = curthread->sigmask; /* Change the caller's mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGSUSPEND, __FILE__, __LINE__); @@ -58,7 +60,7 @@ _sigsuspend(const sigset_t * set) errno = EINTR; /* Restore the signal mask: */ - _thread_run->sigmask = oset; + curthread->sigmask = oset; } else { /* Return an invalid argument error: */ errno = EINVAL; @@ -69,7 +71,7 @@ _sigsuspend(const sigset_t * set) } int -sigsuspend(const sigset_t * set) +__sigsuspend(const sigset_t * set) { int ret; @@ -79,4 +81,3 @@ sigsuspend(const sigset_t * set) return ret; } -#endif diff --git a/lib/libkse/thread/thr_sigwait.c b/lib/libkse/thread/thr_sigwait.c index f2c6ea1..71fe007 100644 --- a/lib/libkse/thread/thr_sigwait.c +++ b/lib/libkse/thread/thr_sigwait.c @@ -35,13 +35,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigwait=_sigwait + int -sigwait(const sigset_t *set, int *sig) +_sigwait(const sigset_t *set, int *sig) { + struct pthread *curthread = _get_curthread(); int ret = 0; int i; sigset_t tempset, waitset; @@ -69,7 +71,7 @@ sigwait(const sigset_t *set, int *sig) sigdelset(&waitset, SIGINFO); /* Check to see if a pending signal is in the wait mask. */ - tempset = _thread_run->sigpend; + tempset = curthread->sigpend; SIGSETOR(tempset, _process_sigpending); SIGSETAND(tempset, waitset); if (SIGNOTEMPTY(tempset)) { @@ -80,8 +82,8 @@ sigwait(const sigset_t *set, int *sig) } /* Clear the pending signal: */ - if (sigismember(&_thread_run->sigpend,i)) - sigdelset(&_thread_run->sigpend,i); + if (sigismember(&curthread->sigpend,i)) + sigdelset(&curthread->sigpend,i); else sigdelset(&_process_sigpending,i); @@ -115,7 +117,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]++; sigaddset(&tempset, i); if (_thread_dfl_count[i] == 1) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -129,19 +131,19 @@ sigwait(const sigset_t *set, int *sig) * mask is independent of the threads signal mask * and requires separate storage. */ - _thread_run->data.sigwait = &waitset; + curthread->data.sigwait = &waitset; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__); /* Return the signal number to the caller: */ - *sig = _thread_run->signo; + *sig = curthread->signo; /* * 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; + curthread->data.sigwait = NULL; } /* @@ -157,7 +159,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]--; if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) && (_thread_dfl_count[i] == 0)) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -170,4 +172,3 @@ sigwait(const sigset_t *set, int *sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libkse/thread/thr_single_np.c b/lib/libkse/thread/thr_single_np.c index d6ecb48..52cd6ed 100644 --- a/lib/libkse/thread/thr_single_np.c +++ b/lib/libkse/thread/thr_single_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_single_np() +#pragma weak pthread_single_np=_pthread_single_np + +int _pthread_single_np() { + struct pthread *curthread = _get_curthread(); + /* Enter single-threaded (non-POSIX) scheduling mode: */ - _thread_single = _thread_run; + _thread_single = curthread; return(0); } -#endif diff --git a/lib/libkse/thread/thr_sleep.c b/lib/libkse/thread/thr_sleep.c index fdfeeec..acc6e55 100644 --- a/lib/libkse/thread/thr_sleep.c +++ b/lib/libkse/thread/thr_sleep.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sleep=_sleep + unsigned int -sleep(unsigned int seconds) +_sleep(unsigned int seconds) { unsigned int ret; @@ -45,4 +46,3 @@ sleep(unsigned int seconds) return ret; } -#endif diff --git a/lib/libkse/thread/thr_spec.c b/lib/libkse/thread/thr_spec.c index 1485cbb..783617f 100644 --- a/lib/libkse/thread/thr_spec.c +++ b/lib/libkse/thread/thr_spec.c @@ -35,15 +35,20 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" /* Static variables: */ static struct pthread_key key_table[PTHREAD_KEYS_MAX]; +#pragma weak pthread_key_create=_pthread_key_create +#pragma weak pthread_key_delete=_pthread_key_delete +#pragma weak pthread_getspecific=_pthread_getspecific +#pragma weak pthread_setspecific=_pthread_setspecific + + int -pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) +_pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) { for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) { /* Lock the key table entry: */ @@ -65,7 +70,7 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) } int -pthread_key_delete(pthread_key_t key) +_pthread_key_delete(pthread_key_t key) { int ret = 0; @@ -88,23 +93,24 @@ pthread_key_delete(pthread_key_t key) void _thread_cleanupspecific(void) { - void *data = NULL; - int key; - int itr; + struct pthread *curthread = _get_curthread(); + void *data = NULL; + int key; + int itr; void (*destructor)( void *); for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) { for (key = 0; key < PTHREAD_KEYS_MAX; key++) { - if (_thread_run->specific_data_count) { + if (curthread->specific_data_count) { /* Lock the key table entry: */ _SPINLOCK(&key_table[key].lock); destructor = NULL; if (key_table[key].allocated) { - if (_thread_run->specific_data[key]) { - data = (void *) _thread_run->specific_data[key]; - _thread_run->specific_data[key] = NULL; - _thread_run->specific_data_count--; + if (curthread->specific_data[key]) { + data = (void *) curthread->specific_data[key]; + curthread->specific_data[key] = NULL; + curthread->specific_data_count--; destructor = key_table[key].destructor; } } @@ -119,14 +125,14 @@ _thread_cleanupspecific(void) if (destructor) destructor(data); } else { - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; return; } } } - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; } static inline const void ** @@ -140,13 +146,13 @@ pthread_key_allocate_data(void) } int -pthread_setspecific(pthread_key_t key, const void *value) +_pthread_setspecific(pthread_key_t key, const void *value) { - pthread_t pthread; - int ret = 0; + struct pthread *pthread; + int ret = 0; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); if ((pthread->specific_data) || (pthread->specific_data = pthread_key_allocate_data())) { @@ -171,13 +177,13 @@ pthread_setspecific(pthread_key_t key, const void *value) } void * -pthread_getspecific(pthread_key_t key) +_pthread_getspecific(pthread_key_t key) { - pthread_t pthread; + struct pthread *pthread; void *data; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); /* Check if there is specific data: */ if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) { @@ -197,4 +203,3 @@ pthread_getspecific(pthread_key_t key) data = NULL; return (data); } -#endif diff --git a/lib/libkse/thread/thr_spinlock.c b/lib/libkse/thread/thr_spinlock.c index e797f90..696174e 100644 --- a/lib/libkse/thread/thr_spinlock.c +++ b/lib/libkse/thread/thr_spinlock.c @@ -51,18 +51,20 @@ extern char *__progname; void _spinlock(spinlock_t *lck) { + struct pthread *curthread = _get_curthread(); + /* * Try to grab the lock and loop if another thread grabs * it before we do. */ while(_atomic_lock(&lck->access_lock)) { /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, __FILE__, __LINE__); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; } /* @@ -78,6 +80,7 @@ _spinlock(spinlock_t *lck) void _spinlock_debug(spinlock_t *lck, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int cnt = 0; /* @@ -88,19 +91,19 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno) cnt++; if (cnt > 100) { char str[256]; - snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, _thread_run, lck, fname, lineno, lck->fname, lck->lineno); - _thread_sys_write(2,str,strlen(str)); + snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno); + __sys_write(2,str,strlen(str)); __sleep(1); cnt = 0; } /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, fname, lineno); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; lck->fname = fname; lck->lineno = lineno; } diff --git a/lib/libkse/thread/thr_suspend_np.c b/lib/libkse/thread/thr_suspend_np.c index 083f552..5c204a2 100644 --- a/lib/libkse/thread/thr_suspend_np.c +++ b/lib/libkse/thread/thr_suspend_np.c @@ -32,15 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" static void finish_suspension(void *arg); +#pragma weak pthread_suspend_np=_pthread_suspend_np + /* Suspend a thread: */ int -pthread_suspend_np(pthread_t thread) +_pthread_suspend_np(pthread_t thread) { int ret; @@ -144,9 +145,10 @@ pthread_suspend_np(pthread_t thread) static void finish_suspension(void *arg) { - if (_thread_run->suspended != SUSP_NO) + struct pthread *curthread = _get_curthread(); + + if (curthread->suspended != SUSP_NO) _thread_kern_sched_state(PS_SUSPENDED, __FILE__, __LINE__); } -#endif diff --git a/lib/libkse/thread/thr_switch_np.c b/lib/libkse/thread/thr_switch_np.c index 9b83545..e999259 100644 --- a/lib/libkse/thread/thr_switch_np.c +++ b/lib/libkse/thread/thr_switch_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" +#pragma weak pthread_switch_add_np=_pthread_switch_add_np +#pragma weak pthread_switch_delete_np=_pthread_switch_delete_np + int -pthread_switch_add_np(pthread_switch_routine_t routine) +_pthread_switch_add_np(pthread_switch_routine_t routine) { int ret = 0; @@ -54,7 +56,7 @@ pthread_switch_add_np(pthread_switch_routine_t routine) } int -pthread_switch_delete_np(pthread_switch_routine_t routine) +_pthread_switch_delete_np(pthread_switch_routine_t routine) { int ret = 0; @@ -67,4 +69,3 @@ pthread_switch_delete_np(pthread_switch_routine_t routine) return(ret); } -#endif diff --git a/lib/libkse/thread/thr_system.c b/lib/libkse/thread/thr_system.c index 47a572d..d3a16cb 100644 --- a/lib/libkse/thread/thr_system.c +++ b/lib/libkse/thread/thr_system.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak system=_system + int -system(const char *string) +_system(const char *string) { int ret; @@ -45,4 +46,3 @@ system(const char *string) return ret; } -#endif diff --git a/lib/libkse/thread/thr_tcdrain.c b/lib/libkse/thread/thr_tcdrain.c index 6470882..7700791 100644 --- a/lib/libkse/thread/thr_tcdrain.c +++ b/lib/libkse/thread/thr_tcdrain.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak tcdrain=_tcdrain + int -tcdrain(int fd) +_tcdrain(int fd) { int ret; @@ -45,4 +46,3 @@ tcdrain(int fd) return ret; } -#endif diff --git a/lib/libkse/thread/thr_vfork.c b/lib/libkse/thread/thr_vfork.c index bbfcf00..78d9f01 100644 --- a/lib/libkse/thread/thr_vfork.c +++ b/lib/libkse/thread/thr_vfork.c @@ -1,9 +1,12 @@ +/* + * $FreeBSD$ + */ #include -#ifdef _THREAD_SAFE + +#pragma weak vfork=_vfork int -vfork(void) +_vfork(void) { return (fork()); } -#endif diff --git a/lib/libkse/thread/thr_wait.c b/lib/libkse/thread/thr_wait.c index c34d997..9482586 100644 --- a/lib/libkse/thread/thr_wait.c +++ b/lib/libkse/thread/thr_wait.c @@ -29,12 +29,13 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait=_wait + pid_t -wait(int *istat) +_wait(int *istat) { pid_t ret; @@ -44,4 +45,3 @@ wait(int *istat) return ret; } -#endif diff --git a/lib/libkse/thread/thr_wait4.c b/lib/libkse/thread/thr_wait4.c index da69a4f..8b44366 100644 --- a/lib/libkse/thread/thr_wait4.c +++ b/lib/libkse/thread/thr_wait4.c @@ -35,27 +35,29 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait4=_wait4 + pid_t _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) { + struct pthread *curthread = _get_curthread(); pid_t ret; _thread_kern_sig_defer(); /* Perform a non-blocking wait4 syscall: */ - while ((ret = _thread_sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { + while ((ret = __sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Schedule the next thread while this one waits: */ _thread_kern_sched_state(PS_WAIT_WAIT, __FILE__, __LINE__); /* Check if this call was interrupted by a signal: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -66,6 +68,3 @@ _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) return (ret); } - -__strong_reference(_wait4, wait4); -#endif diff --git a/lib/libkse/thread/thr_waitpid.c b/lib/libkse/thread/thr_waitpid.c index 28fcd78..672183d 100644 --- a/lib/libkse/thread/thr_waitpid.c +++ b/lib/libkse/thread/thr_waitpid.c @@ -31,12 +31,13 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak waitpid=_waitpid + pid_t -waitpid(pid_t wpid, int *status, int options) +_waitpid(pid_t wpid, int *status, int options) { pid_t ret; @@ -46,4 +47,3 @@ waitpid(pid_t wpid, int *status, int options) return ret; } -#endif diff --git a/lib/libkse/thread/thr_write.c b/lib/libkse/thread/thr_write.c index 5d9ef35..b841726 100644 --- a/lib/libkse/thread/thr_write.c +++ b/lib/libkse/thread/thr_write.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak write=__write + ssize_t _write(int fd, const void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int blocking; int type; ssize_t n; @@ -76,7 +78,7 @@ _write(int fd, const void *buf, size_t nbytes) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_write(fd, buf + num, nbytes - num); + n = __sys_write(fd, buf + num, nbytes - num); /* Check if one or more bytes were written: */ if (n > 0) @@ -94,11 +96,11 @@ _write(int fd, const void *buf, size_t nbytes) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && num < nbytes))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -107,7 +109,7 @@ _write(int fd, const void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -133,7 +135,7 @@ _write(int fd, const void *buf, size_t nbytes) } ssize_t -write(int fd, const void *buf, size_t nbytes) +__write(int fd, const void *buf, size_t nbytes) { ssize_t ret; @@ -143,4 +145,3 @@ write(int fd, const void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libkse/thread/thr_writev.c b/lib/libkse/thread/thr_writev.c index 5f31468..66b451f 100644 --- a/lib/libkse/thread/thr_writev.c +++ b/lib/libkse/thread/thr_writev.c @@ -39,13 +39,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak writev=_writev + ssize_t _writev(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int blocking; int idx = 0; int type; @@ -92,7 +94,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx); + n = __sys_writev(fd, &p_iov[idx], iovcnt - idx); /* Check if one or more bytes were written: */ if (n > 0) { @@ -158,11 +160,11 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -171,7 +173,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -200,6 +202,3 @@ _writev(int fd, const struct iovec * iov, int iovcnt) return (ret); } - -__strong_reference(_writev, writev); -#endif diff --git a/lib/libkse/thread/thr_yield.c b/lib/libkse/thread/thr_yield.c index 7d64283..67ca501 100644 --- a/lib/libkse/thread/thr_yield.c +++ b/lib/libkse/thread/thr_yield.c @@ -31,15 +31,19 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sched_yield=_sched_yield +#pragma weak pthread_yield=_pthread_yield + int -sched_yield(void) +_sched_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); @@ -50,12 +54,13 @@ sched_yield(void) /* Draft 4 yield */ void -pthread_yield(void) +_pthread_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); } -#endif diff --git a/lib/libpthread/Makefile b/lib/libpthread/Makefile index 9b20a24..d7b21bc 100644 --- a/lib/libpthread/Makefile +++ b/lib/libpthread/Makefile @@ -22,23 +22,6 @@ CFLAGS+=-D_PTHREADS_INVARIANTS AINC= -I${.CURDIR}/../libc/${MACHINE_ARCH} -I${.CURDIR}/uthread PRECIOUSLIB= yes -# -# This is a list of syscalls that are renamed as _thread_sys_{syscall} -# so that libc_r can provide replacement functions. -# -HIDDEN_SYSCALLS= aio_suspend.o accept.o bind.o close.o connect.o dup.o dup2.o \ - execve.o fchflags.o fchmod.o fchown.o fcntl.o \ - flock.o fpathconf.o fstat.o fstatfs.o fsync.o getdirentries.o \ - getlogin.o getpeername.o getsockname.o getsockopt.o ioctl.o \ - kevent.o listen.o \ - msync.o nanosleep.o nfssvc.o open.o poll.o read.o readv.o recvfrom.o \ - recvmsg.o sched_yield.o select.o sendfile.o sendmsg.o sendto.o \ - setsockopt.o shutdown.o sigaction.o sigaltstack.o \ - signanosleep.o sigpending.o sigprocmask.o sigreturn.o sigsetmask.o \ - sigsuspend.o socket.o \ - socketpair.o wait4.o write.o writev.o - -.include "${.CURDIR}/../libc/Makefile.inc" .include "${.CURDIR}/man/Makefile.inc" .include "${.CURDIR}/uthread/Makefile.inc" .include "${.CURDIR}/sys/Makefile.inc" diff --git a/lib/libpthread/thread/thr_aio_suspend.c b/lib/libpthread/thread/thr_aio_suspend.c index 1253f20..ea77f4e 100644 --- a/lib/libpthread/thread/thr_aio_suspend.c +++ b/lib/libpthread/thread/thr_aio_suspend.c @@ -30,10 +30,11 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak aio_suspend=_aio_suspend + int _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct timespec *timeout) @@ -41,11 +42,9 @@ _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct int ret; _thread_enter_cancellation_point(); - ret = _aio_suspend(iocbs, niocb, timeout); + ret = __sys_aio_suspend(iocbs, niocb, timeout); _thread_leave_cancellation_point(); - + return ret; } -__strong_reference(_aio_suspend, aio_suspend); -#endif diff --git a/lib/libpthread/thread/thr_attr_destroy.c b/lib/libpthread/thread/thr_attr_destroy.c index dfe668e..c025072 100644 --- a/lib/libpthread/thread/thr_attr_destroy.c +++ b/lib/libpthread/thread/thr_attr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_destroy(pthread_attr_t *attr) +#pragma weak pthread_attr_destroy=_pthread_attr_destroy + +int +_pthread_attr_destroy(pthread_attr_t *attr) { int ret; @@ -58,4 +60,3 @@ int pthread_attr_destroy(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getdetachstate.c b/lib/libpthread/thread/thr_attr_getdetachstate.c index d120553..581c952 100644 --- a/lib/libpthread/thread/thr_attr_getdetachstate.c +++ b/lib/libpthread/thread/thr_attr_getdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getdetachstate=_pthread_attr_getdetachstate + int -pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) +_pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) { int ret; @@ -56,4 +57,3 @@ pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getinheritsched.c b/lib/libpthread/thread/thr_attr_getinheritsched.c index 7e243ed..65e347b 100644 --- a/lib/libpthread/thread/thr_attr_getinheritsched.c +++ b/lib/libpthread/thread/thr_attr_getinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getinheritsched=_pthread_attr_getinheritsched + int -pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) +_pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getschedparam.c b/lib/libpthread/thread/thr_attr_getschedparam.c index 46586ff..454e1c2 100644 --- a/lib/libpthread/thread/thr_attr_getschedparam.c +++ b/lib/libpthread/thread/thr_attr_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedparam=_pthread_attr_getschedparam + int -pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) +_pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getschedpolicy.c b/lib/libpthread/thread/thr_attr_getschedpolicy.c index 19f835c..8328b3c 100644 --- a/lib/libpthread/thread/thr_attr_getschedpolicy.c +++ b/lib/libpthread/thread/thr_attr_getschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getschedpolicy=_pthread_attr_getschedpolicy + int -pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) +_pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getscope.c b/lib/libpthread/thread/thr_attr_getscope.c index 176f01b..ca0f4c9 100644 --- a/lib/libpthread/thread/thr_attr_getscope.c +++ b/lib/libpthread/thread/thr_attr_getscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getscope=_pthread_attr_getscope + int -pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) +_pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) { int ret = 0; @@ -51,4 +52,3 @@ pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getstackaddr.c b/lib/libpthread/thread/thr_attr_getstackaddr.c index 442dcc2..dd14176 100644 --- a/lib/libpthread/thread/thr_attr_getstackaddr.c +++ b/lib/libpthread/thread/thr_attr_getstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstackaddr=_pthread_attr_getstackaddr + int -pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) +_pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_getstacksize.c b/lib/libpthread/thread/thr_attr_getstacksize.c index d9aaca4..d66e473 100644 --- a/lib/libpthread/thread/thr_attr_getstacksize.c +++ b/lib/libpthread/thread/thr_attr_getstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_getstacksize=_pthread_attr_getstacksize + int -pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) +_pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_init.c b/lib/libpthread/thread/thr_attr_init.c index dbe3091..2e560c7 100644 --- a/lib/libpthread/thread/thr_attr_init.c +++ b/lib/libpthread/thread/thr_attr_init.c @@ -34,11 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_attr_init(pthread_attr_t *attr) +#pragma weak pthread_attr_init=_pthread_attr_init + +int +_pthread_attr_init(pthread_attr_t *attr) { int ret; pthread_attr_t pattr; @@ -57,4 +59,3 @@ int pthread_attr_init(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setcreatesuspend_np.c b/lib/libpthread/thread/thr_attr_setcreatesuspend_np.c index d230397..5520368 100644 --- a/lib/libpthread/thread/thr_attr_setcreatesuspend_np.c +++ b/lib/libpthread/thread/thr_attr_setcreatesuspend_np.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setcreatesuspend_np=_pthread_attr_setcreatesuspend_np + int -pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) +_pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +50,3 @@ pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setdetachstate.c b/lib/libpthread/thread/thr_attr_setdetachstate.c index 6be3e4e..8c8a5fc 100644 --- a/lib/libpthread/thread/thr_attr_setdetachstate.c +++ b/lib/libpthread/thread/thr_attr_setdetachstate.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setdetachstate=_pthread_attr_setdetachstate + int -pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) +_pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) { int ret; @@ -58,4 +59,3 @@ pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setinheritsched.c b/lib/libpthread/thread/thr_attr_setinheritsched.c index eb2384b..e35b4e9 100644 --- a/lib/libpthread/thread/thr_attr_setinheritsched.c +++ b/lib/libpthread/thread/thr_attr_setinheritsched.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setinheritsched=_pthread_attr_setinheritsched + int -pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) +_pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) { int ret = 0; @@ -48,4 +49,3 @@ pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setschedparam.c b/lib/libpthread/thread/thr_attr_setschedparam.c index 755bb13..a18494b 100644 --- a/lib/libpthread/thread/thr_attr_setschedparam.c +++ b/lib/libpthread/thread/thr_attr_setschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedparam=_pthread_attr_setschedparam + int -pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) +_pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) { int ret = 0; @@ -54,4 +55,3 @@ pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setschedpolicy.c b/lib/libpthread/thread/thr_attr_setschedpolicy.c index 2788deb..29de2f4 100644 --- a/lib/libpthread/thread/thr_attr_setschedpolicy.c +++ b/lib/libpthread/thread/thr_attr_setschedpolicy.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setschedpolicy=_pthread_attr_setschedpolicy + int -pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) +_pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) { int ret = 0; @@ -50,4 +51,3 @@ pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setscope.c b/lib/libpthread/thread/thr_attr_setscope.c index de2fe8e..81a09fa 100644 --- a/lib/libpthread/thread/thr_attr_setscope.c +++ b/lib/libpthread/thread/thr_attr_setscope.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setscope=_pthread_attr_setscope + int -pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) +_pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) { int ret = 0; @@ -53,4 +54,3 @@ pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setstackaddr.c b/lib/libpthread/thread/thr_attr_setstackaddr.c index 46469e4..8552ea4 100644 --- a/lib/libpthread/thread/thr_attr_setstackaddr.c +++ b/lib/libpthread/thread/thr_attr_setstackaddr.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstackaddr=_pthread_attr_setstackaddr + int -pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) +_pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_attr_setstacksize.c b/lib/libpthread/thread/thr_attr_setstacksize.c index 3897811..4233cb5 100644 --- a/lib/libpthread/thread/thr_attr_setstacksize.c +++ b/lib/libpthread/thread/thr_attr_setstacksize.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_attr_setstacksize=_pthread_attr_setstacksize + int -pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) +_pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) { int ret; @@ -51,4 +52,3 @@ pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_cancel.c b/lib/libpthread/thread/thr_cancel.c index c6df52e..55f94fb 100644 --- a/lib/libpthread/thread/thr_cancel.c +++ b/lib/libpthread/thread/thr_cancel.c @@ -8,8 +8,13 @@ static void finish_cancellation(void *arg); +#pragma weak pthread_cancel=_pthread_cancel +#pragma weak pthread_setcancelstate=_pthread_setcancelstate +#pragma weak pthread_setcanceltype=_pthread_setcanceltype +#pragma weak pthread_testcancel=_pthread_testcancel + int -pthread_cancel(pthread_t pthread) +_pthread_cancel(pthread_t pthread) { int ret; @@ -111,26 +116,27 @@ pthread_cancel(pthread_t pthread) } int -pthread_setcancelstate(int state, int *oldstate) +_pthread_setcancelstate(int state, int *oldstate) { + struct pthread *curthread = _get_curthread(); int ostate; int ret; - ostate = _thread_run->cancelflags & PTHREAD_CANCEL_DISABLE; + ostate = curthread->cancelflags & PTHREAD_CANCEL_DISABLE; switch (state) { case PTHREAD_CANCEL_ENABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_DISABLE; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) + curthread->cancelflags &= ~PTHREAD_CANCEL_DISABLE; + if ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0) pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DISABLE: if (oldstate != NULL) *oldstate = ostate; - _thread_run->cancelflags |= PTHREAD_CANCEL_DISABLE; + curthread->cancelflags |= PTHREAD_CANCEL_DISABLE; ret = 0; break; default: @@ -141,24 +147,25 @@ pthread_setcancelstate(int state, int *oldstate) } int -pthread_setcanceltype(int type, int *oldtype) +_pthread_setcanceltype(int type, int *oldtype) { + struct pthread *curthread = _get_curthread(); int otype; int ret; - otype = _thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; + otype = curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS; switch (type) { case PTHREAD_CANCEL_ASYNCHRONOUS: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags |= PTHREAD_CANCEL_ASYNCHRONOUS; pthread_testcancel(); ret = 0; break; case PTHREAD_CANCEL_DEFERRED: if (oldtype != NULL) *oldtype = otype; - _thread_run->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; + curthread->cancelflags &= ~PTHREAD_CANCEL_ASYNCHRONOUS; ret = 0; break; default: @@ -169,16 +176,18 @@ pthread_setcanceltype(int type, int *oldtype) } void -pthread_testcancel(void) +_pthread_testcancel(void) { - if (((_thread_run->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCELLING) != 0)) { + struct pthread *curthread = _get_curthread(); + + if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) && + ((curthread->cancelflags & PTHREAD_CANCELLING) != 0)) { /* * It is possible for this thread to be swapped out * while performing cancellation; do not allow it * to be cancelled again. */ - _thread_run->cancelflags &= ~PTHREAD_CANCELLING; + curthread->cancelflags &= ~PTHREAD_CANCELLING; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); PANIC("cancel"); @@ -188,15 +197,19 @@ pthread_testcancel(void) void _thread_enter_cancellation_point(void) { + struct pthread *curthread = _get_curthread(); + /* Look for a cancellation before we block: */ pthread_testcancel(); - _thread_run->cancelflags |= PTHREAD_AT_CANCEL_POINT; + curthread->cancelflags |= PTHREAD_AT_CANCEL_POINT; } void _thread_leave_cancellation_point(void) { - _thread_run->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; + struct pthread *curthread = _get_curthread(); + + curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT; /* Look for a cancellation after we unblock: */ pthread_testcancel(); } @@ -204,11 +217,13 @@ _thread_leave_cancellation_point(void) static void finish_cancellation(void *arg) { - _thread_run->continuation = NULL; - _thread_run->interrupted = 0; + struct pthread *curthread = _get_curthread(); + + curthread->continuation = NULL; + curthread->interrupted = 0; - if ((_thread_run->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { - _thread_run->cancelflags &= ~PTHREAD_CANCEL_NEEDED; + if ((curthread->cancelflags & PTHREAD_CANCEL_NEEDED) != 0) { + curthread->cancelflags &= ~PTHREAD_CANCEL_NEEDED; _thread_exit_cleanup(); pthread_exit(PTHREAD_CANCELED); } diff --git a/lib/libpthread/thread/thr_clean.c b/lib/libpthread/thread/thr_clean.c index bba5500..9ce3fc2 100644 --- a/lib/libpthread/thread/thr_clean.c +++ b/lib/libpthread/thread/thr_clean.c @@ -34,31 +34,35 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_cleanup_push=_pthread_cleanup_push +#pragma weak pthread_cleanup_pop=_pthread_cleanup_pop + void -pthread_cleanup_push(void (*routine) (void *), void *routine_arg) +_pthread_cleanup_push(void (*routine) (void *), void *routine_arg) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *new; if ((new = (struct pthread_cleanup *) malloc(sizeof(struct pthread_cleanup))) != NULL) { new->routine = routine; new->routine_arg = routine_arg; - new->next = _thread_run->cleanup; + new->next = curthread->cleanup; - _thread_run->cleanup = new; + curthread->cleanup = new; } } void -pthread_cleanup_pop(int execute) +_pthread_cleanup_pop(int execute) { + struct pthread *curthread = _get_curthread(); struct pthread_cleanup *old; - if ((old = _thread_run->cleanup) != NULL) { - _thread_run->cleanup = old->next; + if ((old = curthread->cleanup) != NULL) { + curthread->cleanup = old->next; if (execute) { old->routine(old->routine_arg); } @@ -66,4 +70,3 @@ pthread_cleanup_pop(int execute) } } -#endif diff --git a/lib/libpthread/thread/thr_close.c b/lib/libpthread/thread/thr_close.c index 9dee2bc..43c3b22 100644 --- a/lib/libpthread/thread/thr_close.c +++ b/lib/libpthread/thread/thr_close.c @@ -36,10 +36,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak close=__close + int _close(int fd) { @@ -60,7 +61,7 @@ _close(int fd) * the file descriptor status: */ else if (((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) && - ((ret = _thread_sys_fstat(fd, &sb)) == 0)) { + ((ret = __sys_fstat(fd, &sb)) == 0)) { /* * Check if the file should be left as blocking. * @@ -83,9 +84,9 @@ _close(int fd) */ if ((S_ISREG(sb.st_mode) || S_ISCHR(sb.st_mode)) && (_thread_fd_table[fd]->flags & O_NONBLOCK) == 0) { /* Get the current flags: */ - flags = _thread_sys_fcntl(fd, F_GETFL, NULL); + flags = __sys_fcntl(fd, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); } /* XXX: Assumes well behaved threads. */ @@ -95,13 +96,13 @@ _close(int fd) free(entry); /* Close the file descriptor: */ - ret = _thread_sys_close(fd); + ret = __sys_close(fd); } return (ret); } int -close(int fd) +__close(int fd) { int ret; @@ -111,4 +112,3 @@ close(int fd) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_cond.c b/lib/libpthread/thread/thr_cond.c index 9b93c1a..6f2c509 100644 --- a/lib/libpthread/thread/thr_cond.c +++ b/lib/libpthread/thread/thr_cond.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -45,6 +44,14 @@ static inline pthread_t cond_queue_deq(pthread_cond_t); static inline void cond_queue_remove(pthread_cond_t, pthread_t); static inline void cond_queue_enq(pthread_cond_t, pthread_t); +#pragma weak pthread_cond_init=_pthread_cond_init +#pragma weak pthread_cond_destroy=_pthread_cond_destroy +#pragma weak pthread_cond_wait=_pthread_cond_wait +#pragma weak pthread_cond_timedwait=_pthread_cond_timedwait +#pragma weak pthread_cond_signal=_pthread_cond_signal +#pragma weak pthread_cond_broadcast=_pthread_cond_broadcast + + /* Reinitialize a condition variable to defaults. */ int _cond_reinit(pthread_cond_t *cond) @@ -70,7 +77,7 @@ _cond_reinit(pthread_cond_t *cond) } int -pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) +_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { enum pthread_cond_type type; pthread_cond_t pcond; @@ -130,7 +137,7 @@ pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) } int -pthread_cond_destroy(pthread_cond_t *cond) +_pthread_cond_destroy(pthread_cond_t *cond) { int rval = 0; @@ -157,8 +164,9 @@ pthread_cond_destroy(pthread_cond_t *cond) } int -pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) +_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -213,21 +221,21 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) rval = EINVAL; } else { /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; seqno = (*cond)->c_seqno; /* Wait forever: */ - _thread_run->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_sec = -1; /* Unlock the mutex: */ if ((unlock_mutex != 0) && @@ -237,7 +245,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == @@ -264,7 +272,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) done = (seqno != (*cond)->c_seqno); - if ((_thread_run->flags & + if ((curthread->flags & PTHREAD_FLAGS_IN_CONDQ) != 0) { /* * Lock the condition variable @@ -273,7 +281,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) _SPINLOCK(&(*cond)->lock); cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -286,7 +294,7 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) * Save the interrupted flag; locking * the mutex will destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Note that even though this thread may have @@ -308,8 +316,8 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -319,9 +327,10 @@ pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) } int -pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, +_pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime) { + struct pthread *curthread = _get_curthread(); int rval = 0; int done = 0; int interrupted = 0; @@ -375,20 +384,20 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); } else { /* Set the wakeup time: */ - _thread_run->wakeup_time.tv_sec = + curthread->wakeup_time.tv_sec = abstime->tv_sec; - _thread_run->wakeup_time.tv_nsec = + curthread->wakeup_time.tv_nsec = abstime->tv_nsec; /* Reset the timeout and interrupted flags: */ - _thread_run->timeout = 0; - _thread_run->interrupted = 0; + curthread->timeout = 0; + curthread->interrupted = 0; /* * Queue the running thread for the condition * variable: */ - cond_queue_enq(*cond, _thread_run); + cond_queue_enq(*cond, curthread); /* Remember the mutex and sequence number: */ (*cond)->c_mutex = *mutex; @@ -402,7 +411,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * the running thread from the condition * variable queue: */ - cond_queue_remove(*cond, _thread_run); + cond_queue_remove(*cond, curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -432,8 +441,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * interrupted (canceled), or needs to * be resumed after handling a signal. */ - if ((_thread_run->timeout == 0) && - (_thread_run->interrupted == 0) && + if ((curthread->timeout == 0) && + (curthread->interrupted == 0) && (done != 0)) { /* Lock the mutex: */ rval = _mutex_cv_lock(mutex); @@ -447,7 +456,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, * variable queue: */ cond_queue_remove(*cond, - _thread_run); + curthread); /* Check for no more waiters: */ if (TAILQ_FIRST(&(*cond)->c_queue) == NULL) @@ -457,14 +466,14 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, _SPINUNLOCK(&(*cond)->lock); /* Return a timeout error: */ - if (_thread_run->timeout != 0) + if (curthread->timeout != 0) rval = ETIMEDOUT; /* * Save the interrupted flag; * locking the mutex will * destroy it. */ - interrupted = _thread_run->interrupted; + interrupted = curthread->interrupted; /* * Lock the mutex and ignore any @@ -490,8 +499,8 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, break; } - if ((interrupted != 0) && (_thread_run->continuation != NULL)) - _thread_run->continuation((void *) _thread_run); + if ((interrupted != 0) && (curthread->continuation != NULL)) + curthread->continuation((void *) curthread); } while ((done == 0) && (rval == 0)); _thread_leave_cancellation_point(); @@ -501,7 +510,7 @@ pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, } int -pthread_cond_signal(pthread_cond_t * cond) +_pthread_cond_signal(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -569,7 +578,7 @@ pthread_cond_signal(pthread_cond_t * cond) } int -pthread_cond_broadcast(pthread_cond_t * cond) +_pthread_cond_broadcast(pthread_cond_t * cond) { int rval = 0; pthread_t pthread; @@ -752,4 +761,3 @@ cond_queue_enq(pthread_cond_t cond, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_CONDQ; pthread->data.cond = cond; } -#endif diff --git a/lib/libpthread/thread/thr_condattr_destroy.c b/lib/libpthread/thread/thr_condattr_destroy.c index ad91228..578523a 100644 --- a/lib/libpthread/thread/thr_condattr_destroy.c +++ b/lib/libpthread/thread/thr_condattr_destroy.c @@ -33,11 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_condattr_destroy(pthread_condattr_t *attr) +#pragma weak pthread_condattr_destroy=_pthread_condattr_destroy + +int +_pthread_condattr_destroy(pthread_condattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -49,4 +51,3 @@ int pthread_condattr_destroy(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_condattr_init.c b/lib/libpthread/thread/thr_condattr_init.c index 3379898..dc8059d 100644 --- a/lib/libpthread/thread/thr_condattr_init.c +++ b/lib/libpthread/thread/thr_condattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_condattr_init=_pthread_condattr_init + int -pthread_condattr_init(pthread_condattr_t *attr) +_pthread_condattr_init(pthread_condattr_t *attr) { int ret; pthread_condattr_t pattr; @@ -55,4 +56,3 @@ pthread_condattr_init(pthread_condattr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_creat.c b/lib/libpthread/thread/thr_creat.c index 1b51fec..56100dd 100644 --- a/lib/libpthread/thread/thr_creat.c +++ b/lib/libpthread/thread/thr_creat.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak creat=___creat + int -creat(const char *path, mode_t mode) +___creat(const char *path, mode_t mode) { int ret; @@ -45,4 +46,3 @@ creat(const char *path, mode_t mode) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_create.c b/lib/libpthread/thread/thr_create.c index 430869e..a0a51ec 100644 --- a/lib/libpthread/thread/thr_create.c +++ b/lib/libpthread/thread/thr_create.c @@ -40,7 +40,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -64,10 +63,13 @@ int _thread_CTX_JB_value = CTX_JB; int _thread_CTX_SJB_value = CTX_SJB; int _thread_CTX_UC_value = CTX_UC; +#pragma weak pthread_create=_pthread_create + int -pthread_create(pthread_t * thread, const pthread_attr_t * attr, +_pthread_create(pthread_t * thread, const pthread_attr_t * attr, void *(*start_routine) (void *), void *arg) { + struct pthread *curthread = _get_curthread(); struct itimerval itimer; int f_gc = 0; int ret = 0; @@ -180,7 +182,7 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, new_thread->magic = PTHREAD_MAGIC; /* Initialise the thread for signals: */ - new_thread->sigmask = _thread_run->sigmask; + new_thread->sigmask = curthread->sigmask; new_thread->sigmask_seqno = 0; /* Initialize the signal frame: */ @@ -214,13 +216,13 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { /* Copy the scheduling attributes: */ new_thread->base_priority = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.prio = - _thread_run->base_priority & + curthread->base_priority & ~PTHREAD_SIGNAL_PRIORITY; new_thread->attr.sched_policy = - _thread_run->attr.sched_policy; + curthread->attr.sched_policy; } else { /* * Use just the thread priority, leaving the @@ -315,13 +317,14 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr, void _thread_start(void) { + struct pthread *curthread = _get_curthread(); + /* 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)); + pthread_exit(curthread->start_routine(curthread->arg)); /* This point should never be reached. */ PANIC("Thread has resumed after exit"); } -#endif diff --git a/lib/libpthread/thread/thr_detach.c b/lib/libpthread/thread/thr_detach.c index 6dd762a..c387926 100644 --- a/lib/libpthread/thread/thr_detach.c +++ b/lib/libpthread/thread/thr_detach.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_detach=_pthread_detach + int -pthread_detach(pthread_t pthread) +_pthread_detach(pthread_t pthread) { int rval = 0; pthread_t next_thread; @@ -85,4 +86,3 @@ pthread_detach(pthread_t pthread) /* Return the completion status: */ return (rval); } -#endif diff --git a/lib/libpthread/thread/thr_equal.c b/lib/libpthread/thread/thr_equal.c index e03ba4a..334d023 100644 --- a/lib/libpthread/thread/thr_equal.c +++ b/lib/libpthread/thread/thr_equal.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_equal=_pthread_equal + int -pthread_equal(pthread_t t1, pthread_t t2) +_pthread_equal(pthread_t t1, pthread_t t2) { /* Compare the two thread pointers: */ return (t1 == t2); } -#endif diff --git a/lib/libpthread/thread/thr_exit.c b/lib/libpthread/thread/thr_exit.c index aef12fe..42bf025 100644 --- a/lib/libpthread/thread/thr_exit.c +++ b/lib/libpthread/thread/thr_exit.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" #define FLAGS_IN_SCHEDQ \ (PTHREAD_FLAGS_IN_PRIOQ|PTHREAD_FLAGS_IN_WAITQ|PTHREAD_FLAGS_IN_WORKQ) +#pragma weak _exit=__exit +#pragma weak pthread_exit=_pthread_exit + void __exit(int status) { int flags; @@ -58,8 +60,8 @@ void __exit(int status) setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL); /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* * Enter a loop to set all file descriptors to blocking @@ -70,18 +72,16 @@ void __exit(int status) if (_thread_fd_table[i] != NULL && !(_thread_fd_table[i]->flags & O_NONBLOCK)) { /* Get the current flags: */ - flags = _thread_sys_fcntl(i, F_GETFL, NULL); + flags = __sys_fcntl(i, F_GETFL, NULL); /* Clear the nonblocking file descriptor flag: */ - _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); + __sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK); } } /* Call the _exit syscall: */ - _thread_sys__exit(status); + __sys__exit(status); } -__strong_reference(__exit, _exit); - void _thread_exit(char *fname, int lineno, char *string) { @@ -97,7 +97,7 @@ _thread_exit(char *fname, int lineno, char *string) strcat(s, ")\n"); /* Write the string to the standard error file descriptor: */ - _thread_sys_write(2, s, strlen(s)); + __sys_write(2, s, strlen(s)); /* Force this process to exit: */ /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */ @@ -116,6 +116,8 @@ _thread_exit(char *fname, int lineno, char *string) void _thread_exit_cleanup(void) { + struct pthread *curthread = _get_curthread(); + /* * POSIX states that cancellation/termination of a thread should * not release any visible resources (such as mutexes) and that @@ -124,13 +126,13 @@ _thread_exit_cleanup(void) * are not visible to the application and need to be released. */ /* Unlock all owned fd locks: */ - _thread_fd_unlock_owned(_thread_run); + _thread_fd_unlock_owned(curthread); /* Unlock all owned file locks: */ - _funlock_owned(_thread_run); + _funlock_owned(curthread); /* Unlock all private mutexes: */ - _mutex_unlock_private(_thread_run); + _mutex_unlock_private(curthread); /* * This still isn't quite correct because we don't account @@ -139,39 +141,40 @@ _thread_exit_cleanup(void) } void -pthread_exit(void *status) +_pthread_exit(void *status) { + struct pthread *curthread = _get_curthread(); pthread_t pthread; /* Check if this thread is already in the process of exiting: */ - if ((_thread_run->flags & PTHREAD_EXITING) != 0) { + if ((curthread->flags & PTHREAD_EXITING) != 0) { char msg[128]; - 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); + 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!",curthread); PANIC(msg); } /* Flag this thread as exiting: */ - _thread_run->flags |= PTHREAD_EXITING; + curthread->flags |= PTHREAD_EXITING; /* Save the return value: */ - _thread_run->ret = status; + curthread->ret = status; - while (_thread_run->cleanup != NULL) { + while (curthread->cleanup != NULL) { pthread_cleanup_pop(1); } - if (_thread_run->attr.cleanup_attr != NULL) { - _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr); + if (curthread->attr.cleanup_attr != NULL) { + curthread->attr.cleanup_attr(curthread->attr.arg_attr); } /* Check if there is thread specific data: */ - if (_thread_run->specific_data != NULL) { + if (curthread->specific_data != NULL) { /* Run the thread-specific data destructors: */ _thread_cleanupspecific(); } /* Free thread-specific poll_data structure, if allocated: */ - if (_thread_run->poll_data.fds != NULL) { - free(_thread_run->poll_data.fds); - _thread_run->poll_data.fds = NULL; + if (curthread->poll_data.fds != NULL) { + free(curthread->poll_data.fds); + curthread->poll_data.fds = NULL; } /* @@ -182,7 +185,7 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Add this thread to the list of dead threads. */ - TAILQ_INSERT_HEAD(&_dead_list, _thread_run, dle); + TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); /* * Signal the garbage collector thread that there is something @@ -203,9 +206,9 @@ pthread_exit(void *status) PANIC("Cannot lock gc mutex"); /* Check if there are any threads joined to this one: */ - while ((pthread = TAILQ_FIRST(&(_thread_run->join_queue))) != NULL) { + while ((pthread = TAILQ_FIRST(&(curthread->join_queue))) != NULL) { /* Remove the thread from the queue: */ - TAILQ_REMOVE(&_thread_run->join_queue, pthread, sqe); + TAILQ_REMOVE(&curthread->join_queue, pthread, sqe); pthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; /* @@ -217,16 +220,16 @@ pthread_exit(void *status) /* * Set the return value for the woken thread: */ - if ((_thread_run->attr.flags & PTHREAD_DETACHED) != 0) + if ((curthread->attr.flags & PTHREAD_DETACHED) != 0) pthread->error = ESRCH; else { - pthread->ret = _thread_run->ret; + pthread->ret = curthread->ret; pthread->error = 0; } } /* Remove this thread from the thread list: */ - TAILQ_REMOVE(&_thread_list, _thread_run, tle); + TAILQ_REMOVE(&_thread_list, curthread, tle); /* This thread will never be re-scheduled. */ _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__); @@ -234,4 +237,3 @@ pthread_exit(void *status) /* This point should not be reached. */ PANIC("Dead thread has resumed"); } -#endif diff --git a/lib/libpthread/thread/thr_fcntl.c b/lib/libpthread/thread/thr_fcntl.c index f5dd064..84f4678 100644 --- a/lib/libpthread/thread/thr_fcntl.c +++ b/lib/libpthread/thread/thr_fcntl.c @@ -34,10 +34,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fcntl=__fcntl + int _fcntl(int fd, int cmd,...) { @@ -63,12 +64,12 @@ _fcntl(int fd, int cmd,...) oldfd = va_arg(ap, int); /* Initialise the file descriptor table entry: */ - if ((ret = _thread_sys_fcntl(fd, cmd, oldfd)) < 0) { + if ((ret = __sys_fcntl(fd, cmd, oldfd)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(ret) != 0) { /* Quietly close the file: */ - _thread_sys_close(ret); + __sys_close(ret); /* Reset the file descriptor: */ ret = -1; @@ -82,10 +83,10 @@ _fcntl(int fd, int cmd,...) break; case F_SETFD: flags = va_arg(ap, int); - ret = _thread_sys_fcntl(fd, cmd, flags); + ret = __sys_fcntl(fd, cmd, flags); break; case F_GETFD: - ret = _thread_sys_fcntl(fd, cmd, 0); + ret = __sys_fcntl(fd, cmd, 0); break; case F_GETFL: ret = _thread_fd_table[fd]->flags; @@ -104,10 +105,10 @@ _fcntl(int fd, int cmd,...) nonblock = flags & O_NONBLOCK; /* Set the file descriptor flags: */ - if ((ret = _thread_sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { + if ((ret = __sys_fcntl(fd, cmd, flags | O_NONBLOCK)) != 0) { /* Get the flags so that we behave like the kernel: */ - } else if ((flags = _thread_sys_fcntl(fd, + } else if ((flags = __sys_fcntl(fd, F_GETFL, 0)) == -1) { /* Error getting flags: */ ret = -1; @@ -125,7 +126,7 @@ _fcntl(int fd, int cmd,...) break; default: /* Might want to make va_arg use a union */ - ret = _thread_sys_fcntl(fd, cmd, va_arg(ap, void *)); + ret = __sys_fcntl(fd, cmd, va_arg(ap, void *)); break; } @@ -140,7 +141,7 @@ _fcntl(int fd, int cmd,...) } int -fcntl(int fd, int cmd,...) +__fcntl(int fd, int cmd,...) { int ret; va_list ap; @@ -167,4 +168,3 @@ fcntl(int fd, int cmd,...) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_find_thread.c b/lib/libpthread/thread/thr_find_thread.c index d4a3bbd..861c4cb 100644 --- a/lib/libpthread/thread/thr_find_thread.c +++ b/lib/libpthread/thread/thr_find_thread.c @@ -32,7 +32,6 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -97,4 +96,3 @@ _find_dead_thread(pthread_t pthread) /* Return zero if the thread exists: */ return ((pthread1 != NULL) ? 0:ESRCH); } -#endif diff --git a/lib/libpthread/thread/thr_fork.c b/lib/libpthread/thread/thr_fork.c index 97039b3..34d640c 100644 --- a/lib/libpthread/thread/thr_fork.c +++ b/lib/libpthread/thread/thr_fork.c @@ -36,13 +36,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fork=_fork + pid_t _fork(void) { + struct pthread *curthread = _get_curthread(); int i, flags; pid_t ret; pthread_t pthread; @@ -55,42 +57,42 @@ _fork(void) _thread_kern_sig_defer(); /* Fork a new process: */ - if ((ret = _thread_sys_fork()) != 0) { + if ((ret = __sys_fork()) != 0) { /* Parent process or error. Nothing to do here. */ } else { /* Close the pthread kernel pipe: */ - _thread_sys_close(_thread_kern_pipe[0]); - _thread_sys_close(_thread_kern_pipe[1]); + __sys_close(_thread_kern_pipe[0]); + __sys_close(_thread_kern_pipe[1]); /* Reset signals pending for the running thread: */ - sigemptyset(&_thread_run->sigpend); + sigemptyset(&curthread->sigpend); /* * Create a pipe that is written to by the signal handler to * prevent signals being missed in calls to - * _thread_sys_select: + * __sys_select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create pthread kernel pipe for forked process"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ abort(); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ abort(); } @@ -125,7 +127,7 @@ _fork(void) pthread = TAILQ_NEXT(pthread, dle); /* Make sure this isn't the running thread: */ - if (pthread_save != _thread_run) { + if (pthread_save != curthread) { /* Remove this thread from the list: */ TAILQ_REMOVE(&_thread_list, pthread_save, tle); @@ -165,7 +167,7 @@ _fork(void) } /* Treat the current thread as the initial thread: */ - _thread_initial = _thread_run; + _thread_initial = curthread; /* Re-init the dead thread list: */ TAILQ_INIT(&_dead_list); @@ -175,7 +177,7 @@ _fork(void) TAILQ_INIT(&_workq); /* Re-init the threads mutex queue: */ - TAILQ_INIT(&_thread_run->mutexq); + TAILQ_INIT(&curthread->mutexq); /* No spinlocks yet: */ _spinblock_count = 0; @@ -217,6 +219,3 @@ _fork(void) /* Return the process ID: */ return (ret); } - -__strong_reference(_fork, fork); -#endif diff --git a/lib/libpthread/thread/thr_fsync.c b/lib/libpthread/thread/thr_fsync.c index 0d88320..b90d896 100644 --- a/lib/libpthread/thread/thr_fsync.c +++ b/lib/libpthread/thread/thr_fsync.c @@ -32,24 +32,25 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak fsync=__fsync + int _fsync(int fd) { int ret; if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { - ret = _thread_sys_fsync(fd); + ret = __sys_fsync(fd); _FD_UNLOCK(fd, FD_RDWR); } return (ret); } int -fsync(int fd) +__fsync(int fd) { int ret; @@ -59,4 +60,3 @@ fsync(int fd) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_gc.c b/lib/libpthread/thread/thr_gc.c index a81ea91..cdc9681 100644 --- a/lib/libpthread/thread/thr_gc.c +++ b/lib/libpthread/thread/thr_gc.c @@ -47,6 +47,7 @@ pthread_addr_t _thread_gc(pthread_addr_t arg) { + struct pthread *curthread = _get_curthread(); int f_debug; int f_done = 0; int ret; @@ -61,13 +62,13 @@ _thread_gc(pthread_addr_t arg) pthread_sigmask(SIG_BLOCK, &mask, NULL); /* Mark this thread as a library thread (not a user thread). */ - _thread_run->flags |= PTHREAD_FLAGS_PRIVATE; + curthread->flags |= PTHREAD_FLAGS_PRIVATE; /* Set a debug flag based on an environment variable. */ f_debug = (getenv("LIBC_R_DEBUG") != NULL); /* Set the name of this thread. */ - pthread_set_name_np(_thread_run,"GC"); + pthread_set_name_np(curthread,"GC"); while (!f_done) { /* Check if debugging this application. */ @@ -82,8 +83,8 @@ _thread_gc(pthread_addr_t arg) _thread_kern_sig_defer(); /* Check if this is the last running thread: */ - if (TAILQ_FIRST(&_thread_list) == _thread_run && - TAILQ_NEXT(_thread_run, tle) == NULL) + if (TAILQ_FIRST(&_thread_list) == curthread && + TAILQ_NEXT(curthread, tle) == NULL) /* * This is the last thread, so it can exit * now. diff --git a/lib/libpthread/thread/thr_getprio.c b/lib/libpthread/thread/thr_getprio.c index 2f94b86..35eeb5b 100644 --- a/lib/libpthread/thread/thr_getprio.c +++ b/lib/libpthread/thread/thr_getprio.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getprio=_pthread_getprio + int -pthread_getprio(pthread_t pthread) +_pthread_getprio(pthread_t pthread) { int policy, ret; struct sched_param param; @@ -53,4 +54,3 @@ pthread_getprio(pthread_t pthread) /* Return the thread priority or an error status: */ return (ret); } -#endif diff --git a/lib/libpthread/thread/thr_getschedparam.c b/lib/libpthread/thread/thr_getschedparam.c index 5dbd1f0..02f27c2 100644 --- a/lib/libpthread/thread/thr_getschedparam.c +++ b/lib/libpthread/thread/thr_getschedparam.c @@ -32,12 +32,13 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_getschedparam=_pthread_getschedparam + int -pthread_getschedparam(pthread_t pthread, int *policy, +_pthread_getschedparam(pthread_t pthread, int *policy, struct sched_param *param) { int ret; @@ -56,4 +57,3 @@ pthread_getschedparam(pthread_t pthread, int *policy, return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_info.c b/lib/libpthread/thread/thr_info.c index 956ae7c..9c85e84 100644 --- a/lib/libpthread/thread/thr_info.c +++ b/lib/libpthread/thread/thr_info.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -47,6 +46,7 @@ static void dump_thread(int fd, pthread_t pthread, int long_version); +#pragma weak pthread_set_name_np=_pthread_set_name_np struct s_thread_info { enum pthread_state state; @@ -92,7 +92,7 @@ _thread_dump_info(void) snprintf(tmpfile, sizeof(tmpfile), "/tmp/uthread.dump.%u.%i", getpid(), i); /* Open the dump file for append and create it if necessary: */ - if ((fd = _thread_sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, + if ((fd = __sys_open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 0666)) < 0) { /* Can't open the dump file. */ if (errno == EEXIST) @@ -114,7 +114,7 @@ _thread_dump_info(void) } else { /* Output a header for active threads: */ strcpy(s, "\n\n=============\nACTIVE THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the global list: */ TAILQ_FOREACH(pthread, &_thread_list, tle) { @@ -123,7 +123,7 @@ _thread_dump_info(void) /* Output a header for ready threads: */ strcpy(s, "\n\n=============\nREADY THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the ready queue: */ TAILQ_FOREACH (pq_list, &_readyq.pq_queue, pl_link) { @@ -134,7 +134,7 @@ _thread_dump_info(void) /* Output a header for waiting threads: */ strcpy(s, "\n\n=============\nWAITING THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_waitingq, pqe) { @@ -143,7 +143,7 @@ _thread_dump_info(void) /* Output a header for threads in the work queue: */ strcpy(s, "\n\n=============\nTHREADS IN WORKQ\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report each thread in the waiting queue: */ TAILQ_FOREACH (pthread, &_workq, qe) { @@ -154,11 +154,11 @@ _thread_dump_info(void) if (TAILQ_FIRST(&_dead_list) == NULL) { /* Output a record: */ strcpy(s, "\n\nTHERE ARE NO DEAD THREADS\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } else { /* Output a header for dead threads: */ strcpy(s, "\n\nDEAD THREADS\n\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* * Enter a loop to report each thread in the global @@ -172,7 +172,7 @@ _thread_dump_info(void) /* Output a header for file descriptors: */ snprintf(s, sizeof(s), "\n\n=============\nFILE DESCRIPTOR " "TABLE (table size %d)\n\n", _thread_dtablesize); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); /* Enter a loop to report file descriptor lock usage: */ for (i = 0; i < _thread_dtablesize; i++) { @@ -193,18 +193,19 @@ _thread_dump_info(void) _thread_fd_table[i]->w_lockcount, _thread_fd_table[i]->w_fname, _thread_fd_table[i]->w_lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } } /* Close the dump file: */ - _thread_sys_close(fd); + __sys_close(fd); } } static void dump_thread(int fd, pthread_t pthread, int long_version) { + struct pthread *curthread = _get_curthread(); char s[512]; int i; @@ -219,20 +220,20 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread, (pthread->name == NULL) ? "" : pthread->name, pthread->active_priority, thread_info[i].name, pthread->fname, pthread->lineno); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); if (long_version != 0) { /* Check if this is the running thread: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Output a record for the running thread: */ strcpy(s, "This is the running thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Check if this is the initial thread: */ if (pthread == _thread_initial) { /* Output a record for the initial thread: */ strcpy(s, "This is the initial thread\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } /* Process according to thread state: */ switch (pthread->state) { @@ -246,22 +247,22 @@ dump_thread(int fd, pthread_t pthread, int long_version) pthread->data.fd.fd, pthread->data.fd.fname, pthread->data.fd.branch); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); 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)); + __sys_write(fd, s, strlen(s)); break; case PS_SIGWAIT: snprintf(s, sizeof(s), "sigmask (hi)"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); for (i = _SIG_WORDS - 1; i >= 0; i--) { snprintf(s, sizeof(s), "%08x\n", pthread->sigmask.__bits[i]); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); } snprintf(s, sizeof(s), "(lo)\n"); - _thread_sys_write(fd, s, strlen(s)); + __sys_write(fd, s, strlen(s)); break; /* * Trap other states that are not explicitly @@ -276,7 +277,7 @@ dump_thread(int fd, pthread_t pthread, int long_version) /* Set the thread name for debug: */ void -pthread_set_name_np(pthread_t thread, char *name) +_pthread_set_name_np(pthread_t thread, char *name) { /* Check if the caller has specified a valid thread: */ if (thread != NULL && thread->magic == PTHREAD_MAGIC) { @@ -287,4 +288,3 @@ pthread_set_name_np(pthread_t thread, char *name) thread->name = strdup(name); } } -#endif diff --git a/lib/libpthread/thread/thr_init.c b/lib/libpthread/thread/thr_init.c index 93d7849..c92efe3 100644 --- a/lib/libpthread/thread/thr_init.c +++ b/lib/libpthread/thread/thr_init.c @@ -35,74 +35,117 @@ /* Allocate space for global thread variables here: */ #define GLOBAL_PTHREAD_PRIVATE -#include -#include -#include -#include -#include -#include -#include +#include "namespace.h" +#include +#include +#include + #include +#include +#include +#include +#include +#include #include #include #include -#include #include +#include #include -#ifdef _THREAD_SAFE -#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include "un-namespace.h" + #include "pthread_private.h" /* + * 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 +}; + +/* * 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. */ -static void *thread_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, - &pthread_cond_init, - &pthread_cond_destroy, - &pthread_cond_wait, - &pthread_cond_timedwait, - &pthread_cond_signal, - &pthread_cond_broadcast +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 }; -#ifdef GCC_2_8_MADE_THREAD_AWARE -typedef void *** (*dynamic_handler_allocator)(); -extern void __set_dynamic_handler_allocator(dynamic_handler_allocator); - -static pthread_key_t except_head_key; - -typedef struct { - void **__dynamic_handler_chain; - void *top_elt[2]; -} except_struct; - -static void ***dynamic_allocator_handler_fn() -{ - except_struct *dh = (except_struct *)pthread_getspecific(except_head_key); - - if(dh == NULL) { - dh = (except_struct *)malloc( sizeof(except_struct) ); - memset(dh, '\0', sizeof(except_struct)); - dh->__dynamic_handler_chain= dh->top_elt; - pthread_setspecific(except_head_key, (void *)dh); - } - return &dh->__dynamic_handler_chain; -} -#endif /* GCC_2_8_MADE_THREAD_AWARE */ /* * Threaded process initialization @@ -124,11 +167,11 @@ _thread_init(void) return; /* - * Make gcc quiescent about thread_references not being + * Make gcc quiescent about {,libgcc_}references not being * referenced: */ - if (thread_references[0] == NULL) - PANIC("Mandatory pthread_* functions not loaded"); + if ((references[0] == NULL) || (libgcc_references[0] == NULL)) + PANIC("Failed loading mandatory references in _thread_init"); /* * Check for the special case of this process running as @@ -143,22 +186,22 @@ _thread_init(void) PANIC("Can't set session ID"); if (revoke(_PATH_CONSOLE) != 0) PANIC("Can't revoke console"); - if ((fd = _thread_sys_open(_PATH_CONSOLE, O_RDWR)) < 0) + if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0) PANIC("Can't open console"); if (setlogin("root") == -1) PANIC("Can't set login to root"); - if (_thread_sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) + if (__sys_ioctl(fd,TIOCSCTTY, (char *) NULL) == -1) PANIC("Can't set controlling terminal"); - if (_thread_sys_dup2(fd,0) == -1 || - _thread_sys_dup2(fd,1) == -1 || - _thread_sys_dup2(fd,2) == -1) + if (__sys_dup2(fd,0) == -1 || + __sys_dup2(fd,1) == -1 || + __sys_dup2(fd,2) == -1) PANIC("Can't dup2"); } /* Get the standard I/O flags before messing with them : */ for (i = 0; i < 3; i++) if (((_pthread_stdio_flags[i] = - _thread_sys_fcntl(i,F_GETFL, NULL)) == -1) && + __sys_fcntl(i,F_GETFL, NULL)) == -1) && (errno != EBADF)) PANIC("Cannot get stdio flags"); @@ -166,27 +209,27 @@ _thread_init(void) * Create a pipe that is written to by the signal handler to prevent * signals being missed in calls to _select: */ - if (_thread_sys_pipe(_thread_kern_pipe) != 0) { + if (__sys_pipe(_thread_kern_pipe) != 0) { /* Cannot create pipe, so abort: */ PANIC("Cannot create kernel pipe"); } /* Get the flags for the read pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel read pipe flags"); } /* Make the read pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot make kernel read pipe non-blocking"); } /* Get the flags for the write pipe: */ - else if ((flags = _thread_sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { + else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } /* Make the write pipe non-blocking: */ - else if (_thread_sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { + else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) { /* Abort this application: */ PANIC("Cannot get kernel write pipe flags"); } @@ -270,6 +313,9 @@ _thread_init(void) /* Initialise the state of the initial thread: */ _thread_initial->state = PS_RUNNING; + /* Set the name of the thread: */ + _thread_initial->name = strdup("_thread_initial"); + /* Initialise the queue: */ TAILQ_INIT(&(_thread_initial->join_queue)); @@ -299,7 +345,7 @@ _thread_init(void) _thread_initial->error = 0; TAILQ_INIT(&_thread_list); TAILQ_INSERT_HEAD(&_thread_list, _thread_initial, tle); - _thread_run = _thread_initial; + _set_curthread(_thread_initial); /* Initialise the global signal action structure: */ sigfillset(&act.sa_mask); @@ -320,7 +366,7 @@ _thread_init(void) _thread_sigstack.ss_size = SIGSTKSZ; _thread_sigstack.ss_flags = 0; if ((_thread_sigstack.ss_sp == NULL) || - (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0)) + (__sys_sigaltstack(&_thread_sigstack, NULL) != 0)) PANIC("Unable to install alternate signal stack"); /* Enter a loop to get the existing signal status: */ @@ -330,7 +376,7 @@ _thread_init(void) } /* Get the signal handler details: */ - else if (_thread_sys_sigaction(i, NULL, + else if (__sys_sigaction(i, NULL, &_thread_sigact[i - 1]) != 0) { /* * Abort this process if signal @@ -348,9 +394,9 @@ _thread_init(void) * signals that the user-thread kernel needs. Actually * SIGINFO isn't really needed, but it is nice to have. */ - if (_thread_sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || - _thread_sys_sigaction(SIGINFO, &act, NULL) != 0 || - _thread_sys_sigaction(SIGCHLD, &act, NULL) != 0) { + if (__sys_sigaction(_SCHED_SIGNAL, &act, NULL) != 0 || + __sys_sigaction(SIGINFO, &act, NULL) != 0 || + __sys_sigaction(SIGCHLD, &act, NULL) != 0) { /* * Abort this process if signal initialisation fails: */ @@ -361,7 +407,7 @@ _thread_init(void) _thread_sigact[SIGCHLD - 1].sa_flags = SA_SIGINFO; /* Get the process signal mask: */ - _thread_sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); + __sys_sigprocmask(SIG_SETMASK, NULL, &_process_sigmask); /* Get the kernel clockrate: */ mib[0] = CTL_KERN; @@ -416,17 +462,8 @@ _thread_init(void) } } -#ifdef GCC_2_8_MADE_THREAD_AWARE - /* Create the thread-specific data for the exception linked list. */ - if(pthread_key_create(&except_head_key, NULL) != 0) - PANIC("Failed to create thread specific execption head"); - - /* Setup the gcc exception handler per thread. */ - __set_dynamic_handler_allocator( dynamic_allocator_handler_fn ); -#endif /* GCC_2_8_MADE_THREAD_AWARE */ - /* Initialise the garbage collector mutex and condition variable. */ - if (pthread_mutex_init(&_gc_mutex,NULL) != 0 || + if (_pthread_mutex_init(&_gc_mutex,NULL) != 0 || pthread_cond_init(&_gc_cond,NULL) != 0) PANIC("Failed to initialise garbage collector mutex or condvar"); } @@ -445,12 +482,3 @@ _thread_main(int argc, char *argv[], char *env) return (main(argc, argv, env)); } #endif -#else -/* - * A stub for non-threaded programs. - */ -void -_thread_init(void) -{ -} -#endif diff --git a/lib/libpthread/thread/thr_join.c b/lib/libpthread/thread/thr_join.c index b4a7c61..2615f73 100644 --- a/lib/libpthread/thread/thr_join.c +++ b/lib/libpthread/thread/thr_join.c @@ -32,13 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_join=_pthread_join + int -pthread_join(pthread_t pthread, void **thread_return) +_pthread_join(pthread_t pthread, void **thread_return) { + struct pthread *curthread = _get_curthread(); int ret = 0; _thread_enter_cancellation_point(); @@ -51,7 +53,7 @@ pthread_join(pthread_t pthread, void **thread_return) } /* Check if the caller has specified itself: */ - if (pthread == _thread_run) { + if (pthread == curthread) { /* Avoid a deadlock condition: */ _thread_leave_cancellation_point(); return(EDEADLK); @@ -72,7 +74,7 @@ pthread_join(pthread_t pthread, void **thread_return) /* Check if the thread is not dead: */ else if (pthread->state != PS_DEAD) { - PTHREAD_ASSERT_NOT_IN_SYNCQ(_thread_run); + PTHREAD_ASSERT_NOT_IN_SYNCQ(curthread); /* * Enter a loop in case this thread is woken prematurely @@ -80,7 +82,7 @@ pthread_join(pthread_t pthread, void **thread_return) */ for (;;) { /* Clear the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Protect against being context switched out while @@ -90,25 +92,25 @@ pthread_join(pthread_t pthread, void **thread_return) /* Add the running thread to the join queue: */ TAILQ_INSERT_TAIL(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags |= PTHREAD_FLAGS_IN_JOINQ; - _thread_run->data.thread = pthread; + curthread, sqe); + curthread->flags |= PTHREAD_FLAGS_IN_JOINQ; + curthread->data.thread = pthread; /* Schedule the next thread: */ _thread_kern_sched_state(PS_JOIN, __FILE__, __LINE__); - if ((_thread_run->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { + if ((curthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&(pthread->join_queue), - _thread_run, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread, sqe); + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } - _thread_run->data.thread = NULL; + curthread->data.thread = NULL; _thread_kern_sig_undefer(); - if (_thread_run->interrupted != 0) { - if (_thread_run->continuation != NULL) - _thread_run->continuation(_thread_run); + if (curthread->interrupted != 0) { + if (curthread->continuation != NULL) + curthread->continuation(curthread); /* * This thread was interrupted, probably to * invoke a signal handler. Make sure the @@ -134,9 +136,9 @@ pthread_join(pthread_t pthread, void **thread_return) * by the thread we're joining to when it * exits or detaches: */ - ret = _thread_run->error; + ret = curthread->error; if ((ret == 0) && (thread_return != NULL)) - *thread_return = _thread_run->ret; + *thread_return = curthread->ret; /* We're done; break out of the loop. */ break; @@ -156,11 +158,12 @@ pthread_join(pthread_t pthread, void **thread_return) void _join_backout(pthread_t pthread) { + struct pthread *curthread = _get_curthread(); + _thread_kern_sig_defer(); if ((pthread->flags & PTHREAD_FLAGS_IN_JOINQ) != 0) { TAILQ_REMOVE(&pthread->data.thread->join_queue, pthread, sqe); - _thread_run->flags &= ~PTHREAD_FLAGS_IN_JOINQ; + curthread->flags &= ~PTHREAD_FLAGS_IN_JOINQ; } _thread_kern_sig_undefer(); } -#endif diff --git a/lib/libpthread/thread/thr_kern.c b/lib/libpthread/thread/thr_kern.c index 6f4bb6e..8a32253 100644 --- a/lib/libpthread/thread/thr_kern.c +++ b/lib/libpthread/thread/thr_kern.c @@ -48,7 +48,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -80,6 +79,8 @@ static int called_from_handler = 0; void _thread_kern_sched_frame(struct pthread_signal_frame *psf) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a signal from interrupting this execution and @@ -88,10 +89,10 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) _thread_kern_in_sched = 1; /* Restore the signal frame: */ - _thread_sigframe_restore(_thread_run, psf); + _thread_sigframe_restore(curthread, psf); /* The signal mask was restored; check for any pending signals: */ - _thread_run->check_pending = 1; + curthread->check_pending = 1; /* Switch to the thread scheduler: */ ___longjmp(_thread_kern_sched_jb, 1); @@ -101,6 +102,8 @@ _thread_kern_sched_frame(struct pthread_signal_frame *psf) void _thread_kern_sched(ucontext_t *scp) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -114,13 +117,13 @@ _thread_kern_sched(ucontext_t *scp) DBG_MSG("Entering scheduler due to signal\n"); } else { /* Save the state of the current thread: */ - if (_setjmp(_thread_run->ctx.jb) == 0) { + if (_setjmp(curthread->ctx.jb) == 0) { /* Flag the jump buffer was the last state saved: */ - _thread_run->ctxtype = CTX_JB_NOSIG; - _thread_run->longjmp_val = 1; + curthread->ctxtype = CTX_JB_NOSIG; + curthread->longjmp_val = 1; } else { DBG_MSG("Returned from ___longjmp, thread %p\n", - _thread_run); + curthread); /* * This point is reached when a longjmp() is called * to restore the state of a thread. @@ -129,10 +132,10 @@ _thread_kern_sched(ucontext_t *scp) */ _thread_kern_in_sched = 0; - if (_thread_run->sig_defer_count == 0) { - if (((_thread_run->cancelflags & + if (curthread->sig_defer_count == 0) { + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) /* * Cancellations override signals. @@ -150,7 +153,7 @@ _thread_kern_sched(ucontext_t *scp) if (_sched_switch_hook != NULL) { /* Run the installed switch hook: */ thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } return; } @@ -162,7 +165,9 @@ _thread_kern_sched(ucontext_t *scp) void _thread_kern_sched_sig(void) { - _thread_run->check_pending = 1; + struct pthread *curthread = _get_curthread(); + + curthread->check_pending = 1; _thread_kern_sched(NULL); } @@ -172,13 +177,14 @@ _thread_kern_scheduler(void) { struct timespec ts; struct timeval tv; + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; unsigned int current_tick; int add_to_prioq; /* If the currently running thread is a user thread, save it: */ - if ((_thread_run->flags & PTHREAD_FLAGS_PRIVATE) == 0) - _last_user_thread = _thread_run; + if ((curthread->flags & PTHREAD_FLAGS_PRIVATE) == 0) + _last_user_thread = curthread; if (called_from_handler != 0) { called_from_handler = 0; @@ -188,7 +194,7 @@ _thread_kern_scheduler(void) * the current thread. Restore the process signal * mask. */ - if (_thread_sys_sigprocmask(SIG_SETMASK, + if (__sys_sigprocmask(SIG_SETMASK, &_process_sigmask, NULL) != 0) PANIC("Unable to restore process mask after signal"); @@ -196,14 +202,14 @@ _thread_kern_scheduler(void) * Since the signal handler didn't return normally, we * have to tell the kernel to reuse the signal stack. */ - if (_thread_sys_sigaltstack(&_thread_sigstack, NULL) != 0) + if (__sys_sigaltstack(&_thread_sigstack, NULL) != 0) PANIC("Unable to restore alternate signal stack"); } /* Are there pending signals for this thread? */ - if (_thread_run->check_pending != 0) { - _thread_run->check_pending = 0; - _thread_sig_check_pending(_thread_run); + if (curthread->check_pending != 0) { + curthread->check_pending = 0; + _thread_sig_check_pending(curthread); } /* @@ -226,22 +232,22 @@ _thread_kern_scheduler(void) _queue_signals = 1; add_to_prioq = 0; - if (_thread_run != &_thread_kern_thread) { + if (curthread != &_thread_kern_thread) { /* * This thread no longer needs to yield the CPU. */ - _thread_run->yield_on_sig_undefer = 0; + curthread->yield_on_sig_undefer = 0; - if (_thread_run->state != PS_RUNNING) { + if (curthread->state != PS_RUNNING) { /* * Save the current time as the time that the * thread became inactive: */ - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ + curthread->last_inactive =+ UINT_MAX + 1; } } @@ -250,7 +256,7 @@ _thread_kern_scheduler(void) * Place the currently running thread into the * appropriate queue(s). */ - switch (_thread_run->state) { + switch (curthread->state) { case PS_DEAD: case PS_STATE_MAX: /* to silence -Wall */ case PS_SUSPENDED: @@ -285,31 +291,31 @@ _thread_kern_scheduler(void) case PS_SIGWAIT: case PS_WAIT_WAIT: /* No timeouts for these states: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States which can timeout: */ case PS_COND_WAIT: case PS_SLEEP_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); break; /* States that require periodic work: */ case PS_SPINBLOCK: /* No timeouts for this state: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; /* Increment spinblock count: */ _spinblock_count++; @@ -320,13 +326,13 @@ _thread_kern_scheduler(void) case PS_POLL_WAIT: case PS_SELECT_WAIT: /* Restart the time slice: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Insert into the waiting queue: */ - PTHREAD_WAITQ_INSERT(_thread_run); + PTHREAD_WAITQ_INSERT(curthread); /* Insert into the work queue: */ - PTHREAD_WORKQ_INSERT(_thread_run); + PTHREAD_WORKQ_INSERT(curthread); break; } } @@ -342,7 +348,7 @@ _thread_kern_scheduler(void) * has occurred or if we have no more runnable threads. */ else if (((current_tick = _sched_ticks) != last_tick) || - ((_thread_run->state != PS_RUNNING) && + ((curthread->state != PS_RUNNING) && (PTHREAD_PRIOQ_FIRST() == NULL))) { /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -407,43 +413,43 @@ _thread_kern_scheduler(void) * thread became inactive: */ current_tick = _sched_ticks; - _thread_run->last_inactive = (long)current_tick; - if (_thread_run->last_inactive < - _thread_run->last_active) { + curthread->last_inactive = (long)current_tick; + if (curthread->last_inactive < + curthread->last_active) { /* Account for a rollover: */ - _thread_run->last_inactive =+ UINT_MAX + 1; + curthread->last_inactive =+ UINT_MAX + 1; } - if ((_thread_run->slice_usec != -1) && - (_thread_run->attr.sched_policy != SCHED_FIFO)) { + if ((curthread->slice_usec != -1) && + (curthread->attr.sched_policy != SCHED_FIFO)) { /* * Accumulate the number of microseconds for * which the current thread has run: */ - _thread_run->slice_usec += - (_thread_run->last_inactive - - _thread_run->last_active) * + curthread->slice_usec += + (curthread->last_inactive - + curthread->last_active) * (long)_clock_res_usec; /* Check for time quantum exceeded: */ - if (_thread_run->slice_usec > TIMESLICE_USEC) - _thread_run->slice_usec = -1; + if (curthread->slice_usec > TIMESLICE_USEC) + curthread->slice_usec = -1; } - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* * The thread exceeded its time * quantum or it yielded the CPU; * place it at the tail of the * queue for its priority. */ - PTHREAD_PRIOQ_INSERT_TAIL(_thread_run); + PTHREAD_PRIOQ_INSERT_TAIL(curthread); } else { /* * The thread hasn't exceeded its * interval. Place it at the head * of the queue for its priority. */ - PTHREAD_PRIOQ_INSERT_HEAD(_thread_run); + PTHREAD_PRIOQ_INSERT_HEAD(curthread); } } @@ -459,9 +465,11 @@ _thread_kern_scheduler(void) * the running thread to point to the global kernel * thread structure: */ - _thread_run = &_thread_kern_thread; + _set_curthread(&_thread_kern_thread); + curthread = &_thread_kern_thread; + DBG_MSG("No runnable threads, using kernel thread %p\n", - _thread_run); + curthread); /* Unprotect the scheduling queues: */ _queue_signals = 0; @@ -532,23 +540,24 @@ _thread_kern_scheduler(void) } /* Make the selected thread the current thread: */ - _thread_run = pthread_h; + _set_curthread(pthread_h); + curthread = pthread_h; /* * Save the current time as the time that the thread * became active: */ current_tick = _sched_ticks; - _thread_run->last_active = (long) current_tick; + curthread->last_active = (long) current_tick; /* * Check if this thread is running for the first time * or running again after using its full time slice * allocation: */ - if (_thread_run->slice_usec == -1) { + if (curthread->slice_usec == -1) { /* Reset the accumulated time slice period: */ - _thread_run->slice_usec = 0; + curthread->slice_usec = 0; } /* @@ -556,29 +565,29 @@ _thread_kern_scheduler(void) * installed switch hooks. */ if ((_sched_switch_hook != NULL) && - (_last_user_thread != _thread_run)) { + (_last_user_thread != curthread)) { thread_run_switch_hook(_last_user_thread, - _thread_run); + curthread); } /* * Continue the thread at its current frame: */ - switch(_thread_run->ctxtype) { + switch(curthread->ctxtype) { case CTX_JB_NOSIG: - ___longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + ___longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_JB: - __longjmp(_thread_run->ctx.jb, - _thread_run->longjmp_val); + __longjmp(curthread->ctx.jb, + curthread->longjmp_val); break; case CTX_SJB: - __siglongjmp(_thread_run->ctx.sigjb, - _thread_run->longjmp_val); + __siglongjmp(curthread->ctx.sigjb, + curthread->longjmp_val); break; case CTX_UC: /* XXX - Restore FP regsisters? */ - FP_RESTORE_UC(&_thread_run->ctx.uc); + FP_RESTORE_UC(&curthread->ctx.uc); /* * Do a sigreturn to restart the thread that @@ -587,15 +596,15 @@ _thread_kern_scheduler(void) _thread_kern_in_sched = 0; #if NOT_YET - _setcontext(&_thread_run->ctx.uc); + _setcontext(&curthread->ctx.uc); #else /* * Ensure the process signal mask is set * correctly: */ - _thread_run->ctx.uc.uc_sigmask = + curthread->ctx.uc.uc_sigmask = _process_sigmask; - _thread_sys_sigreturn(&_thread_run->ctx.uc); + __sys_sigreturn(&curthread->ctx.uc); #endif break; } @@ -611,6 +620,8 @@ _thread_kern_scheduler(void) void _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -625,9 +636,9 @@ _thread_kern_sched_state(enum pthread_state state, char *fname, int lineno) _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; /* Schedule the next thread that is ready: */ _thread_kern_sched(NULL); @@ -637,6 +648,8 @@ void _thread_kern_sched_state_unlock(enum pthread_state state, spinlock_t *lock, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); + /* * Flag the pthread kernel as executing scheduler code * to avoid a scheduler signal from interrupting this @@ -652,9 +665,9 @@ _thread_kern_sched_state_unlock(enum pthread_state state, _queue_signals = 1; /* Change the state of the current thread: */ - _thread_run->state = state; - _thread_run->fname = fname; - _thread_run->lineno = lineno; + curthread->state = state; + curthread->fname = fname; + curthread->lineno = lineno; _SPINUNLOCK(lock); @@ -817,7 +830,7 @@ thread_kern_poll(int wait_reqd) * Wait for a file descriptor to be ready for read, write, or * an exception, or a timeout to occur: */ - count = _thread_sys_poll(_thread_pfd_table, nfds, timeout_ms); + count = __sys_poll(_thread_pfd_table, nfds, timeout_ms); if (kern_pipe_added != 0) /* @@ -991,11 +1004,12 @@ thread_kern_poll(int wait_reqd) void _thread_kern_set_timeout(const struct timespec * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec current_time; struct timeval tv; /* Reset the timeout flag for the running thread: */ - _thread_run->timeout = 0; + curthread->timeout = 0; /* Check if the thread is to wait forever: */ if (timeout == NULL) { @@ -1003,28 +1017,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) * Set the wakeup time to something that can be recognised as * different to an actual time of day: */ - _thread_run->wakeup_time.tv_sec = -1; - _thread_run->wakeup_time.tv_nsec = -1; + curthread->wakeup_time.tv_sec = -1; + curthread->wakeup_time.tv_nsec = -1; } /* Check if no waiting is required: */ else if (timeout->tv_sec == 0 && timeout->tv_nsec == 0) { /* Set the wake up time to 'immediately': */ - _thread_run->wakeup_time.tv_sec = 0; - _thread_run->wakeup_time.tv_nsec = 0; + curthread->wakeup_time.tv_sec = 0; + curthread->wakeup_time.tv_nsec = 0; } else { /* Get the current time: */ GET_CURRENT_TOD(tv); TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + timeout->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + timeout->tv_nsec; /* Check if the nanosecond field needs to wrap: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } } } @@ -1032,24 +1046,28 @@ _thread_kern_set_timeout(const struct timespec * timeout) void _thread_kern_sig_defer(void) { + struct pthread *curthread = _get_curthread(); + /* Allow signal deferral to be recursive. */ - _thread_run->sig_defer_count++; + curthread->sig_defer_count++; } void _thread_kern_sig_undefer(void) { + struct pthread *curthread = _get_curthread(); + /* * Perform checks to yield only if we are about to undefer * signals. */ - if (_thread_run->sig_defer_count > 1) { + if (curthread->sig_defer_count > 1) { /* Decrement the signal deferral count. */ - _thread_run->sig_defer_count--; + curthread->sig_defer_count--; } - else if (_thread_run->sig_defer_count == 1) { + else if (curthread->sig_defer_count == 1) { /* Reenable signals: */ - _thread_run->sig_defer_count = 0; + curthread->sig_defer_count = 0; /* * Check if there are queued signals: @@ -1061,8 +1079,8 @@ _thread_kern_sig_undefer(void) * Check for asynchronous cancellation before delivering any * pending signals: */ - if (((_thread_run->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && - ((_thread_run->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) + if (((curthread->cancelflags & PTHREAD_AT_CANCEL_POINT) == 0) && + ((curthread->cancelflags & PTHREAD_CANCEL_ASYNCHRONOUS) != 0)) pthread_testcancel(); /* @@ -1071,9 +1089,9 @@ _thread_kern_sig_undefer(void) * * XXX - Come back and revisit the pending signal problem */ - if ((_thread_run->yield_on_sig_undefer != 0) || - SIGNOTEMPTY(_thread_run->sigpend)) { - _thread_run->yield_on_sig_undefer = 0; + if ((curthread->yield_on_sig_undefer != 0) || + SIGNOTEMPTY(curthread->sigpend)) { + curthread->yield_on_sig_undefer = 0; _thread_kern_sched(NULL); } } @@ -1088,7 +1106,7 @@ dequeue_signals(void) /* * Enter a loop to clear the pthread kernel pipe: */ - while (((num = _thread_sys_read(_thread_kern_pipe[0], bufr, + while (((num = __sys_read(_thread_kern_pipe[0], bufr, sizeof(bufr))) > 0) || (num == -1 && errno == EINTR)) { } if ((num < 0) && (errno != EAGAIN)) { @@ -1120,4 +1138,18 @@ thread_run_switch_hook(pthread_t thread_out, pthread_t thread_in) _sched_switch_hook(tid_out, tid_in); } } -#endif + +struct pthread * +_get_curthread(void) +{ + if (_thread_initial == NULL) + _thread_init(); + + return (_thread_run); +} + +void +_set_curthread(struct pthread *newthread) +{ + _thread_run = newthread; +} diff --git a/lib/libpthread/thread/thr_kill.c b/lib/libpthread/thread/thr_kill.c index 4bf1761..7a24ffb 100644 --- a/lib/libpthread/thread/thr_kill.c +++ b/lib/libpthread/thread/thr_kill.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_kill=_pthread_kill + int -pthread_kill(pthread_t pthread, int sig) +_pthread_kill(pthread_t pthread, int sig) { int ret; @@ -71,4 +72,3 @@ pthread_kill(pthread_t pthread, int sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libpthread/thread/thr_mattr_init.c b/lib/libpthread/thread/thr_mattr_init.c index 63d4401..4859915 100644 --- a/lib/libpthread/thread/thr_mattr_init.c +++ b/lib/libpthread/thread/thr_mattr_init.c @@ -34,12 +34,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_init=_pthread_mutexattr_init + int -pthread_mutexattr_init(pthread_mutexattr_t *attr) +_pthread_mutexattr_init(pthread_mutexattr_t *attr) { int ret; pthread_mutexattr_t pattr; @@ -55,4 +56,3 @@ pthread_mutexattr_init(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_mattr_kind_np.c b/lib/libpthread/thread/thr_mattr_kind_np.c index 1a0832f..5ad7928 100644 --- a/lib/libpthread/thread/thr_mattr_kind_np.c +++ b/lib/libpthread/thread/thr_mattr_kind_np.c @@ -32,12 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_setkind_np=_pthread_mutexattr_setkind_np +#pragma weak pthread_mutexattr_getkind_np=_pthread_mutexattr_getkind_np +#pragma weak pthread_mutexattr_gettype=_pthread_mutexattr_gettype +#pragma weak pthread_mutexattr_settype=_pthread_mutexattr_settype + int -pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) +_pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) { int ret; if (attr == NULL || *attr == NULL) { @@ -51,7 +55,7 @@ pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind) } int -pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) +_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) { int ret; if (attr == NULL) { @@ -64,7 +68,7 @@ pthread_mutexattr_getkind_np(pthread_mutexattr_t attr) } int -pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) +_pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { int ret; if (attr == NULL || *attr == NULL || type >= MUTEX_TYPE_MAX) { @@ -78,7 +82,7 @@ pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) } int -pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) +_pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) { int ret; @@ -91,4 +95,3 @@ pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type) } return ret; } -#endif diff --git a/lib/libpthread/thread/thr_msync.c b/lib/libpthread/thread/thr_msync.c index 2ae6ac7..2952da8 100644 --- a/lib/libpthread/thread/thr_msync.c +++ b/lib/libpthread/thread/thr_msync.c @@ -8,22 +8,23 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak msync=__msync + int _msync(void *addr, size_t len, int flags) { int ret; - ret = _thread_sys_msync(addr, len, flags); + ret = __sys_msync(addr, len, flags); return (ret); } int -msync(void *addr, size_t len, int flags) +__msync(void *addr, size_t len, int flags) { int ret; @@ -39,4 +40,3 @@ msync(void *addr, size_t len, int flags) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_multi_np.c b/lib/libpthread/thread/thr_multi_np.c index 9a2f7c1..cf58bb4 100644 --- a/lib/libpthread/thread/thr_multi_np.c +++ b/lib/libpthread/thread/thr_multi_np.c @@ -32,15 +32,15 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_multi_np=_pthread_multi_np + int -pthread_multi_np() +_pthread_multi_np() { /* Return to multi-threaded scheduling mode: */ _thread_single = NULL; return(0); } -#endif diff --git a/lib/libpthread/thread/thr_mutex.c b/lib/libpthread/thread/thr_mutex.c index e0ad7da..1c72b7b 100644 --- a/lib/libpthread/thread/thr_mutex.c +++ b/lib/libpthread/thread/thr_mutex.c @@ -36,7 +36,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -75,6 +74,20 @@ static inline void mutex_queue_enq(pthread_mutex_t, pthread_t); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; +static struct pthread_mutex_attr static_mutex_attr = + PTHREAD_MUTEXATTR_STATIC_INITIALIZER; +static pthread_mutexattr_t static_mattr = &static_mutex_attr; + +/* Single underscore versions provided for libc internal usage: */ +#pragma weak pthread_mutex_trylock=__pthread_mutex_trylock +#pragma weak pthread_mutex_lock=__pthread_mutex_lock + +/* No difference between libc and application usage of these: */ +#pragma weak pthread_mutex_init=_pthread_mutex_init +#pragma weak pthread_mutex_destroy=_pthread_mutex_destroy +#pragma weak pthread_mutex_unlock=_pthread_mutex_unlock + + /* Reinitialize a mutex to defaults. */ int _mutex_reinit(pthread_mutex_t * mutex) @@ -106,7 +119,7 @@ _mutex_reinit(pthread_mutex_t * mutex) } int -pthread_mutex_init(pthread_mutex_t * mutex, +_pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t * mutex_attr) { enum pthread_mutextype type; @@ -201,7 +214,7 @@ pthread_mutex_init(pthread_mutex_t * mutex, } int -pthread_mutex_destroy(pthread_mutex_t * mutex) +_pthread_mutex_destroy(pthread_mutex_t * mutex) { int ret = 0; @@ -259,165 +272,203 @@ init_static(pthread_mutex_t *mutex) return(ret); } -int -pthread_mutex_trylock(pthread_mutex_t * mutex) +static int +init_static_private(pthread_mutex_t *mutex) { + int ret; + + _SPINLOCK(&static_init_lock); + + if (*mutex == NULL) + ret = pthread_mutex_init(mutex, &static_mattr); + else + ret = 0; + + _SPINUNLOCK(&static_init_lock); + + return(ret); +} + +static int +mutex_trylock_common(pthread_mutex_t *mutex) +{ + struct pthread *curthread = _get_curthread(); int ret = 0; - if (mutex == NULL) - ret = EINVAL; + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* - * If the mutex is statically initialized, perform the dynamic - * initialization: + * Defer signals to protect the scheduling queues from + * access by the signal handler: */ - else if (*mutex != NULL || (ret = init_static(mutex)) == 0) { - /* - * Defer signals to protect the scheduling queues from - * access by the signal handler: - */ - _thread_kern_sig_defer(); + _thread_kern_sig_defer(); - /* Lock the mutex structure: */ - _SPINLOCK(&(*mutex)->lock); + /* Lock the mutex structure: */ + _SPINLOCK(&(*mutex)->lock); - /* - * If the mutex was statically allocated, properly - * initialize the tail queue. - */ - if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { - TAILQ_INIT(&(*mutex)->m_queue); - _MUTEX_INIT_LINK(*mutex); - (*mutex)->m_flags |= MUTEX_FLAGS_INITED; - } + /* + * If the mutex was statically allocated, properly + * initialize the tail queue. + */ + if (((*mutex)->m_flags & MUTEX_FLAGS_INITED) == 0) { + TAILQ_INIT(&(*mutex)->m_queue); + _MUTEX_INIT_LINK(*mutex); + (*mutex)->m_flags |= MUTEX_FLAGS_INITED; + } - /* Process according to mutex type: */ - switch ((*mutex)->m_protocol) { - /* Default POSIX mutex: */ - case PTHREAD_PRIO_NONE: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Process according to mutex type: */ + switch ((*mutex)->m_protocol) { + /* Default POSIX mutex: */ + case PTHREAD_PRIO_NONE: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority inheritence mutex: */ - case PTHREAD_PRIO_INHERIT: - /* Check if this mutex is not locked: */ - if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* POSIX priority inheritence mutex: */ + case PTHREAD_PRIO_INHERIT: + /* Check if this mutex is not locked: */ + if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The mutex takes on the attributes of the - * running thread when there are no waiters. - */ - (*mutex)->m_prio = _thread_run->active_priority; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; + /* + * The mutex takes on the attributes of the + * running thread when there are no waiters. + */ + (*mutex)->m_prio = curthread->active_priority; + (*mutex)->m_saved_prio = + curthread->inherited_priority; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* POSIX priority protection mutex: */ - case PTHREAD_PRIO_PROTECT: - /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) - ret = EINVAL; + /* POSIX priority protection mutex: */ + case PTHREAD_PRIO_PROTECT: + /* Check for a priority ceiling violation: */ + if (curthread->active_priority > (*mutex)->m_prio) + ret = EINVAL; - /* Check if this mutex is not locked: */ - else if ((*mutex)->m_owner == NULL) { - /* Lock the mutex for the running thread: */ - (*mutex)->m_owner = _thread_run; + /* Check if this mutex is not locked: */ + else if ((*mutex)->m_owner == NULL) { + /* Lock the mutex for the running thread: */ + (*mutex)->m_owner = curthread; - /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + /* Track number of priority mutexes owned: */ + curthread->priority_mutex_count++; - /* - * The running thread inherits the ceiling - * priority of the mutex and executes at that - * priority. - */ - _thread_run->active_priority = (*mutex)->m_prio; - (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = - (*mutex)->m_prio; + /* + * The running thread inherits the ceiling + * priority of the mutex and executes at that + * priority. + */ + curthread->active_priority = (*mutex)->m_prio; + (*mutex)->m_saved_prio = + curthread->inherited_priority; + curthread->inherited_priority = + (*mutex)->m_prio; - /* Add to the list of owned mutexes: */ - _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, - (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) - ret = mutex_self_trylock(*mutex); - else - /* Return a busy error: */ - ret = EBUSY; - break; + /* Add to the list of owned mutexes: */ + _MUTEX_ASSERT_NOT_OWNED(*mutex); + TAILQ_INSERT_TAIL(&curthread->mutexq, + (*mutex), m_qe); + } else if ((*mutex)->m_owner == curthread) + ret = mutex_self_trylock(*mutex); + else + /* Return a busy error: */ + ret = EBUSY; + break; - /* Trap invalid mutex types: */ - default: - /* Return an invalid argument error: */ - ret = EINVAL; - break; - } + /* Trap invalid mutex types: */ + default: + /* Return an invalid argument error: */ + ret = EINVAL; + break; + } - /* Unlock the mutex structure: */ - _SPINUNLOCK(&(*mutex)->lock); + /* Unlock the mutex structure: */ + _SPINUNLOCK(&(*mutex)->lock); - /* - * Undefer and handle pending signals, yielding if - * necessary: - */ - _thread_kern_sig_undefer(); - } + /* + * Undefer and handle pending signals, yielding if + * necessary: + */ + _thread_kern_sig_undefer(); /* Return the completion status: */ return (ret); } int -pthread_mutex_lock(pthread_mutex_t * mutex) +__pthread_mutex_trylock(pthread_mutex_t *mutex) { int ret = 0; - if (_thread_initial == NULL) - _thread_init(); - if (mutex == NULL) - return (EINVAL); + ret = EINVAL; /* * If the mutex is statically initialized, perform the dynamic * initialization: */ - if ((*mutex == NULL) && - ((ret = init_static(mutex)) != 0)) - return (ret); + else if ((*mutex != NULL) || (ret = init_static(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking the mutex private (delete safe): + */ + else if ((*mutex != NULL) || (ret = init_static_private(mutex)) == 0) + ret = mutex_trylock_common(mutex); + + return (ret); +} + +static int +mutex_lock_common(pthread_mutex_t * mutex) +{ + struct pthread *curthread = _get_curthread(); + int ret = 0; + + PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL), + "Uninitialized mutex in pthread_mutex_trylock_basic"); /* Reset the interrupted flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* * Enter a loop waiting to become the mutex owner. We need a @@ -453,27 +504,27 @@ pthread_mutex_lock(pthread_mutex_t * mutex) case PTHREAD_PRIO_NONE: if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* * Unlock the mutex structure and schedule the @@ -492,42 +543,42 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* Check if this mutex is not locked: */ if ((*mutex)->m_owner == NULL) { /* Lock the mutex for this thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The mutex takes on attributes of the * running thread when there are no waiters. */ - (*mutex)->m_prio = _thread_run->active_priority; + (*mutex)->m_prio = curthread->active_priority; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; - if (_thread_run->active_priority > + if (curthread->active_priority > (*mutex)->m_prio) /* Adjust priorities: */ mutex_priority_adjust(*mutex); @@ -547,7 +598,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* POSIX priority protection mutex: */ case PTHREAD_PRIO_PROTECT: /* Check for a priority ceiling violation: */ - if (_thread_run->active_priority > (*mutex)->m_prio) + if (curthread->active_priority > (*mutex)->m_prio) ret = EINVAL; /* Check if this mutex is not locked: */ @@ -556,43 +607,43 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Lock the mutex for the running * thread: */ - (*mutex)->m_owner = _thread_run; + (*mutex)->m_owner = curthread; /* Track number of priority mutexes owned: */ - _thread_run->priority_mutex_count++; + curthread->priority_mutex_count++; /* * The running thread inherits the ceiling * priority of the mutex and executes at that * priority: */ - _thread_run->active_priority = (*mutex)->m_prio; + curthread->active_priority = (*mutex)->m_prio; (*mutex)->m_saved_prio = - _thread_run->inherited_priority; - _thread_run->inherited_priority = + curthread->inherited_priority; + curthread->inherited_priority = (*mutex)->m_prio; /* Add to the list of owned mutexes: */ _MUTEX_ASSERT_NOT_OWNED(*mutex); - TAILQ_INSERT_TAIL(&_thread_run->mutexq, + TAILQ_INSERT_TAIL(&curthread->mutexq, (*mutex), m_qe); - } else if ((*mutex)->m_owner == _thread_run) + } else if ((*mutex)->m_owner == curthread) ret = mutex_self_lock(*mutex); else { /* * Join the queue of threads waiting to lock * the mutex: */ - mutex_queue_enq(*mutex, _thread_run); + mutex_queue_enq(*mutex, curthread); /* * Keep a pointer to the mutex this thread * is waiting on: */ - _thread_run->data.mutex = *mutex; + curthread->data.mutex = *mutex; /* Clear any previous error: */ - _thread_run->error = 0; + curthread->error = 0; /* * Unlock the mutex structure and schedule the @@ -609,8 +660,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * waiting for the mutex causing a ceiling * violation. */ - ret = _thread_run->error; - _thread_run->error = 0; + ret = curthread->error; + curthread->error = 0; } break; @@ -625,8 +676,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * Check to see if this thread was interrupted and * is still in the mutex queue of waiting threads: */ - if (_thread_run->interrupted != 0) - mutex_queue_remove(*mutex, _thread_run); + if (curthread->interrupted != 0) + mutex_queue_remove(*mutex, curthread); /* Unlock the mutex structure: */ _SPINUNLOCK(&(*mutex)->lock); @@ -636,19 +687,61 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * necessary: */ _thread_kern_sig_undefer(); - } while (((*mutex)->m_owner != _thread_run) && (ret == 0) && - (_thread_run->interrupted == 0)); + } while (((*mutex)->m_owner != curthread) && (ret == 0) && + (curthread->interrupted == 0)); - if (_thread_run->interrupted != 0 && - _thread_run->continuation != NULL) - _thread_run->continuation((void *) _thread_run); + if (curthread->interrupted != 0 && + curthread->continuation != NULL) + curthread->continuation((void *) curthread); /* Return the completion status: */ return (ret); } int -pthread_mutex_unlock(pthread_mutex_t * mutex) +__pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization: + */ + else if ((*mutex != NULL) || ((ret = init_static(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_lock(pthread_mutex_t *mutex) +{ + int ret = 0; + + if (_thread_initial == NULL) + _thread_init(); + + if (mutex == NULL) + ret = EINVAL; + + /* + * If the mutex is statically initialized, perform the dynamic + * initialization marking it private (delete safe): + */ + else if ((*mutex != NULL) || ((ret = init_static_private(mutex)) == 0)) + ret = mutex_lock_common(mutex); + + return (ret); +} + +int +_pthread_mutex_unlock(pthread_mutex_t * mutex) { return (mutex_unlock_common(mutex, /* add reference */ 0)); } @@ -738,6 +831,7 @@ mutex_self_lock(pthread_mutex_t mutex) static inline int mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) { + struct pthread *curthread = _get_curthread(); int ret = 0; if (mutex == NULL || *mutex == NULL) { @@ -760,7 +854,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -828,7 +922,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -852,16 +946,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -946,7 +1040,7 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * Check if the running thread is not the owner of the * mutex: */ - if ((*mutex)->m_owner != _thread_run) { + if ((*mutex)->m_owner != curthread) { /* * Return an invalid argument error for no * owner and a permission error otherwise: @@ -970,16 +1064,16 @@ mutex_unlock_common(pthread_mutex_t * mutex, int add_reference) * not to override changes in the threads base * priority subsequent to locking the mutex). */ - _thread_run->inherited_priority = + curthread->inherited_priority = (*mutex)->m_saved_prio; - _thread_run->active_priority = - MAX(_thread_run->inherited_priority, - _thread_run->base_priority); + curthread->active_priority = + MAX(curthread->inherited_priority, + curthread->base_priority); /* * This thread now owns one less priority mutex. */ - _thread_run->priority_mutex_count--; + curthread->priority_mutex_count--; /* Remove the mutex from the threads queue. */ _MUTEX_ASSERT_IS_OWNED(*mutex); @@ -1477,4 +1571,3 @@ mutex_queue_enq(pthread_mutex_t mutex, pthread_t pthread) pthread->flags |= PTHREAD_FLAGS_IN_MUTEXQ; } -#endif diff --git a/lib/libpthread/thread/thr_mutex_prioceiling.c b/lib/libpthread/thread/thr_mutex_prioceiling.c index c193c82..d19f521 100644 --- a/lib/libpthread/thread/thr_mutex_prioceiling.c +++ b/lib/libpthread/thread/thr_mutex_prioceiling.c @@ -34,12 +34,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprioceiling=_pthread_mutexattr_getprioceiling +#pragma weak pthread_mutexattr_setprioceiling=_pthread_mutexattr_setprioceiling +#pragma weak pthread_mutex_getprioceiling=_pthread_mutex_getprioceiling +#pragma weak pthread_mutex_setprioceiling=_pthread_mutex_setprioceiling + int -pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) +_pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) { int ret = 0; @@ -54,7 +58,7 @@ pthread_mutexattr_getprioceiling(pthread_mutexattr_t *mattr, int *prioceiling) } int -pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) +_pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) { int ret = 0; @@ -69,8 +73,8 @@ pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling) } int -pthread_mutex_getprioceiling(pthread_mutex_t *mutex, - int *prioceiling) +_pthread_mutex_getprioceiling(pthread_mutex_t *mutex, + int *prioceiling) { int ret; @@ -85,8 +89,8 @@ pthread_mutex_getprioceiling(pthread_mutex_t *mutex, } int -pthread_mutex_setprioceiling(pthread_mutex_t *mutex, - int prioceiling, int *old_ceiling) +_pthread_mutex_setprioceiling(pthread_mutex_t *mutex, + int prioceiling, int *old_ceiling) { int ret = 0; @@ -107,4 +111,3 @@ pthread_mutex_setprioceiling(pthread_mutex_t *mutex, } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_mutex_protocol.c b/lib/libpthread/thread/thr_mutex_protocol.c index 9847ae5..1ad9b15 100644 --- a/lib/libpthread/thread/thr_mutex_protocol.c +++ b/lib/libpthread/thread/thr_mutex_protocol.c @@ -34,12 +34,14 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_getprotocol=_pthread_mutexattr_getprotocol +#pragma weak pthread_mutexattr_setprotocol=_pthread_mutexattr_setprotocol + int -pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) +_pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) { int ret = 0; @@ -52,7 +54,7 @@ pthread_mutexattr_getprotocol(pthread_mutexattr_t *mattr, int *protocol) } int -pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) +_pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) { int ret = 0; @@ -66,4 +68,3 @@ pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_mutexattr_destroy.c b/lib/libpthread/thread/thr_mutexattr_destroy.c index 6eed2a2..cc766f0 100644 --- a/lib/libpthread/thread/thr_mutexattr_destroy.c +++ b/lib/libpthread/thread/thr_mutexattr_destroy.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_mutexattr_destroy=_pthread_mutexattr_destroy + int -pthread_mutexattr_destroy(pthread_mutexattr_t *attr) +_pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { @@ -50,4 +51,3 @@ pthread_mutexattr_destroy(pthread_mutexattr_t *attr) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_nanosleep.c b/lib/libpthread/thread/thr_nanosleep.c index 67dd08d..54349d4 100644 --- a/lib/libpthread/thread/thr_nanosleep.c +++ b/lib/libpthread/thread/thr_nanosleep.c @@ -33,14 +33,16 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak nanosleep=__nanosleep + int _nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { + struct pthread *curthread = _get_curthread(); int ret = 0; struct timespec current_time; struct timespec current_time1; @@ -63,16 +65,16 @@ _nanosleep(const struct timespec * time_to_sleep, TIMEVAL_TO_TIMESPEC(&tv, ¤t_time); /* Calculate the time for the current thread to wake up: */ - _thread_run->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; - _thread_run->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; + curthread->wakeup_time.tv_sec = current_time.tv_sec + time_to_sleep->tv_sec; + curthread->wakeup_time.tv_nsec = current_time.tv_nsec + time_to_sleep->tv_nsec; /* Check if the nanosecond field has overflowed: */ - if (_thread_run->wakeup_time.tv_nsec >= 1000000000) { + if (curthread->wakeup_time.tv_nsec >= 1000000000) { /* Wrap the nanosecond field: */ - _thread_run->wakeup_time.tv_sec += 1; - _thread_run->wakeup_time.tv_nsec -= 1000000000; + curthread->wakeup_time.tv_sec += 1; + curthread->wakeup_time.tv_nsec -= 1000000000; } - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Reschedule the current thread to sleep: */ _thread_kern_sched_state(PS_SLEEP_WAIT, __FILE__, __LINE__); @@ -118,7 +120,7 @@ _nanosleep(const struct timespec * time_to_sleep, } /* Check if the sleep was interrupted: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an EINTR error : */ errno = EINTR; ret = -1; @@ -128,7 +130,7 @@ _nanosleep(const struct timespec * time_to_sleep, } int -nanosleep(const struct timespec * time_to_sleep, struct timespec * +__nanosleep(const struct timespec * time_to_sleep, struct timespec * time_remaining) { int ret; @@ -139,4 +141,3 @@ nanosleep(const struct timespec * time_to_sleep, struct timespec * return ret; } -#endif diff --git a/lib/libpthread/thread/thr_once.c b/lib/libpthread/thread/thr_once.c index d7bb494..dde5474 100644 --- a/lib/libpthread/thread/thr_once.c +++ b/lib/libpthread/thread/thr_once.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_once=_pthread_once + int -pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) +_pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) { if (once_control->state == PTHREAD_NEEDS_INIT) { if (_thread_initial == NULL) @@ -50,4 +51,3 @@ pthread_once(pthread_once_t * once_control, void (*init_routine) (void)) } return (0); } -#endif diff --git a/lib/libpthread/thread/thr_open.c b/lib/libpthread/thread/thr_open.c index e7fef91..37bd43d 100644 --- a/lib/libpthread/thread/thr_open.c +++ b/lib/libpthread/thread/thr_open.c @@ -37,10 +37,11 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak open=_open + int _open(const char *path, int flags,...) { @@ -56,12 +57,12 @@ _open(const char *path, int flags,...) va_end(ap); } /* Open the file: */ - if ((fd = _thread_sys_open(path, flags, mode)) < 0) { + if ((fd = __sys_open(path, flags, mode)) < 0) { } /* Initialise the file descriptor table entry: */ else if (_thread_fd_table_init(fd) != 0) { /* Quietly close the file: */ - _thread_sys_close(fd); + __sys_close(fd); /* Reset the file descriptor: */ fd = -1; @@ -72,7 +73,7 @@ _open(const char *path, int flags,...) } int -open(const char *path, int flags,...) +__open(const char *path, int flags,...) { int ret; int mode = 0; @@ -93,4 +94,3 @@ open(const char *path, int flags,...) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_pause.c b/lib/libpthread/thread/thr_pause.c index 09e4c17..19501df 100644 --- a/lib/libpthread/thread/thr_pause.c +++ b/lib/libpthread/thread/thr_pause.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pause=_pause + int -pause(void) +_pause(void) { int ret; @@ -45,4 +46,3 @@ pause(void) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_poll.c b/lib/libpthread/thread/thr_poll.c index 3717273..d3f631a 100644 --- a/lib/libpthread/thread/thr_poll.c +++ b/lib/libpthread/thread/thr_poll.c @@ -38,14 +38,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak poll=_poll int _poll(struct pollfd *fds, unsigned int nfds, int timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int numfds = nfds; int i, ret = 0; @@ -71,7 +72,7 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (-1); } - if (((ret = _thread_sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { + if (((ret = __sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { data.nfds = numfds; data.fds = fds; @@ -83,10 +84,10 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) fds[i].revents = 0; } - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_POLL_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; } else { @@ -96,6 +97,3 @@ _poll(struct pollfd *fds, unsigned int nfds, int timeout) return (ret); } - -__strong_reference(_poll, poll); -#endif diff --git a/lib/libpthread/thread/thr_priority_queue.c b/lib/libpthread/thread/thr_priority_queue.c index 84c3065..55d742b 100644 --- a/lib/libpthread/thread/thr_priority_queue.c +++ b/lib/libpthread/thread/thr_priority_queue.c @@ -34,7 +34,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -71,7 +70,7 @@ static int _pq_active = 0; } while (0) #define _PQ_ASSERT_PROTECTED(msg) \ PTHREAD_ASSERT((_thread_kern_in_sched != 0) || \ - (_thread_run->sig_defer_count > 0) || \ + ((_get_curthread())->sig_defer_count > 0) ||\ (_sig_in_handler != 0), msg); #else @@ -336,4 +335,3 @@ _waitq_clearactive(void) _PQ_ASSERT_ACTIVE("_waitq_clearactive: ! pq_active"); _PQ_CLEAR_ACTIVE(); } -#endif diff --git a/lib/libpthread/thread/thr_private.h b/lib/libpthread/thread/thr_private.h index 2c9a469..688a2b3 100644 --- a/lib/libpthread/thread/thr_private.h +++ b/lib/libpthread/thread/thr_private.h @@ -313,6 +313,9 @@ struct pthread_mutex_attr { long m_flags; }; +#define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \ + { PTHREAD_MUTEX_DEFAULT, PTHREAD_PRIO_NONE, 0, MUTEX_FLAGS_PRIVATE } + /* * Condition variable definitions. */ @@ -1207,6 +1210,8 @@ int _find_dead_thread(pthread_t); int _find_thread(pthread_t); void _flockfile_backout(pthread_t); void _funlock_owned(pthread_t); +struct pthread *_get_curthread(void); +void _set_curthread(struct pthread *); void _join_backout(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 *); @@ -1224,6 +1229,19 @@ void _pq_remove(struct pq_queue *pq, struct pthread *); void _pq_insert_head(struct pq_queue *pq, struct pthread *); void _pq_insert_tail(struct pq_queue *pq, struct pthread *); struct pthread *_pq_first(struct pq_queue *pq); +void *_pthread_getspecific(pthread_key_t); +int _pthread_key_create(pthread_key_t *, void (*) (void *)); +int _pthread_key_delete(pthread_key_t); +int _pthread_mutex_destroy(pthread_mutex_t *); +int _pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *); +int _pthread_mutex_lock(pthread_mutex_t *); +int _pthread_mutex_trylock(pthread_mutex_t *); +int _pthread_mutex_unlock(pthread_mutex_t *); +int _pthread_mutexattr_init(pthread_mutexattr_t *); +int _pthread_mutexattr_destroy(pthread_mutexattr_t *); +int _pthread_mutexattr_settype(pthread_mutexattr_t *, int); +int _pthread_once(pthread_once_t *, void (*) (void)); +int _pthread_setspecific(pthread_key_t, const void *); void _waitq_insert(pthread_t pthread); void _waitq_remove(pthread_t pthread); #if defined(_PTHREADS_INVARIANTS) @@ -1263,190 +1281,113 @@ void _thread_enter_cancellation_point(void); void _thread_leave_cancellation_point(void); void _thread_cancellation_point(void); +/* #include */ +#ifdef _AIO_H_ +int __sys_aio_suspend(const struct aiocb **, int, const struct timespec); +#endif + /* #include */ -int _thread_sys_sigaction(int, const struct sigaction *, struct sigaction *); -int _thread_sys_sigpending(sigset_t *); -int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); -int _thread_sys_sigsuspend(const sigset_t *); -int _thread_sys_siginterrupt(int, int); -int _thread_sys_sigpause(int); -int _thread_sys_sigreturn(ucontext_t *); -int _thread_sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); -int _thread_sys_sigstack(const struct sigstack *, struct sigstack *); -int _thread_sys_sigvec(int, struct sigvec *, struct sigvec *); -void _thread_sys_psignal(unsigned int, const char *); -void (*_thread_sys_signal(int, void (*)(int)))(int); +#ifdef _SIGNAL_H_ +int __sys_sigaction(int, const struct sigaction *, struct sigaction *); +int __sys_sigpending(sigset_t *); +int __sys_sigprocmask(int, const sigset_t *, sigset_t *); +int __sys_sigsuspend(const sigset_t *); +int __sys_sigreturn(ucontext_t *); +int __sys_sigaltstack(const struct sigaltstack *, struct sigaltstack *); +#endif /* #include */ #ifdef _SYS_STAT_H_ -int _thread_sys_fchmod(int, mode_t); -int _thread_sys_fstat(int, struct stat *); -int _thread_sys_fchflags(int, u_long); +int __sys_fchmod(int, mode_t); +int __sys_fstat(int, struct stat *); +int __sys_fchflags(int, u_long); #endif /* #include */ #ifdef _SYS_MOUNT_H_ -int _thread_sys_fstatfs(int, struct statfs *); +int __sys_fstatfs(int, struct statfs *); #endif -int _thread_sys_pipe(int *); -/* #include */ -#ifdef _SYS_SOCKET_H_ -int _thread_sys_accept(int, struct sockaddr *, int *); -int _thread_sys_bind(int, const struct sockaddr *, int); -int _thread_sys_connect(int, const struct sockaddr *, int); -int _thread_sys_getpeername(int, struct sockaddr *, int *); -int _thread_sys_getsockname(int, struct sockaddr *, int *); -int _thread_sys_getsockopt(int, int, int, void *, int *); -int _thread_sys_listen(int, int); -int _thread_sys_setsockopt(int, int, int, const void *, int); -int _thread_sys_shutdown(int, int); -int _thread_sys_socket(int, int, int); -int _thread_sys_socketpair(int, int, int, int *); -ssize_t _thread_sys_recv(int, void *, size_t, int); -ssize_t _thread_sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); -ssize_t _thread_sys_recvmsg(int, struct msghdr *, int); -ssize_t _thread_sys_send(int, const void *, size_t, int); -ssize_t _thread_sys_sendmsg(int, const struct msghdr *, int); -ssize_t _thread_sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); +/* #inclde */ +#ifdef _SYS_EVENT_H_ +int __sys_kevent(int, const struct kevent *, int, struct kevent *, + int, const struct timespec *); #endif -/* #include */ -#ifdef _STDIO_H_ -FILE *_thread_sys_fdopen(int, const char *); -FILE *_thread_sys_fopen(const char *, const char *); -FILE *_thread_sys_freopen(const char *, const char *, FILE *); -FILE *_thread_sys_popen(const char *, const char *); -FILE *_thread_sys_tmpfile(void); -char *_thread_sys_ctermid(char *); -char *_thread_sys_cuserid(char *); -char *_thread_sys_fgetln(FILE *, size_t *); -char *_thread_sys_fgets(char *, int, FILE *); -char *_thread_sys_gets(char *); -char *_thread_sys_tempnam(const char *, const char *); -char *_thread_sys_tmpnam(char *); -int _thread_sys_fclose(FILE *); -int _thread_sys_feof(FILE *); -int _thread_sys_ferror(FILE *); -int _thread_sys_fflush(FILE *); -int _thread_sys_fgetc(FILE *); -int _thread_sys_fgetpos(FILE *, fpos_t *); -int _thread_sys_fileno(FILE *); -int _thread_sys_fprintf(FILE *, const char *, ...); -int _thread_sys_fpurge(FILE *); -int _thread_sys_fputc(int, FILE *); -int _thread_sys_fputs(const char *, FILE *); -int _thread_sys_fscanf(FILE *, const char *, ...); -int _thread_sys_fseek(FILE *, long, int); -int _thread_sys_fsetpos(FILE *, const fpos_t *); -int _thread_sys_getc(FILE *); -int _thread_sys_getchar(void); -int _thread_sys_getw(FILE *); -int _thread_sys_pclose(FILE *); -int _thread_sys_printf(const char *, ...); -int _thread_sys_putc(int, FILE *); -int _thread_sys_putchar(int); -int _thread_sys_puts(const char *); -int _thread_sys_putw(int, FILE *); -int _thread_sys_remove(const char *); -int _thread_sys_rename (const char *, const char *); -int _thread_sys_scanf(const char *, ...); -int _thread_sys_setlinebuf(FILE *); -int _thread_sys_setvbuf(FILE *, char *, int, size_t); -int _thread_sys_snprintf(char *, size_t, const char *, ...); -int _thread_sys_sprintf(char *, const char *, ...); -int _thread_sys_sscanf(const char *, const char *, ...); -int _thread_sys_ungetc(int, FILE *); -int _thread_sys_vfprintf(FILE *, const char *, _BSD_VA_LIST_); -int _thread_sys_vprintf(const char *, _BSD_VA_LIST_); -int _thread_sys_vscanf(const char *, _BSD_VA_LIST_); -int _thread_sys_vsnprintf(char *, size_t, const char *, _BSD_VA_LIST_); -int _thread_sys_vsprintf(char *, const char *, _BSD_VA_LIST_); -int _thread_sys_vsscanf(const char *, const char *, _BSD_VA_LIST_); -long _thread_sys_ftell(FILE *); -size_t _thread_sys_fread(void *, size_t, size_t, FILE *); -size_t _thread_sys_fwrite(const void *, size_t, size_t, FILE *); -void _thread_sys_clearerr(FILE *); -void _thread_sys_perror(const char *); -void _thread_sys_rewind(FILE *); -void _thread_sys_setbuf(FILE *, char *); -void _thread_sys_setbuffer(FILE *, char *, int); +/* #include */ +#ifdef _SYS_SOCKET_H_ +int __sys_accept(int, struct sockaddr *, int *); +int __sys_bind(int, const struct sockaddr *, int); +int __sys_connect(int, const struct sockaddr *, int); +int __sys_getpeername(int, struct sockaddr *, int *); +int __sys_getsockname(int, struct sockaddr *, int *); +int __sys_getsockopt(int, int, int, void *, int *); +int __sys_listen(int, int); +int __sys_setsockopt(int, int, int, const void *, int); +int __sys_shutdown(int, int); +int __sys_socket(int, int, int); +int __sys_socketpair(int, int, int, int *); +ssize_t __sys_recvfrom(int, void *, size_t, int, struct sockaddr *, int *); +ssize_t __sys_recvmsg(int, struct msghdr *, int); +ssize_t __sys_send(int, const void *, size_t, int); +int __sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int); +ssize_t __sys_sendmsg(int, const struct msghdr *, int); +ssize_t __sys_sendto(int, const void *,size_t, int, const struct sockaddr *, int); #endif /* #include */ #ifdef _UNISTD_H_ -char *_thread_sys_ttyname(int); -int _thread_sys_close(int); -int _thread_sys_dup(int); -int _thread_sys_dup2(int, int); -int _thread_sys_exect(const char *, char * const *, char * const *); -int _thread_sys_execve(const char *, char * const *, char * const *); -int _thread_sys_fchdir(int); -int _thread_sys_fchown(int, uid_t, gid_t); -int _thread_sys_fsync(int); -int _thread_sys_ftruncate(int, off_t); -int _thread_sys_pause(void); -int _thread_sys_pipe(int *); -int _thread_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); -long _thread_sys_fpathconf(int, int); -off_t _thread_sys_lseek(int, off_t, int); -pid_t _thread_sys_fork(void); -pid_t _thread_sys_tcgetpgrp(int); -ssize_t _thread_sys_read(int, void *, size_t); -ssize_t _thread_sys_write(int, const void *, size_t); -void _thread_sys__exit(int); +int __sys_close(int); +int __sys_dup(int); +int __sys_dup2(int, int); +int __sys_execve(const char *, char * const *, char * const *); +int __sys_fchown(int, uid_t, gid_t); +int __sys_fork(void); +int __sys_fsync(int); +int __sys_pipe(int *); +int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); +long __sys_fpathconf(int, int); +ssize_t __sys_read(int, void *, size_t); +ssize_t __sys_write(int, const void *, size_t); +void __sys__exit(int); #endif /* #include */ #ifdef _SYS_FCNTL_H_ -int _thread_sys_creat(const char *, mode_t); -int _thread_sys_fcntl(int, int, ...); -int _thread_sys_flock(int, int); -int _thread_sys_open(const char *, int, ...); +int __sys_fcntl(int, int, ...); +int __sys_flock(int, int); +int __sys_open(const char *, int, ...); #endif /* #include */ #ifdef _SYS_IOCTL_H_ -int _thread_sys_ioctl(int, unsigned long, ...); +int __sys_ioctl(int, unsigned long, ...); #endif /* #include */ #ifdef _DIRENT_H_ -DIR *___thread_sys_opendir2(const char *, int); -DIR *_thread_sys_opendir(const char *); -int _thread_sys_alphasort(const void *, const void *); -int _thread_sys_scandir(const char *, struct dirent ***, - int (*)(struct dirent *), int (*)(const void *, const void *)); -int _thread_sys_closedir(DIR *); -int _thread_sys_getdirentries(int, char *, int, long *); -long _thread_sys_telldir(const DIR *); -struct dirent *_thread_sys_readdir(DIR *); -void _thread_sys_rewinddir(DIR *); -void _thread_sys_seekdir(DIR *, long); +int __sys_getdirentries(int, char *, int, long *); #endif /* #include */ #ifdef _SYS_UIO_H_ -ssize_t _thread_sys_readv(int, const struct iovec *, int); -ssize_t _thread_sys_writev(int, const struct iovec *, int); +ssize_t __sys_readv(int, const struct iovec *, int); +ssize_t __sys_writev(int, const struct iovec *, int); #endif /* #include */ #ifdef WNOHANG -pid_t _thread_sys_wait(int *); -pid_t _thread_sys_waitpid(pid_t, int *, int); -pid_t _thread_sys_wait3(int *, int, struct rusage *); -pid_t _thread_sys_wait4(pid_t, int *, int, struct rusage *); +pid_t __sys_wait4(pid_t, int *, int, struct rusage *); #endif /* #include */ #ifdef _SYS_POLL_H_ -int _thread_sys_poll(struct pollfd *, unsigned, int); +int __sys_poll(struct pollfd *, unsigned, int); #endif /* #include */ #ifdef _SYS_MMAN_H_ -int _thread_sys_msync(void *, size_t, int); +int __sys_msync(void *, size_t, int); #endif /* #include */ diff --git a/lib/libpthread/thread/thr_read.c b/lib/libpthread/thread/thr_read.c index b4d9694..5f5475d 100644 --- a/lib/libpthread/thread/thr_read.c +++ b/lib/libpthread/thread/thr_read.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak read=__read + ssize_t _read(int fd, void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -66,14 +68,14 @@ _read(int fd, void *buf, size_t nbytes) } /* Perform a non-blocking read syscall: */ - while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) { + while ((ret = __sys_read(fd, buf, nbytes)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -82,7 +84,7 @@ _read(int fd, void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -97,7 +99,7 @@ _read(int fd, void *buf, size_t nbytes) } ssize_t -read(int fd, void *buf, size_t nbytes) +__read(int fd, void *buf, size_t nbytes) { ssize_t ret; @@ -107,4 +109,3 @@ read(int fd, void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_readv.c b/lib/libpthread/thread/thr_readv.c index d8e02c0..2e14a9f 100644 --- a/lib/libpthread/thread/thr_readv.c +++ b/lib/libpthread/thread/thr_readv.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak readv=_readv + ssize_t _readv(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int ret; int type; @@ -61,14 +63,14 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } /* Perform a non-blocking readv syscall: */ - while ((ret = _thread_sys_readv(fd, iov, iovcnt)) < 0) { + while ((ret = __sys_readv(fd, iov, iovcnt)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); @@ -77,7 +79,7 @@ _readv(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -90,6 +92,3 @@ _readv(int fd, const struct iovec * iov, int iovcnt) } return (ret); } - -__strong_reference(_readv, readv); -#endif diff --git a/lib/libpthread/thread/thr_resume_np.c b/lib/libpthread/thread/thr_resume_np.c index 32b3d05..0d4738d 100644 --- a/lib/libpthread/thread/thr_resume_np.c +++ b/lib/libpthread/thread/thr_resume_np.c @@ -32,13 +32,14 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_resume_np=_pthread_resume_np + /* Resume a thread: */ int -pthread_resume_np(pthread_t thread) +_pthread_resume_np(pthread_t thread) { int ret; enum pthread_susp old_suspended; @@ -89,4 +90,3 @@ pthread_resume_np(pthread_t thread) } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_rwlock.c b/lib/libpthread/thread/thr_rwlock.c index 648e8a3..9c87e44 100644 --- a/lib/libpthread/thread/thr_rwlock.c +++ b/lib/libpthread/thread/thr_rwlock.c @@ -26,7 +26,6 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include @@ -37,6 +36,14 @@ /* maximum number of times a read lock may be obtained */ #define MAX_READ_LOCKS (INT_MAX - 1) +#pragma weak pthread_rwlock_destroy=_pthread_rwlock_destroy +#pragma weak pthread_rwlock_init=_pthread_rwlock_init +#pragma weak pthread_rwlock_rdlock=_pthread_rwlock_rdlock +#pragma weak pthread_rwlock_tryrdlock=_pthread_rwlock_tryrdlock +#pragma weak pthread_rwlock_trywrlock=_pthread_rwlock_trywrlock +#pragma weak pthread_rwlock_unlock=_pthread_rwlock_unlock +#pragma weak pthread_rwlock_wrlock=_pthread_rwlock_wrlock + static int init_static (pthread_rwlock_t *rwlock); static spinlock_t static_init_lock = _SPINLOCK_INITIALIZER; @@ -59,7 +66,7 @@ init_static (pthread_rwlock_t *rwlock) } int -pthread_rwlock_destroy (pthread_rwlock_t *rwlock) +_pthread_rwlock_destroy (pthread_rwlock_t *rwlock) { int ret; @@ -84,7 +91,7 @@ pthread_rwlock_destroy (pthread_rwlock_t *rwlock) } int -pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) +_pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { pthread_rwlock_t prwlock; int ret; @@ -127,7 +134,7 @@ pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) } int -pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -178,7 +185,7 @@ pthread_rwlock_rdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -215,7 +222,7 @@ pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -250,7 +257,7 @@ pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_unlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_unlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -287,7 +294,7 @@ pthread_rwlock_unlock (pthread_rwlock_t *rwlock) } int -pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) +_pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) { pthread_rwlock_t prwlock; int ret; @@ -332,4 +339,3 @@ pthread_rwlock_wrlock (pthread_rwlock_t *rwlock) return(ret); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libpthread/thread/thr_rwlockattr.c b/lib/libpthread/thread/thr_rwlockattr.c index 37eb3ea..7dcaaeb 100644 --- a/lib/libpthread/thread/thr_rwlockattr.c +++ b/lib/libpthread/thread/thr_rwlockattr.c @@ -26,15 +26,19 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include #include #include "pthread_private.h" +#pragma weak pthread_rwlockattr_destroy=_pthread_rwlockattr_destroy +#pragma weak pthread_rwlockattr_getpshared=_pthread_rwlockattr_getpshared +#pragma weak pthread_rwlockattr_init=_pthread_rwlockattr_init +#pragma weak pthread_rwlockattr_setpshared=_pthread_rwlockattr_setpshared + int -pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -52,7 +56,7 @@ pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, +_pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, int *pshared) { *pshared = (*rwlockattr)->pshared; @@ -61,7 +65,7 @@ pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr, } int -pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) +_pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) { pthread_rwlockattr_t prwlockattr; @@ -81,7 +85,7 @@ pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr) } int -pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) +_pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) { /* Only PTHREAD_PROCESS_PRIVATE is supported. */ if (pshared != PTHREAD_PROCESS_PRIVATE) @@ -92,4 +96,3 @@ pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared) return(0); } -#endif /* _THREAD_SAFE */ diff --git a/lib/libpthread/thread/thr_select.c b/lib/libpthread/thread/thr_select.c index 0a7c0d4..547046a 100644 --- a/lib/libpthread/thread/thr_select.c +++ b/lib/libpthread/thread/thr_select.c @@ -40,14 +40,16 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak select=_select + int _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout) { + struct pthread *curthread = _get_curthread(); struct timespec ts; int i, ret = 0, f_wait = 1; int pfd_index, got_one = 0, fd_count = 0; @@ -91,9 +93,9 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * Allocate memory for poll data if it hasn't already been * allocated or if previously allocated memory is insufficient. */ - if ((_thread_run->poll_data.fds == NULL) || - (_thread_run->poll_data.nfds < fd_count)) { - data.fds = (struct pollfd *) realloc(_thread_run->poll_data.fds, + if ((curthread->poll_data.fds == NULL) || + (curthread->poll_data.nfds < fd_count)) { + data.fds = (struct pollfd *) realloc(curthread->poll_data.fds, sizeof(struct pollfd) * MAX(128, fd_count)); if (data.fds == NULL) { errno = ENOMEM; @@ -105,13 +107,13 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, * indicates what is allocated, not what is * currently being polled. */ - _thread_run->poll_data.fds = data.fds; - _thread_run->poll_data.nfds = MAX(128, fd_count); + curthread->poll_data.fds = data.fds; + curthread->poll_data.nfds = MAX(128, fd_count); } } if (ret == 0) { /* Setup the wait data. */ - data.fds = _thread_run->poll_data.fds; + data.fds = curthread->poll_data.fds; data.nfds = fd_count; /* @@ -142,12 +144,12 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, pfd_index--; } } - if (((ret = _thread_sys_poll(data.fds, data.nfds, 0)) == 0) && + if (((ret = __sys_poll(data.fds, data.nfds, 0)) == 0) && (f_wait != 0)) { - _thread_run->data.poll_data = &data; - _thread_run->interrupted = 0; + curthread->data.poll_data = &data; + curthread->interrupted = 0; _thread_kern_sched_state(PS_SELECT_WAIT, __FILE__, __LINE__); - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; data.nfds = 0; ret = -1; @@ -203,6 +205,3 @@ _select(int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, return (ret); } - -__strong_reference(_select, select); -#endif diff --git a/lib/libpthread/thread/thr_self.c b/lib/libpthread/thread/thr_self.c index 81ec427..3c92daa 100644 --- a/lib/libpthread/thread/thr_self.c +++ b/lib/libpthread/thread/thr_self.c @@ -31,14 +31,14 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_self=_pthread_self + pthread_t -pthread_self(void) +_pthread_self(void) { /* Return the running thread pointer: */ - return (_thread_run); + return (_get_curthread()); } -#endif diff --git a/lib/libpthread/thread/thr_sem.c b/lib/libpthread/thread/thr_sem.c index eb42919..926f498 100644 --- a/lib/libpthread/thread/thr_sem.c +++ b/lib/libpthread/thread/thr_sem.c @@ -31,7 +31,6 @@ #include #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" @@ -43,8 +42,19 @@ goto RETURN; \ } +#pragma weak sem_init=_sem_init +#pragma weak sem_destroy=_sem_destroy +#pragma weak sem_open=_sem_open +#pragma weak sem_close=_sem_close +#pragma weak sem_unlink=_sem_unlink +#pragma weak sem_wait=_sem_wait +#pragma weak sem_trywait=_sem_trywait +#pragma weak sem_post=_sem_post +#pragma weak sem_getvalue=_sem_getvalue + + int -sem_init(sem_t *sem, int pshared, unsigned int value) +_sem_init(sem_t *sem, int pshared, unsigned int value) { int retval; @@ -103,7 +113,7 @@ sem_init(sem_t *sem, int pshared, unsigned int value) } int -sem_destroy(sem_t *sem) +_sem_destroy(sem_t *sem) { int retval; @@ -131,28 +141,28 @@ sem_destroy(sem_t *sem) } sem_t * -sem_open(const char *name, int oflag, ...) +_sem_open(const char *name, int oflag, ...) { errno = ENOSYS; return SEM_FAILED; } int -sem_close(sem_t *sem) +_sem_close(sem_t *sem) { errno = ENOSYS; return -1; } int -sem_unlink(const char *name) +_sem_unlink(const char *name) { errno = ENOSYS; return -1; } int -sem_wait(sem_t *sem) +_sem_wait(sem_t *sem) { int retval; @@ -178,7 +188,7 @@ sem_wait(sem_t *sem) } int -sem_trywait(sem_t *sem) +_sem_trywait(sem_t *sem) { int retval; @@ -201,7 +211,7 @@ sem_trywait(sem_t *sem) } int -sem_post(sem_t *sem) +_sem_post(sem_t *sem) { int retval; @@ -235,7 +245,7 @@ sem_post(sem_t *sem) } int -sem_getvalue(sem_t *sem, int *sval) +_sem_getvalue(sem_t *sem, int *sval) { int retval; @@ -249,4 +259,3 @@ sem_getvalue(sem_t *sem, int *sval) RETURN: return retval; } -#endif diff --git a/lib/libpthread/thread/thr_seterrno.c b/lib/libpthread/thread/thr_seterrno.c index 1934aac..0d9474f 100644 --- a/lib/libpthread/thread/thr_seterrno.c +++ b/lib/libpthread/thread/thr_seterrno.c @@ -31,7 +31,6 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -58,4 +57,3 @@ _thread_seterrno(pthread_t thread, int error) */ thread->error = error; } -#endif diff --git a/lib/libpthread/thread/thr_setprio.c b/lib/libpthread/thread/thr_setprio.c index c630db5..eee05dc 100644 --- a/lib/libpthread/thread/thr_setprio.c +++ b/lib/libpthread/thread/thr_setprio.c @@ -31,12 +31,13 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setprio=_pthread_setprio + int -pthread_setprio(pthread_t pthread, int prio) +_pthread_setprio(pthread_t pthread, int prio) { int ret, policy; struct sched_param param; @@ -49,4 +50,3 @@ pthread_setprio(pthread_t pthread, int prio) /* Return the error status: */ return (ret); } -#endif diff --git a/lib/libpthread/thread/thr_setschedparam.c b/lib/libpthread/thread/thr_setschedparam.c index 9a44ad7..a03f036 100644 --- a/lib/libpthread/thread/thr_setschedparam.c +++ b/lib/libpthread/thread/thr_setschedparam.c @@ -33,12 +33,13 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_setschedparam=_pthread_setschedparam + int -pthread_setschedparam(pthread_t pthread, int policy, +_pthread_setschedparam(pthread_t pthread, int policy, const struct sched_param *param) { int old_prio, in_readyq = 0, ret = 0; @@ -116,4 +117,3 @@ pthread_setschedparam(pthread_t pthread, int policy, } return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_sig.c b/lib/libpthread/thread/thr_sig.c index a855a61..9559bb7 100644 --- a/lib/libpthread/thread/thr_sig.c +++ b/lib/libpthread/thread/thr_sig.c @@ -39,7 +39,6 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" @@ -70,6 +69,7 @@ static void thread_sigframe_save(pthread_t thread, struct pthread_signal_frame * void _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) { + struct pthread *curthread = _get_curthread(); pthread_t pthread, pthread_h; void *stackp; int in_sched = 0; @@ -77,7 +77,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) if (ucp == NULL) PANIC("Thread signal handler received null context"); - DBG_MSG("Got signal %d, current thread %p\n", sig, _thread_run); + DBG_MSG("Got signal %d, current thread %p\n", sig, curthread); if (_thread_kern_in_sched != 0) in_sched = 1; @@ -104,13 +104,13 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * the currently running thread has deferred thread * signals. */ - else if (_thread_run->sig_defer_count > 0) - _thread_run->yield_on_sig_undefer = 1; + else if (curthread->sig_defer_count > 0) + curthread->yield_on_sig_undefer = 1; else { /* * Save the context of the currently running thread: */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* * Schedule the next thread. This function is not @@ -131,7 +131,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * is accessing the scheduling queues or if there is a currently * running thread that has deferred signals. */ - else if ((in_sched != 0) || (_thread_run->sig_defer_count > 0)) { + else if ((in_sched != 0) || (curthread->sig_defer_count > 0)) { /* Cast the signal number to a character variable: */ c = sig; @@ -140,7 +140,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * be ready to read when this signal handler returns. */ if (_queue_signals != 0) { - _thread_sys_write(_thread_kern_pipe[1], &c, 1); + __sys_write(_thread_kern_pipe[1], &c, 1); DBG_MSG("Got signal %d, queueing to kernel pipe\n", sig); } if (_thread_sigq[sig - 1].blocked == 0) { @@ -221,7 +221,7 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * This also applies if the current thread is the * thread to be signaled. */ - thread_sig_savecontext(_thread_run, ucp); + thread_sig_savecontext(curthread, ucp); /* Setup the target thread to receive the signal: */ thread_sig_add(pthread, sig, /*has_args*/ 1); @@ -241,8 +241,8 @@ _thread_sig_handler(int sig, siginfo_t *info, ucontext_t *ucp) * signal and the currently running thread is not in a * signal handler. */ - if ((pthread == _thread_run) || ((pthread_h != NULL) && - (pthread_h->active_priority > _thread_run->active_priority))) { + if ((pthread == curthread) || ((pthread_h != NULL) && + (pthread_h->active_priority > curthread->active_priority))) { /* Enter the kernel scheduler: */ _thread_kern_sched(ucp); } @@ -272,6 +272,7 @@ thread_sig_savecontext(pthread_t pthread, ucontext_t *ucp) pthread_t thread_sig_find(int sig) { + struct pthread *curthread = _get_curthread(); int handler_installed; pthread_t pthread, pthread_next; pthread_t suspended_thread, signaled_thread; @@ -302,9 +303,9 @@ thread_sig_find(int sig) * installed, the signal only affects threads in sigwait. */ suspended_thread = NULL; - if ((_thread_run != &_thread_kern_thread) && - !sigismember(&_thread_run->sigmask, sig)) - signaled_thread = _thread_run; + if ((curthread != &_thread_kern_thread) && + !sigismember(&curthread->sigmask, sig)) + signaled_thread = curthread; else signaled_thread = NULL; if ((_thread_sigact[sig - 1].sa_handler == SIG_IGN) || @@ -508,7 +509,7 @@ thread_sig_handle_special(int sig) /* * Set the file descriptor to non-blocking: */ - _thread_sys_fcntl(i, F_SETFL, + __sys_fcntl(i, F_SETFL, _thread_fd_table[i]->flags | O_NONBLOCK); } } @@ -568,6 +569,7 @@ thread_sig_handle_special(int sig) static void thread_sig_add(pthread_t pthread, int sig, int has_args) { + struct pthread *curthread = _get_curthread(); int restart; int suppress_handler = 0; @@ -751,7 +753,7 @@ thread_sig_add(pthread_t pthread, int sig, int has_args) * the thread in the run queue. */ pthread->active_priority |= PTHREAD_SIGNAL_PRIORITY; - if (pthread != _thread_run) + if (pthread != curthread) PTHREAD_PRIOQ_INSERT_TAIL(pthread); } } @@ -849,6 +851,8 @@ thread_sig_check_state(pthread_t pthread, int sig) void _thread_sig_send(pthread_t pthread, int sig) { + struct pthread *curthread = _get_curthread(); + /* Check for signals whose actions are SIG_DFL: */ if (_thread_sigact[sig - 1].sa_handler == SIG_DFL) { /* @@ -878,7 +882,7 @@ _thread_sig_send(pthread_t pthread, int sig) /* Return the signal number: */ pthread->signo = sig; - } else if (pthread == _thread_run) { + } else if (pthread == curthread) { /* Add the signal to the pending set: */ sigaddset(&pthread->sigpend, sig); if (!sigismember(&pthread->sigmask, sig)) { @@ -915,9 +919,7 @@ _thread_sig_wrapper(void) { void (*sigfunc)(int, siginfo_t *, void *); struct pthread_signal_frame *psf; - pthread_t thread; - - thread = _thread_run; + struct pthread *thread = _get_curthread(); /* Get the current frame and state: */ psf = thread->curframe; @@ -1117,4 +1119,3 @@ thread_sigframe_save(pthread_t thread, struct pthread_signal_frame *psf) psf->saved_state.psd_sig_defer_count = thread->sig_defer_count; } -#endif diff --git a/lib/libpthread/thread/thr_sigaction.c b/lib/libpthread/thread/thr_sigaction.c index 4d13819..dba2f7e 100644 --- a/lib/libpthread/thread/thr_sigaction.c +++ b/lib/libpthread/thread/thr_sigaction.c @@ -33,10 +33,11 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigaction=_sigaction + int _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) { @@ -49,6 +50,9 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) errno = EINVAL; ret = -1; } else { + if (_thread_initial == NULL) + _thread_init(); + /* * Check if the existing signal action structure contents are * to be returned: @@ -98,7 +102,7 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) gact.sa_handler = (void (*) ()) _thread_sig_handler; /* Change the signal action in the kernel: */ - if (_thread_sys_sigaction(sig,&gact,NULL) != 0) + if (__sys_sigaction(sig,&gact,NULL) != 0) ret = -1; } } @@ -106,6 +110,3 @@ _sigaction(int sig, const struct sigaction * act, struct sigaction * oact) /* Return the completion status: */ return (ret); } - -__strong_reference(_sigaction, sigaction); -#endif diff --git a/lib/libpthread/thread/thr_sigmask.c b/lib/libpthread/thread/thr_sigmask.c index 53d0774..c16d66b 100644 --- a/lib/libpthread/thread/thr_sigmask.c +++ b/lib/libpthread/thread/thr_sigmask.c @@ -36,20 +36,22 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak pthread_sigmask=_pthread_sigmask + int -pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) +_pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { + struct pthread *curthread = _get_curthread(); sigset_t sigset; int ret = 0; /* Check if the existing signal process mask is to be returned: */ if (oset != NULL) { /* Return the current mask: */ - *oset = _thread_run->sigmask; + *oset = curthread->sigmask; } /* Check if a new signal set was provided by the caller: */ if (set != NULL) { @@ -58,19 +60,19 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Block signals: */ case SIG_BLOCK: /* Add signals to the existing mask: */ - SIGSETOR(_thread_run->sigmask, *set); + SIGSETOR(curthread->sigmask, *set); break; /* Unblock signals: */ case SIG_UNBLOCK: /* Clear signals from the existing mask: */ - SIGSETNAND(_thread_run->sigmask, *set); + SIGSETNAND(curthread->sigmask, *set); break; /* Set the signal process mask: */ case SIG_SETMASK: /* Set the new mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; break; /* Trap invalid actions: */ @@ -82,15 +84,15 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) } /* Increment the sequence number: */ - _thread_run->sigmask_seqno++; + curthread->sigmask_seqno++; /* * Check if there are pending signals for the running * thread or process that aren't blocked: */ - sigset = _thread_run->sigpend; + sigset = curthread->sigpend; SIGSETOR(sigset, _process_sigpending); - SIGSETNAND(sigset, _thread_run->sigmask); + SIGSETNAND(sigset, curthread->sigmask); if (SIGNOTEMPTY(sigset)) /* * Call the kernel scheduler which will safely @@ -102,4 +104,3 @@ pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libpthread/thread/thr_sigpending.c b/lib/libpthread/thread/thr_sigpending.c index a118347..98b7d26 100644 --- a/lib/libpthread/thread/thr_sigpending.c +++ b/lib/libpthread/thread/thr_sigpending.c @@ -33,13 +33,15 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigpending=_sigpending + int _sigpending(sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = 0; /* Check for a null signal set pointer: */ @@ -48,11 +50,8 @@ _sigpending(sigset_t * set) ret = EINVAL; } else { - *set = _thread_run->sigpend; + *set = curthread->sigpend; } /* Return the completion status: */ return (ret); } - -__strong_reference(_sigpending, sigpending); -#endif diff --git a/lib/libpthread/thread/thr_sigprocmask.c b/lib/libpthread/thread/thr_sigprocmask.c index f8ca031..f93d463 100644 --- a/lib/libpthread/thread/thr_sigprocmask.c +++ b/lib/libpthread/thread/thr_sigprocmask.c @@ -36,15 +36,13 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigprocmask=_sigprocmask + int _sigprocmask(int how, const sigset_t *set, sigset_t *oset) { return (pthread_sigmask(how, set, oset)); } - -__strong_reference(_sigprocmask, sigprocmask); -#endif diff --git a/lib/libpthread/thread/thr_sigsuspend.c b/lib/libpthread/thread/thr_sigsuspend.c index 28959c8..f0a08e2 100644 --- a/lib/libpthread/thread/thr_sigsuspend.c +++ b/lib/libpthread/thread/thr_sigsuspend.c @@ -33,23 +33,25 @@ */ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigsuspend=__sigsuspend + int _sigsuspend(const sigset_t * set) { + struct pthread *curthread = _get_curthread(); int ret = -1; sigset_t oset; /* Check if a new signal set was provided by the caller: */ if (set != NULL) { /* Save the current signal mask: */ - oset = _thread_run->sigmask; + oset = curthread->sigmask; /* Change the caller's mask: */ - _thread_run->sigmask = *set; + curthread->sigmask = *set; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGSUSPEND, __FILE__, __LINE__); @@ -58,7 +60,7 @@ _sigsuspend(const sigset_t * set) errno = EINTR; /* Restore the signal mask: */ - _thread_run->sigmask = oset; + curthread->sigmask = oset; } else { /* Return an invalid argument error: */ errno = EINVAL; @@ -69,7 +71,7 @@ _sigsuspend(const sigset_t * set) } int -sigsuspend(const sigset_t * set) +__sigsuspend(const sigset_t * set) { int ret; @@ -79,4 +81,3 @@ sigsuspend(const sigset_t * set) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_sigwait.c b/lib/libpthread/thread/thr_sigwait.c index f2c6ea1..71fe007 100644 --- a/lib/libpthread/thread/thr_sigwait.c +++ b/lib/libpthread/thread/thr_sigwait.c @@ -35,13 +35,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sigwait=_sigwait + int -sigwait(const sigset_t *set, int *sig) +_sigwait(const sigset_t *set, int *sig) { + struct pthread *curthread = _get_curthread(); int ret = 0; int i; sigset_t tempset, waitset; @@ -69,7 +71,7 @@ sigwait(const sigset_t *set, int *sig) sigdelset(&waitset, SIGINFO); /* Check to see if a pending signal is in the wait mask. */ - tempset = _thread_run->sigpend; + tempset = curthread->sigpend; SIGSETOR(tempset, _process_sigpending); SIGSETAND(tempset, waitset); if (SIGNOTEMPTY(tempset)) { @@ -80,8 +82,8 @@ sigwait(const sigset_t *set, int *sig) } /* Clear the pending signal: */ - if (sigismember(&_thread_run->sigpend,i)) - sigdelset(&_thread_run->sigpend,i); + if (sigismember(&curthread->sigpend,i)) + sigdelset(&curthread->sigpend,i); else sigdelset(&_process_sigpending,i); @@ -115,7 +117,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]++; sigaddset(&tempset, i); if (_thread_dfl_count[i] == 1) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -129,19 +131,19 @@ sigwait(const sigset_t *set, int *sig) * mask is independent of the threads signal mask * and requires separate storage. */ - _thread_run->data.sigwait = &waitset; + curthread->data.sigwait = &waitset; /* Wait for a signal: */ _thread_kern_sched_state(PS_SIGWAIT, __FILE__, __LINE__); /* Return the signal number to the caller: */ - *sig = _thread_run->signo; + *sig = curthread->signo; /* * 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; + curthread->data.sigwait = NULL; } /* @@ -157,7 +159,7 @@ sigwait(const sigset_t *set, int *sig) _thread_dfl_count[i]--; if ((_thread_sigact[i - 1].sa_handler == SIG_DFL) && (_thread_dfl_count[i] == 0)) { - if (_thread_sys_sigaction(i,&act,NULL) != 0) + if (__sys_sigaction(i,&act,NULL) != 0) ret = -1; } } @@ -170,4 +172,3 @@ sigwait(const sigset_t *set, int *sig) /* Return the completion status: */ return (ret); } -#endif diff --git a/lib/libpthread/thread/thr_single_np.c b/lib/libpthread/thread/thr_single_np.c index d6ecb48..52cd6ed 100644 --- a/lib/libpthread/thread/thr_single_np.c +++ b/lib/libpthread/thread/thr_single_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" -int pthread_single_np() +#pragma weak pthread_single_np=_pthread_single_np + +int _pthread_single_np() { + struct pthread *curthread = _get_curthread(); + /* Enter single-threaded (non-POSIX) scheduling mode: */ - _thread_single = _thread_run; + _thread_single = curthread; return(0); } -#endif diff --git a/lib/libpthread/thread/thr_sleep.c b/lib/libpthread/thread/thr_sleep.c index fdfeeec..acc6e55 100644 --- a/lib/libpthread/thread/thr_sleep.c +++ b/lib/libpthread/thread/thr_sleep.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sleep=_sleep + unsigned int -sleep(unsigned int seconds) +_sleep(unsigned int seconds) { unsigned int ret; @@ -45,4 +46,3 @@ sleep(unsigned int seconds) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_spec.c b/lib/libpthread/thread/thr_spec.c index 1485cbb..783617f 100644 --- a/lib/libpthread/thread/thr_spec.c +++ b/lib/libpthread/thread/thr_spec.c @@ -35,15 +35,20 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" /* Static variables: */ static struct pthread_key key_table[PTHREAD_KEYS_MAX]; +#pragma weak pthread_key_create=_pthread_key_create +#pragma weak pthread_key_delete=_pthread_key_delete +#pragma weak pthread_getspecific=_pthread_getspecific +#pragma weak pthread_setspecific=_pthread_setspecific + + int -pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) +_pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) { for ((*key) = 0; (*key) < PTHREAD_KEYS_MAX; (*key)++) { /* Lock the key table entry: */ @@ -65,7 +70,7 @@ pthread_key_create(pthread_key_t * key, void (*destructor) (void *)) } int -pthread_key_delete(pthread_key_t key) +_pthread_key_delete(pthread_key_t key) { int ret = 0; @@ -88,23 +93,24 @@ pthread_key_delete(pthread_key_t key) void _thread_cleanupspecific(void) { - void *data = NULL; - int key; - int itr; + struct pthread *curthread = _get_curthread(); + void *data = NULL; + int key; + int itr; void (*destructor)( void *); for (itr = 0; itr < PTHREAD_DESTRUCTOR_ITERATIONS; itr++) { for (key = 0; key < PTHREAD_KEYS_MAX; key++) { - if (_thread_run->specific_data_count) { + if (curthread->specific_data_count) { /* Lock the key table entry: */ _SPINLOCK(&key_table[key].lock); destructor = NULL; if (key_table[key].allocated) { - if (_thread_run->specific_data[key]) { - data = (void *) _thread_run->specific_data[key]; - _thread_run->specific_data[key] = NULL; - _thread_run->specific_data_count--; + if (curthread->specific_data[key]) { + data = (void *) curthread->specific_data[key]; + curthread->specific_data[key] = NULL; + curthread->specific_data_count--; destructor = key_table[key].destructor; } } @@ -119,14 +125,14 @@ _thread_cleanupspecific(void) if (destructor) destructor(data); } else { - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; return; } } } - free(_thread_run->specific_data); - _thread_run->specific_data = NULL; + free(curthread->specific_data); + curthread->specific_data = NULL; } static inline const void ** @@ -140,13 +146,13 @@ pthread_key_allocate_data(void) } int -pthread_setspecific(pthread_key_t key, const void *value) +_pthread_setspecific(pthread_key_t key, const void *value) { - pthread_t pthread; - int ret = 0; + struct pthread *pthread; + int ret = 0; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); if ((pthread->specific_data) || (pthread->specific_data = pthread_key_allocate_data())) { @@ -171,13 +177,13 @@ pthread_setspecific(pthread_key_t key, const void *value) } void * -pthread_getspecific(pthread_key_t key) +_pthread_getspecific(pthread_key_t key) { - pthread_t pthread; + struct pthread *pthread; void *data; /* Point to the running thread: */ - pthread = _thread_run; + pthread = _get_curthread(); /* Check if there is specific data: */ if (pthread->specific_data != NULL && key < PTHREAD_KEYS_MAX) { @@ -197,4 +203,3 @@ pthread_getspecific(pthread_key_t key) data = NULL; return (data); } -#endif diff --git a/lib/libpthread/thread/thr_spinlock.c b/lib/libpthread/thread/thr_spinlock.c index e797f90..696174e 100644 --- a/lib/libpthread/thread/thr_spinlock.c +++ b/lib/libpthread/thread/thr_spinlock.c @@ -51,18 +51,20 @@ extern char *__progname; void _spinlock(spinlock_t *lck) { + struct pthread *curthread = _get_curthread(); + /* * Try to grab the lock and loop if another thread grabs * it before we do. */ while(_atomic_lock(&lck->access_lock)) { /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, __FILE__, __LINE__); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; } /* @@ -78,6 +80,7 @@ _spinlock(spinlock_t *lck) void _spinlock_debug(spinlock_t *lck, char *fname, int lineno) { + struct pthread *curthread = _get_curthread(); int cnt = 0; /* @@ -88,19 +91,19 @@ _spinlock_debug(spinlock_t *lck, char *fname, int lineno) cnt++; if (cnt > 100) { char str[256]; - snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, _thread_run, lck, fname, lineno, lck->fname, lck->lineno); - _thread_sys_write(2,str,strlen(str)); + snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", __progname, curthread, lck, fname, lineno, lck->fname, lck->lineno); + __sys_write(2,str,strlen(str)); __sleep(1); cnt = 0; } /* Block the thread until the lock. */ - _thread_run->data.spinlock = lck; + curthread->data.spinlock = lck; _thread_kern_sched_state(PS_SPINBLOCK, fname, lineno); } /* The running thread now owns the lock: */ - lck->lock_owner = (long) _thread_run; + lck->lock_owner = (long) curthread; lck->fname = fname; lck->lineno = lineno; } diff --git a/lib/libpthread/thread/thr_suspend_np.c b/lib/libpthread/thread/thr_suspend_np.c index 083f552..5c204a2 100644 --- a/lib/libpthread/thread/thr_suspend_np.c +++ b/lib/libpthread/thread/thr_suspend_np.c @@ -32,15 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" static void finish_suspension(void *arg); +#pragma weak pthread_suspend_np=_pthread_suspend_np + /* Suspend a thread: */ int -pthread_suspend_np(pthread_t thread) +_pthread_suspend_np(pthread_t thread) { int ret; @@ -144,9 +145,10 @@ pthread_suspend_np(pthread_t thread) static void finish_suspension(void *arg) { - if (_thread_run->suspended != SUSP_NO) + struct pthread *curthread = _get_curthread(); + + if (curthread->suspended != SUSP_NO) _thread_kern_sched_state(PS_SUSPENDED, __FILE__, __LINE__); } -#endif diff --git a/lib/libpthread/thread/thr_switch_np.c b/lib/libpthread/thread/thr_switch_np.c index 9b83545..e999259 100644 --- a/lib/libpthread/thread/thr_switch_np.c +++ b/lib/libpthread/thread/thr_switch_np.c @@ -32,14 +32,16 @@ * $FreeBSD$ */ #include -#ifdef _THREAD_SAFE #include #include #include "pthread_private.h" +#pragma weak pthread_switch_add_np=_pthread_switch_add_np +#pragma weak pthread_switch_delete_np=_pthread_switch_delete_np + int -pthread_switch_add_np(pthread_switch_routine_t routine) +_pthread_switch_add_np(pthread_switch_routine_t routine) { int ret = 0; @@ -54,7 +56,7 @@ pthread_switch_add_np(pthread_switch_routine_t routine) } int -pthread_switch_delete_np(pthread_switch_routine_t routine) +_pthread_switch_delete_np(pthread_switch_routine_t routine) { int ret = 0; @@ -67,4 +69,3 @@ pthread_switch_delete_np(pthread_switch_routine_t routine) return(ret); } -#endif diff --git a/lib/libpthread/thread/thr_system.c b/lib/libpthread/thread/thr_system.c index 47a572d..d3a16cb 100644 --- a/lib/libpthread/thread/thr_system.c +++ b/lib/libpthread/thread/thr_system.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak system=_system + int -system(const char *string) +_system(const char *string) { int ret; @@ -45,4 +46,3 @@ system(const char *string) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_tcdrain.c b/lib/libpthread/thread/thr_tcdrain.c index 6470882..7700791 100644 --- a/lib/libpthread/thread/thr_tcdrain.c +++ b/lib/libpthread/thread/thr_tcdrain.c @@ -30,12 +30,13 @@ */ #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak tcdrain=_tcdrain + int -tcdrain(int fd) +_tcdrain(int fd) { int ret; @@ -45,4 +46,3 @@ tcdrain(int fd) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_vfork.c b/lib/libpthread/thread/thr_vfork.c index bbfcf00..78d9f01 100644 --- a/lib/libpthread/thread/thr_vfork.c +++ b/lib/libpthread/thread/thr_vfork.c @@ -1,9 +1,12 @@ +/* + * $FreeBSD$ + */ #include -#ifdef _THREAD_SAFE + +#pragma weak vfork=_vfork int -vfork(void) +_vfork(void) { return (fork()); } -#endif diff --git a/lib/libpthread/thread/thr_wait.c b/lib/libpthread/thread/thr_wait.c index c34d997..9482586 100644 --- a/lib/libpthread/thread/thr_wait.c +++ b/lib/libpthread/thread/thr_wait.c @@ -29,12 +29,13 @@ * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait=_wait + pid_t -wait(int *istat) +_wait(int *istat) { pid_t ret; @@ -44,4 +45,3 @@ wait(int *istat) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_wait4.c b/lib/libpthread/thread/thr_wait4.c index da69a4f..8b44366 100644 --- a/lib/libpthread/thread/thr_wait4.c +++ b/lib/libpthread/thread/thr_wait4.c @@ -35,27 +35,29 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak wait4=_wait4 + pid_t _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) { + struct pthread *curthread = _get_curthread(); pid_t ret; _thread_kern_sig_defer(); /* Perform a non-blocking wait4 syscall: */ - while ((ret = _thread_sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { + while ((ret = __sys_wait4(pid, istat, options | WNOHANG, rusage)) == 0 && (options & WNOHANG) == 0) { /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; /* Schedule the next thread while this one waits: */ _thread_kern_sched_state(PS_WAIT_WAIT, __FILE__, __LINE__); /* Check if this call was interrupted by a signal: */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { errno = EINTR; ret = -1; break; @@ -66,6 +68,3 @@ _wait4(pid_t pid, int *istat, int options, struct rusage * rusage) return (ret); } - -__strong_reference(_wait4, wait4); -#endif diff --git a/lib/libpthread/thread/thr_waitpid.c b/lib/libpthread/thread/thr_waitpid.c index 28fcd78..672183d 100644 --- a/lib/libpthread/thread/thr_waitpid.c +++ b/lib/libpthread/thread/thr_waitpid.c @@ -31,12 +31,13 @@ #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak waitpid=_waitpid + pid_t -waitpid(pid_t wpid, int *status, int options) +_waitpid(pid_t wpid, int *status, int options) { pid_t ret; @@ -46,4 +47,3 @@ waitpid(pid_t wpid, int *status, int options) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_write.c b/lib/libpthread/thread/thr_write.c index 5d9ef35..b841726 100644 --- a/lib/libpthread/thread/thr_write.c +++ b/lib/libpthread/thread/thr_write.c @@ -37,13 +37,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak write=__write + ssize_t _write(int fd, const void *buf, size_t nbytes) { + struct pthread *curthread = _get_curthread(); int blocking; int type; ssize_t n; @@ -76,7 +78,7 @@ _write(int fd, const void *buf, size_t nbytes) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_write(fd, buf + num, nbytes - num); + n = __sys_write(fd, buf + num, nbytes - num); /* Check if one or more bytes were written: */ if (n > 0) @@ -94,11 +96,11 @@ _write(int fd, const void *buf, size_t nbytes) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && num < nbytes))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -107,7 +109,7 @@ _write(int fd, const void *buf, size_t nbytes) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -133,7 +135,7 @@ _write(int fd, const void *buf, size_t nbytes) } ssize_t -write(int fd, const void *buf, size_t nbytes) +__write(int fd, const void *buf, size_t nbytes) { ssize_t ret; @@ -143,4 +145,3 @@ write(int fd, const void *buf, size_t nbytes) return ret; } -#endif diff --git a/lib/libpthread/thread/thr_writev.c b/lib/libpthread/thread/thr_writev.c index 5f31468..66b451f 100644 --- a/lib/libpthread/thread/thr_writev.c +++ b/lib/libpthread/thread/thr_writev.c @@ -39,13 +39,15 @@ #include #include #include -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak writev=_writev + ssize_t _writev(int fd, const struct iovec * iov, int iovcnt) { + struct pthread *curthread = _get_curthread(); int blocking; int idx = 0; int type; @@ -92,7 +94,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ while (ret == 0) { /* Perform a non-blocking write syscall: */ - n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx); + n = __sys_writev(fd, &p_iov[idx], iovcnt - idx); /* Check if one or more bytes were written: */ if (n > 0) { @@ -158,11 +160,11 @@ _writev(int fd, const struct iovec * iov, int iovcnt) */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) { - _thread_run->data.fd.fd = fd; + curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ - _thread_run->interrupted = 0; + curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); @@ -171,7 +173,7 @@ _writev(int fd, const struct iovec * iov, int iovcnt) * Check if the operation was * interrupted by a signal */ - if (_thread_run->interrupted) { + if (curthread->interrupted) { /* Return an error: */ ret = -1; } @@ -200,6 +202,3 @@ _writev(int fd, const struct iovec * iov, int iovcnt) return (ret); } - -__strong_reference(_writev, writev); -#endif diff --git a/lib/libpthread/thread/thr_yield.c b/lib/libpthread/thread/thr_yield.c index 7d64283..67ca501 100644 --- a/lib/libpthread/thread/thr_yield.c +++ b/lib/libpthread/thread/thr_yield.c @@ -31,15 +31,19 @@ * * $FreeBSD$ */ -#ifdef _THREAD_SAFE #include #include "pthread_private.h" +#pragma weak sched_yield=_sched_yield +#pragma weak pthread_yield=_pthread_yield + int -sched_yield(void) +_sched_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); @@ -50,12 +54,13 @@ sched_yield(void) /* Draft 4 yield */ void -pthread_yield(void) +_pthread_yield(void) { + struct pthread *curthread = _get_curthread(); + /* Reset the accumulated time slice value for the current thread: */ - _thread_run->slice_usec = -1; + curthread->slice_usec = -1; /* Schedule the next thread: */ _thread_kern_sched(NULL); } -#endif -- cgit v1.1