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>
40 #include <pthread_np.h>
41 #include "un-namespace.h"
43 #include "thr_private.h"
45 static int create_stack(struct pthread_attr *pattr);
46 static void thread_start(struct pthread *curthread);
48 __weak_reference(_pthread_create, pthread_create);
51 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
52 void *(*start_routine) (void *), void *arg)
54 struct pthread *curthread, *new_thread;
55 struct thr_param param;
56 struct sched_param sched_param;
58 int ret = 0, locked, create_suspended;
60 cpuset_t *cpusetp = NULL;
67 * Tell libc and others now they need lock to protect their data.
69 if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
72 curthread = _get_curthread();
73 if ((new_thread = _thr_alloc(curthread)) == NULL)
76 memset(¶m, 0, sizeof(param));
78 if (attr == NULL || *attr == NULL)
79 /* Use the default thread attributes: */
80 new_thread->attr = _pthread_attr_default;
82 new_thread->attr = *(*attr);
83 cpusetp = new_thread->attr.cpuset;
84 cpusetsize = new_thread->attr.cpusetsize;
85 new_thread->attr.cpuset = NULL;
86 new_thread->attr.cpusetsize = 0;
88 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
89 /* inherit scheduling contention scope */
90 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
91 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
93 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
95 new_thread->attr.prio = curthread->attr.prio;
96 new_thread->attr.sched_policy = curthread->attr.sched_policy;
99 new_thread->tid = TID_TERMINATED;
101 old_stack_prot = _rtld_get_stack_prot();
102 if (create_stack(&new_thread->attr) != 0) {
103 /* Insufficient memory to create a stack: */
104 _thr_free(curthread, new_thread);
108 * Write a magic value to the thread structure
109 * to help identify valid ones:
111 new_thread->magic = THR_MAGIC;
112 new_thread->start_routine = start_routine;
113 new_thread->arg = arg;
114 new_thread->cancel_enable = 1;
115 new_thread->cancel_async = 0;
116 /* Initialize the mutex queue: */
117 TAILQ_INIT(&new_thread->mutexq);
118 TAILQ_INIT(&new_thread->pp_mutexq);
120 /* Initialise hooks in the thread structure: */
121 if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
122 new_thread->flags = THR_FLAGS_NEED_SUSPEND;
123 create_suspended = 1;
125 create_suspended = 0;
128 new_thread->state = PS_RUNNING;
130 if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
131 new_thread->flags |= THR_FLAGS_DETACHED;
133 /* Add the new thread. */
134 new_thread->refcount = 1;
135 _thr_link(curthread, new_thread);
138 * Handle the race between __pthread_map_stacks_exec and
141 if (old_stack_prot != _rtld_get_stack_prot())
142 _thr_stack_fix_protection(new_thread);
144 /* Return thread pointer eariler so that new thread can use it. */
145 (*thread) = new_thread;
146 if (SHOULD_REPORT_EVENT(curthread, TD_CREATE) || cpusetp != NULL) {
147 THR_THREAD_LOCK(curthread, new_thread);
151 param.start_func = (void (*)(void *)) thread_start;
152 param.arg = new_thread;
153 param.stack_base = new_thread->attr.stackaddr_attr;
154 param.stack_size = new_thread->attr.stacksize_attr;
155 param.tls_base = (char *)new_thread->tcb;
156 param.tls_size = sizeof(struct tcb);
157 param.child_tid = &new_thread->tid;
158 param.parent_tid = &new_thread->tid;
160 if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
161 param.flags |= THR_SYSTEM_SCOPE;
162 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
165 sched_param.sched_priority = new_thread->attr.prio;
166 _schedparam_to_rtp(new_thread->attr.sched_policy,
171 /* Schedule the new thread. */
172 if (create_suspended) {
174 SIGDELSET(set, SIGTRAP);
175 __sys_sigprocmask(SIG_SETMASK, &set, &oset);
176 new_thread->sigmask = oset;
177 SIGDELSET(new_thread->sigmask, SIGCANCEL);
180 ret = thr_new(¶m, sizeof(param));
185 * Translate EPROCLIM into well-known POSIX code EAGAIN.
191 if (create_suspended)
192 __sys_sigprocmask(SIG_SETMASK, &oset, NULL);
196 THR_THREAD_LOCK(curthread, new_thread);
197 new_thread->state = PS_DEAD;
198 new_thread->tid = TID_TERMINATED;
199 new_thread->flags |= THR_FLAGS_DETACHED;
200 new_thread->refcount--;
201 if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
203 _thr_umtx_wake(&new_thread->cycle, INT_MAX, 0);
205 _thr_try_gc(curthread, new_thread); /* thread lock released */
206 atomic_add_int(&_thread_active_threads, -1);
208 if (cpusetp != NULL) {
209 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
210 TID(new_thread), cpusetsize, cpusetp)) {
212 /* kill the new thread */
213 new_thread->force_exit = 1;
214 new_thread->flags |= THR_FLAGS_DETACHED;
215 _thr_try_gc(curthread, new_thread);
216 /* thread lock released */
221 _thr_report_creation(curthread, new_thread);
222 THR_THREAD_UNLOCK(curthread, new_thread);
231 create_stack(struct pthread_attr *pattr)
235 /* Check if a stack was specified in the thread attributes: */
236 if ((pattr->stackaddr_attr) != NULL) {
237 pattr->guardsize_attr = 0;
238 pattr->flags |= THR_STACK_USER;
242 ret = _thr_stack_alloc(pattr);
247 thread_start(struct pthread *curthread)
251 if (curthread->attr.suspend == THR_CREATE_SUSPENDED)
252 set = curthread->sigmask;
255 * This is used as a serialization point to allow parent
256 * to report 'new thread' event to debugger or tweak new thread's
257 * attributes before the new thread does real-world work.
260 THR_UNLOCK(curthread);
262 if (curthread->force_exit)
263 _pthread_exit(PTHREAD_CANCELED);
265 if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
267 /* Done in THR_UNLOCK() */
272 * Parent thread have stored signal mask for us,
273 * we should restore it now.
275 __sys_sigprocmask(SIG_SETMASK, &set, NULL);
278 #ifdef _PTHREAD_FORCED_UNWIND
279 curthread->unwind_stackend = (char *)curthread->attr.stackaddr_attr +
280 curthread->attr.stacksize_attr;
283 /* Run the current thread's start routine with argument: */
284 _pthread_exit(curthread->start_routine(curthread->arg));
286 /* This point should never be reached. */
287 PANIC("Thread has resumed after exit");