xref: /freebsd/lib/libthr/thread/thr_create.c (revision ef35394ab4dd1d067957ddee2ed1096e618d4494)
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;
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 	if (create_stack(&new_thread->attr) != 0) {
96 		/* Insufficient memory to create a stack: */
97 		new_thread->terminated = 1;
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 	getcontext(&uc);
111 	SIGFILLSET(uc.uc_sigmask);
112 	uc.uc_stack.ss_sp = new_thread->attr.stackaddr_attr;
113 	uc.uc_stack.ss_size = new_thread->attr.stacksize_attr;
114 	makecontext(&uc, (void (*)(void))thread_start, 1, new_thread);
115 
116 	/*
117 	 * Check if this thread is to inherit the scheduling
118 	 * attributes from its parent:
119 	 */
120 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
121 		/*
122 		 * Copy the scheduling attributes. Lock the scheduling
123 		 * lock to get consistent scheduling parameters.
124 		 */
125 		THR_LOCK(curthread);
126 		new_thread->base_priority = curthread->base_priority;
127 		new_thread->attr.prio = curthread->base_priority;
128 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
129 		THR_UNLOCK(curthread);
130 	} else {
131 		/*
132 		 * Use just the thread priority, leaving the
133 		 * other scheduling attributes as their
134 		 * default values:
135 		 */
136 		new_thread->base_priority = new_thread->attr.prio;
137 	}
138 	new_thread->active_priority = new_thread->base_priority;
139 
140 	/* Initialize the mutex queue: */
141 	TAILQ_INIT(&new_thread->mutexq);
142 	TAILQ_INIT(&new_thread->pri_mutexq);
143 
144 	/* Initialise hooks in the thread structure: */
145 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
146 		new_thread->flags = THR_FLAGS_SUSPENDED;
147 	new_thread->state = PS_RUNNING;
148 	/*
149 	 * Thread created by thr_create() inherits currrent thread
150 	 * sigmask, however, before new thread setup itself correctly,
151 	 * it can not handle signal, so we should masks all signals here.
152 	 */
153 	SIGFILLSET(sigmask);
154 	__sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
155 	new_thread->sigmask = oldsigmask;
156 	/* Add the new thread. */
157 	_thr_link(curthread, new_thread);
158 	/* Return thread pointer eariler so that new thread can use it. */
159 	(*thread) = new_thread;
160 	/* Schedule the new thread. */
161 	ret = thr_create(&uc, &new_thread->tid, 0);
162 	__sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
163 	if (ret != 0) {
164 		_thr_unlink(curthread, new_thread);
165 		free_thread(curthread, new_thread);
166 		(*thread) = 0;
167 		ret = EAGAIN;
168 	}
169 	return (ret);
170 }
171 
172 static void
173 free_thread(struct pthread *curthread, struct pthread *thread)
174 {
175 	free_stack(curthread, &thread->attr);
176 	curthread->terminated = 1;
177 	_thr_free(curthread, thread);
178 }
179 
180 static int
181 create_stack(struct pthread_attr *pattr)
182 {
183 	int ret;
184 
185 	/* Check if a stack was specified in the thread attributes: */
186 	if ((pattr->stackaddr_attr) != NULL) {
187 		pattr->guardsize_attr = 0;
188 		pattr->flags |= THR_STACK_USER;
189 		ret = 0;
190 	}
191 	else
192 		ret = _thr_stack_alloc(pattr);
193 	return (ret);
194 }
195 
196 static void
197 free_stack(struct pthread *curthread, struct pthread_attr *pattr)
198 {
199 	if ((pattr->flags & THR_STACK_USER) == 0) {
200 		THREAD_LIST_LOCK(curthread);
201 		/* Stack routines don't use malloc/free. */
202 		_thr_stack_free(pattr);
203 		THREAD_LIST_UNLOCK(curthread);
204 	}
205 }
206 
207 static void
208 thread_start(struct pthread *curthread)
209 {
210 	_tcb_set(curthread->tcb);
211 
212 	/* Thread was created with all signals blocked, unblock them. */
213 	__sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
214 
215 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
216 		_thr_suspend_check(curthread);
217 
218 	/* Run the current thread's start routine with argument: */
219 	pthread_exit(curthread->start_routine(curthread->arg));
220 
221 	/* This point should never be reached. */
222 	PANIC("Thread has resumed after exit");
223 }
224