xref: /freebsd/lib/libthr/thread/thr_create.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stddef.h>
40 #include <sys/time.h>
41 #include <machine/reg.h>
42 #include <pthread.h>
43 #include "thr_private.h"
44 #include "libc_private.h"
45 
46 #define OFF(f)	offsetof(struct pthread, f)
47 int _thread_thr_id_offset		= OFF(thr_id);
48 int _thread_next_offset			= OFF(tle.tqe_next);
49 int _thread_name_offset			= OFF(name);
50 int _thread_ctx_offset			= OFF(ctx);
51 #undef OFF
52 
53 int _thread_PS_RUNNING_value		= PS_RUNNING;
54 int _thread_PS_DEAD_value		= PS_DEAD;
55 
56 __weak_reference(_pthread_create, pthread_create);
57 
58 int
59 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
60 	       void *(*start_routine) (void *), void *arg)
61 {
62 	int             ret = 0;
63 	pthread_t       new_thread;
64 	pthread_attr_t	pattr;
65 	int		flags;
66 	void           *stack;
67 
68 	/*
69 	 * Locking functions in libc are required when there are
70 	 * threads other than the initial thread.
71 	 */
72 	__isthreaded = 1;
73 
74 	/* Allocate memory for the thread structure: */
75 	if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL)
76 		return (EAGAIN);
77 
78 	/* Check if default thread attributes are required: */
79 	if (attr == NULL || *attr == NULL)
80 		pattr = &pthread_attr_default;
81 	else
82 		pattr = *attr;
83 
84 	/* Check if a stack was specified in the thread attributes: */
85 	if ((stack = pattr->stackaddr_attr) == NULL) {
86 		stack = _thread_stack_alloc(pattr->stacksize_attr,
87 		    pattr->guardsize_attr);
88 		if (stack == NULL) {
89 			free(new_thread);
90 			return (EAGAIN);
91 		}
92 	}
93 
94 	/* Initialise the thread structure: */
95 	init_td_common(new_thread, pattr, 0);
96 	new_thread->stack = stack;
97 	new_thread->start_routine = start_routine;
98 	new_thread->arg = arg;
99 
100 	/* Initialise the machine context: */
101 	getcontext(&new_thread->ctx);
102 	new_thread->savedsig = new_thread->ctx.uc_sigmask;
103 	new_thread->ctx.uc_stack.ss_sp = new_thread->stack;
104 	new_thread->ctx.uc_stack.ss_size = pattr->stacksize_attr;
105 	makecontext(&new_thread->ctx, (void (*)(void))_thread_start, 1, new_thread);
106 	new_thread->arch_id = _set_curthread(&new_thread->ctx, new_thread, &ret);
107 	if (ret != 0) {
108 		if (pattr->stackaddr_attr == NULL) {
109 			STACK_LOCK;
110 			_thread_stack_free(new_thread->stack,
111 			    pattr->stacksize_attr, pattr->guardsize_attr);
112 			STACK_UNLOCK;
113 		}
114 		free(new_thread);
115 		return (ret);
116 	}
117 
118 	/*
119 	 * Check if this thread is to inherit the scheduling
120 	 * attributes from its parent:
121 	 */
122 	if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
123 		/* Copy the scheduling attributes: */
124 		new_thread->base_priority = curthread->base_priority &
125 		    ~PTHREAD_SIGNAL_PRIORITY;
126 		new_thread->attr.prio = curthread->base_priority &
127 		    ~PTHREAD_SIGNAL_PRIORITY;
128 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
129 	} else {
130 		/*
131 		 * Use just the thread priority, leaving the
132 		 * other scheduling attributes as their
133 		 * default values:
134 		 */
135 		new_thread->base_priority = new_thread->attr.prio;
136 	}
137 	new_thread->active_priority = new_thread->base_priority;
138 
139 	THREAD_LIST_LOCK;
140 
141 	/* Add the thread to the linked list of all threads: */
142 	TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle);
143 
144 	/*
145 	 * Create the thread.
146 	 */
147 	if (pattr->suspend == PTHREAD_CREATE_SUSPENDED)
148 		new_thread->flags |= PTHREAD_FLAGS_SUSPENDED;
149 	/* new thread inherits signal mask in kernel */
150 	_thread_sigblock();
151 	ret = thr_create(&new_thread->ctx, &new_thread->thr_id, flags);
152 	/* restore my signal mask */
153 	_thread_sigunblock();
154 	if (ret != 0) {
155 		_thread_printf(STDERR_FILENO, "thr_create() == %d\n", ret);
156 		PANIC("thr_create");
157 	}
158 
159 	THREAD_LIST_UNLOCK;
160 
161 	/* Return a pointer to the thread structure: */
162 	(*thread) = new_thread;
163 
164 	return (0);
165 }
166 
167 void
168 _thread_start(pthread_t td)
169 {
170 	int ret;
171 
172 	/*
173 	 * for AMD64, we need to set fsbase by thread itself, before
174 	 * fsbase is set, we can not run any other code, for example
175 	 * signal code.
176 	 */
177 	_set_curthread(NULL, td, &ret);
178 
179 	/* restore signal mask inherited before */
180 	__sys_sigprocmask(SIG_SETMASK, &td->savedsig, NULL);
181 
182 	if ((curthread->flags & PTHREAD_FLAGS_SUSPENDED) != 0)
183 		_thread_suspend(curthread, NULL);
184 
185 	pthread_exit(curthread->start_routine(curthread->arg));
186 	/* This point should never be reached. */
187 	PANIC("Thread has resumed after exit");
188 }
189