xref: /freebsd/lib/libthr/thread/thr_exit.c (revision bb535300dd871731b2542594a917bc2892479ca0)
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 	struct pthread	*curthread = _get_curthread();
76bb535300SJeff Roberson 
77bb535300SJeff Roberson 	/*
78bb535300SJeff Roberson 	 * POSIX states that cancellation/termination of a thread should
79bb535300SJeff Roberson 	 * not release any visible resources (such as mutexes) and that
80bb535300SJeff Roberson 	 * it is the applications responsibility.  Resources that are
81bb535300SJeff Roberson 	 * internal to the threads library, including file and fd locks,
82bb535300SJeff Roberson 	 * are not visible to the application and need to be released.
83bb535300SJeff Roberson 	 */
84bb535300SJeff Roberson 	/* Unlock all private mutexes: */
85bb535300SJeff Roberson 	_mutex_unlock_private(curthread);
86bb535300SJeff Roberson 
87bb535300SJeff Roberson 	/*
88bb535300SJeff Roberson 	 * This still isn't quite correct because we don't account
89bb535300SJeff Roberson 	 * for held spinlocks (see libc/stdlib/malloc.c).
90bb535300SJeff Roberson 	 */
91bb535300SJeff Roberson }
92bb535300SJeff Roberson 
93bb535300SJeff Roberson void
94bb535300SJeff Roberson _pthread_exit(void *status)
95bb535300SJeff Roberson {
96bb535300SJeff Roberson 	struct pthread	*curthread = _get_curthread();
97bb535300SJeff Roberson 	pthread_t pthread;
98bb535300SJeff Roberson 
99bb535300SJeff Roberson 	/* Check if this thread is already in the process of exiting: */
100bb535300SJeff Roberson 	if ((curthread->flags & PTHREAD_EXITING) != 0) {
101bb535300SJeff Roberson 		char msg[128];
102bb535300SJeff 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);
103bb535300SJeff Roberson 		PANIC(msg);
104bb535300SJeff Roberson 	}
105bb535300SJeff Roberson 
106bb535300SJeff Roberson 	/* Flag this thread as exiting: */
107bb535300SJeff Roberson 	curthread->flags |= PTHREAD_EXITING;
108bb535300SJeff Roberson 
109bb535300SJeff Roberson 	/* Save the return value: */
110bb535300SJeff Roberson 	curthread->ret = status;
111bb535300SJeff Roberson 
112bb535300SJeff Roberson 	while (curthread->cleanup != NULL) {
113bb535300SJeff Roberson 		pthread_cleanup_pop(1);
114bb535300SJeff Roberson 	}
115bb535300SJeff Roberson 	if (curthread->attr.cleanup_attr != NULL) {
116bb535300SJeff Roberson 		curthread->attr.cleanup_attr(curthread->attr.arg_attr);
117bb535300SJeff Roberson 	}
118bb535300SJeff Roberson 	/* Check if there is thread specific data: */
119bb535300SJeff Roberson 	if (curthread->specific != NULL) {
120bb535300SJeff Roberson 		/* Run the thread-specific data destructors: */
121bb535300SJeff Roberson 		_thread_cleanupspecific();
122bb535300SJeff Roberson 	}
123bb535300SJeff Roberson 
124bb535300SJeff Roberson 	/*
125bb535300SJeff Roberson 	 * Lock the garbage collector mutex to ensure that the garbage
126bb535300SJeff Roberson 	 * collector is not using the dead thread list.
127bb535300SJeff Roberson 	 */
128bb535300SJeff Roberson 	if (pthread_mutex_lock(&_gc_mutex) != 0)
129bb535300SJeff Roberson 		PANIC("Cannot lock gc mutex");
130bb535300SJeff Roberson 
131bb535300SJeff Roberson 	/* Add this thread to the list of dead threads. */
132bb535300SJeff Roberson 	TAILQ_INSERT_HEAD(&_dead_list, curthread, dle);
133bb535300SJeff Roberson 
134bb535300SJeff Roberson 	/*
135bb535300SJeff Roberson 	 * Signal the garbage collector thread that there is something
136bb535300SJeff Roberson 	 * to clean up.
137bb535300SJeff Roberson 	 */
138bb535300SJeff Roberson 	if (pthread_cond_signal(&_gc_cond) != 0)
139bb535300SJeff Roberson 		PANIC("Cannot signal gc cond");
140bb535300SJeff Roberson 
141bb535300SJeff Roberson 	/*
142bb535300SJeff Roberson 	 * Avoid a race condition where a scheduling signal can occur
143bb535300SJeff Roberson 	 * causing the garbage collector thread to run.  If this happens,
144bb535300SJeff Roberson 	 * the current thread can be cleaned out from under us.
145bb535300SJeff Roberson 	 */
146bb535300SJeff Roberson 	GIANT_LOCK(curthread);
147bb535300SJeff Roberson 
148bb535300SJeff Roberson 	/* Unlock the garbage collector mutex. */
149bb535300SJeff Roberson 	if (pthread_mutex_unlock(&_gc_mutex) != 0)
150bb535300SJeff Roberson 		PANIC("Cannot unlock gc mutex");
151bb535300SJeff Roberson 
152bb535300SJeff Roberson 	/* Check if there is a thread joining this one: */
153bb535300SJeff Roberson 	if (curthread->joiner != NULL) {
154bb535300SJeff Roberson 		pthread = curthread->joiner;
155bb535300SJeff Roberson 		curthread->joiner = NULL;
156bb535300SJeff Roberson 
157bb535300SJeff Roberson 		/* Make the joining thread runnable: */
158bb535300SJeff Roberson 		PTHREAD_NEW_STATE(pthread, PS_RUNNING);
159bb535300SJeff Roberson 
160bb535300SJeff Roberson 		/* Set the return value for the joining thread: */
161bb535300SJeff Roberson 		pthread->join_status.ret = curthread->ret;
162bb535300SJeff Roberson 		pthread->join_status.error = 0;
163bb535300SJeff Roberson 		pthread->join_status.thread = NULL;
164bb535300SJeff Roberson 
165bb535300SJeff Roberson 		/* Make this thread collectable by the garbage collector. */
166bb535300SJeff Roberson 		PTHREAD_ASSERT(((curthread->attr.flags & PTHREAD_DETACHED) ==
167bb535300SJeff Roberson 		    0), "Cannot join a detached thread");
168bb535300SJeff Roberson 		curthread->attr.flags |= PTHREAD_DETACHED;
169bb535300SJeff Roberson 	}
170bb535300SJeff Roberson 
171bb535300SJeff Roberson 	/* Remove this thread from the thread list: */
172bb535300SJeff Roberson 	TAILQ_REMOVE(&_thread_list, curthread, tle);
173bb535300SJeff Roberson 
174bb535300SJeff Roberson 	PTHREAD_SET_STATE(curthread, PS_DEAD);
175bb535300SJeff Roberson 	GIANT_UNLOCK(curthread);
176bb535300SJeff Roberson 
177bb535300SJeff Roberson 	/*
178bb535300SJeff Roberson 	 * Retire the architecture specific id so that it can be used for
179bb535300SJeff Roberson 	 * new threads.
180bb535300SJeff Roberson 	 */
181bb535300SJeff Roberson 	_retire_thread(curthread->arch_id);
182bb535300SJeff Roberson 	_thr_exit();
183bb535300SJeff Roberson 
184bb535300SJeff Roberson 	/* This point should not be reached. */
185bb535300SJeff Roberson 	PANIC("Dead thread has resumed");
186bb535300SJeff Roberson }
187