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