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 static u_int64_t next_uniqueid = 1; 47 48 #define OFF(f) offsetof(struct pthread, f) 49 int _thread_next_offset = OFF(tle.tqe_next); 50 int _thread_uniqueid_offset = OFF(uniqueid); 51 int _thread_state_offset = OFF(state); 52 int _thread_name_offset = OFF(name); 53 int _thread_ctx_offset = OFF(ctx); 54 #undef OFF 55 56 int _thread_PS_RUNNING_value = PS_RUNNING; 57 int _thread_PS_DEAD_value = PS_DEAD; 58 59 __weak_reference(_pthread_create, pthread_create); 60 61 int 62 _pthread_create(pthread_t * thread, const pthread_attr_t * attr, 63 void *(*start_routine) (void *), void *arg) 64 { 65 struct itimerval itimer; 66 int f_gc = 0; 67 int ret = 0; 68 pthread_t gc_thread; 69 pthread_t new_thread; 70 pthread_attr_t pattr; 71 int flags; 72 void *stack; 73 74 /* 75 * Locking functions in libc are required when there are 76 * threads other than the initial thread. 77 */ 78 __isthreaded = 1; 79 80 /* Allocate memory for the thread structure: */ 81 if ((new_thread = (pthread_t) malloc(sizeof(struct pthread))) == NULL) 82 return (EAGAIN); 83 84 /* Check if default thread attributes are required: */ 85 if (attr == NULL || *attr == NULL) 86 pattr = &pthread_attr_default; 87 else 88 pattr = *attr; 89 90 /* Check if a stack was specified in the thread attributes: */ 91 if ((stack = pattr->stackaddr_attr) == NULL) { 92 stack = _thread_stack_alloc(pattr->stacksize_attr, 93 pattr->guardsize_attr); 94 if (stack == NULL) { 95 free(new_thread); 96 return (EAGAIN); 97 } 98 } 99 100 /* Initialise the thread structure: */ 101 memset(new_thread, 0, sizeof(struct pthread)); 102 new_thread->stack = stack; 103 new_thread->start_routine = start_routine; 104 new_thread->arg = arg; 105 106 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE | 107 PTHREAD_CANCEL_DEFERRED; 108 109 /* 110 * Write a magic value to the thread structure 111 * to help identify valid ones: 112 */ 113 new_thread->magic = PTHREAD_MAGIC; 114 115 /* Initialise the machine context: */ 116 getcontext(&new_thread->ctx); 117 new_thread->ctx.uc_stack.ss_sp = new_thread->stack; 118 new_thread->ctx.uc_stack.ss_size = pattr->stacksize_attr; 119 makecontext(&new_thread->ctx, _thread_start, 0); 120 new_thread->arch_id = _set_curthread(&new_thread->ctx, new_thread); 121 122 /* Copy the thread attributes: */ 123 memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr)); 124 125 /* 126 * Check if this thread is to inherit the scheduling 127 * attributes from its parent: 128 */ 129 if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) { 130 /* Copy the scheduling attributes: */ 131 new_thread->base_priority = curthread->base_priority & 132 ~PTHREAD_SIGNAL_PRIORITY; 133 new_thread->attr.prio = curthread->base_priority & 134 ~PTHREAD_SIGNAL_PRIORITY; 135 new_thread->attr.sched_policy = curthread->attr.sched_policy; 136 } else { 137 /* 138 * Use just the thread priority, leaving the 139 * other scheduling attributes as their 140 * default values: 141 */ 142 new_thread->base_priority = new_thread->attr.prio; 143 } 144 new_thread->active_priority = new_thread->base_priority; 145 new_thread->inherited_priority = 0; 146 147 /* Initialize joiner to NULL (no joiner): */ 148 new_thread->joiner = NULL; 149 150 /* Initialize the mutex queue: */ 151 TAILQ_INIT(&new_thread->mutexq); 152 153 /* Initialise hooks in the thread structure: */ 154 new_thread->specific = NULL; 155 new_thread->cleanup = NULL; 156 new_thread->flags = 0; 157 158 /* 159 * Protect the scheduling queues. 160 */ 161 GIANT_LOCK(curthread); 162 163 /* 164 * Initialise the unique id which GDB uses to 165 * track threads. 166 */ 167 new_thread->uniqueid = next_uniqueid++; 168 169 /* 170 * Check if the garbage collector thread 171 * needs to be started. 172 */ 173 f_gc = (TAILQ_FIRST(&_thread_list) == _thread_initial); 174 175 /* Add the thread to the linked list of all threads: */ 176 TAILQ_INSERT_HEAD(&_thread_list, new_thread, tle); 177 178 /* 179 * Create the thread. 180 * 181 */ 182 if (pattr->suspend == PTHREAD_FLAGS_SUSPENDED) 183 flags = THR_SUSPENDED; 184 else 185 flags = 0; 186 187 ret = thr_create(&new_thread->ctx, &new_thread->thr_id, flags); 188 189 if (ret != 0) { 190 _thread_printf(STDERR_FILENO, "thr_create() == %d\n", ret); 191 PANIC("thr_create"); 192 } 193 194 GIANT_UNLOCK(curthread); 195 196 /* Return a pointer to the thread structure: */ 197 (*thread) = new_thread; 198 199 /* 200 * Start a garbage collector thread 201 * if necessary. 202 */ 203 #if 0 204 if (f_gc && pthread_create(&gc_thread,NULL, _thread_gc,NULL) != 0) 205 PANIC("Can't create gc thread"); 206 #endif 207 208 return (0); 209 } 210 211 void 212 _thread_start(void) 213 { 214 215 /* Run the current thread's start routine with argument: */ 216 pthread_exit(curthread->start_routine(curthread->arg)); 217 218 /* This point should never be reached. */ 219 PANIC("Thread has resumed after exit"); 220 } 221