thr_create.c (144518) | thr_create.c (144921) |
---|---|
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: --- 16 unchanged lines hidden (view full) --- 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 * | 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: --- 16 unchanged lines hidden (view full) --- 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/libthr/thread/thr_create.c 144518 2005-04-02 01:20:00Z davidxu $ | 33 * $FreeBSD: head/lib/libthr/thread/thr_create.c 144921 2005-04-12 03:00:28Z davidxu $ |
34 */ 35 36#include <errno.h> 37#include <stdlib.h> 38#include <string.h> 39#include <stddef.h> 40#include <pthread.h> 41#include <sys/signalvar.h> --- 9 unchanged lines hidden (view full) --- 51 52int 53_pthread_create(pthread_t * thread, const pthread_attr_t * attr, 54 void *(*start_routine) (void *), void *arg) 55{ 56 ucontext_t uc; 57 sigset_t sigmask, oldsigmask; 58 struct pthread *curthread, *new_thread; | 34 */ 35 36#include <errno.h> 37#include <stdlib.h> 38#include <string.h> 39#include <stddef.h> 40#include <pthread.h> 41#include <sys/signalvar.h> --- 9 unchanged lines hidden (view full) --- 51 52int 53_pthread_create(pthread_t * thread, const pthread_attr_t * attr, 54 void *(*start_routine) (void *), void *arg) 55{ 56 ucontext_t uc; 57 sigset_t sigmask, oldsigmask; 58 struct pthread *curthread, *new_thread; |
59 int ret = 0; | 59 int ret = 0, locked; |
60 61 _thr_check_init(); 62 63 /* 64 * Tell libc and others now they need lock to protect their data. 65 */ 66 if (_thr_isthreaded() == 0 && _thr_setthreaded(1)) 67 return (EAGAIN); --- 19 unchanged lines hidden (view full) --- 87 */ 88 } 89 90 if (_thr_scope_system > 0) 91 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 92 else if (_thr_scope_system < 0) 93 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM; 94 | 60 61 _thr_check_init(); 62 63 /* 64 * Tell libc and others now they need lock to protect their data. 65 */ 66 if (_thr_isthreaded() == 0 && _thr_setthreaded(1)) 67 return (EAGAIN); --- 19 unchanged lines hidden (view full) --- 87 */ 88 } 89 90 if (_thr_scope_system > 0) 91 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM; 92 else if (_thr_scope_system < 0) 93 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM; 94 |
95 new_thread->tid = TID_TERMINATED; 96 |
|
95 if (create_stack(&new_thread->attr) != 0) { 96 /* Insufficient memory to create a stack: */ | 97 if (create_stack(&new_thread->attr) != 0) { 98 /* Insufficient memory to create a stack: */ |
97 new_thread->terminated = 1; | |
98 _thr_free(curthread, new_thread); 99 return (EAGAIN); 100 } 101 /* 102 * Write a magic value to the thread structure 103 * to help identify valid ones: 104 */ 105 new_thread->magic = THR_MAGIC; --- 40 unchanged lines hidden (view full) --- 146 new_thread->flags = THR_FLAGS_SUSPENDED; 147 new_thread->state = PS_RUNNING; 148 /* 149 * Thread created by thr_create() inherits currrent thread 150 * sigmask, however, before new thread setup itself correctly, 151 * it can not handle signal, so we should masks all signals here. 152 */ 153 SIGFILLSET(sigmask); | 99 _thr_free(curthread, new_thread); 100 return (EAGAIN); 101 } 102 /* 103 * Write a magic value to the thread structure 104 * to help identify valid ones: 105 */ 106 new_thread->magic = THR_MAGIC; --- 40 unchanged lines hidden (view full) --- 147 new_thread->flags = THR_FLAGS_SUSPENDED; 148 new_thread->state = PS_RUNNING; 149 /* 150 * Thread created by thr_create() inherits currrent thread 151 * sigmask, however, before new thread setup itself correctly, 152 * it can not handle signal, so we should masks all signals here. 153 */ 154 SIGFILLSET(sigmask); |
155 SIGDELSET(sigmask, SIGTRAP); |
|
154 __sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask); 155 new_thread->sigmask = oldsigmask; 156 /* Add the new thread. */ 157 _thr_link(curthread, new_thread); 158 /* Return thread pointer eariler so that new thread can use it. */ 159 (*thread) = new_thread; | 156 __sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask); 157 new_thread->sigmask = oldsigmask; 158 /* Add the new thread. */ 159 _thr_link(curthread, new_thread); 160 /* Return thread pointer eariler so that new thread can use it. */ 161 (*thread) = new_thread; |
162 if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) { 163 THR_THREAD_LOCK(curthread, new_thread); 164 locked = 1; 165 } else 166 locked = 0; |
|
160 /* Schedule the new thread. */ 161 ret = thr_create(&uc, &new_thread->tid, 0); 162 __sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL); 163 if (ret != 0) { | 167 /* Schedule the new thread. */ 168 ret = thr_create(&uc, &new_thread->tid, 0); 169 __sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL); 170 if (ret != 0) { |
171 if (locked) 172 THR_THREAD_UNLOCK(curthread, new_thread); |
|
164 _thr_unlink(curthread, new_thread); 165 free_thread(curthread, new_thread); 166 (*thread) = 0; 167 ret = EAGAIN; | 173 _thr_unlink(curthread, new_thread); 174 free_thread(curthread, new_thread); 175 (*thread) = 0; 176 ret = EAGAIN; |
177 } else if (locked) { 178 _thr_report_creation(curthread, new_thread); 179 THR_THREAD_UNLOCK(curthread, new_thread); |
|
168 } 169 return (ret); 170} 171 172static void 173free_thread(struct pthread *curthread, struct pthread *thread) 174{ 175 free_stack(curthread, &thread->attr); | 180 } 181 return (ret); 182} 183 184static void 185free_thread(struct pthread *curthread, struct pthread *thread) 186{ 187 free_stack(curthread, &thread->attr); |
176 curthread->terminated = 1; | 188 curthread->tid = TID_TERMINATED; |
177 _thr_free(curthread, thread); 178} 179 180static int 181create_stack(struct pthread_attr *pattr) 182{ 183 int ret; 184 --- 25 unchanged lines hidden (view full) --- 210 _tcb_set(curthread->tcb); 211 212 /* Thread was created with all signals blocked, unblock them. */ 213 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL); 214 215 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) 216 _thr_suspend_check(curthread); 217 | 189 _thr_free(curthread, thread); 190} 191 192static int 193create_stack(struct pthread_attr *pattr) 194{ 195 int ret; 196 --- 25 unchanged lines hidden (view full) --- 222 _tcb_set(curthread->tcb); 223 224 /* Thread was created with all signals blocked, unblock them. */ 225 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL); 226 227 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) 228 _thr_suspend_check(curthread); 229 |
230 THR_LOCK(curthread); 231 THR_UNLOCK(curthread); 232 |
|
218 /* Run the current thread's start routine with argument: */ 219 pthread_exit(curthread->start_routine(curthread->arg)); 220 221 /* This point should never be reached. */ 222 PANIC("Thread has resumed after exit"); 223} | 233 /* Run the current thread's start routine with argument: */ 234 pthread_exit(curthread->start_routine(curthread->arg)); 235 236 /* This point should never be reached. */ 237 PANIC("Thread has resumed after exit"); 238} |