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 35 #include <errno.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <pthread.h> 39 40 #include "thr_private.h" 41 42 void _pthread_exit(void *status); 43 44 __weak_reference(_pthread_exit, pthread_exit); 45 46 void 47 _thread_exit(const char *fname, int lineno, const char *msg) 48 { 49 50 /* Write an error message to the standard error file descriptor: */ 51 _thread_printf(2, 52 "Fatal error '%s' at line %d in file %s (errno = %d)\n", 53 msg, lineno, fname, errno); 54 55 abort(); 56 } 57 58 /* 59 * Only called when a thread is cancelled. It may be more useful 60 * to call it from pthread_exit() if other ways of asynchronous or 61 * abnormal thread termination can be found. 62 */ 63 void 64 _thr_exit_cleanup(void) 65 { 66 struct pthread *curthread = _get_curthread(); 67 68 /* 69 * POSIX states that cancellation/termination of a thread should 70 * not release any visible resources (such as mutexes) and that 71 * it is the applications responsibility. Resources that are 72 * internal to the threads library, including file and fd locks, 73 * are not visible to the application and need to be released. 74 */ 75 /* Unlock all private mutexes: */ 76 _mutex_unlock_private(curthread); 77 78 /* 79 * This still isn't quite correct because we don't account 80 * for held spinlocks (see libc/stdlib/malloc.c). 81 */ 82 } 83 84 void 85 _pthread_exit(void *status) 86 { 87 struct pthread *curthread = _get_curthread(); 88 89 /* Check if this thread is already in the process of exiting: */ 90 if ((curthread->cancelflags & THR_CANCEL_EXITING) != 0) { 91 char msg[128]; 92 snprintf(msg, sizeof(msg), "Thread %p has called " 93 "pthread_exit() from a destructor. POSIX 1003.1 " 94 "1996 s16.2.5.2 does not allow this!", curthread); 95 PANIC(msg); 96 } 97 98 /* Flag this thread as exiting. */ 99 atomic_set_int(&curthread->cancelflags, THR_CANCEL_EXITING); 100 101 _thr_exit_cleanup(); 102 103 /* Save the return value: */ 104 curthread->ret = status; 105 while (curthread->cleanup != NULL) { 106 pthread_cleanup_pop(1); 107 } 108 109 /* Check if there is thread specific data: */ 110 if (curthread->specific != NULL) { 111 /* Run the thread-specific data destructors: */ 112 _thread_cleanupspecific(); 113 } 114 115 if (!_thr_isthreaded()) 116 exit(0); 117 118 THREAD_LIST_LOCK(curthread); 119 _thread_active_threads--; 120 if (_thread_active_threads == 0) { 121 THREAD_LIST_UNLOCK(curthread); 122 exit(0); 123 /* Never reach! */ 124 } 125 THR_LOCK(curthread); 126 curthread->state = PS_DEAD; 127 THR_UNLOCK(curthread); 128 /* 129 * Thread was created with initial refcount 1, we drop the 130 * reference count to allow it to be garbage collected. 131 */ 132 curthread->refcount--; 133 if (curthread->tlflags & TLFLAGS_DETACHED) 134 THR_GCLIST_ADD(curthread); 135 THREAD_LIST_UNLOCK(curthread); 136 if (SHOULD_REPORT_EVENT(curthread, TD_DEATH)) 137 _thr_report_death(curthread); 138 139 /* 140 * Kernel will do wakeup at the address, so joiner thread 141 * will be resumed if it is sleeping at the address. 142 */ 143 thr_exit(&curthread->tid); 144 PANIC("thr_exit() returned"); 145 /* Never reach! */ 146 } 147