xref: /freebsd/lib/libthr/thread/thr_create.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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 <sys/types.h>
37 #include <sys/signalvar.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stddef.h>
42 #include <pthread.h>
43 
44 #include "thr_private.h"
45 
46 static void free_thread(struct pthread *curthread, struct pthread *thread);
47 static int  create_stack(struct pthread_attr *pattr);
48 static void free_stack(struct pthread *curthread, struct pthread_attr *pattr);
49 static void thread_start(struct pthread *curthread);
50 
51 __weak_reference(_pthread_create, pthread_create);
52 
53 int
54 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
55 	       void *(*start_routine) (void *), void *arg)
56 {
57 	struct pthread *curthread, *new_thread;
58 	struct thr_param param;
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 	memset(&param, 0, sizeof(param));
74 
75 	if (attr == NULL || *attr == NULL)
76 		/* Use the default thread attributes: */
77 		new_thread->attr = _pthread_attr_default;
78 	else
79 		new_thread->attr = *(*attr);
80 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
81 		/* inherit scheduling contention scope */
82 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
83 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
84 		else
85 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
86 		/*
87 		 * scheduling policy and scheduling parameters will be
88 		 * inherited in following code.
89 		 */
90 	}
91 
92 	if (_thr_scope_system > 0)
93 		new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
94 	else if (_thr_scope_system < 0)
95 		new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
96 
97 	new_thread->tid = TID_TERMINATED;
98 
99 	if (create_stack(&new_thread->attr) != 0) {
100 		/* Insufficient memory to create a stack: */
101 		_thr_free(curthread, new_thread);
102 		return (EAGAIN);
103 	}
104 	/*
105 	 * Write a magic value to the thread structure
106 	 * to help identify valid ones:
107 	 */
108 	new_thread->magic = THR_MAGIC;
109 	new_thread->start_routine = start_routine;
110 	new_thread->arg = arg;
111 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
112 	    PTHREAD_CANCEL_DEFERRED;
113 	/*
114 	 * Check if this thread is to inherit the scheduling
115 	 * attributes from its parent:
116 	 */
117 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
118 		/*
119 		 * Copy the scheduling attributes. Lock the scheduling
120 		 * lock to get consistent scheduling parameters.
121 		 */
122 		THR_LOCK(curthread);
123 		new_thread->base_priority = curthread->base_priority;
124 		new_thread->attr.prio = curthread->base_priority;
125 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
126 		THR_UNLOCK(curthread);
127 	} else {
128 		/*
129 		 * Use just the thread priority, leaving the
130 		 * other scheduling attributes as their
131 		 * default values:
132 		 */
133 		new_thread->base_priority = new_thread->attr.prio;
134 	}
135 	new_thread->active_priority = new_thread->base_priority;
136 
137 	/* Initialize the mutex queue: */
138 	TAILQ_INIT(&new_thread->mutexq);
139 	TAILQ_INIT(&new_thread->pri_mutexq);
140 
141 	/* Initialise hooks in the thread structure: */
142 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
143 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
144 	new_thread->state = PS_RUNNING;
145 
146 	/* Add the new thread. */
147 	_thr_link(curthread, new_thread);
148 	/* Return thread pointer eariler so that new thread can use it. */
149 	(*thread) = new_thread;
150 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
151 		THR_THREAD_LOCK(curthread, new_thread);
152 		locked = 1;
153 	} else
154 		locked = 0;
155 	param.start_func = (void (*)(void *)) thread_start;
156 	param.arg = new_thread;
157 	param.stack_base = new_thread->attr.stackaddr_attr;
158 	param.stack_size = new_thread->attr.stacksize_attr;
159 	param.tls_base = (char *)new_thread->tcb;
160 	param.tls_size = sizeof(struct tcb);
161 	param.child_tid = &new_thread->tid;
162 	param.parent_tid = &new_thread->tid;
163 	param.flags = 0;
164 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
165 		param.flags |= THR_SYSTEM_SCOPE;
166 	/* Schedule the new thread. */
167 	ret = thr_new(&param, sizeof(param));
168 	if (ret != 0) {
169 		if (locked)
170 			THR_THREAD_UNLOCK(curthread, new_thread);
171 		_thr_unlink(curthread, new_thread);
172 		free_thread(curthread, new_thread);
173 		(*thread) = 0;
174 		ret = EAGAIN;
175 	} else if (locked) {
176 		_thr_report_creation(curthread, new_thread);
177 		THR_THREAD_UNLOCK(curthread, new_thread);
178 	}
179 	return (ret);
180 }
181 
182 static void
183 free_thread(struct pthread *curthread, struct pthread *thread)
184 {
185 	free_stack(curthread, &thread->attr);
186 	curthread->tid = TID_TERMINATED;
187 	_thr_free(curthread, thread);
188 }
189 
190 static int
191 create_stack(struct pthread_attr *pattr)
192 {
193 	int ret;
194 
195 	/* Check if a stack was specified in the thread attributes: */
196 	if ((pattr->stackaddr_attr) != NULL) {
197 		pattr->guardsize_attr = 0;
198 		pattr->flags |= THR_STACK_USER;
199 		ret = 0;
200 	}
201 	else
202 		ret = _thr_stack_alloc(pattr);
203 	return (ret);
204 }
205 
206 static void
207 free_stack(struct pthread *curthread, struct pthread_attr *pattr)
208 {
209 	if ((pattr->flags & THR_STACK_USER) == 0) {
210 		THREAD_LIST_LOCK(curthread);
211 		/* Stack routines don't use malloc/free. */
212 		_thr_stack_free(pattr);
213 		THREAD_LIST_UNLOCK(curthread);
214 	}
215 }
216 
217 static void
218 thread_start(struct pthread *curthread)
219 {
220 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
221 		_thr_suspend_check(curthread);
222 
223 	THR_LOCK(curthread);
224 	THR_UNLOCK(curthread);
225 
226 	/* Run the current thread's start routine with argument: */
227 	pthread_exit(curthread->start_routine(curthread->arg));
228 
229 	/* This point should never be reached. */
230 	PANIC("Thread has resumed after exit");
231 }
232