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