xref: /trueos/lib/libkse/thread/thr_exit.c (revision 8cf2d070a8052f74c73cb0b3d2b09bc755e9ce6e)
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. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "namespace.h"
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include "un-namespace.h"
41 #include "thr_private.h"
42 
43 void	_pthread_exit(void *status);
44 
45 __weak_reference(_pthread_exit, pthread_exit);
46 
47 void
_thr_exit(const char * fname,int lineno,const char * msg)48 _thr_exit(const char *fname, int lineno, const char *msg)
49 {
50 
51 	/* Write an error message to the standard error file descriptor: */
52 	_thread_printf(2,
53 	    "Fatal error '%s' at line %d in file %s (errno = %d)\n",
54 	    msg, lineno, fname, errno);
55 
56 	abort();
57 }
58 
59 /*
60  * Only called when a thread is cancelled.  It may be more useful
61  * to call it from pthread_exit() if other ways of asynchronous or
62  * abnormal thread termination can be found.
63  */
64 void
_thr_exit_cleanup(void)65 _thr_exit_cleanup(void)
66 {
67 	struct pthread	*curthread = _get_curthread();
68 
69 	/*
70 	 * POSIX states that cancellation/termination of a thread should
71 	 * not release any visible resources (such as mutexes) and that
72 	 * it is the applications responsibility.  Resources that are
73 	 * internal to the threads library, including file and fd locks,
74 	 * are not visible to the application and need to be released.
75 	 */
76 	/* Unlock all private mutexes: */
77 	_mutex_unlock_private(curthread);
78 
79 	/*
80 	 * This still isn't quite correct because we don't account
81 	 * for held spinlocks (see libc/stdlib/malloc.c).
82 	 */
83 }
84 
85 void
_pthread_exit(void * status)86 _pthread_exit(void *status)
87 {
88 	struct pthread *curthread = _get_curthread();
89 	kse_critical_t crit;
90 	struct kse *curkse;
91 
92 	/* Check if this thread is already in the process of exiting: */
93 	if ((curthread->flags & THR_FLAGS_EXITING) != 0) {
94 		char msg[128];
95 		snprintf(msg, sizeof(msg), "Thread %p has called "
96 		    "pthread_exit() from a destructor. POSIX 1003.1 "
97 		    "1996 s16.2.5.2 does not allow this!", curthread);
98 		PANIC(msg);
99 	}
100 
101 	/*
102 	 * Flag this thread as exiting.  Threads should now be prevented
103 	 * from joining to this thread.
104 	 */
105 	THR_SCHED_LOCK(curthread, curthread);
106 	curthread->flags |= THR_FLAGS_EXITING;
107 	THR_SCHED_UNLOCK(curthread, curthread);
108 
109 	/*
110 	 * To avoid signal-lost problem, if signals had already been
111 	 * delivered to us, handle it. we have already set EXITING flag
112 	 * so no new signals should be delivered to us.
113 	 * XXX this is not enough if signal was delivered just before
114 	 * thread called sigprocmask and masked it! in this case, we
115 	 * might have to re-post the signal by kill() if the signal
116 	 * is targeting process (not for a specified thread).
117 	 * Kernel has same signal-lost problem, a signal may be delivered
118 	 * to a thread which is on the way to call sigprocmask or thr_exit()!
119 	 */
120 	if (curthread->check_pending)
121 		_thr_sig_check_pending(curthread);
122 	/* Save the return value: */
123 	curthread->ret = status;
124 	while (curthread->cleanup != NULL) {
125 		_pthread_cleanup_pop(1);
126 	}
127 	if (curthread->attr.cleanup_attr != NULL) {
128 		curthread->attr.cleanup_attr(curthread->attr.arg_attr);
129 	}
130 	/* Check if there is thread specific data: */
131 	if (curthread->specific != NULL) {
132 		/* Run the thread-specific data destructors: */
133 		_thread_cleanupspecific();
134 	}
135 	if (!_kse_isthreaded())
136 		exit(0);
137 	crit = _kse_critical_enter();
138 	curkse = _get_curkse();
139 	KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
140 	/* Use thread_list_lock */
141 	_thread_active_threads--;
142 	if ((_thread_scope_system <= 0 && _thread_active_threads == 1) ||
143 	    (_thread_scope_system > 0 && _thread_active_threads == 0)) {
144 		KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
145 		_kse_critical_leave(crit);
146 		exit(0);
147 		/* Never reach! */
148 	}
149 	KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
150 
151 	/* This thread will never be re-scheduled. */
152 	KSE_LOCK(curkse);
153 	THR_SET_STATE(curthread, PS_DEAD);
154 	_thr_sched_switch_unlocked(curthread);
155 	/* Never reach! */
156 
157 	/* This point should not be reached. */
158 	PANIC("Dead thread has resumed");
159 }
160