- Define curthread as _get_curthread() and remove all direct calls to

_get_curthread().  This is similar to the kernel's curthread.  Doing
   this saves stack overhead and is more convenient to the programmer.
 - Pass the pointer to the newly created thread to _thread_init().
 - Remove _get_curthread_slow().
This commit is contained in:
Jeff Roberson 2003-04-02 03:05:39 +00:00
parent dd3dd8724d
commit 26f52e2f8b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=112965
20 changed files with 21 additions and 96 deletions

View file

@ -15,7 +15,6 @@ int
_pthread_cancel(pthread_t pthread)
{
int ret;
pthread_t curthread;
if ((ret = _find_thread(pthread)) != 0) {
/* NOTHING */
@ -23,7 +22,6 @@ _pthread_cancel(pthread_t pthread)
|| (pthread->flags & PTHREAD_EXITING) != 0) {
ret = 0;
} else {
curthread = _get_curthread();
GIANT_LOCK(curthread);
if (((pthread->cancelflags & PTHREAD_CANCEL_DISABLE) != 0) ||
@ -94,7 +92,6 @@ _pthread_cancel(pthread_t pthread)
int
_pthread_setcancelstate(int state, int *oldstate)
{
struct pthread *curthread = _get_curthread();
int ostate;
GIANT_LOCK(curthread);
@ -127,7 +124,6 @@ _pthread_setcancelstate(int state, int *oldstate)
int
_pthread_setcanceltype(int type, int *oldtype)
{
struct pthread *curthread = _get_curthread();
int otype;
GIANT_LOCK(curthread);
@ -160,8 +156,6 @@ _pthread_setcanceltype(int type, int *oldtype)
void
_pthread_testcancel(void)
{
struct pthread *curthread = _get_curthread();
GIANT_LOCK(curthread);
if (((curthread->cancelflags & PTHREAD_CANCEL_DISABLE) == 0) &&
((curthread->cancelflags & PTHREAD_CANCELLING) != 0) &&
@ -183,8 +177,6 @@ _pthread_testcancel(void)
void
_thread_enter_cancellation_point(void)
{
struct pthread *curthread = _get_curthread();
pthread_testcancel();
GIANT_LOCK(curthread);
@ -195,8 +187,6 @@ _thread_enter_cancellation_point(void)
void
_thread_leave_cancellation_point(void)
{
struct pthread *curthread = _get_curthread();
GIANT_LOCK(curthread);
curthread->cancelflags &= ~PTHREAD_AT_CANCEL_POINT;
GIANT_UNLOCK(curthread);

View file

@ -43,7 +43,6 @@ __weak_reference(_pthread_cleanup_pop, pthread_cleanup_pop);
void
_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) {
@ -58,7 +57,6 @@ _pthread_cleanup_push(void (*routine) (void *), void *routine_arg)
void
_pthread_cleanup_pop(int execute)
{
struct pthread *curthread = _get_curthread();
struct pthread_cleanup *old;
if ((old = curthread->cleanup) != NULL) {

View file

@ -137,8 +137,6 @@ _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
int
_pthread_cond_destroy(pthread_cond_t *cond)
{
struct pthread *curthread = _get_curthread();
if (cond == NULL || *cond == NULL)
return (EINVAL);
@ -182,7 +180,6 @@ int
_pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
const struct timespec * abstime)
{
struct pthread *curthread = _get_curthread();
struct timespec *time;
int rval = 0;
int done = 0;
@ -340,7 +337,6 @@ _pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
int
_pthread_cond_signal(pthread_cond_t * cond)
{
struct pthread *curthread = _get_curthread();
int rval = 0;
pthread_t pthread;
@ -388,7 +384,6 @@ _pthread_cond_signal(pthread_cond_t * cond)
int
_pthread_cond_broadcast(pthread_cond_t * cond)
{
struct pthread *curthread = _get_curthread();
int rval = 0;
pthread_t pthread;
@ -442,7 +437,6 @@ _pthread_cond_broadcast(pthread_cond_t * cond)
void
_cond_wait_backout(pthread_t pthread)
{
struct pthread *curthread = _get_curthread();
pthread_cond_t cond;
cond = pthread->data.cond;

View file

@ -62,7 +62,6 @@ int
_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;
@ -117,7 +116,7 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
getcontext(&new_thread->ctx);
new_thread->ctx.uc_stack.ss_sp = new_thread->stack;
new_thread->ctx.uc_stack.ss_size = pattr->stacksize_attr;
makecontext(&new_thread->ctx, _thread_start, 1);
makecontext(&new_thread->ctx, _thread_start, 1, new_thread);
/* Copy the thread attributes: */
memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr));
@ -209,19 +208,12 @@ _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
}
void
_thread_start(void)
_thread_start(pthread_t thread)
{
struct pthread *curthread = _get_curthread_slow();
curthread->arch_id = _set_curthread(curthread);
if (_get_curthread() != curthread) {
_thread_printf("%x - %x\n", _get_curthread(), curthread);
abort();
}
thread->arch_id = _set_curthread(thread);
/* Run the current thread's start routine with argument: */
pthread_exit(curthread->start_routine(curthread->arg));
pthread_exit(thread->start_routine(thread->arg));
/* This point should never be reached. */
PANIC("Thread has resumed after exit");

View file

@ -40,16 +40,12 @@ __weak_reference(_pthread_detach, pthread_detach);
int
_pthread_detach(pthread_t pthread)
{
pthread_t curthread;
if (pthread == NULL || pthread->magic != PTHREAD_MAGIC)
return (EINVAL);
if (pthread->attr.flags & PTHREAD_DETACHED)
return (EINVAL);
curthread = _get_curthread();
pthread->attr.flags |= PTHREAD_DETACHED;
/*

View file

@ -72,8 +72,6 @@ _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
@ -93,7 +91,6 @@ _thread_exit_cleanup(void)
void
_pthread_exit(void *status)
{
struct pthread *curthread = _get_curthread();
pthread_t pthread;
/* Check if this thread is already in the process of exiting: */

View file

@ -39,13 +39,11 @@
int
_find_thread(pthread_t pthread)
{
pthread_t curthread;
pthread_t pthread1;
if (pthread == NULL || pthread->magic != PTHREAD_MAGIC)
return(EINVAL);
curthread = _get_curthread();
GIANT_LOCK(curthread);
/* Search for the specified thread: */

View file

@ -46,7 +46,6 @@
pthread_addr_t
_thread_gc(pthread_addr_t arg)
{
struct pthread *curthread = _get_curthread();
int f_debug;
int f_done = 0;
int ret;

View file

@ -135,7 +135,6 @@ _thread_dump_info(void)
static void
dump_thread(int fd, pthread_t pthread, int long_version)
{
struct pthread *curthread = _get_curthread();
char s[512];
int i;

View file

@ -331,22 +331,6 @@ _thread_init(void)
PANIC("Failed to initialise garbage collector mutex or condvar");
}
struct pthread *
_get_curthread_slow(void)
{
struct pthread *td;
thr_id_t id;
if (_thread_initial == NULL)
_thread_init();
thr_self(&id);
TAILQ_FOREACH(td, &_thread_list, tle)
if (td->thr_id == id)
return (td);
return (NULL);
}
/*
* Special start up code for NetBSD/Alpha
*/

View file

@ -40,7 +40,6 @@ __weak_reference(_pthread_join, pthread_join);
int
_pthread_join(pthread_t pthread, void **thread_return)
{
struct pthread *curthread = _get_curthread();
int ret = 0;
pthread_t thread;

View file

@ -63,7 +63,7 @@
* Prototypes
*/
static inline int mutex_self_trylock(pthread_mutex_t);
static inline int mutex_self_lock(pthread_t, pthread_mutex_t);
static inline int mutex_self_lock(pthread_mutex_t);
static inline int mutex_unlock_common(pthread_mutex_t *, int);
static void mutex_priority_adjust(pthread_mutex_t);
static void mutex_rescan_owned (pthread_t, pthread_mutex_t);
@ -300,7 +300,6 @@ init_static_private(pthread_mutex_t *mutex)
static int
mutex_trylock_common(pthread_mutex_t *mutex)
{
struct pthread *curthread = _get_curthread();
int ret = 0;
PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL),
@ -469,7 +468,6 @@ _pthread_mutex_trylock(pthread_mutex_t *mutex)
static int
mutex_lock_common(pthread_mutex_t * mutex)
{
struct pthread *curthread = _get_curthread();
int ret = 0;
PTHREAD_ASSERT((mutex != NULL) && (*mutex != NULL),
@ -517,7 +515,7 @@ mutex_lock_common(pthread_mutex_t * mutex)
(*mutex), m_qe);
} else if ((*mutex)->m_owner == curthread)
ret = mutex_self_lock(curthread, *mutex);
ret = mutex_self_lock(*mutex);
else {
/*
* Join the queue of threads waiting to lock
@ -571,7 +569,7 @@ mutex_lock_common(pthread_mutex_t * mutex)
(*mutex), m_qe);
} else if ((*mutex)->m_owner == curthread)
ret = mutex_self_lock(curthread, *mutex);
ret = mutex_self_lock(*mutex);
else {
/*
* Join the queue of threads waiting to lock
@ -637,7 +635,7 @@ mutex_lock_common(pthread_mutex_t * mutex)
TAILQ_INSERT_TAIL(&curthread->mutexq,
(*mutex), m_qe);
} else if ((*mutex)->m_owner == curthread)
ret = mutex_self_lock(curthread, *mutex);
ret = mutex_self_lock(*mutex);
else {
/*
* Join the queue of threads waiting to lock
@ -802,7 +800,7 @@ mutex_self_trylock(pthread_mutex_t mutex)
}
static inline int
mutex_self_lock(pthread_t curthread, pthread_mutex_t mutex)
mutex_self_lock(pthread_mutex_t mutex)
{
int ret = 0;
@ -844,7 +842,6 @@ mutex_self_lock(pthread_t curthread, 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) {

View file

@ -75,6 +75,11 @@
#define stdout_debug(args...) _thread_printf(STDOUT_FILENO, args)
#define stderr_debug(args...) _thread_printf(STDOUT_FILENO, args)
/*
* Currently executing thread.
*/
#define curthread _get_curthread()
/*
* State change macro without scheduling queue change:
*/
@ -665,8 +670,8 @@ SCLASS volatile int _spinblock_count
* And, should we climb the beanstalk,
* We'll meet his brother, Giant.
*/
void GIANT_LOCK(pthread_t curthread);
void GIANT_UNLOCK(pthread_t curthread);
void GIANT_LOCK(pthread_t);
void GIANT_UNLOCK(pthread_t);
/* Undefine the storage class specifier: */
#undef SCLASS
@ -680,8 +685,7 @@ char *__ttyname_r_basic(int, char *, size_t);
char *ttyname_r(int, char *, size_t);
void _cond_wait_backout(pthread_t);
int _find_thread(pthread_t);
struct pthread *_get_curthread_slow(void);
struct pthread *_get_curthread(void);
pthread_t _get_curthread(void);
void *_set_curthread(struct pthread *);
void _retire_thread(void *arch_id);
void *_thread_stack_alloc(size_t, size_t);
@ -716,7 +720,7 @@ void _thread_dump_info(void);
void _thread_init(void);
void _thread_sig_wrapper(int sig, siginfo_t *info, ucontext_t *context);
void _thread_printf(int fd, const char *, ...);
void _thread_start(void);
void _thread_start(pthread_t);
void _thread_seterrno(pthread_t, int);
pthread_addr_t _thread_gc(pthread_addr_t);
void _thread_enter_cancellation_point(void);

View file

@ -48,9 +48,6 @@ _pthread_resume_np(pthread_t thread)
/* Find the thread in the list of active threads: */
if ((ret = _find_thread(thread)) == 0) {
pthread_t curthread;
curthread = _get_curthread();
GIANT_LOCK(curthread);
if ((thread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
@ -64,7 +61,6 @@ _pthread_resume_np(pthread_t thread)
void
_pthread_resume_all_np(void)
{
struct pthread *curthread = _get_curthread();
struct pthread *thread;
GIANT_LOCK(curthread);

View file

@ -40,5 +40,5 @@ pthread_t
_pthread_self(void)
{
/* Return the running thread pointer: */
return (_get_curthread());
return (curthread);
}

View file

@ -213,12 +213,10 @@ _sem_trywait(sem_t *sem)
int
_sem_post(sem_t *sem)
{
pthread_t curthread;
int retval;
_SEM_CHECK_VALIDITY(sem);
curthread = _get_curthread();
/*
* sem_post() is required to be safe to call from within signal
* handlers. Thus, we must defer signals.

View file

@ -42,7 +42,6 @@ int
_pthread_setschedparam(pthread_t pthread, int policy,
const struct sched_param *param)
{
pthread_t curthread;
int old_prio, in_readyq = 0, ret = 0;
if ((param == NULL) || (policy < SCHED_FIFO) || (policy > SCHED_RR))
@ -52,7 +51,6 @@ _pthread_setschedparam(pthread_t pthread, int policy,
(param->sched_priority > PTHREAD_MAX_PRIORITY))
return (ENOTSUP);
curthread = _get_curthread();
/* Find the thread in the list of active threads: */
if ((ret = _find_thread(pthread)) == 0) {
GIANT_LOCK(curthread);

View file

@ -116,7 +116,6 @@ void
_thread_sig_wrapper(int sig, siginfo_t *info, ucontext_t *context)
{
struct pthread_state_data psd;
struct pthread *curthread = _get_curthread();
__siginfohandler_t *handler;
GIANT_LOCK(curthread);

View file

@ -102,7 +102,6 @@ _pthread_key_delete(pthread_key_t key)
void
_thread_cleanupspecific(void)
{
struct pthread *curthread = _get_curthread();
void *data = NULL;
int key;
int itr;
@ -165,11 +164,8 @@ pthread_key_allocate_data(void)
int
_pthread_setspecific(pthread_key_t key, const void *value)
{
struct pthread *pthread;
int ret = 0;
/* Point to the running thread: */
pthread = _get_curthread();
pthread_t pthread = curthread;
if ((pthread->specific) ||
(pthread->specific = pthread_key_allocate_data())) {
@ -198,12 +194,9 @@ _pthread_setspecific(pthread_key_t key, const void *value)
void *
_pthread_getspecific(pthread_key_t key)
{
struct pthread *pthread;
pthread_t pthread = curthread;
void *data;
/* Point to the running thread: */
pthread = _get_curthread();
/* Check if there is specific data: */
if (pthread->specific != NULL && key < PTHREAD_KEYS_MAX) {
/* Check if this key has been used before: */

View file

@ -47,8 +47,6 @@
void
_spinunlock(spinlock_t *lck)
{
struct pthread *curthread = _get_curthread();
if (umtx_unlock((struct umtx *)lck, curthread->thr_id))
abort();
}
@ -62,8 +60,6 @@ _spinunlock(spinlock_t *lck)
void
_spinlock(spinlock_t *lck)
{
struct pthread *curthread = _get_curthread();
if (umtx_lock((struct umtx *)lck, curthread->thr_id))
abort();
}
@ -81,8 +77,6 @@ _spinlock(spinlock_t *lck)
void
_spinlock_debug(spinlock_t *lck, char *fname, int lineno)
{
struct pthread *curthread = _get_curthread();
if (umtx_lock((struct umtx *)lck, curthread->thr_id))
abort();
}