1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  *
26  * $FreeBSD: src/lib/libpthread/thread/thr_create.c,v 1.58 2004/10/23 23:28:36 davidxu Exp $
27  */
28 
29 #include "namespace.h"
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stddef.h>
36 #include <sys/time.h>
37 #include <machine/reg.h>
38 #include <machine/tls.h>
39 #include <pthread.h>
40 #include <sys/signalvar.h>
41 #include "un-namespace.h"
42 
43 #include "libc_private.h"
44 #include "thr_private.h"
45 
46 static int  create_stack(struct pthread_attr *pattr);
47 static void thread_start(void *);
48 
49 int
50 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
51     void *(*start_routine) (void *), void *arg)
52 {
53 	struct lwp_params create_params;
54 	void *stack;
55 	sigset_t sigmask, oldsigmask;
56 	struct pthread *curthread, *new_thread;
57 	int ret = 0, locked;
58 
59 	_thr_check_init();
60 
61 	/*
62 	 * Tell libc and others now they need lock to protect their data.
63 	 */
64 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
65 		return (EAGAIN);
66 
67 	curthread = tls_get_curthread();
68 	if ((new_thread = _thr_alloc(curthread)) == NULL)
69 		return (EAGAIN);
70 
71 	if (attr == NULL || *attr == NULL) {
72 		/* Use the default thread attributes: */
73 		new_thread->attr = _pthread_attr_default;
74 	} else {
75 		new_thread->attr = *(*attr);
76 	}
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;
81 		else
82 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
83 		/*
84 		 * scheduling policy and scheduling parameters will be
85 		 * inherited in following code.
86 		 */
87 	}
88 
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);
93 		return (EAGAIN);
94 	}
95 	/*
96 	 * Write a magic value to the thread structure
97 	 * to help identify valid ones:
98 	 */
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;
104 	/*
105 	 * Check if this thread is to inherit the scheduling
106 	 * attributes from its parent:
107 	 */
108 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
109 		/*
110 		 * Copy the scheduling attributes. Lock the scheduling
111 		 * lock to get consistent scheduling parameters.
112 		 */
113 		THR_LOCK(curthread);
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);
118 	} else {
119 		/*
120 		 * Use just the thread priority, leaving the
121 		 * other scheduling attributes as their
122 		 * default values:
123 		 */
124 		new_thread->base_priority = new_thread->attr.prio;
125 	}
126 	new_thread->active_priority = new_thread->base_priority;
127 
128 	/* Initialize the mutex queue: */
129 	TAILQ_INIT(&new_thread->mutexq);
130 
131 	/* Initialise hooks in the thread structure: */
132 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
133 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
134 
135 	new_thread->state = PS_RUNNING;
136 
137 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
138 		new_thread->tlflags |= TLFLAGS_DETACHED;
139 
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);
147 		locked = 1;
148 	} else
149 		locked = 0;
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;
158 	/*
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.
164 	 */
165 	SIGFILLSET(sigmask);
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);
170 	if (ret != 0) {
171 		if (!locked)
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) {
176 			new_thread->cycle++;
177 			_thr_umtx_wake(&new_thread->cycle, INT_MAX);
178 		}
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);
185 		(*thread) = NULL;
186 		ret = EAGAIN;
187 	} else if (locked) {
188 		_thr_report_creation(curthread, new_thread);
189 		THR_THREAD_UNLOCK(curthread, new_thread);
190 	}
191 	return (ret);
192 }
193 
194 static int
195 create_stack(struct pthread_attr *pattr)
196 {
197 	int ret;
198 
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;
203 		ret = 0;
204 	}
205 	else
206 		ret = _thr_stack_alloc(pattr);
207 	return (ret);
208 }
209 
210 static void
211 thread_start(void *arg)
212 {
213 	struct pthread *curthread = (struct pthread *)arg;
214 
215 	tls_set_tcb(curthread->tcb);
216 
217 	/* Thread was created with all signals blocked, unblock them. */
218 	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
219 
220 	THR_LOCK(curthread);
221 	THR_UNLOCK(curthread);
222 
223 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
224 		_thr_suspend_check(curthread);
225 	_nmalloc_thr_init();
226 
227 	/* Run the current thread's start routine with argument: */
228 	_pthread_exit(curthread->start_routine(curthread->arg));
229 
230 	/* This point should never be reached. */
231 	PANIC("Thread has resumed after exit");
232 }
233 
234 __strong_reference(_pthread_create, pthread_create);
235