thr_exit.c revision 112918
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 112918 2003-04-01 03:46:29Z jeff $
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
45void
46_thread_exit(char *fname, int lineno, char *string)
47{
48	char            s[256];
49
50	/* Prepare an error message string: */
51	snprintf(s, sizeof(s),
52	    "Fatal error '%s' at line %d in file %s (errno = %d)\n",
53	    string, lineno, fname, errno);
54
55	/* Write the string to the standard error file descriptor: */
56	__sys_write(2, s, strlen(s));
57
58	/* Force this process to exit: */
59	/* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */
60#if defined(_PTHREADS_INVARIANTS)
61	abort();
62#else
63	__sys_exit(1);
64#endif
65}
66
67/*
68 * Only called when a thread is cancelled.  It may be more useful
69 * to call it from pthread_exit() if other ways of asynchronous or
70 * abnormal thread termination can be found.
71 */
72void
73_thread_exit_cleanup(void)
74{
75	struct pthread	*curthread = _get_curthread();
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	*curthread = _get_curthread();
97	pthread_t pthread;
98
99	/* Check if this thread is already in the process of exiting: */
100	if ((curthread->flags & PTHREAD_EXITING) != 0) {
101		char msg[128];
102		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);
103		PANIC(msg);
104	}
105
106	/* Flag this thread as exiting: */
107	curthread->flags |= PTHREAD_EXITING;
108
109	/* Save the return value: */
110	curthread->ret = status;
111
112	while (curthread->cleanup != NULL) {
113		pthread_cleanup_pop(1);
114	}
115	if (curthread->attr.cleanup_attr != NULL) {
116		curthread->attr.cleanup_attr(curthread->attr.arg_attr);
117	}
118	/* Check if there is thread specific data: */
119	if (curthread->specific != NULL) {
120		/* Run the thread-specific data destructors: */
121		_thread_cleanupspecific();
122	}
123
124	/*
125	 * Lock the garbage collector mutex to ensure that the garbage
126	 * collector is not using the dead thread list.
127	 */
128	if (pthread_mutex_lock(&_gc_mutex) != 0)
129		PANIC("Cannot lock gc mutex");
130
131	/* Add this thread to the list of dead threads. */
132	TAILQ_INSERT_HEAD(&_dead_list, curthread, dle);
133
134	/*
135	 * Signal the garbage collector thread that there is something
136	 * to clean up.
137	 */
138	if (pthread_cond_signal(&_gc_cond) != 0)
139		PANIC("Cannot signal gc cond");
140
141	/*
142	 * Avoid a race condition where a scheduling signal can occur
143	 * causing the garbage collector thread to run.  If this happens,
144	 * the current thread can be cleaned out from under us.
145	 */
146	GIANT_LOCK(curthread);
147
148	/* Unlock the garbage collector mutex. */
149	if (pthread_mutex_unlock(&_gc_mutex) != 0)
150		PANIC("Cannot unlock gc mutex");
151
152	/* Check if there is a thread joining this one: */
153	if (curthread->joiner != NULL) {
154		pthread = curthread->joiner;
155		curthread->joiner = NULL;
156
157		/* Make the joining thread runnable: */
158		PTHREAD_NEW_STATE(pthread, PS_RUNNING);
159
160		/* Set the return value for the joining thread: */
161		pthread->join_status.ret = curthread->ret;
162		pthread->join_status.error = 0;
163		pthread->join_status.thread = NULL;
164
165		/* Make this thread collectable by the garbage collector. */
166		PTHREAD_ASSERT(((curthread->attr.flags & PTHREAD_DETACHED) ==
167		    0), "Cannot join a detached thread");
168		curthread->attr.flags |= PTHREAD_DETACHED;
169	}
170
171	/* Remove this thread from the thread list: */
172	TAILQ_REMOVE(&_thread_list, curthread, tle);
173
174	PTHREAD_SET_STATE(curthread, PS_DEAD);
175	GIANT_UNLOCK(curthread);
176
177	/*
178	 * Retire the architecture specific id so that it can be used for
179	 * new threads.
180	 */
181	_retire_thread(curthread->arch_id);
182	_thr_exit();
183
184	/* This point should not be reached. */
185	PANIC("Dead thread has resumed");
186}
187