2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 #include "namespace.h"
40 #include "un-namespace.h"
41 #include "thr_private.h"
43 void _pthread_exit(void *status);
45 __weak_reference(_pthread_exit, pthread_exit);
48 _thr_exit(const char *fname, int lineno, const char *msg)
51 /* Write an error message to the standard error file descriptor: */
53 "Fatal error '%s' at line %d in file %s (errno = %d)\n",
54 msg, lineno, fname, errno);
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.
65 _thr_exit_cleanup(void)
67 struct pthread *curthread = _get_curthread();
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.
76 /* Unlock all private mutexes: */
77 _mutex_unlock_private(curthread);
80 * This still isn't quite correct because we don't account
81 * for held spinlocks (see libc/stdlib/malloc.c).
86 _pthread_exit(void *status)
88 struct pthread *curthread = _get_curthread();
92 /* Check if this thread is already in the process of exiting: */
93 if ((curthread->flags & THR_FLAGS_EXITING) != 0) {
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);
102 * Flag this thread as exiting. Threads should now be prevented
103 * from joining to this thread.
105 THR_SCHED_LOCK(curthread, curthread);
106 curthread->flags |= THR_FLAGS_EXITING;
107 THR_SCHED_UNLOCK(curthread, curthread);
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()!
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);
127 if (curthread->attr.cleanup_attr != NULL) {
128 curthread->attr.cleanup_attr(curthread->attr.arg_attr);
130 /* Check if there is thread specific data: */
131 if (curthread->specific != NULL) {
132 /* Run the thread-specific data destructors: */
133 _thread_cleanupspecific();
135 if (!_kse_isthreaded())
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);
149 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
151 /* This thread will never be re-scheduled. */
153 THR_SET_STATE(curthread, PS_DEAD);
154 _thr_sched_switch_unlocked(curthread);
157 /* This point should not be reached. */
158 PANIC("Dead thread has resumed");