thr_exit.c revision 144921
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 144921 2005-04-12 03:00:28Z davidxu $
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
42void	_pthread_exit(void *status);
43
44__weak_reference(_pthread_exit, pthread_exit);
45
46void
47_thread_exit(char *fname, int lineno, 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 */
63void
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
84void
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	if (curthread->attr.cleanup_attr != NULL) {
109		curthread->attr.cleanup_attr(curthread->attr.arg_attr);
110	}
111	/* Check if there is thread specific data: */
112	if (curthread->specific != NULL) {
113		/* Run the thread-specific data destructors: */
114		_thread_cleanupspecific();
115	}
116
117	if (!_thr_isthreaded())
118		exit(0);
119
120	THREAD_LIST_LOCK(curthread);
121	_thread_active_threads--;
122	if (_thread_active_threads == 0) {
123		THREAD_LIST_UNLOCK(curthread);
124		exit(0);
125		/* Never reach! */
126	}
127	if (curthread->tlflags & TLFLAGS_DETACHED)
128		THR_GCLIST_ADD(curthread);
129	curthread->state = PS_DEAD;
130	THREAD_LIST_UNLOCK(curthread);
131	if (curthread->joiner)
132		_thr_umtx_wake(&curthread->state, INT_MAX);
133	if (SHOULD_REPORT_EVENT(curthread, TD_DEATH))
134		_thr_report_death(curthread);
135	thr_exit(&curthread->tid);
136	PANIC("thr_exit() returned");
137	/* Never reach! */
138}
139