2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
43 #include <machine/reg.h>
45 #include "pthread_private.h"
46 #include "libc_private.h"
49 pthread_create(pthread_t * thread, const pthread_attr_t * attr,
50 void *(*start_routine) (void *), void *arg)
60 * Locking functions in libc are required when there are
61 * threads other than the initial thread.
65 /* Allocate memory for the thread structure: */
66 if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) {
67 /* Insufficient memory to create a thread: */
70 /* Check if default thread attributes are required: */
71 if (attr == NULL || *attr == NULL) {
72 /* Use the default thread attributes: */
73 pattr = &pthread_attr_default;
77 /* Check if a stack was specified in the thread attributes: */
78 if ((stack = pattr->stackaddr_attr) != NULL) {
80 /* Allocate memory for a default-size stack: */
81 else if (pattr->stacksize_attr == PTHREAD_STACK_DEFAULT) {
82 struct stack *spare_stack;
84 /* Allocate or re-use a default-size stack. */
87 * Use the garbage collector mutex for synchronization
88 * of the spare stack list.
90 if (pthread_mutex_lock(&_gc_mutex) != 0)
91 PANIC("Cannot lock gc mutex");
93 if ((spare_stack = SLIST_FIRST(&_stackq)) != NULL) {
94 /* Use the spare stack. */
95 SLIST_REMOVE_HEAD(&_stackq, qe);
97 /* Unlock the garbage collector mutex. */
98 if (pthread_mutex_unlock(&_gc_mutex) != 0)
99 PANIC("Cannot unlock gc mutex");
101 stack = sizeof(struct stack)
102 + (void *) spare_stack
103 - PTHREAD_STACK_DEFAULT;
105 /* Unlock the garbage collector mutex. */
106 if (pthread_mutex_unlock(&_gc_mutex) != 0)
107 PANIC("Cannot unlock gc mutex");
109 /* Allocate a new stack. */
110 stack = _next_stack + PTHREAD_STACK_GUARD;
112 * Even if stack allocation fails, we don't want
113 * to try to use this location again, so
114 * unconditionally decrement _next_stack. Under
115 * normal operating conditions, the most likely
116 * reason for an mmap() error is a stack
117 * overflow of the adjacent thread stack.
119 _next_stack -= (PTHREAD_STACK_DEFAULT
120 + PTHREAD_STACK_GUARD);
123 if (mmap(_next_stack, PTHREAD_STACK_GUARD, 0,
124 MAP_ANON, -1, 0) == MAP_FAILED) {
130 PTHREAD_STACK_DEFAULT,
131 PROT_READ | PROT_WRITE,
137 -1, 0) == MAP_FAILED) {
140 PTHREAD_STACK_GUARD);
146 * The user wants a stack of a particular size. Lets hope they
147 * really know what they want, and simply malloc the stack.
149 else if ((stack = (void *) malloc(pattr->stacksize_attr))
151 /* Insufficient memory to create a thread: */
156 /* Check for errors: */
159 /* Initialise the thread structure: */
160 memset(new_thread, 0, sizeof(struct pthread));
161 new_thread->slice_usec = -1;
162 new_thread->sig_saved = 0;
163 new_thread->stack = stack;
164 new_thread->start_routine = start_routine;
165 new_thread->arg = arg;
167 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
168 PTHREAD_CANCEL_DEFERRED;
171 * Write a magic value to the thread structure
172 * to help identify valid ones:
174 new_thread->magic = PTHREAD_MAGIC;
176 /* Initialise the thread for signals: */
177 new_thread->sigmask = _thread_run->sigmask;
179 /* Initialise the jump buffer: */
180 setjmp(new_thread->saved_jmp_buf);
183 * Set up new stack frame so that it looks like it
184 * returned from a longjmp() to the beginning of
187 #if defined(__FreeBSD__)
188 #if defined(__alpha__)
189 new_thread->saved_jmp_buf[0]._jb[2] = (long) _thread_start;
190 new_thread->saved_jmp_buf[0]._jb[4 + R_RA] = 0;
191 new_thread->saved_jmp_buf[0]._jb[4 + R_T12] = (long) _thread_start;
193 new_thread->saved_jmp_buf[0]._jb[0] = (long) _thread_start;
195 #elif defined(__NetBSD__)
196 #if defined(__alpha__)
197 new_thread->saved_jmp_buf[2] = (long) _thread_start;
198 new_thread->saved_jmp_buf[4 + R_RA] = 0;
199 new_thread->saved_jmp_buf[4 + R_T12] = (long) _thread_start;
201 new_thread->saved_jmp_buf[0] = (long) _thread_start;
204 #error "Don't recognize this operating system!"
207 /* The stack starts high and builds down: */
208 #if defined(__FreeBSD__)
209 #if defined(__alpha__)
210 new_thread->saved_jmp_buf[0]._jb[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double);
212 new_thread->saved_jmp_buf[0]._jb[2] = (int) (new_thread->stack + pattr->stacksize_attr - sizeof(double));
214 #elif defined(__NetBSD__)
215 #if defined(__alpha__)
216 new_thread->saved_jmp_buf[4 + R_SP] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double);
218 new_thread->saved_jmp_buf[2] = (long) new_thread->stack + pattr->stacksize_attr - sizeof(double);
221 #error "Don't recognize this operating system!"
224 /* Copy the thread attributes: */
225 memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr));
228 * Check if this thread is to inherit the scheduling
229 * attributes from its parent:
231 if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
232 /* Copy the scheduling attributes: */
233 new_thread->base_priority
234 = _thread_run->base_priority;
235 new_thread->attr.prio
236 = _thread_run->base_priority;
237 new_thread->attr.sched_policy
238 = _thread_run->attr.sched_policy;
241 * Use just the thread priority, leaving the
242 * other scheduling attributes as their
245 new_thread->base_priority
246 = new_thread->attr.prio;
248 new_thread->active_priority = new_thread->base_priority;
249 new_thread->inherited_priority = 0;
251 /* Initialise the join queue for the new thread: */
252 TAILQ_INIT(&(new_thread->join_queue));
254 /* Initialize the mutex queue: */
255 TAILQ_INIT(&new_thread->mutexq);
257 /* Initialise hooks in the thread structure: */
258 new_thread->specific_data = NULL;
259 new_thread->cleanup = NULL;
260 new_thread->flags = 0;
261 new_thread->poll_data.nfds = 0;
262 new_thread->poll_data.fds = NULL;
265 * Defer signals to protect the scheduling queues
266 * from access by the signal handler:
268 _thread_kern_sig_defer();
271 * Check if the garbage collector thread
272 * needs to be started.
274 f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial);
276 /* Add the thread to the linked list of all threads: */
277 TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle);
279 if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) {
280 new_thread->state = PS_SUSPENDED;
281 PTHREAD_WAITQ_INSERT(new_thread);
283 new_thread->state = PS_RUNNING;
284 PTHREAD_PRIOQ_INSERT_TAIL(new_thread);
288 * Undefer and handle pending signals, yielding
291 _thread_kern_sig_undefer();
293 /* Return a pointer to the thread structure: */
294 (*thread) = new_thread;
296 /* Schedule the new user thread: */
297 _thread_kern_sched(NULL);
300 * Start a garbage collector thread
303 if (f_gc && pthread_create(&gc_thread,NULL,
304 _thread_gc,NULL) != 0)
305 PANIC("Can't create gc thread");
309 /* Return the status: */
316 /* We just left the scheduler via longjmp: */
317 _thread_kern_in_sched = 0;
319 /* Run the current thread's start routine with argument: */
320 pthread_exit(_thread_run->start_routine(_thread_run->arg));
322 /* This point should never be reached. */
323 PANIC("Thread has resumed after exit");