freebsd-nq/lib/libpthread/thread/thr_create.c
Daniel Eischen a0240e2cb0 Revamp libpthread so that it has a chance of working in an SMP
environment.  This includes support for multiple KSEs and KSEGs.

The ability to create more than 1 KSE via pthread_setconcurrency()
is in the works as well as support for PTHREAD_SCOPE_SYSTEM threads.
Those should come shortly.

There are still some known issues which davidxu and I are working
on, but it'll make it easier for us by committing what we have.

This library now passes all of the ACE tests that libc_r passes
with the exception of one.  It also seems to work OK with KDE
including konqueror, kwrite, etc.  I haven't been able to get
mozilla to run due to lack of java plugin, so I'd be interested
to see how it works with that.

Reviewed by:	davidxu
2003-04-18 05:04:16 +00:00

312 lines
9.7 KiB
C

/*
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by John Birrell.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/time.h>
#include <machine/reg.h>
#include <pthread.h>
#include "thr_private.h"
#include "libc_private.h"
static u_int64_t next_uniqueid = 1;
#define OFF(f) offsetof(struct pthread, f)
int _thread_next_offset = OFF(tle.tqe_next);
int _thread_uniqueid_offset = OFF(uniqueid);
int _thread_state_offset = OFF(state);
int _thread_name_offset = OFF(name);
int _thread_ctx_offset = OFF(tmbx.tm_context);
#undef OFF
int _thread_PS_RUNNING_value = PS_RUNNING;
int _thread_PS_DEAD_value = PS_DEAD;
static int create_stack(struct pthread_attr *pattr);
static void thread_start(struct pthread *curthread,
void *(*start_routine) (void *), void *arg);
__weak_reference(_pthread_create, pthread_create);
/*
* Some notes on new thread creation and first time initializion
* to enable multi-threading.
*
* There are basically two things that need to be done.
*
* 1) The internal library variables must be initialized.
* 2) Upcalls need to be enabled to allow multiple threads
* to be run.
*
* The first may be done as a result of other pthread functions
* being called. When _thr_initial is null, _libpthread_init is
* called to initialize the internal variables; this also creates
* or sets the initial thread. It'd be nice to automatically
* have _libpthread_init called on program execution so we don't
* have to have checks throughout the library.
*
* The second part is only triggered by the creation of the first
* thread (other than the initial/main thread). If the thread
* being created is a scope system thread, then a new KSE/KSEG
* pair needs to be allocated. Also, if upcalls haven't been
* enabled on the initial thread's KSE, they must be now that
* there is more than one thread; this could be delayed until
* the initial KSEG has more than one thread.
*/
int
_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
void *(*start_routine) (void *), void *arg)
{
struct kse *curkse;
struct pthread *curthread, *new_thread;
struct kse *kse = NULL;
struct kse_group *kseg = NULL;
kse_critical_t crit;
int i;
int ret = 0;
if (_thr_initial == NULL)
_libpthread_init(NULL);
crit = _kse_critical_enter();
curthread = _get_curthread();
curkse = curthread->kse;
/* Allocate memory for the thread structure: */
if ((new_thread = _thr_alloc(curkse)) == NULL) {
/* Insufficient memory to create a thread: */
ret = EAGAIN;
} else {
/* Initialize the thread structure: */
memset(new_thread, 0, sizeof(struct pthread));
/* Check if default thread attributes are required: */
if (attr == NULL || *attr == NULL)
/* Use the default thread attributes: */
new_thread->attr = _pthread_attr_default;
else
new_thread->attr = *(*attr);
if (create_stack(&new_thread->attr) != 0) {
/* Insufficient memory to create a stack: */
ret = EAGAIN;
_thr_free(curkse, new_thread);
}
else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) &&
(((kse = _kse_alloc(curkse)) == NULL)
|| ((kseg = _kseg_alloc(curkse)) == NULL))) {
/* Insufficient memory to create a new KSE/KSEG: */
ret = EAGAIN;
if (kse != NULL)
_kse_free(curkse, kse);
if ((new_thread->attr.flags & THR_STACK_USER) == 0) {
KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
_thr_stack_free(&new_thread->attr);
KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
}
_thr_free(curkse, new_thread);
}
else {
if (kseg != NULL) {
/* Add the KSE to the KSEG's list of KSEs. */
TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_qe);
kse->k_kseg = kseg;
kse->k_schedq = &kseg->kg_schedq;
}
/*
* Write a magic value to the thread structure
* to help identify valid ones:
*/
new_thread->magic = THR_MAGIC;
new_thread->slice_usec = -1;
new_thread->start_routine = start_routine;
new_thread->arg = arg;
new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
PTHREAD_CANCEL_DEFERRED;
/* Initialize the thread for signals: */
new_thread->sigmask = curthread->sigmask;
/* No thread is wanting to join to this one: */
new_thread->joiner = NULL;
/* Initialize the signal frame: */
new_thread->curframe = NULL;
/* Initialize the machine context: */
THR_GETCONTEXT(&new_thread->tmbx.tm_context);
new_thread->tmbx.tm_udata = new_thread;
new_thread->tmbx.tm_context.uc_sigmask =
new_thread->sigmask;
new_thread->tmbx.tm_context.uc_stack.ss_size =
new_thread->attr.stacksize_attr;
new_thread->tmbx.tm_context.uc_stack.ss_sp =
new_thread->attr.stackaddr_attr;
makecontext(&new_thread->tmbx.tm_context,
(void (*)(void))thread_start, 4, new_thread,
start_routine, arg);
/*
* Check if this thread is to inherit the scheduling
* attributes from its parent:
*/
if ((new_thread->attr.flags & PTHREAD_INHERIT_SCHED) != 0) {
/* Copy the scheduling attributes: */
new_thread->base_priority =
curthread->base_priority &
~THR_SIGNAL_PRIORITY;
new_thread->attr.prio =
curthread->base_priority &
~THR_SIGNAL_PRIORITY;
new_thread->attr.sched_policy =
curthread->attr.sched_policy;
} else {
/*
* Use just the thread priority, leaving the
* other scheduling attributes as their
* default values:
*/
new_thread->base_priority =
new_thread->attr.prio;
}
new_thread->active_priority = new_thread->base_priority;
new_thread->inherited_priority = 0;
/* Initialize the mutex queue: */
TAILQ_INIT(&new_thread->mutexq);
/* Initialize thread locking. */
if (_lock_init(&new_thread->lock, LCK_ADAPTIVE,
_thr_lock_wait, _thr_lock_wakeup) != 0)
PANIC("Cannot initialize thread lock");
for (i = 0; i < MAX_THR_LOCKLEVEL; i++) {
_lockuser_init(&new_thread->lockusers[i],
(void *)new_thread);
_LCK_SET_PRIVATE2(&new_thread->lockusers[i],
(void *)new_thread);
}
/* Initialise hooks in the thread structure: */
new_thread->specific = NULL;
new_thread->cleanup = NULL;
new_thread->flags = 0;
new_thread->continuation = NULL;
if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
new_thread->state = PS_SUSPENDED;
else
new_thread->state = PS_RUNNING;
/*
* System scope threads have their own kse and
* kseg. Process scope threads are all hung
* off the main process kseg.
*/
if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) {
new_thread->kseg = _kse_initial->k_kseg;
new_thread->kse = _kse_initial;
}
else {
kse->k_curthread = NULL;
kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
new_thread->kse = kse;
new_thread->kseg = kse->k_kseg;
kse->k_mbx.km_udata = kse;
kse->k_mbx.km_curthread = NULL;
}
KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock);
/*
* Initialise the unique id which GDB uses to
* track threads.
*/
new_thread->uniqueid = next_uniqueid++;
/* Add the thread to the linked list of all threads: */
THR_LIST_ADD(new_thread);
KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock);
/*
* Schedule the new thread starting a new KSEG/KSE
* pair if necessary.
*/
_thr_schedule_add(curthread, new_thread);
/* Return a pointer to the thread structure: */
(*thread) = new_thread;
}
}
_kse_critical_leave(crit);
if ((ret == 0) && (_kse_isthreaded() == 0))
_kse_setthreaded(1);
/* Return the status: */
return (ret);
}
static int
create_stack(struct pthread_attr *pattr)
{
int ret;
/* Check if a stack was specified in the thread attributes: */
if ((pattr->stackaddr_attr) != NULL) {
pattr->guardsize_attr = 0;
pattr->flags = THR_STACK_USER;
ret = 0;
}
else
ret = _thr_stack_alloc(pattr);
return (ret);
}
static void
thread_start(struct pthread *curthread, void *(*start_routine) (void *),
void *arg)
{
/* Run the current thread's start routine with argument: */
pthread_exit(start_routine(arg));
/* This point should never be reached. */
PANIC("Thread has resumed after exit");
}