2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
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, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/lib/libpthread/thread/thr_create.c,v 1.58 2004/10/23 23:28:36 davidxu Exp $
29 #include "namespace.h"
37 #include <machine/reg.h>
38 #include <machine/tls.h>
40 #include <sys/signalvar.h>
41 #include "un-namespace.h"
43 #include "libc_private.h"
44 #include "thr_private.h"
46 static int create_stack(struct pthread_attr *pattr);
47 static void thread_start(void *);
50 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
51 void *(*start_routine) (void *), void *arg)
53 struct lwp_params create_params;
55 sigset_t sigmask, oldsigmask;
56 struct pthread *curthread, *new_thread;
62 * Tell libc and others now they need lock to protect their data.
64 if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
67 curthread = tls_get_curthread();
68 if ((new_thread = _thr_alloc(curthread)) == NULL)
71 if (attr == NULL || *attr == NULL) {
72 /* Use the default thread attributes: */
73 new_thread->attr = _pthread_attr_default;
75 new_thread->attr = *(*attr);
77 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
78 /* inherit scheduling contention scope */
79 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
80 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
82 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
84 * scheduling policy and scheduling parameters will be
85 * inherited in following code.
89 if (create_stack(&new_thread->attr) != 0) {
90 /* Insufficient memory to create a stack: */
91 new_thread->terminated = 1;
92 _thr_free(curthread, new_thread);
96 * Write a magic value to the thread structure
97 * to help identify valid ones:
99 new_thread->magic = THR_MAGIC;
100 new_thread->start_routine = start_routine;
101 new_thread->arg = arg;
102 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
103 PTHREAD_CANCEL_DEFERRED;
105 * Check if this thread is to inherit the scheduling
106 * attributes from its parent:
108 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
110 * Copy the scheduling attributes. Lock the scheduling
111 * lock to get consistent scheduling parameters.
114 new_thread->base_priority = curthread->base_priority;
115 new_thread->attr.prio = curthread->attr.prio;
116 new_thread->attr.sched_policy = curthread->attr.sched_policy;
117 THR_UNLOCK(curthread);
120 * Use just the thread priority, leaving the
121 * other scheduling attributes as their
124 new_thread->base_priority = new_thread->attr.prio;
126 new_thread->active_priority = new_thread->base_priority;
128 /* Initialize the mutex queue: */
129 TAILQ_INIT(&new_thread->mutexq);
131 /* Initialise hooks in the thread structure: */
132 if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
133 new_thread->flags = THR_FLAGS_NEED_SUSPEND;
135 new_thread->state = PS_RUNNING;
137 if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
138 new_thread->tlflags |= TLFLAGS_DETACHED;
140 /* Add the new thread. */
141 new_thread->refcount = 1;
142 _thr_link(curthread, new_thread);
143 /* Return thread pointer eariler so that new thread can use it. */
144 (*thread) = new_thread;
145 if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
146 THR_THREAD_LOCK(curthread, new_thread);
150 /* Schedule the new thread. */
151 stack = (char *)new_thread->attr.stackaddr_attr +
152 new_thread->attr.stacksize_attr;
153 bzero(&create_params, sizeof(create_params));
154 create_params.lwp_func = thread_start;
155 create_params.lwp_arg = new_thread;
156 create_params.lwp_stack = stack;
157 create_params.lwp_tid1 = &new_thread->tid;
159 * Thread created by thr_create() inherits currrent thread
160 * sigmask, however, before new thread setup itself correctly,
161 * it can not handle signal, so we should mask all signals here.
162 * We do this at the very last moment, so that we don't run
163 * into problems while we have all signals disabled.
166 __sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
167 new_thread->sigmask = oldsigmask;
168 ret = lwp_create(&create_params);
169 __sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
172 THR_THREAD_LOCK(curthread, new_thread);
173 new_thread->state = PS_DEAD;
174 new_thread->terminated = 1;
175 if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
177 _thr_umtx_wake(&new_thread->cycle, INT_MAX);
179 THR_THREAD_UNLOCK(curthread, new_thread);
180 THREAD_LIST_LOCK(curthread);
181 _thread_active_threads--;
182 new_thread->tlflags |= TLFLAGS_DETACHED;
183 _thr_ref_delete_unlocked(curthread, new_thread);
184 THREAD_LIST_UNLOCK(curthread);
188 _thr_report_creation(curthread, new_thread);
189 THR_THREAD_UNLOCK(curthread, new_thread);
195 create_stack(struct pthread_attr *pattr)
199 /* Check if a stack was specified in the thread attributes: */
200 if ((pattr->stackaddr_attr) != NULL) {
201 pattr->guardsize_attr = 0;
202 pattr->flags |= THR_STACK_USER;
206 ret = _thr_stack_alloc(pattr);
211 thread_start(void *arg)
213 struct pthread *curthread = (struct pthread *)arg;
215 tls_set_tcb(curthread->tcb);
217 /* Thread was created with all signals blocked, unblock them. */
218 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
221 THR_UNLOCK(curthread);
223 if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
224 _thr_suspend_check(curthread);
227 /* Run the current thread's start routine with argument: */
228 _pthread_exit(curthread->start_routine(curthread->arg));
230 /* This point should never be reached. */
231 PANIC("Thread has resumed after exit");
234 __strong_reference(_pthread_create, pthread_create);