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 <sys/lwp.h>
42 #include "un-namespace.h"
43 
44 #include "libc_private.h"
45 #include "thr_private.h"
46 
47 static int  create_stack(pthread_attr_t pattr);
48 static void thread_start(void *);
49 
50 int
51 _pthread_create(pthread_t * __restrict thread,
52     const pthread_attr_t * __restrict attr, void *(*start_routine) (void *),
53     void * __restrict arg)
54 {
55 	struct lwp_params create_params;
56 	void *stack;
57 	sigset_t sigmask, oldsigmask;
58 	pthread_t curthread, new_thread;
59 	const cpu_set_t *cpumask = NULL;
60 	int ret = 0, locked;
61 
62 	_thr_check_init();
63 
64 	/*
65 	 * Tell libc and others now they need lock to protect their data.
66 	 */
67 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
68 		return (EAGAIN);
69 
70 	curthread = tls_get_curthread();
71 	if ((new_thread = _thr_alloc(curthread)) == NULL)
72 		return (EAGAIN);
73 
74 	if (attr == NULL || *attr == NULL) {
75 		/* Use the default thread attributes: */
76 		new_thread->attr = _pthread_attr_default;
77 	} else {
78 		new_thread->attr = *(*attr);
79 	}
80 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
81 		/* inherit scheduling contention scope */
82 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
83 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
84 		else
85 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
86 		/*
87 		 * scheduling policy and scheduling parameters will be
88 		 * inherited in following code.
89 		 */
90 	}
91 	if (new_thread->attr.flags & THR_CPUMASK)
92 		cpumask = &new_thread->attr.cpumask;
93 
94 	if (create_stack(&new_thread->attr) != 0) {
95 		/* Insufficient memory to create a stack: */
96 		new_thread->terminated = 1;
97 		_thr_free(curthread, new_thread);
98 		return (EAGAIN);
99 	}
100 	/*
101 	 * Write a magic value to the thread structure
102 	 * to help identify valid ones:
103 	 */
104 	new_thread->magic = THR_MAGIC;
105 	new_thread->start_routine = start_routine;
106 	new_thread->arg = arg;
107 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
108 	    PTHREAD_CANCEL_DEFERRED;
109 	/*
110 	 * Check if this thread is to inherit the scheduling
111 	 * attributes from its parent:
112 	 */
113 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
114 		/*
115 		 * Copy the scheduling attributes. Lock the scheduling
116 		 * lock to get consistent scheduling parameters.
117 		 */
118 		THR_LOCK(curthread);
119 		new_thread->base_priority = curthread->base_priority;
120 		new_thread->attr.prio = curthread->attr.prio;
121 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
122 		THR_UNLOCK(curthread);
123 	} else {
124 		/*
125 		 * Use just the thread priority, leaving the
126 		 * other scheduling attributes as their
127 		 * default values:
128 		 */
129 		new_thread->base_priority = new_thread->attr.prio;
130 	}
131 	new_thread->active_priority = new_thread->base_priority;
132 
133 	/* Initialize the mutex queue: */
134 	TAILQ_INIT(&new_thread->mutexq);
135 
136 	/* Initialise hooks in the thread structure: */
137 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
138 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
139 
140 	new_thread->state = PS_RUNNING;
141 
142 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
143 		new_thread->tlflags |= TLFLAGS_DETACHED;
144 
145 	/* Add the new thread. */
146 	new_thread->refcount = 1;
147 	_thr_link(curthread, new_thread);
148 	/* Return thread pointer eariler so that new thread can use it. */
149 	(*thread) = new_thread;
150 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
151 		THR_THREAD_LOCK(curthread, new_thread);
152 		locked = 1;
153 	} else
154 		locked = 0;
155 	/* Schedule the new thread. */
156 	stack = (char *)new_thread->attr.stackaddr_attr +
157 			new_thread->attr.stacksize_attr;
158 	bzero(&create_params, sizeof(create_params));
159 	create_params.lwp_func = thread_start;
160 	create_params.lwp_arg = new_thread;
161 	create_params.lwp_stack = stack;
162 	create_params.lwp_tid1 = &new_thread->tid;
163 	/*
164 	 * Thread created by thr_create() inherits currrent thread
165 	 * sigmask, however, before new thread setup itself correctly,
166 	 * it can not handle signal, so we should mask all signals here.
167 	 * We do this at the very last moment, so that we don't run
168 	 * into problems while we have all signals disabled.
169 	 */
170 	SIGFILLSET(sigmask);
171 	__sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
172 	new_thread->sigmask = oldsigmask;
173 	ret = lwp_create2(&create_params, cpumask);
174 	__sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
175 	if (ret != 0) {
176 		if (!locked)
177 			THR_THREAD_LOCK(curthread, new_thread);
178 		new_thread->state = PS_DEAD;
179 		new_thread->terminated = 1;
180 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
181 			new_thread->cycle++;
182 			_thr_umtx_wake(&new_thread->cycle, 0);
183 		}
184 		THR_THREAD_UNLOCK(curthread, new_thread);
185 		THREAD_LIST_LOCK(curthread);
186 		_thread_active_threads--;
187 		new_thread->tlflags |= TLFLAGS_DETACHED;
188 		_thr_ref_delete_unlocked(curthread, new_thread);
189 		THREAD_LIST_UNLOCK(curthread);
190 		(*thread) = NULL;
191 		ret = EAGAIN;
192 	} else if (locked) {
193 		_thr_report_creation(curthread, new_thread);
194 		THR_THREAD_UNLOCK(curthread, new_thread);
195 	}
196 	return (ret);
197 }
198 
199 static int
200 create_stack(pthread_attr_t pattr)
201 {
202 	int ret;
203 
204 	/* Check if a stack was specified in the thread attributes: */
205 	if ((pattr->stackaddr_attr) != NULL) {
206 		pattr->guardsize_attr = 0;
207 		pattr->flags |= THR_STACK_USER;
208 		ret = 0;
209 	}
210 	else
211 		ret = _thr_stack_alloc(pattr);
212 	return (ret);
213 }
214 
215 static void
216 thread_start(void *arg)
217 {
218 	pthread_t curthread = (pthread_t)arg;
219 
220 	tls_set_tcb(curthread->tcb);
221 
222 	/* Thread was created with all signals blocked, unblock them. */
223 	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
224 
225 	THR_LOCK(curthread);
226 	THR_UNLOCK(curthread);
227 
228 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
229 		_thr_suspend_check(curthread);
230 	_libc_thr_init();
231 
232 	/* Run the current thread's start routine with argument: */
233 	_pthread_exit(curthread->start_routine(curthread->arg));
234 
235 	/* This point should never be reached. */
236 	PANIC("Thread has resumed after exit");
237 }
238 
239 __strong_reference(_pthread_create, pthread_create);
240