1 /*
2  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
4  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by John Birrell.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD: src/lib/libpthread/thread/thr_create.c,v 1.58 2004/10/23 23:28:36 davidxu Exp $
35  */
36 
37 #include "namespace.h"
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <stddef.h>
44 #include <sys/time.h>
45 #include <machine/reg.h>
46 #include <machine/tls.h>
47 
48 #include <pthread.h>
49 #include <sys/signalvar.h>
50 #include "un-namespace.h"
51 
52 #include "thr_private.h"
53 #include "libc_private.h"
54 
55 static int  create_stack(struct pthread_attr *pattr);
56 static void thread_start(void *);
57 
58 int
59 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
60        void *(*start_routine) (void *), void *arg)
61 {
62 	struct lwp_params create_params;
63 	void *stack;
64 	sigset_t sigmask, oldsigmask;
65 	struct pthread *curthread, *new_thread;
66 	int ret = 0, locked;
67 
68 	_thr_check_init();
69 
70 	/*
71 	 * Tell libc and others now they need lock to protect their data.
72 	 */
73 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
74 		return (EAGAIN);
75 
76 	curthread = tls_get_curthread();
77 	if ((new_thread = _thr_alloc(curthread)) == NULL)
78 		return (EAGAIN);
79 
80 	if (attr == NULL || *attr == NULL)
81 		/* Use the default thread attributes: */
82 		new_thread->attr = _pthread_attr_default;
83 	else
84 		new_thread->attr = *(*attr);
85 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
86 		/* inherit scheduling contention scope */
87 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
88 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
89 		else
90 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
91 		/*
92 		 * scheduling policy and scheduling parameters will be
93 		 * inherited in following code.
94 		 */
95 	}
96 
97 	if (_thread_scope_system > 0)
98 		new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
99 	else if (_thread_scope_system < 0)
100 		new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
101 
102 	if (create_stack(&new_thread->attr) != 0) {
103 		/* Insufficient memory to create a stack: */
104 		new_thread->terminated = 1;
105 		_thr_free(curthread, new_thread);
106 		return (EAGAIN);
107 	}
108 	/*
109 	 * Write a magic value to the thread structure
110 	 * to help identify valid ones:
111 	 */
112 	new_thread->magic = THR_MAGIC;
113 	new_thread->start_routine = start_routine;
114 	new_thread->arg = arg;
115 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
116 	    PTHREAD_CANCEL_DEFERRED;
117 	/*
118 	 * Check if this thread is to inherit the scheduling
119 	 * attributes from its parent:
120 	 */
121 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
122 		/*
123 		 * Copy the scheduling attributes. Lock the scheduling
124 		 * lock to get consistent scheduling parameters.
125 		 */
126 		THR_LOCK(curthread);
127 		new_thread->base_priority = curthread->base_priority;
128 		new_thread->attr.prio = curthread->attr.prio;
129 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
130 		THR_UNLOCK(curthread);
131 	} else {
132 		/*
133 		 * Use just the thread priority, leaving the
134 		 * other scheduling attributes as their
135 		 * default values:
136 		 */
137 		new_thread->base_priority = new_thread->attr.prio;
138 	}
139 	new_thread->active_priority = new_thread->base_priority;
140 
141 	/* Initialize the mutex queue: */
142 	TAILQ_INIT(&new_thread->mutexq);
143 
144 	/* Initialise hooks in the thread structure: */
145 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
146 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
147 
148 	new_thread->state = PS_RUNNING;
149 
150 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
151 		new_thread->tlflags |= TLFLAGS_DETACHED;
152 
153 	/* Add the new thread. */
154 	new_thread->refcount = 1;
155 	_thr_link(curthread, new_thread);
156 	/* Return thread pointer eariler so that new thread can use it. */
157 	(*thread) = new_thread;
158 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
159 		THR_THREAD_LOCK(curthread, new_thread);
160 		locked = 1;
161 	} else
162 		locked = 0;
163 	/* Schedule the new thread. */
164 	stack = (char *)new_thread->attr.stackaddr_attr +
165 		        new_thread->attr.stacksize_attr;
166 	bzero(&create_params, sizeof(create_params));
167 	create_params.lwp_func = thread_start;
168 	create_params.lwp_arg = new_thread;
169 	create_params.lwp_stack = stack;
170 	create_params.lwp_tid1 = &new_thread->tid;
171 	/*
172 	 * Thread created by thr_create() inherits currrent thread
173 	 * sigmask, however, before new thread setup itself correctly,
174 	 * it can not handle signal, so we should mask all signals here.
175 	 * We do this at the very last moment, so that we don't run
176 	 * into problems while we have all signals disabled.
177 	 */
178 	SIGFILLSET(sigmask);
179 	__sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
180 	new_thread->sigmask = oldsigmask;
181 	ret = lwp_create(&create_params);
182 	__sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
183 	if (ret != 0) {
184 		if (!locked)
185 			THR_THREAD_LOCK(curthread, new_thread);
186 		new_thread->state = PS_DEAD;
187 		new_thread->terminated = 1;
188 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
189 			new_thread->cycle++;
190 			_thr_umtx_wake(&new_thread->cycle, INT_MAX);
191 		}
192 		THR_THREAD_UNLOCK(curthread, new_thread);
193 		THREAD_LIST_LOCK(curthread);
194 		_thread_active_threads--;
195 		new_thread->tlflags |= TLFLAGS_DETACHED;
196 		_thr_ref_delete_unlocked(curthread, new_thread);
197 		THREAD_LIST_UNLOCK(curthread);
198 		(*thread) = NULL;
199 		ret = EAGAIN;
200 	} else if (locked) {
201 		_thr_report_creation(curthread, new_thread);
202 		THR_THREAD_UNLOCK(curthread, new_thread);
203 	}
204 	return (ret);
205 }
206 
207 static int
208 create_stack(struct pthread_attr *pattr)
209 {
210 	int ret;
211 
212 	/* Check if a stack was specified in the thread attributes: */
213 	if ((pattr->stackaddr_attr) != NULL) {
214 		pattr->guardsize_attr = 0;
215 		pattr->flags |= THR_STACK_USER;
216 		ret = 0;
217 	}
218 	else
219 		ret = _thr_stack_alloc(pattr);
220 	return (ret);
221 }
222 
223 static void
224 thread_start(void *arg)
225 {
226 	struct pthread *curthread = (struct pthread *)arg;
227 
228 	tls_set_tcb(curthread->tcb);
229 
230 	/* Thread was created with all signals blocked, unblock them. */
231 	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
232 
233 	THR_LOCK(curthread);
234 	THR_UNLOCK(curthread);
235 
236 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
237 		_thr_suspend_check(curthread);
238 	_nmalloc_thr_init();
239 
240 	/* Run the current thread's start routine with argument: */
241 	_pthread_exit(curthread->start_routine(curthread->arg));
242 
243 	/* This point should never be reached. */
244 	PANIC("Thread has resumed after exit");
245 }
246 
247 __strong_reference(_pthread_create, pthread_create);
248