1bb535300SJeff Roberson /* 2bb535300SJeff Roberson * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 3bb535300SJeff Roberson * All rights reserved. 4bb535300SJeff Roberson * 5bb535300SJeff Roberson * Redistribution and use in source and binary forms, with or without 6bb535300SJeff Roberson * modification, are permitted provided that the following conditions 7bb535300SJeff Roberson * are met: 8bb535300SJeff Roberson * 1. Redistributions of source code must retain the above copyright 9bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer. 10bb535300SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 11bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer in the 12bb535300SJeff Roberson * documentation and/or other materials provided with the distribution. 13bb535300SJeff Roberson * 3. All advertising materials mentioning features or use of this software 14bb535300SJeff Roberson * must display the following acknowledgement: 15bb535300SJeff Roberson * This product includes software developed by John Birrell. 16bb535300SJeff Roberson * 4. Neither the name of the author nor the names of any co-contributors 17bb535300SJeff Roberson * may be used to endorse or promote products derived from this software 18bb535300SJeff Roberson * without specific prior written permission. 19bb535300SJeff Roberson * 20bb535300SJeff Roberson * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21bb535300SJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22bb535300SJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23bb535300SJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24bb535300SJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25bb535300SJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26bb535300SJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27bb535300SJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28bb535300SJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29bb535300SJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30bb535300SJeff Roberson * SUCH DAMAGE. 31bb535300SJeff Roberson * 32bb535300SJeff Roberson * $FreeBSD$ 33bb535300SJeff Roberson */ 34bb535300SJeff Roberson #include <errno.h> 35bb535300SJeff Roberson #include <unistd.h> 36bb535300SJeff Roberson #include <fcntl.h> 37bb535300SJeff Roberson #include <stdio.h> 38bb535300SJeff Roberson #include <stdlib.h> 39bb535300SJeff Roberson #include <string.h> 40bb535300SJeff Roberson #include <pthread.h> 41bb535300SJeff Roberson #include "thr_private.h" 42bb535300SJeff Roberson 43bb535300SJeff Roberson __weak_reference(_pthread_exit, pthread_exit); 44bb535300SJeff Roberson 45bb535300SJeff Roberson void 46bb535300SJeff Roberson _thread_exit(char *fname, int lineno, char *string) 47bb535300SJeff Roberson { 48bb535300SJeff Roberson char s[256]; 49bb535300SJeff Roberson 50bb535300SJeff Roberson /* Prepare an error message string: */ 51bb535300SJeff Roberson snprintf(s, sizeof(s), 52bb535300SJeff Roberson "Fatal error '%s' at line %d in file %s (errno = %d)\n", 53bb535300SJeff Roberson string, lineno, fname, errno); 54bb535300SJeff Roberson 55bb535300SJeff Roberson /* Write the string to the standard error file descriptor: */ 56bb535300SJeff Roberson __sys_write(2, s, strlen(s)); 57bb535300SJeff Roberson 58bb535300SJeff Roberson /* Force this process to exit: */ 59bb535300SJeff Roberson /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */ 60bb535300SJeff Roberson #if defined(_PTHREADS_INVARIANTS) 61bb535300SJeff Roberson abort(); 62bb535300SJeff Roberson #else 63bb535300SJeff Roberson __sys_exit(1); 64bb535300SJeff Roberson #endif 65bb535300SJeff Roberson } 66bb535300SJeff Roberson 67bb535300SJeff Roberson /* 68bb535300SJeff Roberson * Only called when a thread is cancelled. It may be more useful 69bb535300SJeff Roberson * to call it from pthread_exit() if other ways of asynchronous or 70bb535300SJeff Roberson * abnormal thread termination can be found. 71bb535300SJeff Roberson */ 72bb535300SJeff Roberson void 73bb535300SJeff Roberson _thread_exit_cleanup(void) 74bb535300SJeff Roberson { 75bb535300SJeff Roberson /* 76bb535300SJeff Roberson * POSIX states that cancellation/termination of a thread should 77bb535300SJeff Roberson * not release any visible resources (such as mutexes) and that 78bb535300SJeff Roberson * it is the applications responsibility. Resources that are 79bb535300SJeff Roberson * internal to the threads library, including file and fd locks, 80bb535300SJeff Roberson * are not visible to the application and need to be released. 81bb535300SJeff Roberson */ 82bb535300SJeff Roberson /* Unlock all private mutexes: */ 83bb535300SJeff Roberson _mutex_unlock_private(curthread); 84bb535300SJeff Roberson 85bb535300SJeff Roberson /* 86bb535300SJeff Roberson * This still isn't quite correct because we don't account 87bb535300SJeff Roberson * for held spinlocks (see libc/stdlib/malloc.c). 88bb535300SJeff Roberson */ 89bb535300SJeff Roberson } 90bb535300SJeff Roberson 91bb535300SJeff Roberson void 92bb535300SJeff Roberson _pthread_exit(void *status) 93bb535300SJeff Roberson { 94bb535300SJeff Roberson pthread_t pthread; 95bb535300SJeff Roberson 96bb535300SJeff Roberson /* Check if this thread is already in the process of exiting: */ 97bb535300SJeff Roberson if ((curthread->flags & PTHREAD_EXITING) != 0) { 98bb535300SJeff Roberson char msg[128]; 99bb535300SJeff Roberson 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); 100bb535300SJeff Roberson PANIC(msg); 101bb535300SJeff Roberson } 102bb535300SJeff Roberson 103bb535300SJeff Roberson /* Flag this thread as exiting: */ 104bb535300SJeff Roberson curthread->flags |= PTHREAD_EXITING; 105bb535300SJeff Roberson 106bb535300SJeff Roberson /* Save the return value: */ 107bb535300SJeff Roberson curthread->ret = status; 108bb535300SJeff Roberson 109bb535300SJeff Roberson while (curthread->cleanup != NULL) { 110bb535300SJeff Roberson pthread_cleanup_pop(1); 111bb535300SJeff Roberson } 112bb535300SJeff Roberson if (curthread->attr.cleanup_attr != NULL) { 113bb535300SJeff Roberson curthread->attr.cleanup_attr(curthread->attr.arg_attr); 114bb535300SJeff Roberson } 115bb535300SJeff Roberson /* Check if there is thread specific data: */ 116bb535300SJeff Roberson if (curthread->specific != NULL) { 117bb535300SJeff Roberson /* Run the thread-specific data destructors: */ 118bb535300SJeff Roberson _thread_cleanupspecific(); 119bb535300SJeff Roberson } 120bb535300SJeff Roberson 121bb535300SJeff Roberson /* 122bb535300SJeff Roberson * Lock the garbage collector mutex to ensure that the garbage 123bb535300SJeff Roberson * collector is not using the dead thread list. 124bb535300SJeff Roberson */ 125bb535300SJeff Roberson if (pthread_mutex_lock(&_gc_mutex) != 0) 126bb535300SJeff Roberson PANIC("Cannot lock gc mutex"); 127bb535300SJeff Roberson 128bb535300SJeff Roberson /* Add this thread to the list of dead threads. */ 129bb535300SJeff Roberson TAILQ_INSERT_HEAD(&_dead_list, curthread, dle); 130bb535300SJeff Roberson 131bb535300SJeff Roberson /* 132bb535300SJeff Roberson * Signal the garbage collector thread that there is something 133bb535300SJeff Roberson * to clean up. 134bb535300SJeff Roberson */ 135bb535300SJeff Roberson if (pthread_cond_signal(&_gc_cond) != 0) 136bb535300SJeff Roberson PANIC("Cannot signal gc cond"); 137bb535300SJeff Roberson 138bb535300SJeff Roberson /* 139bb535300SJeff Roberson * Avoid a race condition where a scheduling signal can occur 140bb535300SJeff Roberson * causing the garbage collector thread to run. If this happens, 141bb535300SJeff Roberson * the current thread can be cleaned out from under us. 142bb535300SJeff Roberson */ 143bb535300SJeff Roberson GIANT_LOCK(curthread); 144bb535300SJeff Roberson 145bb535300SJeff Roberson /* Unlock the garbage collector mutex. */ 146bb535300SJeff Roberson if (pthread_mutex_unlock(&_gc_mutex) != 0) 147bb535300SJeff Roberson PANIC("Cannot unlock gc mutex"); 148bb535300SJeff Roberson 149bb535300SJeff Roberson /* Check if there is a thread joining this one: */ 150bb535300SJeff Roberson if (curthread->joiner != NULL) { 151bb535300SJeff Roberson pthread = curthread->joiner; 152bb535300SJeff Roberson curthread->joiner = NULL; 153bb535300SJeff Roberson 154bb535300SJeff Roberson /* Make the joining thread runnable: */ 155bb535300SJeff Roberson PTHREAD_NEW_STATE(pthread, PS_RUNNING); 156bb535300SJeff Roberson 157bb535300SJeff Roberson /* Set the return value for the joining thread: */ 158bb535300SJeff Roberson pthread->join_status.ret = curthread->ret; 159bb535300SJeff Roberson pthread->join_status.error = 0; 160bb535300SJeff Roberson pthread->join_status.thread = NULL; 161bb535300SJeff Roberson 162bb535300SJeff Roberson /* Make this thread collectable by the garbage collector. */ 163bb535300SJeff Roberson PTHREAD_ASSERT(((curthread->attr.flags & PTHREAD_DETACHED) == 164bb535300SJeff Roberson 0), "Cannot join a detached thread"); 165bb535300SJeff Roberson curthread->attr.flags |= PTHREAD_DETACHED; 166bb535300SJeff Roberson } 167bb535300SJeff Roberson 168bb535300SJeff Roberson /* Remove this thread from the thread list: */ 169bb535300SJeff Roberson TAILQ_REMOVE(&_thread_list, curthread, tle); 170bb535300SJeff Roberson 171bb535300SJeff Roberson PTHREAD_SET_STATE(curthread, PS_DEAD); 172bb535300SJeff Roberson GIANT_UNLOCK(curthread); 173bb535300SJeff Roberson 174f97591bfSMike Makonnen /* If we're the last thread, call it quits */ 175f97591bfSMike Makonnen if (TAILQ_EMPTY(&_thread_list)) 176f97591bfSMike Makonnen exit(curthread->ret); 177f97591bfSMike Makonnen 178bb535300SJeff Roberson /* 179bb535300SJeff Roberson * Retire the architecture specific id so that it can be used for 180bb535300SJeff Roberson * new threads. 181bb535300SJeff Roberson */ 182bb535300SJeff Roberson _retire_thread(curthread->arch_id); 183bb535300SJeff Roberson _thr_exit(); 184bb535300SJeff Roberson 185bb535300SJeff Roberson /* This point should not be reached. */ 186bb535300SJeff Roberson PANIC("Dead thread has resumed"); 187bb535300SJeff Roberson } 188