thr_exit.c revision 136262
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: head/lib/libthr/thread/thr_exit.c 136262 2004-10-08 14:48:02Z mtm $ 33 */ 34#include <errno.h> 35#include <unistd.h> 36#include <fcntl.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <pthread.h> 41#include "thr_private.h" 42 43__weak_reference(_pthread_exit, pthread_exit); 44 45static void deadlist_free_threads(); 46 47void 48_thread_exit(char *fname, int lineno, char *string) 49{ 50 char s[256]; 51 52 /* Prepare an error message string: */ 53 snprintf(s, sizeof(s), 54 "Fatal error '%s' at line %d in file %s (errno = %d)\n", 55 string, lineno, fname, errno); 56 57 /* Write the string to the standard error file descriptor: */ 58 __sys_write(2, s, strlen(s)); 59 60 /* Force this process to exit: */ 61 /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */ 62#if defined(_PTHREADS_INVARIANTS) 63 abort(); 64#else 65 __sys_exit(1); 66#endif 67} 68 69/* 70 * Only called when a thread is cancelled. It may be more useful 71 * to call it from pthread_exit() if other ways of asynchronous or 72 * abnormal thread termination can be found. 73 */ 74void 75_thread_exit_cleanup(void) 76{ 77 /* 78 * POSIX states that cancellation/termination of a thread should 79 * not release any visible resources (such as mutexes) and that 80 * it is the applications responsibility. Resources that are 81 * internal to the threads library, including file and fd locks, 82 * are not visible to the application and need to be released. 83 */ 84 /* Unlock all private mutexes: */ 85 _mutex_unlock_private(curthread); 86 87 /* 88 * This still isn't quite correct because we don't account 89 * for held spinlocks (see libc/stdlib/malloc.c). 90 */ 91} 92 93void 94_pthread_exit(void *status) 95{ 96 struct pthread *pthread; 97 int exitNow = 0; 98 99 /* 100 * This thread will no longer handle any signals. 101 */ 102 _thread_sigblock(); 103 104 /* Check if this thread is already in the process of exiting: */ 105 if (curthread->exiting) { 106 char msg[128]; 107 snprintf(msg, sizeof(msg), "Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",curthread); 108 PANIC(msg); 109 } 110 111 /* Flag this thread as exiting: */ 112 curthread->exiting = 1; 113 114 /* Save the return value: */ 115 curthread->ret = status; 116 117 while (curthread->cleanup != NULL) { 118 pthread_cleanup_pop(1); 119 } 120 if (curthread->attr.cleanup_attr != NULL) { 121 curthread->attr.cleanup_attr(curthread->attr.arg_attr); 122 } 123 /* Check if there is thread specific data: */ 124 if (curthread->specific != NULL) { 125 /* Run the thread-specific data destructors: */ 126 _thread_cleanupspecific(); 127 } 128 129 /* 130 * Remove read-write lock list. It is allocated as-needed. 131 * Therefore, it must be checked for validity before freeing. 132 */ 133 if (curthread->rwlockList != NULL) 134 free(curthread->rwlockList); 135 136 /* Lock the dead list first to maintain correct lock order */ 137 DEAD_LIST_LOCK; 138 THREAD_LIST_LOCK; 139 140 /* Check if there is a thread joining this one: */ 141 if (curthread->joiner != NULL) { 142 pthread = curthread->joiner; 143 curthread->joiner = NULL; 144 145 /* Set the return value for the joining thread: */ 146 pthread->join_status.ret = curthread->ret; 147 pthread->join_status.error = 0; 148 pthread->join_status.thread = NULL; 149 150 /* Make the joining thread runnable: */ 151 PTHREAD_WAKE(pthread); 152 153 curthread->attr.flags |= PTHREAD_DETACHED; 154 } 155 156 /* 157 * Free any memory allocated for dead threads. 158 * Add this thread to the list of dead threads, and 159 * also remove it from the active threads list. 160 */ 161 deadlist_free_threads(); 162 TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); 163 TAILQ_REMOVE(&_thread_list, curthread, tle); 164 165 /* If we're the last thread, call it quits */ 166 if (TAILQ_EMPTY(&_thread_list)) 167 exitNow = 1; 168 169 THREAD_LIST_UNLOCK; 170 DEAD_LIST_UNLOCK; 171 172 if (exitNow) 173 exit(0); 174 175 /* 176 * This function will not return unless we are the last 177 * thread, which we can't be because we've already checked 178 * for that. 179 */ 180 thr_exit((long *)&curthread->isdead); 181 182 /* This point should not be reached. */ 183 PANIC("Dead thread has resumed"); 184} 185 186/* 187 * Note: this function must be called with the dead thread list 188 * locked. 189 */ 190static void 191deadlist_free_threads() 192{ 193 struct pthread *ptd, *ptdTemp; 194 195 TAILQ_FOREACH_SAFE(ptd, &_dead_list, dle, ptdTemp) { 196 /* Don't destroy the initial thread or non-detached threads. */ 197 if (ptd == _thread_initial || 198 (ptd->attr.flags & PTHREAD_DETACHED) == 0) 199 continue; 200 TAILQ_REMOVE(&_dead_list, ptd, dle); 201 deadlist_free_onethread(ptd); 202 } 203} 204 205void 206deadlist_free_onethread(struct pthread *ptd) 207{ 208 209 if (ptd->attr.stackaddr_attr == NULL && ptd->stack != NULL) { 210 STACK_LOCK; 211 _thread_stack_free(ptd->stack, ptd->attr.stacksize_attr, 212 ptd->attr.guardsize_attr); 213 STACK_UNLOCK; 214 } 215 _retire_thread(ptd->arch_id); 216 free(ptd); 217} 218