thr_create.c revision 172491
1208149Snwhitehorn/* 2208149Snwhitehorn * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com> 3208149Snwhitehorn * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 4208149Snwhitehorn * All rights reserved. 5208149Snwhitehorn * 6208149Snwhitehorn * Redistribution and use in source and binary forms, with or without 7208149Snwhitehorn * modification, are permitted provided that the following conditions 8208149Snwhitehorn * are met: 9208149Snwhitehorn * 1. Redistributions of source code must retain the above copyright 10208149Snwhitehorn * notice, this list of conditions and the following disclaimer. 11208149Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 12208149Snwhitehorn * notice, this list of conditions and the following disclaimer in the 13208149Snwhitehorn * documentation and/or other materials provided with the distribution. 14208149Snwhitehorn * 3. Neither the name of the author nor the names of any co-contributors 15208149Snwhitehorn * may be used to endorse or promote products derived from this software 16208149Snwhitehorn * without specific prior written permission. 17208149Snwhitehorn * 18208149Snwhitehorn * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 19208149Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20208149Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21208149Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22208149Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23208149Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24208149Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25208149Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26227843Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27227843Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28227843Smarius * SUCH DAMAGE. 29208149Snwhitehorn * 30208149Snwhitehorn * $FreeBSD: head/lib/libkse/thread/thr_create.c 165967 2007-01-12 07:26:21Z imp $ 31208149Snwhitehorn */ 32208149Snwhitehorn#include <errno.h> 33208149Snwhitehorn#include <stdlib.h> 34208149Snwhitehorn#include <string.h> 35208149Snwhitehorn#include <fcntl.h> 36208149Snwhitehorn#include <unistd.h> 37208149Snwhitehorn#include <stddef.h> 38208149Snwhitehorn#include <sys/time.h> 39208149Snwhitehorn#include <machine/reg.h> 40208149Snwhitehorn#include <pthread.h> 41208149Snwhitehorn#include "thr_private.h" 42208149Snwhitehorn#include "libc_private.h" 43208149Snwhitehorn 44208149SnwhitehornLT10_COMPAT_PRIVATE(_pthread_create); 45208149SnwhitehornLT10_COMPAT_DEFAULT(pthread_create); 46208149Snwhitehorn 47208149Snwhitehornstatic void free_thread(struct pthread *curthread, struct pthread *thread); 48208149Snwhitehornstatic int create_stack(struct pthread_attr *pattr); 49208149Snwhitehornstatic void free_stack(struct pthread_attr *pattr); 50208149Snwhitehornstatic void thread_start(struct pthread *curthread, 51208149Snwhitehorn void *(*start_routine) (void *), void *arg); 52230993Snwhitehorn 53208149Snwhitehorn__weak_reference(_pthread_create, pthread_create); 54208149Snwhitehorn 55208149Snwhitehorn/* 56208149Snwhitehorn * Some notes on new thread creation and first time initializion 57208149Snwhitehorn * to enable multi-threading. 58208149Snwhitehorn * 59208149Snwhitehorn * There are basically two things that need to be done. 60208149Snwhitehorn * 61208149Snwhitehorn * 1) The internal library variables must be initialized. 62208149Snwhitehorn * 2) Upcalls need to be enabled to allow multiple threads 63208149Snwhitehorn * to be run. 64208149Snwhitehorn * 65208149Snwhitehorn * The first may be done as a result of other pthread functions 66208149Snwhitehorn * being called. When _thr_initial is null, _libpthread_init is 67208149Snwhitehorn * called to initialize the internal variables; this also creates 68208149Snwhitehorn * or sets the initial thread. It'd be nice to automatically 69208149Snwhitehorn * have _libpthread_init called on program execution so we don't 70208149Snwhitehorn * have to have checks throughout the library. 71208149Snwhitehorn * 72208149Snwhitehorn * The second part is only triggered by the creation of the first 73208149Snwhitehorn * thread (other than the initial/main thread). If the thread 74208149Snwhitehorn * being created is a scope system thread, then a new KSE/KSEG 75208149Snwhitehorn * pair needs to be allocated. Also, if upcalls haven't been 76208149Snwhitehorn * enabled on the initial thread's KSE, they must be now that 77208149Snwhitehorn * there is more than one thread; this could be delayed until 78208149Snwhitehorn * the initial KSEG has more than one thread. 79208149Snwhitehorn */ 80208149Snwhitehornint 81208149Snwhitehorn_pthread_create(pthread_t * thread, const pthread_attr_t * attr, 82208149Snwhitehorn void *(*start_routine) (void *), void *arg) 83208149Snwhitehorn{ 84208149Snwhitehorn struct pthread *curthread, *new_thread; 85208149Snwhitehorn struct kse *kse = NULL; 86208149Snwhitehorn struct kse_group *kseg = NULL; 87208149Snwhitehorn kse_critical_t crit; 88208149Snwhitehorn int ret = 0; 89208149Snwhitehorn 90208149Snwhitehorn if (_thr_initial == NULL) 91208149Snwhitehorn _libpthread_init(NULL); 92208149Snwhitehorn 93208149Snwhitehorn /* 94227843Smarius * Turn on threaded mode, if failed, it is unnecessary to 95208149Snwhitehorn * do further work. 96208149Snwhitehorn */ 97208149Snwhitehorn if (_kse_isthreaded() == 0 && _kse_setthreaded(1)) { 98208149Snwhitehorn return (EAGAIN); 99230993Snwhitehorn } 100230993Snwhitehorn curthread = _get_curthread(); 101266160Sian 102208149Snwhitehorn /* 103208149Snwhitehorn * Allocate memory for the thread structure. 104208149Snwhitehorn * Some functions use malloc, so don't put it 105208149Snwhitehorn * in a critical region. 106208149Snwhitehorn */ 107208149Snwhitehorn if ((new_thread = _thr_alloc(curthread)) == NULL) { 108208149Snwhitehorn /* Insufficient memory to create a thread: */ 109208149Snwhitehorn ret = EAGAIN; 110208149Snwhitehorn } else { 111208149Snwhitehorn /* Check if default thread attributes are required: */ 112208149Snwhitehorn if (attr == NULL || *attr == NULL) 113208149Snwhitehorn /* Use the default thread attributes: */ 114208149Snwhitehorn new_thread->attr = _pthread_attr_default; 115208149Snwhitehorn else { 116208149Snwhitehorn new_thread->attr = *(*attr); 117208149Snwhitehorn if ((*attr)->sched_inherit == PTHREAD_INHERIT_SCHED) { 118208149Snwhitehorn /* inherit scheduling contention scop */ 119208149Snwhitehorn if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM) 120208149Snwhitehorn new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 121208149Snwhitehorn else 122208149Snwhitehorn new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM; 123208149Snwhitehorn /* 124208149Snwhitehorn * scheduling policy and scheduling parameters will be 125208149Snwhitehorn * inherited in following code. 126208149Snwhitehorn */ 127208149Snwhitehorn } 128208149Snwhitehorn } 129208149Snwhitehorn if (_thread_scope_system > 0) 130208149Snwhitehorn new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 131208149Snwhitehorn else if ((_thread_scope_system < 0) 132208149Snwhitehorn && (thread != &_thr_sig_daemon)) 133208149Snwhitehorn new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM; 134208149Snwhitehorn if (create_stack(&new_thread->attr) != 0) { 135208149Snwhitehorn /* Insufficient memory to create a stack: */ 136208149Snwhitehorn ret = EAGAIN; 137266019Sian _thr_free(curthread, new_thread); 138266019Sian } 139266019Sian else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) && 140208149Snwhitehorn (((kse = _kse_alloc(curthread, 1)) == NULL) 141208149Snwhitehorn || ((kseg = _kseg_alloc(curthread)) == NULL))) { 142208149Snwhitehorn /* Insufficient memory to create a new KSE/KSEG: */ 143208149Snwhitehorn ret = EAGAIN; 144208149Snwhitehorn if (kse != NULL) { 145208149Snwhitehorn kse->k_kcb->kcb_kmbx.km_flags |= KMF_DONE; 146208149Snwhitehorn _kse_free(curthread, kse); 147208149Snwhitehorn } 148208149Snwhitehorn free_stack(&new_thread->attr); 149208149Snwhitehorn _thr_free(curthread, new_thread); 150208149Snwhitehorn } 151208149Snwhitehorn else { 152208149Snwhitehorn if (kseg != NULL) { 153208149Snwhitehorn /* Add the KSE to the KSEG's list of KSEs. */ 154266019Sian TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_kgqe); 155266019Sian kseg->kg_ksecount = 1; 156266019Sian kse->k_kseg = kseg; 157266019Sian kse->k_schedq = &kseg->kg_schedq; 158266019Sian } 159266019Sian /* 160266019Sian * Write a magic value to the thread structure 161208149Snwhitehorn * to help identify valid ones: 162208149Snwhitehorn */ 163266019Sian new_thread->magic = THR_MAGIC; 164266019Sian 165266019Sian new_thread->slice_usec = -1; 166230993Snwhitehorn new_thread->start_routine = start_routine; 167208149Snwhitehorn new_thread->arg = arg; 168208149Snwhitehorn new_thread->cancelflags = PTHREAD_CANCEL_ENABLE | 169208149Snwhitehorn PTHREAD_CANCEL_DEFERRED; 170208149Snwhitehorn 171208149Snwhitehorn /* No thread is wanting to join to this one: */ 172208149Snwhitehorn new_thread->joiner = NULL; 173208149Snwhitehorn 174208149Snwhitehorn /* 175208149Snwhitehorn * Initialize the machine context. 176208149Snwhitehorn * Enter a critical region to get consistent context. 177208149Snwhitehorn */ 178208149Snwhitehorn crit = _kse_critical_enter(); 179208149Snwhitehorn THR_GETCONTEXT(&new_thread->tcb->tcb_tmbx.tm_context); 180208149Snwhitehorn /* Initialize the thread for signals: */ 181208149Snwhitehorn new_thread->sigmask = curthread->sigmask; 182208149Snwhitehorn _kse_critical_leave(crit); 183208149Snwhitehorn 184208149Snwhitehorn new_thread->tcb->tcb_tmbx.tm_udata = new_thread; 185208149Snwhitehorn new_thread->tcb->tcb_tmbx.tm_context.uc_sigmask = 186208149Snwhitehorn new_thread->sigmask; 187208149Snwhitehorn new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_size = 188208149Snwhitehorn new_thread->attr.stacksize_attr; 189208149Snwhitehorn new_thread->tcb->tcb_tmbx.tm_context.uc_stack.ss_sp = 190208149Snwhitehorn new_thread->attr.stackaddr_attr; 191208149Snwhitehorn makecontext(&new_thread->tcb->tcb_tmbx.tm_context, 192208149Snwhitehorn (void (*)(void))thread_start, 3, new_thread, 193208149Snwhitehorn start_routine, arg); 194208149Snwhitehorn /* 195208149Snwhitehorn * Check if this thread is to inherit the scheduling 196208149Snwhitehorn * attributes from its parent: 197208149Snwhitehorn */ 198208149Snwhitehorn if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) { 199208149Snwhitehorn /* 200208149Snwhitehorn * Copy the scheduling attributes. 201208149Snwhitehorn * Lock the scheduling lock to get consistent 202208149Snwhitehorn * scheduling parameters. 203208149Snwhitehorn */ 204208149Snwhitehorn THR_SCHED_LOCK(curthread, curthread); 205208149Snwhitehorn new_thread->base_priority = 206208149Snwhitehorn curthread->base_priority & 207208149Snwhitehorn ~THR_SIGNAL_PRIORITY; 208208149Snwhitehorn new_thread->attr.prio = 209208149Snwhitehorn curthread->base_priority & 210208149Snwhitehorn ~THR_SIGNAL_PRIORITY; 211208149Snwhitehorn new_thread->attr.sched_policy = 212208149Snwhitehorn curthread->attr.sched_policy; 213208149Snwhitehorn THR_SCHED_UNLOCK(curthread, curthread); 214208149Snwhitehorn } else { 215208149Snwhitehorn /* 216208149Snwhitehorn * Use just the thread priority, leaving the 217208149Snwhitehorn * other scheduling attributes as their 218208149Snwhitehorn * default values: 219208149Snwhitehorn */ 220208149Snwhitehorn new_thread->base_priority = 221208149Snwhitehorn new_thread->attr.prio; 222208149Snwhitehorn } 223208149Snwhitehorn new_thread->active_priority = new_thread->base_priority; 224208149Snwhitehorn new_thread->inherited_priority = 0; 225208149Snwhitehorn 226208149Snwhitehorn /* Initialize the mutex queue: */ 227208149Snwhitehorn TAILQ_INIT(&new_thread->mutexq); 228230993Snwhitehorn 229230993Snwhitehorn /* Initialise hooks in the thread structure: */ 230208149Snwhitehorn new_thread->specific = NULL; 231208149Snwhitehorn new_thread->specific_data_count = 0; 232208149Snwhitehorn new_thread->cleanup = NULL; 233208149Snwhitehorn new_thread->flags = 0; 234208149Snwhitehorn new_thread->tlflags = 0; 235208149Snwhitehorn new_thread->sigbackout = NULL; 236208149Snwhitehorn new_thread->continuation = NULL; 237208149Snwhitehorn new_thread->wakeup_time.tv_sec = -1; 238208149Snwhitehorn new_thread->lock_switch = 0; 239208149Snwhitehorn sigemptyset(&new_thread->sigpend); 240208149Snwhitehorn new_thread->check_pending = 0; 241230993Snwhitehorn new_thread->locklevel = 0; 242208149Snwhitehorn new_thread->rdlock_count = 0; 243208149Snwhitehorn new_thread->sigstk.ss_sp = 0; 244208149Snwhitehorn new_thread->sigstk.ss_size = 0; 245208149Snwhitehorn new_thread->sigstk.ss_flags = SS_DISABLE; 246208149Snwhitehorn new_thread->oldsigmask = NULL; 247208149Snwhitehorn 248208149Snwhitehorn if (new_thread->attr.suspend == THR_CREATE_SUSPENDED) { 249208149Snwhitehorn new_thread->state = PS_SUSPENDED; 250208149Snwhitehorn new_thread->flags = THR_FLAGS_SUSPENDED; 251208149Snwhitehorn } 252208149Snwhitehorn else 253208149Snwhitehorn new_thread->state = PS_RUNNING; 254208149Snwhitehorn 255208149Snwhitehorn /* 256208149Snwhitehorn * System scope threads have their own kse and 257208149Snwhitehorn * kseg. Process scope threads are all hung 258208149Snwhitehorn * off the main process kseg. 259208149Snwhitehorn */ 260208149Snwhitehorn if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) { 261208149Snwhitehorn new_thread->kseg = _kse_initial->k_kseg; 262208149Snwhitehorn new_thread->kse = _kse_initial; 263208149Snwhitehorn } 264208149Snwhitehorn else { 265 kse->k_curthread = NULL; 266 kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD; 267 new_thread->kse = kse; 268 new_thread->kseg = kse->k_kseg; 269 kse->k_kcb->kcb_kmbx.km_udata = kse; 270 kse->k_kcb->kcb_kmbx.km_curthread = NULL; 271 } 272 273 /* 274 * Schedule the new thread starting a new KSEG/KSE 275 * pair if necessary. 276 */ 277 ret = _thr_schedule_add(curthread, new_thread); 278 if (ret != 0) 279 free_thread(curthread, new_thread); 280 else { 281 /* Return a pointer to the thread structure: */ 282 (*thread) = new_thread; 283 } 284 } 285 } 286 287 /* Return the status: */ 288 return (ret); 289} 290 291static void 292free_thread(struct pthread *curthread, struct pthread *thread) 293{ 294 free_stack(&thread->attr); 295 if ((thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) { 296 /* Free the KSE and KSEG. */ 297 _kseg_free(thread->kseg); 298 _kse_free(curthread, thread->kse); 299 } 300 _thr_free(curthread, thread); 301} 302 303static int 304create_stack(struct pthread_attr *pattr) 305{ 306 int ret; 307 308 /* Check if a stack was specified in the thread attributes: */ 309 if ((pattr->stackaddr_attr) != NULL) { 310 pattr->guardsize_attr = 0; 311 pattr->flags |= THR_STACK_USER; 312 ret = 0; 313 } 314 else 315 ret = _thr_stack_alloc(pattr); 316 return (ret); 317} 318 319static void 320free_stack(struct pthread_attr *pattr) 321{ 322 struct kse *curkse; 323 kse_critical_t crit; 324 325 if ((pattr->flags & THR_STACK_USER) == 0) { 326 crit = _kse_critical_enter(); 327 curkse = _get_curkse(); 328 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock); 329 /* Stack routines don't use malloc/free. */ 330 _thr_stack_free(pattr); 331 KSE_LOCK_RELEASE(curkse, &_thread_list_lock); 332 _kse_critical_leave(crit); 333 } 334} 335 336static void 337thread_start(struct pthread *curthread, void *(*start_routine) (void *), 338 void *arg) 339{ 340 /* Run the current thread's start routine with argument: */ 341 pthread_exit(start_routine(arg)); 342 343 /* This point should never be reached. */ 344 PANIC("Thread has resumed after exit"); 345} 346