thr_exit.c revision 212837
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. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/lib/libthr/thread/thr_exit.c 212837 2010-09-19 05:19:47Z davidxu $ 30 */ 31 32#include "namespace.h" 33#include <errno.h> 34#ifdef _PTHREAD_FORCED_UNWIND 35#include <dlfcn.h> 36#endif 37#include <stdio.h> 38#include <stdlib.h> 39#include <pthread.h> 40#include <sys/types.h> 41#include <sys/signalvar.h> 42#include "un-namespace.h" 43 44#include "libc_private.h" 45#include "thr_private.h" 46 47void _pthread_exit(void *status); 48 49static void exit_thread(void) __dead2; 50 51__weak_reference(_pthread_exit, pthread_exit); 52 53#ifdef _PTHREAD_FORCED_UNWIND 54 55static void thread_unwind(void) __dead2; 56#ifdef PIC 57static void thread_uw_init(void); 58static _Unwind_Reason_Code thread_unwind_stop(int version, 59 _Unwind_Action actions, 60 _Unwind_Exception_Class exc_class, 61 struct _Unwind_Exception *exc_obj, 62 struct _Unwind_Context *context, void *stop_parameter); 63/* unwind library pointers */ 64static _Unwind_Reason_Code (*uwl_forcedunwind)(struct _Unwind_Exception *, 65 _Unwind_Stop_Fn, void *); 66static void (*uwl_resume)(struct _Unwind_Exception *exc); 67static _Unwind_Word (*uwl_getcfa)(struct _Unwind_Context *); 68 69static void 70thread_uw_init(void) 71{ 72 static int inited = 0; 73 Dl_info dlinfo; 74 void *handle; 75 void *forcedunwind, *resume, *getcfa; 76 77 if (inited) 78 return; 79 handle = RTLD_DEFAULT; 80 if ((forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind")) != NULL) { 81 if (dladdr(forcedunwind, &dlinfo)) { 82 if ((handle = dlopen(dlinfo.dli_fname, RTLD_LAZY)) != NULL) { 83 forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind"); 84 resume = dlsym(handle, "_Unwind_Resume"); 85 getcfa = dlsym(handle, "_Unwind_GetCFA"); 86 if (forcedunwind != NULL && resume != NULL && 87 getcfa != NULL) { 88 uwl_forcedunwind = forcedunwind; 89 uwl_resume = resume; 90 uwl_getcfa = getcfa; 91 } else { 92 dlclose(handle); 93 } 94 } 95 } 96 } 97 inited = 1; 98} 99 100void 101_Unwind_Resume(struct _Unwind_Exception *ex) 102{ 103 (*uwl_resume)(ex); 104} 105 106_Unwind_Reason_Code 107_Unwind_ForcedUnwind(struct _Unwind_Exception *ex, _Unwind_Stop_Fn stop_func, 108 void *stop_arg) 109{ 110 return (*uwl_forcedunwind)(ex, stop_func, stop_arg); 111} 112 113_Unwind_Word 114_Unwind_GetCFA(struct _Unwind_Context *context) 115{ 116 return (*uwl_getcfa)(context); 117} 118#else 119#pragma weak _Unwind_GetCFA 120#pragma weak _Unwind_ForcedUnwind 121#pragma weak _Unwind_Resume 122#endif /* PIC */ 123 124static void 125thread_unwind_cleanup(_Unwind_Reason_Code code, struct _Unwind_Exception *e) 126{ 127 /* 128 * Specification said that _Unwind_Resume should not be used here, 129 * instead, user should rethrow the exception. For C++ user, they 130 * should put "throw" sentence in catch(...) block. 131 */ 132 PANIC("exception should be rethrown"); 133} 134 135static _Unwind_Reason_Code 136thread_unwind_stop(int version, _Unwind_Action actions, 137 _Unwind_Exception_Class exc_class, 138 struct _Unwind_Exception *exc_obj, 139 struct _Unwind_Context *context, void *stop_parameter) 140{ 141 struct pthread *curthread = _get_curthread(); 142 struct pthread_cleanup *cur; 143 uintptr_t cfa; 144 int done = 0; 145 146 /* XXX assume stack grows down to lower address */ 147 148 cfa = _Unwind_GetCFA(context); 149 if (actions & _UA_END_OF_STACK) { 150 done = 1; 151 } else if (cfa >= (uintptr_t)curthread->unwind_stackend) { 152 done = 1; 153 } 154 155 while ((cur = curthread->cleanup) != NULL && 156 (done || 157 ((uintptr_t)cur < (uintptr_t)curthread->unwind_stackend && 158 (uintptr_t)cur >= cfa))) { 159 __pthread_cleanup_pop_imp(1); 160 } 161 162 if (done) 163 exit_thread(); /* Never return! */ 164 165 return (_URC_NO_REASON); 166} 167 168static void 169thread_unwind(void) 170{ 171 struct pthread *curthread = _get_curthread(); 172 173 curthread->ex.exception_class = 0; 174 curthread->ex.exception_cleanup = thread_unwind_cleanup; 175 _Unwind_ForcedUnwind(&curthread->ex, thread_unwind_stop, NULL); 176 PANIC("_Unwind_ForcedUnwind returned"); 177} 178 179#endif 180 181void 182_thread_exit(const char *fname, int lineno, const char *msg) 183{ 184 185 /* Write an error message to the standard error file descriptor: */ 186 _thread_printf(2, 187 "Fatal error '%s' at line %d in file %s (errno = %d)\n", 188 msg, lineno, fname, errno); 189 190 abort(); 191} 192 193void 194_pthread_exit(void *status) 195{ 196 _pthread_exit_mask(status, NULL); 197} 198 199void 200_pthread_exit_mask(void *status, sigset_t *mask) 201{ 202 struct pthread *curthread = _get_curthread(); 203 204 /* Check if this thread is already in the process of exiting: */ 205 if (curthread->cancelling) { 206 char msg[128]; 207 snprintf(msg, sizeof(msg), "Thread %p has called " 208 "pthread_exit() from a destructor. POSIX 1003.1 " 209 "1996 s16.2.5.2 does not allow this!", curthread); 210 PANIC(msg); 211 } 212 213 /* Flag this thread as exiting. */ 214 curthread->cancelling = 1; 215 curthread->cancel_enable = 0; 216 curthread->cancel_async = 0; 217 curthread->cancel_point = 0; 218 if (mask != NULL) 219 __sys_sigprocmask(SIG_SETMASK, mask, NULL); 220 if (curthread->unblock_sigcancel) { 221 sigset_t set; 222 223 curthread->unblock_sigcancel = 0; 224 SIGEMPTYSET(set); 225 SIGADDSET(set, SIGCANCEL); 226 __sys_sigprocmask(SIG_UNBLOCK, mask, NULL); 227 } 228 229 /* Save the return value: */ 230 curthread->ret = status; 231#ifdef _PTHREAD_FORCED_UNWIND 232#ifdef PIC 233 thread_uw_init(); 234 if (uwl_forcedunwind != NULL) { 235 thread_unwind(); 236 } 237#else 238 if (_Unwind_ForcedUnwind != NULL) { 239 thread_unwind(); 240 } 241#endif /* PIC */ 242 243 else { 244 while (curthread->cleanup != NULL) { 245 __pthread_cleanup_pop_imp(1); 246 } 247 exit_thread(); 248 } 249 250#else 251 while (curthread->cleanup != NULL) { 252 __pthread_cleanup_pop_imp(1); 253 } 254 255 exit_thread(); 256#endif /* _PTHREAD_FORCED_UNWIND */ 257} 258 259static void 260exit_thread(void) 261{ 262 struct pthread *curthread = _get_curthread(); 263 264 /* Check if there is thread specific data: */ 265 if (curthread->specific != NULL) { 266 /* Run the thread-specific data destructors: */ 267 _thread_cleanupspecific(); 268 } 269 270 if (!_thr_isthreaded()) 271 exit(0); 272 273 if (atomic_fetchadd_int(&_thread_active_threads, -1) == 1) { 274 exit(0); 275 /* Never reach! */ 276 } 277 278 /* Tell malloc that the thread is exiting. */ 279 _malloc_thread_cleanup(); 280 281 THR_LOCK(curthread); 282 curthread->state = PS_DEAD; 283 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) { 284 curthread->cycle++; 285 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0); 286 } 287 /* 288 * Thread was created with initial refcount 1, we drop the 289 * reference count to allow it to be garbage collected. 290 */ 291 curthread->refcount--; 292 _thr_try_gc(curthread, curthread); /* thread lock released */ 293 294 if (!curthread->force_exit && SHOULD_REPORT_EVENT(curthread, TD_DEATH)) 295 _thr_report_death(curthread); 296 297#if defined(_PTHREADS_INVARIANTS) 298 if (THR_IN_CRITICAL(curthread)) 299 PANIC("thread exits with resources held!"); 300#endif 301 /* 302 * Kernel will do wakeup at the address, so joiner thread 303 * will be resumed if it is sleeping at the address. 304 */ 305 thr_exit(&curthread->tid); 306 PANIC("thr_exit() returned"); 307 /* Never reach! */ 308} 309