#include "namespace.h"
#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 <machine/tls.h>
#include <pthread.h>
#include <sys/signalvar.h>
#include <sys/lwp.h>
#include "un-namespace.h"
#include "libc_private.h"
#include "thr_private.h"
static int create_stack(pthread_attr_t pattr);
static void thread_start(void *);
int
_pthread_create(pthread_t * __restrict thread,
const pthread_attr_t * __restrict attr, void *(*start_routine) (void *),
void * __restrict arg)
{
struct lwp_params create_params;
void *stack;
sigset_t sigmask, oldsigmask;
pthread_t curthread, new_thread;
const cpu_set_t *cpumask = NULL;
int ret = 0, locked;
_thr_check_init();
if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
return (EAGAIN);
curthread = tls_get_curthread();
if ((new_thread = _thr_alloc(curthread)) == NULL)
return (EAGAIN);
if (attr == NULL || *attr == NULL) {
new_thread->attr = _pthread_attr_default;
} else {
new_thread->attr = *(*attr);
}
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
else
new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
}
if (new_thread->attr.flags & THR_CPUMASK)
cpumask = &new_thread->attr.cpumask;
if (create_stack(&new_thread->attr) != 0) {
new_thread->terminated = 1;
_thr_free(curthread, new_thread);
return (EAGAIN);
}
new_thread->magic = THR_MAGIC;
new_thread->start_routine = start_routine;
new_thread->arg = arg;
new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
PTHREAD_CANCEL_DEFERRED;
if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
THR_LOCK(curthread);
new_thread->base_priority = curthread->base_priority;
new_thread->attr.prio = curthread->attr.prio;
new_thread->attr.sched_policy = curthread->attr.sched_policy;
THR_UNLOCK(curthread);
} else {
new_thread->base_priority = new_thread->attr.prio;
}
new_thread->active_priority = new_thread->base_priority;
TAILQ_INIT(&new_thread->mutexq);
if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
new_thread->flags = THR_FLAGS_NEED_SUSPEND;
new_thread->state = PS_RUNNING;
if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
new_thread->tlflags |= TLFLAGS_DETACHED;
new_thread->refcount = 1;
_thr_link(curthread, new_thread);
(*thread) = new_thread;
if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
THR_THREAD_LOCK(curthread, new_thread);
locked = 1;
} else
locked = 0;
stack = (char *)new_thread->attr.stackaddr_attr +
new_thread->attr.stacksize_attr;
bzero(&create_params, sizeof(create_params));
create_params.lwp_func = thread_start;
create_params.lwp_arg = new_thread;
create_params.lwp_stack = stack;
create_params.lwp_tid1 = &new_thread->tid;
SIGFILLSET(sigmask);
__sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
new_thread->sigmask = oldsigmask;
ret = lwp_create2(&create_params, cpumask);
__sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
if (ret != 0) {
if (!locked)
THR_THREAD_LOCK(curthread, new_thread);
new_thread->state = PS_DEAD;
new_thread->terminated = 1;
if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
new_thread->cycle++;
_thr_umtx_wake(&new_thread->cycle, 0);
}
THR_THREAD_UNLOCK(curthread, new_thread);
THREAD_LIST_LOCK(curthread);
_thread_active_threads--;
new_thread->tlflags |= TLFLAGS_DETACHED;
_thr_ref_delete_unlocked(curthread, new_thread);
THREAD_LIST_UNLOCK(curthread);
(*thread) = NULL;
ret = EAGAIN;
} else if (locked) {
_thr_report_creation(curthread, new_thread);
THR_THREAD_UNLOCK(curthread, new_thread);
}
return (ret);
}
static int
create_stack(pthread_attr_t pattr)
{
int ret;
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(void *arg)
{
pthread_t curthread = (pthread_t)arg;
tls_set_tcb(curthread->tcb);
__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
THR_LOCK(curthread);
THR_UNLOCK(curthread);
if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
_thr_suspend_check(curthread);
_libc_thr_init();
_pthread_exit(curthread->start_routine(curthread->arg));
PANIC("Thread has resumed after exit");
}
__strong_reference(_pthread_create, pthread_create);