2 * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/rtprio.h>
33 #include <sys/signalvar.h>
39 #include "un-namespace.h"
41 #include "thr_private.h"
43 static int create_stack(struct pthread_attr *pattr);
44 static void thread_start(struct pthread *curthread);
46 __weak_reference(_pthread_create, pthread_create);
49 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
50 void *(*start_routine) (void *), void *arg)
52 struct pthread *curthread, *new_thread;
53 struct thr_param param;
54 struct sched_param sched_param;
56 int ret = 0, locked, create_suspended;
62 * Tell libc and others now they need lock to protect their data.
64 if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
67 curthread = _get_curthread();
68 if ((new_thread = _thr_alloc(curthread)) == NULL)
71 memset(¶m, 0, sizeof(param));
73 if (attr == NULL || *attr == NULL)
74 /* Use the default thread attributes: */
75 new_thread->attr = _pthread_attr_default;
77 new_thread->attr = *(*attr);
78 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
79 /* inherit scheduling contention scope */
80 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
81 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
83 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
85 new_thread->attr.prio = curthread->attr.prio;
86 new_thread->attr.sched_policy = curthread->attr.sched_policy;
89 new_thread->tid = TID_TERMINATED;
91 if (create_stack(&new_thread->attr) != 0) {
92 /* Insufficient memory to create a stack: */
93 _thr_free(curthread, new_thread);
97 * Write a magic value to the thread structure
98 * to help identify valid ones:
100 new_thread->magic = THR_MAGIC;
101 new_thread->start_routine = start_routine;
102 new_thread->arg = arg;
103 new_thread->cancel_enable = 1;
104 new_thread->cancel_async = 0;
105 /* Initialize the mutex queue: */
106 TAILQ_INIT(&new_thread->mutexq);
107 TAILQ_INIT(&new_thread->pp_mutexq);
109 /* Initialise hooks in the thread structure: */
110 if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
111 new_thread->flags = THR_FLAGS_NEED_SUSPEND;
112 create_suspended = 1;
114 create_suspended = 0;
117 new_thread->state = PS_RUNNING;
119 if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
120 new_thread->tlflags |= TLFLAGS_DETACHED;
122 /* Add the new thread. */
123 new_thread->refcount = 1;
124 _thr_link(curthread, new_thread);
125 /* Return thread pointer eariler so that new thread can use it. */
126 (*thread) = new_thread;
127 if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
128 THR_THREAD_LOCK(curthread, new_thread);
132 param.start_func = (void (*)(void *)) thread_start;
133 param.arg = new_thread;
134 param.stack_base = new_thread->attr.stackaddr_attr;
135 param.stack_size = new_thread->attr.stacksize_attr;
136 param.tls_base = (char *)new_thread->tcb;
137 param.tls_size = sizeof(struct tcb);
138 param.child_tid = &new_thread->tid;
139 param.parent_tid = &new_thread->tid;
141 if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
142 param.flags |= THR_SYSTEM_SCOPE;
143 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
146 sched_param.sched_priority = new_thread->attr.prio;
147 _schedparam_to_rtp(new_thread->attr.sched_policy,
152 /* Schedule the new thread. */
153 if (create_suspended) {
155 SIGDELSET(set, SIGTRAP);
156 __sys_sigprocmask(SIG_SETMASK, &set, &oset);
157 new_thread->sigmask = oset;
160 ret = thr_new(¶m, sizeof(param));
165 * Translate EPROCLIM into well-known POSIX code EAGAIN.
171 if (create_suspended)
172 __sys_sigprocmask(SIG_SETMASK, &oset, NULL);
176 THR_THREAD_LOCK(curthread, new_thread);
177 new_thread->state = PS_DEAD;
178 new_thread->tid = TID_TERMINATED;
179 if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
181 _thr_umtx_wake(&new_thread->cycle, INT_MAX);
183 THR_THREAD_UNLOCK(curthread, new_thread);
184 THREAD_LIST_LOCK(curthread);
185 _thread_active_threads--;
186 new_thread->tlflags |= TLFLAGS_DETACHED;
187 _thr_ref_delete_unlocked(curthread, new_thread);
188 THREAD_LIST_UNLOCK(curthread);
191 _thr_report_creation(curthread, new_thread);
192 THR_THREAD_UNLOCK(curthread, new_thread);
198 create_stack(struct pthread_attr *pattr)
202 /* Check if a stack was specified in the thread attributes: */
203 if ((pattr->stackaddr_attr) != NULL) {
204 pattr->guardsize_attr = 0;
205 pattr->flags |= THR_STACK_USER;
209 ret = _thr_stack_alloc(pattr);
214 thread_start(struct pthread *curthread)
216 if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
217 sigset_t set = curthread->sigmask;
222 * Parent thread have stored signal mask for us,
223 * we should restore it now.
225 sigprocmask(SIG_SETMASK, &set, NULL);
229 * This is used as a serialization point to allow parent
230 * to report 'new thread' event to debugger before the thread
234 THR_UNLOCK(curthread);
236 /* Run the current thread's start routine with argument: */
237 _pthread_exit(curthread->start_routine(curthread->arg));
239 /* This point should never be reached. */
240 PANIC("Thread has resumed after exit");