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: head/lib/libkse/thread/thr_create.c 118514 2003-08-06 00:23:40Z marcel $
|
33 * $FreeBSD: head/lib/libkse/thread/thr_create.c 120074 2003-09-14 22:52:16Z davidxu $ |
34 */ 35#include <errno.h> 36#include <stdlib.h> 37#include <string.h> 38#include <fcntl.h> 39#include <unistd.h> 40#include <stddef.h> 41#include <sys/time.h> 42#include <machine/reg.h> 43#include <pthread.h> 44#include "thr_private.h" 45#include "libc_private.h" 46 47#define OFF(f) offsetof(struct pthread, f) 48int _thread_next_offset = OFF(tle.tqe_next); 49int _thread_uniqueid_offset = OFF(uniqueid); 50int _thread_state_offset = OFF(state); 51int _thread_name_offset = OFF(name); 52void *_thread_tcb_offset = OFF(tcb); 53#undef OFF 54#define OFF(f) offsetof(struct tcb, f) 55int _thread_ctx_offset = OFF(tcb_tmbx.tm_context); 56#undef OFF 57 58int _thread_PS_RUNNING_value = PS_RUNNING; 59int _thread_PS_DEAD_value = PS_DEAD; 60 61static void free_thread(struct pthread *curthread, struct pthread *thread); 62static int create_stack(struct pthread_attr *pattr); 63static void free_stack(struct pthread_attr *pattr); 64static void thread_start(struct pthread *curthread, 65 void *(*start_routine) (void *), void *arg); 66 67__weak_reference(_pthread_create, pthread_create); 68 69/* 70 * Some notes on new thread creation and first time initializion 71 * to enable multi-threading. 72 * 73 * There are basically two things that need to be done. 74 * 75 * 1) The internal library variables must be initialized. 76 * 2) Upcalls need to be enabled to allow multiple threads 77 * to be run. 78 * 79 * The first may be done as a result of other pthread functions 80 * being called. When _thr_initial is null, _libpthread_init is 81 * called to initialize the internal variables; this also creates 82 * or sets the initial thread. It'd be nice to automatically 83 * have _libpthread_init called on program execution so we don't 84 * have to have checks throughout the library. 85 * 86 * The second part is only triggered by the creation of the first 87 * thread (other than the initial/main thread). If the thread 88 * being created is a scope system thread, then a new KSE/KSEG 89 * pair needs to be allocated. Also, if upcalls haven't been 90 * enabled on the initial thread's KSE, they must be now that 91 * there is more than one thread; this could be delayed until 92 * the initial KSEG has more than one thread. 93 */ 94int 95_pthread_create(pthread_t * thread, const pthread_attr_t * attr, 96 void *(*start_routine) (void *), void *arg) 97{ 98 struct pthread *curthread, *new_thread; 99 struct kse *kse = NULL; 100 struct kse_group *kseg = NULL; 101 kse_critical_t crit;
|
102 int i;
|
102 int ret = 0; 103 104 if (_thr_initial == NULL) 105 _libpthread_init(NULL); 106 107 /* 108 * Turn on threaded mode, if failed, it is unnecessary to 109 * do further work. 110 */ 111 if (_kse_isthreaded() == 0 && _kse_setthreaded(1)) { 112 return (EAGAIN); 113 } 114 curthread = _get_curthread(); 115 116 /* 117 * Allocate memory for the thread structure. 118 * Some functions use malloc, so don't put it 119 * in a critical region. 120 */ 121 if ((new_thread = _thr_alloc(curthread)) == NULL) { 122 /* Insufficient memory to create a thread: */ 123 ret = EAGAIN; 124 } else { 125 /* Check if default thread attributes are required: */ 126 if (attr == NULL || *attr == NULL) 127 /* Use the default thread attributes: */ 128 new_thread->attr = _pthread_attr_default;
|
130 else
|
129 else { |
130 new_thread->attr = *(*attr);
|
131 if ((*attr)->sched_inherit == PTHREAD_INHERIT_SCHED) { 132 /* inherit scheduling contention scop */ 133 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) 134 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 135 else 136 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM; 137 /* 138 * scheduling policy and scheduling parameters will be 139 * inherited in following code. 140 */ 141 } 142 } |
143#ifdef SYSTEM_SCOPE_ONLY 144 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 145#endif 146 if (create_stack(&new_thread->attr) != 0) { 147 /* Insufficient memory to create a stack: */ 148 ret = EAGAIN; 149 _thr_free(curthread, new_thread); 150 } 151 else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) && 152 (((kse = _kse_alloc(curthread, 1)) == NULL) 153 || ((kseg = _kseg_alloc(curthread)) == NULL))) { 154 /* Insufficient memory to create a new KSE/KSEG: */ 155 ret = EAGAIN; 156 if (kse != NULL) { 157 kse->k_kcb->kcb_kmbx.km_flags |= KMF_DONE; 158 _kse_free(curthread, kse); 159 } 160 free_stack(&new_thread->attr); 161 _thr_free(curthread, new_thread); 162 } 163 else { 164 if (kseg != NULL) { 165 /* Add the KSE to the KSEG's list of KSEs. */ 166 TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_kgqe); 167 kseg->kg_ksecount = 1; 168 kse->k_kseg = kseg; 169 kse->k_schedq = &kseg->kg_schedq; 170 } 171 /* 172 * Write a magic value to the thread structure 173 * to help identify valid ones: 174 */ 175 new_thread->magic = THR_MAGIC; 176 177 new_thread->slice_usec = -1; 178 new_thread->start_routine = start_routine; 179 new_thread->arg = arg; 180 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE | 181 PTHREAD_CANCEL_DEFERRED; 182 183 /* No thread is wanting to join to this one: */ 184 new_thread->joiner = NULL; 185 186 /* Initialize the signal frame: */ 187 new_thread->curframe = NULL; 188 189 /* 190 * Initialize the machine context. 191 * Enter a critical region to get consistent context. 192 */ 193 crit = _kse_critical_enter(); 194 THR_GETCONTEXT(&new_thread->tcb->tcb_tmbx.tm_context); 195 /* Initialize the thread for signals: */ 196 new_thread->sigmask = curthread->sigmask; 197 _kse_critical_leave(crit); 198 199 new_thread->tcb->tcb_tmbx.tm_udata = new_thread; 200 new_thread->tcb->tcb_tmbx.tm_context.uc_sigmask = 201 new_thread->sigmask; 202 new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size = 203 new_thread->attr.stacksize_attr; 204 new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp = 205 new_thread->attr.stackaddr_attr; 206 makecontext(&new_thread->tcb->tcb_tmbx.tm_context, 207 (void (*)(void))thread_start, 3, new_thread, 208 start_routine, arg); 209 /* 210 * Check if this thread is to inherit the scheduling 211 * attributes from its parent: 212 */
|
202 if ((new_thread->attr.flags & PTHREAD_INHERIT_SCHED) != 0) {
|
213 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) { |
214 /* 215 * Copy the scheduling attributes. 216 * Lock the scheduling lock to get consistent 217 * scheduling parameters. 218 */ 219 THR_SCHED_LOCK(curthread, curthread); 220 new_thread->base_priority = 221 curthread->base_priority & 222 ~THR_SIGNAL_PRIORITY; 223 new_thread->attr.prio = 224 curthread->base_priority & 225 ~THR_SIGNAL_PRIORITY; 226 new_thread->attr.sched_policy = 227 curthread->attr.sched_policy; 228 THR_SCHED_UNLOCK(curthread, curthread); 229 } else { 230 /* 231 * Use just the thread priority, leaving the 232 * other scheduling attributes as their 233 * default values: 234 */ 235 new_thread->base_priority = 236 new_thread->attr.prio; 237 } 238 new_thread->active_priority = new_thread->base_priority; 239 new_thread->inherited_priority = 0; 240 241 /* Initialize the mutex queue: */ 242 TAILQ_INIT(&new_thread->mutexq); 243
|
233 /*
234 * Initialize thread locking.
235 * Lock initializing needs malloc, so don't
236 * enter critical region before doing this!
237 */
238 if (_lock_init(&new_thread->lock, LCK_ADAPTIVE,
239 _thr_lock_wait, _thr_lock_wakeup) != 0)
240 PANIC("Cannot initialize thread lock");
241 for (i = 0; i < MAX_THR_LOCKLEVEL; i++) {
242 _lockuser_init(&new_thread->lockusers[i],
243 (void *)new_thread);
244 _LCK_SET_PRIVATE2(&new_thread->lockusers[i],
245 (void *)new_thread);
246 }
247
|
244 /* Initialise hooks in the thread structure: */ 245 new_thread->specific = NULL;
|
246 new_thread->specific_data_count = 0; |
247 new_thread->cleanup = NULL; 248 new_thread->flags = 0; 249 new_thread->continuation = NULL;
|
250 new_thread->wakeup_time.tv_sec = -1; 251 new_thread->lock_switch = 0; 252 sigemptyset(&new_thread->sigpend); 253 new_thread->check_pending = 0; 254 new_thread->locklevel = 0; |
255 256 if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) { 257 new_thread->state = PS_SUSPENDED; 258 new_thread->flags = THR_FLAGS_SUSPENDED; 259 } 260 else 261 new_thread->state = PS_RUNNING; 262 263 /* 264 * System scope threads have their own kse and 265 * kseg. Process scope threads are all hung 266 * off the main process kseg. 267 */ 268 if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) { 269 new_thread->kseg = _kse_initial->k_kseg; 270 new_thread->kse = _kse_initial; 271 } 272 else { 273 kse->k_curthread = NULL; 274 kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD; 275 new_thread->kse = kse; 276 new_thread->kseg = kse->k_kseg; 277 kse->k_kcb->kcb_kmbx.km_udata = kse; 278 kse->k_kcb->kcb_kmbx.km_curthread = NULL; 279 } 280 281 /* 282 * Schedule the new thread starting a new KSEG/KSE 283 * pair if necessary. 284 */ 285 ret = _thr_schedule_add(curthread, new_thread); 286 if (ret != 0) 287 free_thread(curthread, new_thread); 288 else { 289 /* Return a pointer to the thread structure: */ 290 (*thread) = new_thread; 291 } 292 } 293 } 294 295 /* Return the status: */ 296 return (ret); 297} 298 299static void 300free_thread(struct pthread *curthread, struct pthread *thread) 301{ 302 free_stack(&thread->attr); 303 if ((thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) { 304 /* Free the KSE and KSEG. */ 305 _kseg_free(thread->kseg); 306 _kse_free(curthread, thread->kse); 307 } 308 _thr_free(curthread, thread); 309} 310 311static int 312create_stack(struct pthread_attr *pattr) 313{ 314 int ret; 315 316 /* Check if a stack was specified in the thread attributes: */ 317 if ((pattr->stackaddr_attr) != NULL) { 318 pattr->guardsize_attr = 0; 319 pattr->flags |= THR_STACK_USER; 320 ret = 0; 321 } 322 else 323 ret = _thr_stack_alloc(pattr); 324 return (ret); 325} 326 327static void 328free_stack(struct pthread_attr *pattr) 329{ 330 struct kse *curkse; 331 kse_critical_t crit; 332 333 if ((pattr->flags & THR_STACK_USER) == 0) { 334 crit = _kse_critical_enter(); 335 curkse = _get_curkse(); 336 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock); 337 /* Stack routines don't use malloc/free. */ 338 _thr_stack_free(pattr); 339 KSE_LOCK_RELEASE(curkse, &_thread_list_lock); 340 _kse_critical_leave(crit); 341 } 342} 343 344static void 345thread_start(struct pthread *curthread, void *(*start_routine) (void *), 346 void *arg) 347{ 348 /* Run the current thread's start routine with argument: */ 349 pthread_exit(start_routine(arg)); 350 351 /* This point should never be reached. */ 352 PANIC("Thread has resumed after exit"); 353}
|