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"
34 #ifdef _PTHREAD_FORCED_UNWIND
40 #include <sys/types.h>
41 #include <sys/signalvar.h>
42 #include "un-namespace.h"
44 #include "libc_private.h"
45 #include "thr_private.h"
47 void _pthread_exit(void *status);
49 static void exit_thread(void) __dead2;
51 __weak_reference(_pthread_exit, pthread_exit);
53 #ifdef _PTHREAD_FORCED_UNWIND
54 static int message_printed;
56 static void thread_unwind(void) __dead2;
58 static void thread_uw_init(void);
59 static _Unwind_Reason_Code thread_unwind_stop(int version,
60 _Unwind_Action actions,
62 struct _Unwind_Exception *exc_obj,
63 struct _Unwind_Context *context, void *stop_parameter);
64 /* unwind library pointers */
65 static _Unwind_Reason_Code (*uwl_forcedunwind)(struct _Unwind_Exception *,
66 _Unwind_Stop_Fn, void *);
67 static unsigned long (*uwl_getcfa)(struct _Unwind_Context *);
72 static int inited = 0;
75 void *forcedunwind, *getcfa;
79 handle = RTLD_DEFAULT;
80 if ((forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind")) != NULL) {
81 if (dladdr(forcedunwind, &dlinfo)) {
83 * Make sure the address is always valid by holding the library,
84 * also assume functions are in same library.
86 if ((handle = dlopen(dlinfo.dli_fname, RTLD_LAZY)) != NULL) {
87 forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind");
88 getcfa = dlsym(handle, "_Unwind_GetCFA");
89 if (forcedunwind != NULL && getcfa != NULL) {
91 atomic_store_rel_ptr((volatile void *)&uwl_forcedunwind,
92 (uintptr_t)forcedunwind);
103 _Unwind_ForcedUnwind(struct _Unwind_Exception *ex, _Unwind_Stop_Fn stop_func,
106 return (*uwl_forcedunwind)(ex, stop_func, stop_arg);
110 _Unwind_GetCFA(struct _Unwind_Context *context)
112 return (*uwl_getcfa)(context);
115 #pragma weak _Unwind_GetCFA
116 #pragma weak _Unwind_ForcedUnwind
120 thread_unwind_cleanup(_Unwind_Reason_Code code, struct _Unwind_Exception *e)
123 * Specification said that _Unwind_Resume should not be used here,
124 * instead, user should rethrow the exception. For C++ user, they
125 * should put "throw" sentence in catch(...) block.
127 PANIC("exception should be rethrown");
130 static _Unwind_Reason_Code
131 thread_unwind_stop(int version, _Unwind_Action actions,
133 struct _Unwind_Exception *exc_obj,
134 struct _Unwind_Context *context, void *stop_parameter)
136 struct pthread *curthread = _get_curthread();
137 struct pthread_cleanup *cur;
141 /* XXX assume stack grows down to lower address */
143 cfa = _Unwind_GetCFA(context);
144 if (actions & _UA_END_OF_STACK ||
145 cfa >= (uintptr_t)curthread->unwind_stackend) {
149 while ((cur = curthread->cleanup) != NULL &&
150 (done || (uintptr_t)cur <= cfa)) {
151 __pthread_cleanup_pop_imp(1);
155 exit_thread(); /* Never return! */
157 return (_URC_NO_REASON);
163 struct pthread *curthread = _get_curthread();
165 curthread->ex.exception_class = 0;
166 curthread->ex.exception_cleanup = thread_unwind_cleanup;
167 _Unwind_ForcedUnwind(&curthread->ex, thread_unwind_stop, NULL);
168 PANIC("_Unwind_ForcedUnwind returned");
174 _thread_exit(const char *fname, int lineno, const char *msg)
177 /* Write an error message to the standard error file descriptor: */
179 "Fatal error '%s' at line %d in file %s (errno = %d)\n",
180 msg, lineno, fname, errno);
186 _pthread_exit(void *status)
188 _pthread_exit_mask(status, NULL);
192 _pthread_exit_mask(void *status, sigset_t *mask)
194 struct pthread *curthread = _get_curthread();
196 /* Check if this thread is already in the process of exiting: */
197 if (curthread->cancelling) {
199 snprintf(msg, sizeof(msg), "Thread %p has called "
200 "pthread_exit() from a destructor. POSIX 1003.1 "
201 "1996 s16.2.5.2 does not allow this!", curthread);
205 /* Flag this thread as exiting. */
206 curthread->cancelling = 1;
207 curthread->no_cancel = 1;
208 curthread->cancel_async = 0;
209 curthread->cancel_point = 0;
211 __sys_sigprocmask(SIG_SETMASK, mask, NULL);
212 if (curthread->unblock_sigcancel) {
215 curthread->unblock_sigcancel = 0;
217 SIGADDSET(set, SIGCANCEL);
218 __sys_sigprocmask(SIG_UNBLOCK, mask, NULL);
221 /* Save the return value: */
222 curthread->ret = status;
223 #ifdef _PTHREAD_FORCED_UNWIND
230 if (uwl_forcedunwind != NULL) {
232 if (_Unwind_ForcedUnwind != NULL) {
234 if (curthread->unwind_disabled) {
235 if (message_printed == 0) {
237 _thread_printf(2, "Warning: old _pthread_cleanup_push was called, "
238 "stack unwinding is disabled.\n");
246 while (curthread->cleanup != NULL) {
247 __pthread_cleanup_pop_imp(1);
253 while (curthread->cleanup != NULL) {
254 __pthread_cleanup_pop_imp(1);
258 #endif /* _PTHREAD_FORCED_UNWIND */
264 struct pthread *curthread = _get_curthread();
266 /* Check if there is thread specific data: */
267 if (curthread->specific != NULL) {
268 /* Run the thread-specific data destructors: */
269 _thread_cleanupspecific();
272 if (!_thr_isthreaded())
275 if (atomic_fetchadd_int(&_thread_active_threads, -1) == 1) {
280 /* Tell malloc that the thread is exiting. */
281 _malloc_thread_cleanup();
284 curthread->state = PS_DEAD;
285 if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
287 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
289 if (!curthread->force_exit && SHOULD_REPORT_EVENT(curthread, TD_DEATH))
290 _thr_report_death(curthread);
292 * Thread was created with initial refcount 1, we drop the
293 * reference count to allow it to be garbage collected.
295 curthread->refcount--;
296 _thr_try_gc(curthread, curthread); /* thread lock released */
298 #if defined(_PTHREADS_INVARIANTS)
299 if (THR_IN_CRITICAL(curthread))
300 PANIC("thread exits with resources held!");
303 * Kernel will do wakeup at the address, so joiner thread
304 * will be resumed if it is sleeping at the address.
306 thr_exit(&curthread->tid);
307 PANIC("thr_exit() returned");