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