xref: /freebsd-src/lib/libthr/thread/thr_create.c (revision 17ed18fef3ee30b72f8f88ef7b43d082ae8d8bf1)
1 /*
2  * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/types.h>
31 #include <sys/signalvar.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stddef.h>
36 #include <pthread.h>
37 
38 #include "thr_private.h"
39 
40 static void free_thread(struct pthread *curthread, struct pthread *thread);
41 static int  create_stack(struct pthread_attr *pattr);
42 static void free_stack(struct pthread *curthread, struct pthread_attr *pattr);
43 static void thread_start(struct pthread *curthread);
44 
45 __weak_reference(_pthread_create, pthread_create);
46 
47 int
48 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
49 	       void *(*start_routine) (void *), void *arg)
50 {
51 	struct pthread *curthread, *new_thread;
52 	struct thr_param param;
53 	int ret = 0, locked;
54 
55 	_thr_check_init();
56 
57 	/*
58 	 * Tell libc and others now they need lock to protect their data.
59 	 */
60 	if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
61 		return (EAGAIN);
62 
63 	curthread = _get_curthread();
64 	if ((new_thread = _thr_alloc(curthread)) == NULL)
65 		return (EAGAIN);
66 
67 	memset(&param, 0, sizeof(param));
68 
69 	if (attr == NULL || *attr == NULL)
70 		/* Use the default thread attributes: */
71 		new_thread->attr = _pthread_attr_default;
72 	else
73 		new_thread->attr = *(*attr);
74 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
75 		/* inherit scheduling contention scope */
76 		if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
77 			new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
78 		else
79 			new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
80 		/*
81 		 * scheduling policy and scheduling parameters will be
82 		 * inherited in following code.
83 		 */
84 	}
85 
86 	if (_thr_scope_system > 0)
87 		new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
88 	else if (_thr_scope_system < 0)
89 		new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
90 
91 	new_thread->tid = TID_TERMINATED;
92 
93 	if (create_stack(&new_thread->attr) != 0) {
94 		/* Insufficient memory to create a stack: */
95 		_thr_free(curthread, new_thread);
96 		return (EAGAIN);
97 	}
98 	/*
99 	 * Write a magic value to the thread structure
100 	 * to help identify valid ones:
101 	 */
102 	new_thread->magic = THR_MAGIC;
103 	new_thread->start_routine = start_routine;
104 	new_thread->arg = arg;
105 	new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
106 	    PTHREAD_CANCEL_DEFERRED;
107 	/*
108 	 * Check if this thread is to inherit the scheduling
109 	 * attributes from its parent:
110 	 */
111 	if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
112 		/*
113 		 * Copy the scheduling attributes. Lock the scheduling
114 		 * lock to get consistent scheduling parameters.
115 		 */
116 		THR_LOCK(curthread);
117 		new_thread->base_priority = curthread->base_priority;
118 		new_thread->attr.prio = curthread->base_priority;
119 		new_thread->attr.sched_policy = curthread->attr.sched_policy;
120 		THR_UNLOCK(curthread);
121 	} else {
122 		/*
123 		 * Use just the thread priority, leaving the
124 		 * other scheduling attributes as their
125 		 * default values:
126 		 */
127 		new_thread->base_priority = new_thread->attr.prio;
128 	}
129 	new_thread->active_priority = new_thread->base_priority;
130 
131 	/* Initialize the mutex queue: */
132 	TAILQ_INIT(&new_thread->mutexq);
133 	TAILQ_INIT(&new_thread->pri_mutexq);
134 
135 	/* Initialise hooks in the thread structure: */
136 	if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
137 		new_thread->flags = THR_FLAGS_NEED_SUSPEND;
138 	new_thread->state = PS_RUNNING;
139 
140 	/* Add the new thread. */
141 	_thr_link(curthread, new_thread);
142 	/* Return thread pointer eariler so that new thread can use it. */
143 	(*thread) = new_thread;
144 	if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
145 		THR_THREAD_LOCK(curthread, new_thread);
146 		locked = 1;
147 	} else
148 		locked = 0;
149 	param.start_func = (void (*)(void *)) thread_start;
150 	param.arg = new_thread;
151 	param.stack_base = new_thread->attr.stackaddr_attr;
152 	param.stack_size = new_thread->attr.stacksize_attr;
153 	param.tls_base = (char *)new_thread->tcb;
154 	param.tls_size = sizeof(struct tcb);
155 	param.child_tid = &new_thread->tid;
156 	param.parent_tid = &new_thread->tid;
157 	param.flags = 0;
158 	if (new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM)
159 		param.flags |= THR_SYSTEM_SCOPE;
160 	/* Schedule the new thread. */
161 	ret = thr_new(&param, sizeof(param));
162 	if (ret != 0) {
163 		if (locked)
164 			THR_THREAD_UNLOCK(curthread, new_thread);
165 		_thr_unlink(curthread, new_thread);
166 		free_thread(curthread, new_thread);
167 		(*thread) = 0;
168 		ret = EAGAIN;
169 	} else if (locked) {
170 		_thr_report_creation(curthread, new_thread);
171 		THR_THREAD_UNLOCK(curthread, new_thread);
172 	}
173 	return (ret);
174 }
175 
176 static void
177 free_thread(struct pthread *curthread, struct pthread *thread)
178 {
179 	free_stack(curthread, &thread->attr);
180 	curthread->tid = TID_TERMINATED;
181 	_thr_free(curthread, thread);
182 }
183 
184 static int
185 create_stack(struct pthread_attr *pattr)
186 {
187 	int ret;
188 
189 	/* Check if a stack was specified in the thread attributes: */
190 	if ((pattr->stackaddr_attr) != NULL) {
191 		pattr->guardsize_attr = 0;
192 		pattr->flags |= THR_STACK_USER;
193 		ret = 0;
194 	}
195 	else
196 		ret = _thr_stack_alloc(pattr);
197 	return (ret);
198 }
199 
200 static void
201 free_stack(struct pthread *curthread, struct pthread_attr *pattr)
202 {
203 	if ((pattr->flags & THR_STACK_USER) == 0) {
204 		THREAD_LIST_LOCK(curthread);
205 		/* Stack routines don't use malloc/free. */
206 		_thr_stack_free(pattr);
207 		THREAD_LIST_UNLOCK(curthread);
208 	}
209 }
210 
211 static void
212 thread_start(struct pthread *curthread)
213 {
214 	if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
215 		_thr_suspend_check(curthread);
216 
217 	THR_LOCK(curthread);
218 	THR_UNLOCK(curthread);
219 
220 	/* Run the current thread's start routine with argument: */
221 	pthread_exit(curthread->start_routine(curthread->arg));
222 
223 	/* This point should never be reached. */
224 	PANIC("Thread has resumed after exit");
225 }
226