thr_create.c revision 103419
1239478Sadrian/*
2239478Sadrian * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3239478Sadrian * All rights reserved.
4239478Sadrian *
5239478Sadrian * Redistribution and use in source and binary forms, with or without
6239478Sadrian * modification, are permitted provided that the following conditions
7239478Sadrian * are met:
8239478Sadrian * 1. Redistributions of source code must retain the above copyright
9239478Sadrian *    notice, this list of conditions and the following disclaimer.
10239478Sadrian * 2. Redistributions in binary form must reproduce the above copyright
11239478Sadrian *    notice, this list of conditions and the following disclaimer in the
12239478Sadrian *    documentation and/or other materials provided with the distribution.
13239478Sadrian * 3. All advertising materials mentioning features or use of this software
14239478Sadrian *    must display the following acknowledgement:
15239478Sadrian *	This product includes software developed by John Birrell.
16239478Sadrian * 4. Neither the name of the author nor the names of any co-contributors
17239478Sadrian *    may be used to endorse or promote products derived from this software
18239478Sadrian *    without specific prior written permission.
19239478Sadrian *
20239478Sadrian * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21239478Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22239478Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23239478Sadrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24239478Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25239478Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26239478Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27239478Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28239478Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29239478Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30239478Sadrian * SUCH DAMAGE.
31239478Sadrian *
32239478Sadrian * $FreeBSD: head/lib/libkse/thread/thr_create.c 103419 2002-09-16 19:52:52Z mini $
33239478Sadrian */
34239478Sadrian#include <errno.h>
35239478Sadrian#include <stdlib.h>
36239478Sadrian#include <string.h>
37239478Sadrian#include <fcntl.h>
38239478Sadrian#include <unistd.h>
39239478Sadrian#include <stddef.h>
40239478Sadrian#include <sys/time.h>
41239478Sadrian#include <machine/reg.h>
42239478Sadrian#include <pthread.h>
43239478Sadrian#include "thr_private.h"
44239478Sadrian#include "libc_private.h"
45239478Sadrian
46239478Sadrianstatic u_int64_t next_uniqueid = 1;
47239478Sadrian
48239478Sadrian#define OFF(f)	offsetof(struct pthread, f)
49239478Sadrianint _thread_next_offset			= OFF(tle.tqe_next);
50239478Sadrianint _thread_uniqueid_offset		= OFF(uniqueid);
51239478Sadrianint _thread_state_offset		= OFF(state);
52239478Sadrianint _thread_name_offset			= OFF(name);
53239478Sadrianint _thread_ctx_offset			= OFF(ctx);
54239478Sadrian#undef OFF
55239478Sadrian
56239478Sadrianint _thread_PS_RUNNING_value		= PS_RUNNING;
57239478Sadrianint _thread_PS_DEAD_value		= PS_DEAD;
58239478Sadrian
59239478Sadrian__weak_reference(_pthread_create, pthread_create);
60239478Sadrian
61239478Sadrianint
62239478Sadrian_pthread_create(pthread_t * thread, const pthread_attr_t * attr,
63239478Sadrian	       void *(*start_routine) (void *), void *arg)
64239478Sadrian{
65239478Sadrian	struct pthread	*curthread = _get_curthread();
66239478Sadrian	struct itimerval itimer;
67239478Sadrian	int		f_gc = 0;
68239478Sadrian	int             ret = 0;
69239478Sadrian	pthread_t       gc_thread;
70239478Sadrian	pthread_t       new_thread;
71239478Sadrian	pthread_attr_t	pattr;
72239478Sadrian	void           *stack;
73239478Sadrian
74239478Sadrian	/*
75239478Sadrian	 * Locking functions in libc are required when there are
76239478Sadrian	 * threads other than the initial thread.
77239478Sadrian	 */
78239478Sadrian	__isthreaded = 1;
79239478Sadrian
80239478Sadrian	/* Allocate memory for the thread structure: */
81239478Sadrian	if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) {
82239478Sadrian		/* Insufficient memory to create a thread: */
83239478Sadrian		ret = EAGAIN;
84239478Sadrian	} else {
85239478Sadrian		/* Check if default thread attributes are required: */
86239478Sadrian		if (attr == NULL || *attr == NULL) {
87239478Sadrian			/* Use the default thread attributes: */
88239478Sadrian			pattr = &pthread_attr_default;
89239478Sadrian		} else {
90239478Sadrian			pattr = *attr;
91239478Sadrian		}
92239478Sadrian		/* Check if a stack was specified in the thread attributes: */
93239478Sadrian		if ((stack = pattr->stackaddr_attr) != NULL) {
94239478Sadrian		}
95239478Sadrian		/* Allocate a stack: */
96239478Sadrian		else {
97239478Sadrian			stack = _thread_stack_alloc(pattr->stacksize_attr,
98239478Sadrian			    pattr->guardsize_attr);
99239478Sadrian			if (stack == NULL) {
100239478Sadrian				ret = EAGAIN;
101239478Sadrian				free(new_thread);
102239478Sadrian			}
103239478Sadrian		}
104239478Sadrian
105239478Sadrian		/* Check for errors: */
106239478Sadrian		if (ret != 0) {
107239478Sadrian		} else {
108239478Sadrian			/* Initialise the thread structure: */
109239478Sadrian			memset(new_thread, 0, sizeof(struct pthread));
110239478Sadrian			new_thread->slice_usec = -1;
111239478Sadrian			new_thread->stack = stack;
112239478Sadrian			new_thread->start_routine = start_routine;
113239478Sadrian			new_thread->arg = arg;
114239478Sadrian
115239478Sadrian			new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
116239478Sadrian			    PTHREAD_CANCEL_DEFERRED;
117239478Sadrian
118239478Sadrian			/*
119239478Sadrian			 * Write a magic value to the thread structure
120239478Sadrian			 * to help identify valid ones:
121239478Sadrian			 */
122239478Sadrian			new_thread->magic = PTHREAD_MAGIC;
123239478Sadrian
124239478Sadrian			/* Initialise the machine context: */
125239478Sadrian			getcontext(&new_thread->ctx);
126239478Sadrian			new_thread->ctx.uc_stack.ss_sp = new_thread->stack;
127239478Sadrian			new_thread->ctx.uc_stack.ss_size =
128239478Sadrian			    pattr->stacksize_attr;
129239478Sadrian			makecontext(&new_thread->ctx, _thread_start, 1);
130239478Sadrian
131239478Sadrian			/* Copy the thread attributes: */
132239478Sadrian			memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr));
133239478Sadrian
134239478Sadrian			/*
135239478Sadrian			 * Check if this thread is to inherit the scheduling
136239478Sadrian			 * attributes from its parent:
137239478Sadrian			 */
138239478Sadrian			if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
139239478Sadrian				/* Copy the scheduling attributes: */
140239478Sadrian				new_thread->base_priority =
141239478Sadrian				    curthread->base_priority &
142239478Sadrian				    ~PTHREAD_SIGNAL_PRIORITY;
143239478Sadrian				new_thread->attr.prio =
144239478Sadrian				    curthread->base_priority &
145239478Sadrian				    ~PTHREAD_SIGNAL_PRIORITY;
146239478Sadrian				new_thread->attr.sched_policy =
147239478Sadrian				    curthread->attr.sched_policy;
148239478Sadrian			} else {
149239478Sadrian				/*
150239478Sadrian				 * Use just the thread priority, leaving the
151239478Sadrian				 * other scheduling attributes as their
152239478Sadrian				 * default values:
153239478Sadrian				 */
154239478Sadrian				new_thread->base_priority =
155239478Sadrian				    new_thread->attr.prio;
156239478Sadrian			}
157239478Sadrian			new_thread->active_priority = new_thread->base_priority;
158239478Sadrian			new_thread->inherited_priority = 0;
159239478Sadrian
160239478Sadrian			/* Initialize joiner to NULL (no joiner): */
161239478Sadrian			new_thread->joiner = NULL;
162239478Sadrian
163239478Sadrian			/* Initialize the mutex queue: */
164239478Sadrian			TAILQ_INIT(&new_thread->mutexq);
165239478Sadrian
166239478Sadrian			/* Initialise hooks in the thread structure: */
167239478Sadrian			new_thread->specific = NULL;
168239478Sadrian			new_thread->cleanup = NULL;
169239478Sadrian			new_thread->flags = 0;
170239478Sadrian			new_thread->continuation = NULL;
171239478Sadrian
172239478Sadrian			/*
173239478Sadrian			 * Defer signals to protect the scheduling queues
174239478Sadrian			 * from access by the signal handler:
175239478Sadrian			 */
176239478Sadrian			_thread_kern_sig_defer();
177239478Sadrian
178239478Sadrian			/*
179239478Sadrian			 * Initialise the unique id which GDB uses to
180239478Sadrian			 * track threads.
181239478Sadrian			 */
182239478Sadrian			new_thread->uniqueid = next_uniqueid++;
183239478Sadrian
184239478Sadrian			/*
185239478Sadrian			 * Check if the garbage collector thread
186239478Sadrian			 * needs to be started.
187239478Sadrian			 */
188239478Sadrian			f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial);
189239478Sadrian
190239478Sadrian			/* Add the thread to the linked list of all threads: */
191239478Sadrian			TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle);
192239478Sadrian
193239478Sadrian			if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) {
194239478Sadrian				new_thread->flags |= PTHREAD_FLAGS_SUSPENDED;
195239478Sadrian				new_thread->state = PS_SUSPENDED;
196239478Sadrian			} else {
197239478Sadrian				new_thread->state = PS_RUNNING;
198239478Sadrian				PTHREAD_PRIOQ_INSERT_TAIL(new_thread);
199239478Sadrian			}
200239478Sadrian
201239478Sadrian			/*
202239478Sadrian			 * Undefer and handle pending signals, yielding
203239478Sadrian			 * if necessary.
204239478Sadrian			 */
205239478Sadrian			_thread_kern_sig_undefer();
206239478Sadrian
207239478Sadrian			/* Return a pointer to the thread structure: */
208239478Sadrian			(*thread) = new_thread;
209239478Sadrian
210239478Sadrian			/* Schedule the new user thread: */
211239478Sadrian			_thread_kern_sched();
212239478Sadrian
213239478Sadrian			/*
214239478Sadrian			 * Start a garbage collector thread
215239478Sadrian			 * if necessary.
216239478Sadrian			 */
217239478Sadrian			if (f_gc && pthread_create(&gc_thread,NULL,
218239478Sadrian				    _thread_gc,NULL) != 0)
219239478Sadrian				PANIC("Can't create gc thread");
220239478Sadrian
221239478Sadrian		}
222239478Sadrian	}
223239478Sadrian
224239478Sadrian	/* Return the status: */
225239478Sadrian	return (ret);
226239478Sadrian}
227239478Sadrian
228239478Sadrianvoid
229239478Sadrian_thread_start(void)
230239478Sadrian{
231239478Sadrian	struct pthread	*curthread = _get_curthread();
232239478Sadrian
233239478Sadrian	/* We just left the scheduler via swapcontext: */
234239478Sadrian	_thread_kern_in_sched = 0;
235239478Sadrian
236239478Sadrian	/* Run the current thread's start routine with argument: */
237239478Sadrian	pthread_exit(curthread->start_routine(curthread->arg));
238239478Sadrian
239239478Sadrian	/* This point should never be reached. */
240239478Sadrian	PANIC("Thread has resumed after exit");
241239478Sadrian}
242239478Sadrian