xref: /freebsd/lib/libthr/thread/thr_create.c (revision ff4b8cb7bd488e9f1e00bf9ed08fa4b377834961)
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 	int ret = 0, locked, create_suspended;
54 	sigset_t set, oset;
55 
56 	_thr_check_init();
57 
58 	/*
59 	 * Tell libc and others now they need lock to protect their data.
60 	 */
61 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
62 		return (EAGAIN);
63 
64 	curthread = _get_curthread();
65 	if ((new_thread = _thr_alloc(curthread)) == NULL)
66 		return (EAGAIN);
67 
68 	memset(&param, 0, sizeof(param));
69 
70 	if (attr == NULL || *attr == NULL)
71 		/* Use the default thread attributes: */
72 		new_thread->attr = _pthread_attr_default;
73 	else
74 		new_thread->attr = *(*attr);
75 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
76 		/* inherit scheduling contention scope */
77 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
78 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
79 		else
80 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
81 		/*
82 		 * scheduling policy and scheduling parameters will be
83 		 * inherited in following code.
84 		 */
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 	/*
109 	 * Check if this thread is to inherit the scheduling
110 	 * attributes from its parent:
111 	 */
112 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
113 		/*
114 		 * Copy the scheduling attributes. Lock the scheduling
115 		 * lock to get consistent scheduling parameters.
116 		 */
117 		THR_LOCK(curthread);
118 		new_thread->base_priority = curthread->base_priority;
119 		new_thread->attr.prio = curthread->base_priority;
120 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
121 		THR_UNLOCK(curthread);
122 	} else {
123 		/*
124 		 * Use just the thread priority, leaving the
125 		 * other scheduling attributes as their
126 		 * default values:
127 		 */
128 		new_thread->base_priority = new_thread->attr.prio;
129 	}
130 	new_thread->active_priority = new_thread->base_priority;
131 
132 	/* Initialize the mutex queue: */
133 	TAILQ_INIT(&new_thread->mutexq);
134 
135 	/* Initialise hooks in the thread structure: */
136 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) {
137 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
138 		create_suspended = 1;
139 	} else {
140 		create_suspended = 0;
141 	}
142 
143 	new_thread->state = PS_RUNNING;
144 
145 	if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
146 		new_thread->tlflags |= TLFLAGS_DETACHED;
147 
148 	/* Add the new thread. */
149 	new_thread->refcount = 1;
150 	_thr_link(curthread, new_thread);
151 	/* Return thread pointer eariler so that new thread can use it. */
152 	(*thread) = new_thread;
153 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
154 		THR_THREAD_LOCK(curthread, new_thread);
155 		locked = 1;
156 	} else
157 		locked = 0;
158 	param.start_func = (void (*)(void *)) thread_start;
159 	param.arg = new_thread;
160 	param.stack_base = new_thread->attr.stackaddr_attr;
161 	param.stack_size = new_thread->attr.stacksize_attr;
162 	param.tls_base = (char *)new_thread->tcb;
163 	param.tls_size = sizeof(struct tcb);
164 	param.child_tid = &new_thread->tid;
165 	param.parent_tid = &new_thread->tid;
166 	param.flags = 0;
167 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
168 		param.flags |= THR_SYSTEM_SCOPE;
169 
170 	/* Schedule the new thread. */
171 	if (create_suspended) {
172 		SIGFILLSET(set);
173 		SIGDELSET(set, SIGTRAP);
174 		__sys_sigprocmask(SIG_SETMASK, &set, &oset);
175 		new_thread->sigmask = oset;
176 	}
177 
178 	ret = thr_new(&param, sizeof(param));
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 		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) = 0;
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(struct pthread *curthread)
225 {
226 	if (curthread->attr.suspend == THR_CREATE_SUSPENDED) {
227 		sigset_t set = curthread->sigmask;
228 
229 		_thr_ast(curthread);
230 
231 		/*
232 		 * Parent thread have stored signal mask for us,
233 		 * we should restore it now.
234 		 */
235 		sigprocmask(SIG_SETMASK, &set, NULL);
236 	}
237 
238 	/*
239 	 * This is used as a serialization point to allow parent
240 	 * to report 'new thread' event to debugger before the thread
241 	 * does real work.
242 	 */
243 	THR_LOCK(curthread);
244 	THR_UNLOCK(curthread);
245 
246 	/* Run the current thread's start routine with argument: */
247 	_pthread_exit(curthread->start_routine(curthread->arg));
248 
249 	/* This point should never be reached. */
250 	PANIC("Thread has resumed after exit");
251 }
252