xref: /freebsd/lib/libthr/thread/thr_create.c (revision f3cec688772252a00e244ae8e7e116f0a3c2473f)
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/signalvar.h>
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stddef.h>
37 #include <pthread.h>
38 #include "un-namespace.h"
39 
40 #include "thr_private.h"
41 
42 static int  create_stack(struct pthread_attr *pattr);
43 static void thread_start(struct pthread *curthread);
44 
45 __weak_reference(_pthread_create, pthread_create);
46 
47 int
48 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
49 	       void *(*start_routine) (void *), void *arg)
50 {
51 	struct pthread *curthread, *new_thread;
52 	struct thr_param param;
53 	struct thr_sched_param sched_param;
54 	int ret = 0, locked, create_suspended;
55 	sigset_t set, oset;
56 
57 	_thr_check_init();
58 
59 	/*
60 	 * Tell libc and others now they need lock to protect their data.
61 	 */
62 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
63 		return (EAGAIN);
64 
65 	curthread = _get_curthread();
66 	if ((new_thread = _thr_alloc(curthread)) == NULL)
67 		return (EAGAIN);
68 
69 	memset(&param, 0, sizeof(param));
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 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
77 		/* inherit scheduling contention scope */
78 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
79 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
80 		else
81 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
82 
83 		new_thread->attr.prio = curthread->attr.prio;
84 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
85 	}
86 
87 	if (_thr_scope_system > 0)
88 		new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
89 	else if (_thr_scope_system < 0)
90 		new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
91 
92 	new_thread->tid = TID_TERMINATED;
93 
94 	if (create_stack(&new_thread->attr) != 0) {
95 		/* Insufficient memory to create a stack: */
96 		_thr_free(curthread, new_thread);
97 		return (EAGAIN);
98 	}
99 	/*
100 	 * Write a magic value to the thread structure
101 	 * to help identify valid ones:
102 	 */
103 	new_thread->magic = THR_MAGIC;
104 	new_thread->start_routine = start_routine;
105 	new_thread->arg = arg;
106 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
107 	    PTHREAD_CANCEL_DEFERRED;
108 	/* Initialize the mutex queue: */
109 	TAILQ_INIT(&new_thread->mutexq);
110 	TAILQ_INIT(&new_thread->pp_mutexq);
111 
112 	/* Initialise hooks in the thread structure: */
113 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
114 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
115 		create_suspended = 1;
116 	} else {
117 		create_suspended = 0;
118 	}
119 
120 	new_thread->state = PS_RUNNING;
121 
122 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
123 		new_thread->tlflags |= TLFLAGS_DETACHED;
124 
125 	/* Add the new thread. */
126 	new_thread->refcount = 1;
127 	_thr_link(curthread, new_thread);
128 	/* Return thread pointer eariler so that new thread can use it. */
129 	(*thread) = new_thread;
130 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
131 		THR_THREAD_LOCK(curthread, new_thread);
132 		locked = 1;
133 	} else
134 		locked = 0;
135 	param.start_func = (void (*)(void *)) thread_start;
136 	param.arg = new_thread;
137 	param.stack_base = new_thread->attr.stackaddr_attr;
138 	param.stack_size = new_thread->attr.stacksize_attr;
139 	param.tls_base = (char *)new_thread->tcb;
140 	param.tls_size = sizeof(struct tcb);
141 	param.child_tid = &new_thread->tid;
142 	param.parent_tid = &new_thread->tid;
143 	param.flags = 0;
144 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
145 		param.flags |= THR_SYSTEM_SCOPE;
146 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED)
147 		param.sched_param = NULL;
148 	else {
149 		param.sched_param = &sched_param;
150 		param.sched_param_size = sizeof(sched_param);
151 		sched_param.policy = new_thread->attr.sched_policy;
152 		sched_param.param.sched_priority = new_thread->attr.prio;
153 	}
154 
155 	/* Schedule the new thread. */
156 	if (create_suspended) {
157 		SIGFILLSET(set);
158 		SIGDELSET(set, SIGTRAP);
159 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
160 		new_thread->sigmask = oset;
161 	}
162 
163 	ret = thr_new(&param, sizeof(param));
164 
165 	if (ret != 0) {
166 		ret = errno;
167 		/*
168 		 * Translate EPROCLIM into well-known POSIX code EAGAIN.
169 		 */
170 		if (ret == EPROCLIM)
171 			ret = EAGAIN;
172 	}
173 
174 	if (create_suspended)
175 		__sys_sigprocmask(SIG_SETMASK, &oset, NULL);
176 
177 	if (ret != 0) {
178 		if (!locked)
179 			THR_THREAD_LOCK(curthread, new_thread);
180 		new_thread->state = PS_DEAD;
181 		new_thread->tid = TID_TERMINATED;
182 		if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
183 			new_thread->cycle++;
184 			_thr_umtx_wake(&new_thread->cycle, INT_MAX);
185 		}
186 		THR_THREAD_UNLOCK(curthread, new_thread);
187 		THREAD_LIST_LOCK(curthread);
188 		_thread_active_threads--;
189 		new_thread->tlflags |= TLFLAGS_DETACHED;
190 		_thr_ref_delete_unlocked(curthread, new_thread);
191 		THREAD_LIST_UNLOCK(curthread);
192 		(*thread) = 0;
193 	} else if (locked) {
194 		_thr_report_creation(curthread, new_thread);
195 		THR_THREAD_UNLOCK(curthread, new_thread);
196 	}
197 	return (ret);
198 }
199 
200 static int
201 create_stack(struct pthread_attr *pattr)
202 {
203 	int ret;
204 
205 	/* Check if a stack was specified in the thread attributes: */
206 	if ((pattr->stackaddr_attr) != NULL) {
207 		pattr->guardsize_attr = 0;
208 		pattr->flags |= THR_STACK_USER;
209 		ret = 0;
210 	}
211 	else
212 		ret = _thr_stack_alloc(pattr);
213 	return (ret);
214 }
215 
216 static void
217 thread_start(struct pthread *curthread)
218 {
219 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
220 		sigset_t set = curthread->sigmask;
221 
222 		_thr_ast(curthread);
223 
224 		/*
225 		 * Parent thread have stored signal mask for us,
226 		 * we should restore it now.
227 		 */
228 		sigprocmask(SIG_SETMASK, &set, NULL);
229 	}
230 
231 	/*
232 	 * This is used as a serialization point to allow parent
233 	 * to report 'new thread' event to debugger before the thread
234 	 * does real work.
235 	 */
236 	THR_LOCK(curthread);
237 	THR_UNLOCK(curthread);
238 
239 	/* Run the current thread's start routine with argument: */
240 	_pthread_exit(curthread->start_routine(curthread->arg));
241 
242 	/* This point should never be reached. */
243 	PANIC("Thread has resumed after exit");
244 }
245