xref: /freebsd/lib/libthr/thread/thr_create.c (revision ebccf1e3a6b11b97cbf5f813dd76636e892a9035)
1 /*
2  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
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, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stddef.h>
40 #include <pthread.h>
41 #include <sys/signalvar.h>
42 
43 #include "thr_private.h"
44 
45 static void free_thread(struct pthread *curthread, struct pthread *thread);
46 static int  create_stack(struct pthread_attr *pattr);
47 static void free_stack(struct pthread *curthread, struct pthread_attr *pattr);
48 static void thread_start(struct pthread *curthread);
49 
50 __weak_reference(_pthread_create, pthread_create);
51 
52 int
53 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
54 	       void *(*start_routine) (void *), void *arg)
55 {
56 	ucontext_t uc;
57 	sigset_t sigmask, oldsigmask;
58 	struct pthread *curthread, *new_thread;
59 	int ret = 0, locked;
60 
61 	_thr_check_init();
62 
63 	/*
64 	 * Tell libc and others now they need lock to protect their data.
65 	 */
66 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
67 		return (EAGAIN);
68 
69 	curthread = _get_curthread();
70 	if ((new_thread = _thr_alloc(curthread)) == NULL)
71 		return (EAGAIN);
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 		 * scheduling policy and scheduling parameters will be
86 		 * inherited in following code.
87 		 */
88 	}
89 
90 	if (_thr_scope_system > 0)
91 		new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
92 	else if (_thr_scope_system < 0)
93 		new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
94 
95 	new_thread->tid = TID_TERMINATED;
96 
97 	if (create_stack(&new_thread->attr) != 0) {
98 		/* Insufficient memory to create a stack: */
99 		_thr_free(curthread, new_thread);
100 		return (EAGAIN);
101 	}
102 	/*
103 	 * Write a magic value to the thread structure
104 	 * to help identify valid ones:
105 	 */
106 	new_thread->magic = THR_MAGIC;
107 	new_thread->start_routine = start_routine;
108 	new_thread->arg = arg;
109 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
110 	    PTHREAD_CANCEL_DEFERRED;
111 	getcontext(&uc);
112 	SIGFILLSET(uc.uc_sigmask);
113 	uc.uc_stack.ss_sp = new_thread->attr.stackaddr_attr;
114 	uc.uc_stack.ss_size = new_thread->attr.stacksize_attr;
115 	makecontext(&uc, (void (*)(void))thread_start, 1, new_thread);
116 
117 	/*
118 	 * Check if this thread is to inherit the scheduling
119 	 * attributes from its parent:
120 	 */
121 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
122 		/*
123 		 * Copy the scheduling attributes. Lock the scheduling
124 		 * lock to get consistent scheduling parameters.
125 		 */
126 		THR_LOCK(curthread);
127 		new_thread->base_priority = curthread->base_priority;
128 		new_thread->attr.prio = curthread->base_priority;
129 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
130 		THR_UNLOCK(curthread);
131 	} else {
132 		/*
133 		 * Use just the thread priority, leaving the
134 		 * other scheduling attributes as their
135 		 * default values:
136 		 */
137 		new_thread->base_priority = new_thread->attr.prio;
138 	}
139 	new_thread->active_priority = new_thread->base_priority;
140 
141 	/* Initialize the mutex queue: */
142 	TAILQ_INIT(&new_thread->mutexq);
143 	TAILQ_INIT(&new_thread->pri_mutexq);
144 
145 	/* Initialise hooks in the thread structure: */
146 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
147 		new_thread->flags = THR_FLAGS_SUSPENDED;
148 	new_thread->state = PS_RUNNING;
149 	/*
150 	 * Thread created by thr_create() inherits currrent thread
151 	 * sigmask, however, before new thread setup itself correctly,
152 	 * it can not handle signal, so we should masks all signals here.
153 	 */
154 	SIGFILLSET(sigmask);
155 	SIGDELSET(sigmask, SIGTRAP);
156 	__sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
157 	new_thread->sigmask = oldsigmask;
158 	/* Add the new thread. */
159 	_thr_link(curthread, new_thread);
160 	/* Return thread pointer eariler so that new thread can use it. */
161 	(*thread) = new_thread;
162 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
163 		THR_THREAD_LOCK(curthread, new_thread);
164 		locked = 1;
165 	} else
166 		locked = 0;
167 	/* Schedule the new thread. */
168 	ret = thr_create(&uc, &new_thread->tid, 0);
169 	__sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
170 	if (ret != 0) {
171 		if (locked)
172 			THR_THREAD_UNLOCK(curthread, new_thread);
173 		_thr_unlink(curthread, new_thread);
174 		free_thread(curthread, new_thread);
175 		(*thread) = 0;
176 		ret = EAGAIN;
177 	} else if (locked) {
178 		_thr_report_creation(curthread, new_thread);
179 		THR_THREAD_UNLOCK(curthread, new_thread);
180 	}
181 	return (ret);
182 }
183 
184 static void
185 free_thread(struct pthread *curthread, struct pthread *thread)
186 {
187 	free_stack(curthread, &thread->attr);
188 	curthread->tid = TID_TERMINATED;
189 	_thr_free(curthread, thread);
190 }
191 
192 static int
193 create_stack(struct pthread_attr *pattr)
194 {
195 	int ret;
196 
197 	/* Check if a stack was specified in the thread attributes: */
198 	if ((pattr->stackaddr_attr) != NULL) {
199 		pattr->guardsize_attr = 0;
200 		pattr->flags |= THR_STACK_USER;
201 		ret = 0;
202 	}
203 	else
204 		ret = _thr_stack_alloc(pattr);
205 	return (ret);
206 }
207 
208 static void
209 free_stack(struct pthread *curthread, struct pthread_attr *pattr)
210 {
211 	if ((pattr->flags & THR_STACK_USER) == 0) {
212 		THREAD_LIST_LOCK(curthread);
213 		/* Stack routines don't use malloc/free. */
214 		_thr_stack_free(pattr);
215 		THREAD_LIST_UNLOCK(curthread);
216 	}
217 }
218 
219 static void
220 thread_start(struct pthread *curthread)
221 {
222 	_tcb_set(curthread->tcb);
223 
224 	/* Thread was created with all signals blocked, unblock them. */
225 	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
226 
227 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
228 		_thr_suspend_check(curthread);
229 
230 	THR_LOCK(curthread);
231 	THR_UNLOCK(curthread);
232 
233 	/* Run the current thread's start routine with argument: */
234 	pthread_exit(curthread->start_routine(curthread->arg));
235 
236 	/* This point should never be reached. */
237 	PANIC("Thread has resumed after exit");
238 }
239