xref: /freebsd/lib/libthr/thread/thr_create.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
1 /*
2  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
11  *    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  *
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.
26  *
27  * $FreeBSD$
28  */
29 
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/rtprio.h>
33 #include <sys/signalvar.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <pthread.h>
39 #include <pthread_np.h>
40 #include "un-namespace.h"
41 
42 #include "thr_private.h"
43 
44 static int  create_stack(struct pthread_attr *pattr);
45 static void thread_start(struct pthread *curthread);
46 
47 __weak_reference(_pthread_create, pthread_create);
48 
49 int
50 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
51 	       void *(*start_routine) (void *), void *arg)
52 {
53 	struct pthread *curthread, *new_thread;
54 	struct thr_param param;
55 	struct sched_param sched_param;
56 	struct rtprio rtp;
57 	int ret = 0, locked, create_suspended;
58 	sigset_t set, oset;
59 	cpuset_t *cpusetp = NULL;
60 	int cpusetsize = 0;
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 = _get_curthread();
71 	if ((new_thread = _thr_alloc(curthread)) == NULL)
72 		return (EAGAIN);
73 
74 	memset(&param, 0, sizeof(param));
75 
76 	if (attr == NULL || *attr == NULL)
77 		/* Use the default thread attributes: */
78 		new_thread->attr = _pthread_attr_default;
79 	else {
80 		new_thread->attr = *(*attr);
81 		cpusetp = new_thread->attr.cpuset;
82 		cpusetsize = new_thread->attr.cpusetsize;
83 		new_thread->attr.cpuset = NULL;
84 		new_thread->attr.cpusetsize = 0;
85 	}
86 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
87 		/* inherit scheduling contention scope */
88 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
89 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
90 		else
91 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
92 
93 		new_thread->attr.prio = curthread->attr.prio;
94 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
95 	}
96 
97 	new_thread->tid = TID_TERMINATED;
98 
99 	if (create_stack(&new_thread->attr) != 0) {
100 		/* Insufficient memory to create a stack: */
101 		_thr_free(curthread, new_thread);
102 		return (EAGAIN);
103 	}
104 	/*
105 	 * Write a magic value to the thread structure
106 	 * to help identify valid ones:
107 	 */
108 	new_thread->magic = THR_MAGIC;
109 	new_thread->start_routine = start_routine;
110 	new_thread->arg = arg;
111 	new_thread->cancel_enable = 1;
112 	new_thread->cancel_async = 0;
113 	/* Initialize the mutex queue: */
114 	TAILQ_INIT(&new_thread->mutexq);
115 	TAILQ_INIT(&new_thread->pp_mutexq);
116 
117 	/* Initialise hooks in the thread structure: */
118 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
119 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
120 		create_suspended = 1;
121 	} else {
122 		create_suspended = 0;
123 	}
124 
125 	new_thread->state = PS_RUNNING;
126 
127 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
128 		new_thread->flags |= THR_FLAGS_DETACHED;
129 
130 	/* Add the new thread. */
131 	new_thread->refcount = 1;
132 	_thr_link(curthread, new_thread);
133 	/* Return thread pointer eariler so that new thread can use it. */
134 	(*thread) = new_thread;
135 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE) || cpusetp != NULL) {
136 		THR_THREAD_LOCK(curthread, new_thread);
137 		locked = 1;
138 	} else
139 		locked = 0;
140 	param.start_func = (void (*)(void *)) thread_start;
141 	param.arg = new_thread;
142 	param.stack_base = new_thread->attr.stackaddr_attr;
143 	param.stack_size = new_thread->attr.stacksize_attr;
144 	param.tls_base = (char *)new_thread->tcb;
145 	param.tls_size = sizeof(struct tcb);
146 	param.child_tid = &new_thread->tid;
147 	param.parent_tid = &new_thread->tid;
148 	param.flags = 0;
149 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
150 		param.flags |= THR_SYSTEM_SCOPE;
151 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
152 		param.rtp = NULL;
153 	else {
154 		sched_param.sched_priority = new_thread->attr.prio;
155 		_schedparam_to_rtp(new_thread->attr.sched_policy,
156 			&sched_param, &rtp);
157 		param.rtp = &rtp;
158 	}
159 
160 	/* Schedule the new thread. */
161 	if (create_suspended) {
162 		SIGFILLSET(set);
163 		SIGDELSET(set, SIGTRAP);
164 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
165 		new_thread->sigmask = oset;
166 		SIGDELSET(new_thread->sigmask, SIGCANCEL);
167 	}
168 
169 	ret = thr_new(&param, sizeof(param));
170 
171 	if (ret != 0) {
172 		ret = errno;
173 		/*
174 		 * Translate EPROCLIM into well-known POSIX code EAGAIN.
175 		 */
176 		if (ret == EPROCLIM)
177 			ret = EAGAIN;
178 	}
179 
180 	if (create_suspended)
181 		__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
182 
183 	if (ret != 0) {
184 		if (!locked)
185 			THR_THREAD_LOCK(curthread, new_thread);
186 		new_thread->state = PS_DEAD;
187 		new_thread->tid = TID_TERMINATED;
188 		new_thread->flags |= THR_FLAGS_DETACHED;
189 		new_thread->refcount--;
190 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
191 			new_thread->cycle++;
192 			_thr_umtx_wake(&new_thread->cycle, INT_MAX, 0);
193 		}
194 		_thr_try_gc(curthread, new_thread); /* thread lock released */
195 		atomic_add_int(&_thread_active_threads, -1);
196 	} else if (locked) {
197 		if (cpusetp != NULL) {
198 			if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
199 				TID(new_thread), cpusetsize, cpusetp)) {
200 				ret = errno;
201 				/* kill the new thread */
202 				new_thread->force_exit = 1;
203 				new_thread->flags |= THR_FLAGS_DETACHED;
204 				_thr_try_gc(curthread, new_thread);
205 				 /* thread lock released */
206 				goto out;
207 			}
208 		}
209 
210 		_thr_report_creation(curthread, new_thread);
211 		THR_THREAD_UNLOCK(curthread, new_thread);
212 	}
213 out:
214 	if (ret)
215 		(*thread) = 0;
216 	return (ret);
217 }
218 
219 static int
220 create_stack(struct pthread_attr *pattr)
221 {
222 	int ret;
223 
224 	/* Check if a stack was specified in the thread attributes: */
225 	if ((pattr->stackaddr_attr) != NULL) {
226 		pattr->guardsize_attr = 0;
227 		pattr->flags |= THR_STACK_USER;
228 		ret = 0;
229 	}
230 	else
231 		ret = _thr_stack_alloc(pattr);
232 	return (ret);
233 }
234 
235 static void
236 thread_start(struct pthread *curthread)
237 {
238 	sigset_t set;
239 
240 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED)
241 		set = curthread->sigmask;
242 
243 	/*
244 	 * This is used as a serialization point to allow parent
245 	 * to report 'new thread' event to debugger or tweak new thread's
246 	 * attributes before the new thread does real-world work.
247 	 */
248 	THR_LOCK(curthread);
249 	THR_UNLOCK(curthread);
250 
251 	if (curthread->force_exit)
252 		_pthread_exit(PTHREAD_CANCELED);
253 
254 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
255 #if 0
256 		/* Done in THR_UNLOCK() */
257 		_thr_ast(curthread);
258 #endif
259 
260 		/*
261 		 * Parent thread have stored signal mask for us,
262 		 * we should restore it now.
263 		 */
264 		__sys_sigprocmask(SIG_SETMASK, &set, NULL);
265 	}
266 
267 #ifdef _PTHREAD_FORCED_UNWIND
268 	curthread->unwind_stackend = (char *)curthread->attr.stackaddr_attr +
269 		curthread->attr.stacksize_attr;
270 #endif
271 
272 	/* Run the current thread's start routine with argument: */
273 	_pthread_exit(curthread->start_routine(curthread->arg));
274 
275 	/* This point should never be reached. */
276 	PANIC("Thread has resumed after exit");
277 }
278