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