2003-04-01 03:46:29 +00:00
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
|
2005-12-17 09:42:45 +00:00
|
|
|
* Copyright (c) 2005, David Xu <davidxu@freebsd.org>
|
2003-04-01 03:46:29 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2005-12-17 09:42:45 +00:00
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
2003-04-01 03:46:29 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2005-12-17 09:42:45 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
2003-04-01 03:46:29 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "namespace.h"
|
2005-09-01 15:21:23 +00:00
|
|
|
#include <sys/types.h>
|
2006-09-21 04:21:30 +00:00
|
|
|
#include <sys/rtprio.h>
|
2005-09-01 15:21:23 +00:00
|
|
|
#include <sys/signalvar.h>
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <pthread.h>
|
2008-03-04 03:03:24 +00:00
|
|
|
#include <pthread_np.h>
|
2006-04-04 02:57:49 +00:00
|
|
|
#include "un-namespace.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
#include "thr_private.h"
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int create_stack(struct pthread_attr *pattr);
|
|
|
|
static void thread_start(struct pthread *curthread);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
__weak_reference(_pthread_create, pthread_create);
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
|
|
|
|
void *(*start_routine) (void *), void *arg)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread, *new_thread;
|
2005-04-23 02:48:59 +00:00
|
|
|
struct thr_param param;
|
2006-09-21 04:21:30 +00:00
|
|
|
struct sched_param sched_param;
|
|
|
|
struct rtprio rtp;
|
2006-01-05 13:51:22 +00:00
|
|
|
int ret = 0, locked, create_suspended;
|
|
|
|
sigset_t set, oset;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
_thr_check_init();
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* Tell libc and others now they need lock to protect their data.
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
|
|
|
|
return (EAGAIN);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
curthread = _get_curthread();
|
|
|
|
if ((new_thread = _thr_alloc(curthread)) == NULL)
|
2003-04-01 03:46:29 +00:00
|
|
|
return (EAGAIN);
|
|
|
|
|
2005-04-23 02:48:59 +00:00
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (attr == NULL || *attr == NULL)
|
|
|
|
/* Use the default thread attributes: */
|
|
|
|
new_thread->attr = _pthread_attr_default;
|
2008-03-04 03:03:24 +00:00
|
|
|
else {
|
2005-04-02 01:20:00 +00:00
|
|
|
new_thread->attr = *(*attr);
|
2008-03-04 03:03:24 +00:00
|
|
|
new_thread->attr.cpuset = NULL;
|
|
|
|
new_thread->attr.cpusetsize = 0;
|
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
|
|
|
|
/* inherit scheduling contention scope */
|
|
|
|
if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
|
|
|
|
new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
|
|
|
|
else
|
|
|
|
new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
|
2006-07-13 22:45:19 +00:00
|
|
|
|
|
|
|
new_thread->attr.prio = curthread->attr.prio;
|
|
|
|
new_thread->attr.sched_policy = curthread->attr.sched_policy;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-12 03:00:28 +00:00
|
|
|
new_thread->tid = TID_TERMINATED;
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (create_stack(&new_thread->attr) != 0) {
|
|
|
|
/* Insufficient memory to create a stack: */
|
|
|
|
_thr_free(curthread, new_thread);
|
|
|
|
return (EAGAIN);
|
2003-05-25 22:40:57 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
/*
|
|
|
|
* Write a magic value to the thread structure
|
|
|
|
* to help identify valid ones:
|
|
|
|
*/
|
|
|
|
new_thread->magic = THR_MAGIC;
|
|
|
|
new_thread->start_routine = start_routine;
|
|
|
|
new_thread->arg = arg;
|
2006-11-24 09:57:38 +00:00
|
|
|
new_thread->cancel_enable = 1;
|
|
|
|
new_thread->cancel_async = 0;
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Initialize the mutex queue: */
|
|
|
|
TAILQ_INIT(&new_thread->mutexq);
|
2006-08-28 04:52:50 +00:00
|
|
|
TAILQ_INIT(&new_thread->pp_mutexq);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Initialise hooks in the thread structure: */
|
2006-01-05 13:51:22 +00:00
|
|
|
if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
|
2005-10-10 12:15:07 +00:00
|
|
|
new_thread->flags = THR_FLAGS_NEED_SUSPEND;
|
2006-01-05 13:51:22 +00:00
|
|
|
create_suspended = 1;
|
|
|
|
} else {
|
|
|
|
create_suspended = 0;
|
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
new_thread->state = PS_RUNNING;
|
2005-04-23 02:48:59 +00:00
|
|
|
|
2006-01-05 13:51:22 +00:00
|
|
|
if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
|
|
|
|
new_thread->tlflags |= TLFLAGS_DETACHED;
|
|
|
|
|
2008-03-04 04:28:59 +00:00
|
|
|
if (curthread->in_sigcancel_handler)
|
|
|
|
new_thread->unblock_sigcancel = 1;
|
|
|
|
else
|
|
|
|
new_thread->unblock_sigcancel = 0;
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Add the new thread. */
|
2006-01-05 13:51:22 +00:00
|
|
|
new_thread->refcount = 1;
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_link(curthread, new_thread);
|
|
|
|
/* Return thread pointer eariler so that new thread can use it. */
|
|
|
|
(*thread) = new_thread;
|
2005-04-12 03:00:28 +00:00
|
|
|
if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
|
|
|
|
THR_THREAD_LOCK(curthread, new_thread);
|
|
|
|
locked = 1;
|
|
|
|
} else
|
|
|
|
locked = 0;
|
2005-04-23 02:48:59 +00:00
|
|
|
param.start_func = (void (*)(void *)) thread_start;
|
|
|
|
param.arg = new_thread;
|
|
|
|
param.stack_base = new_thread->attr.stackaddr_attr;
|
|
|
|
param.stack_size = new_thread->attr.stacksize_attr;
|
|
|
|
param.tls_base = (char *)new_thread->tcb;
|
|
|
|
param.tls_size = sizeof(struct tcb);
|
|
|
|
param.child_tid = &new_thread->tid;
|
|
|
|
param.parent_tid = &new_thread->tid;
|
|
|
|
param.flags = 0;
|
|
|
|
if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
|
|
|
|
param.flags |= THR_SYSTEM_SCOPE;
|
2006-07-12 06:13:18 +00:00
|
|
|
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
|
2006-09-21 04:21:30 +00:00
|
|
|
param.rtp = NULL;
|
2006-07-12 06:13:18 +00:00
|
|
|
else {
|
2006-09-21 04:21:30 +00:00
|
|
|
sched_param.sched_priority = new_thread->attr.prio;
|
|
|
|
_schedparam_to_rtp(new_thread->attr.sched_policy,
|
|
|
|
&sched_param, &rtp);
|
|
|
|
param.rtp = &rtp;
|
2006-07-12 06:13:18 +00:00
|
|
|
}
|
2006-01-05 13:51:22 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Schedule the new thread. */
|
2006-01-05 13:51:22 +00:00
|
|
|
if (create_suspended) {
|
|
|
|
SIGFILLSET(set);
|
|
|
|
SIGDELSET(set, SIGTRAP);
|
|
|
|
__sys_sigprocmask(SIG_SETMASK, &set, &oset);
|
|
|
|
new_thread->sigmask = oset;
|
|
|
|
}
|
|
|
|
|
2005-04-23 02:48:59 +00:00
|
|
|
ret = thr_new(¶m, sizeof(param));
|
2006-01-05 13:51:22 +00:00
|
|
|
|
2006-07-12 06:13:18 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = errno;
|
|
|
|
/*
|
|
|
|
* Translate EPROCLIM into well-known POSIX code EAGAIN.
|
|
|
|
*/
|
|
|
|
if (ret == EPROCLIM)
|
|
|
|
ret = EAGAIN;
|
|
|
|
}
|
|
|
|
|
2008-03-04 04:28:59 +00:00
|
|
|
if (create_suspended) {
|
2006-01-05 13:51:22 +00:00
|
|
|
__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
|
2008-03-04 04:28:59 +00:00
|
|
|
SIGDELSET(oset, SIGCANCEL);
|
|
|
|
}
|
2006-01-05 13:51:22 +00:00
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
if (ret != 0) {
|
2006-01-05 13:51:22 +00:00
|
|
|
if (!locked)
|
|
|
|
THR_THREAD_LOCK(curthread, new_thread);
|
|
|
|
new_thread->state = PS_DEAD;
|
|
|
|
new_thread->tid = TID_TERMINATED;
|
|
|
|
if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
|
|
|
|
new_thread->cycle++;
|
|
|
|
_thr_umtx_wake(&new_thread->cycle, INT_MAX);
|
|
|
|
}
|
|
|
|
THR_THREAD_UNLOCK(curthread, new_thread);
|
|
|
|
THREAD_LIST_LOCK(curthread);
|
2006-01-08 10:13:18 +00:00
|
|
|
_thread_active_threads--;
|
2006-01-05 13:51:22 +00:00
|
|
|
new_thread->tlflags |= TLFLAGS_DETACHED;
|
|
|
|
_thr_ref_delete_unlocked(curthread, new_thread);
|
|
|
|
THREAD_LIST_UNLOCK(curthread);
|
2005-04-02 01:20:00 +00:00
|
|
|
(*thread) = 0;
|
2005-04-12 03:00:28 +00:00
|
|
|
} else if (locked) {
|
|
|
|
_thr_report_creation(curthread, new_thread);
|
|
|
|
THR_THREAD_UNLOCK(curthread, new_thread);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
create_stack(struct pthread_attr *pattr)
|
|
|
|
{
|
|
|
|
int ret;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* 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);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static void
|
|
|
|
thread_start(struct pthread *curthread)
|
|
|
|
{
|
2008-03-04 04:28:59 +00:00
|
|
|
if (curthread->unblock_sigcancel) {
|
|
|
|
sigset_t set;
|
|
|
|
|
|
|
|
SIGEMPTYSET(set);
|
|
|
|
SIGADDSET(set, SIGCANCEL);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &set, NULL);
|
|
|
|
}
|
|
|
|
|
2006-01-05 13:51:22 +00:00
|
|
|
if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
|
|
|
|
sigset_t set = curthread->sigmask;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-01-05 13:51:22 +00:00
|
|
|
_thr_ast(curthread);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parent thread have stored signal mask for us,
|
|
|
|
* we should restore it now.
|
|
|
|
*/
|
|
|
|
sigprocmask(SIG_SETMASK, &set, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is used as a serialization point to allow parent
|
|
|
|
* to report 'new thread' event to debugger before the thread
|
|
|
|
* does real work.
|
|
|
|
*/
|
2005-04-12 03:00:28 +00:00
|
|
|
THR_LOCK(curthread);
|
|
|
|
THR_UNLOCK(curthread);
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Run the current thread's start routine with argument: */
|
2006-04-04 02:57:49 +00:00
|
|
|
_pthread_exit(curthread->start_routine(curthread->arg));
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
/* This point should never be reached. */
|
|
|
|
PANIC("Thread has resumed after exit");
|
|
|
|
}
|