]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libthr/thread/thr_exit.c
Simplify code, and in while loop, fix operator to match the unwinding
[FreeBSD/FreeBSD.git] / lib / libthr / thread / thr_exit.c
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 #ifdef _PTHREAD_FORCED_UNWIND
35 #include <dlfcn.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <pthread.h>
40 #include <sys/types.h>
41 #include <sys/signalvar.h>
42 #include "un-namespace.h"
43
44 #include "libc_private.h"
45 #include "thr_private.h"
46
47 void    _pthread_exit(void *status);
48
49 static void     exit_thread(void) __dead2;
50
51 __weak_reference(_pthread_exit, pthread_exit);
52
53 #ifdef _PTHREAD_FORCED_UNWIND
54
55 static void thread_unwind(void) __dead2;
56 #ifdef PIC
57 static void thread_uw_init(void);
58 static _Unwind_Reason_Code thread_unwind_stop(int version,
59         _Unwind_Action actions,
60         _Unwind_Exception_Class exc_class,
61         struct _Unwind_Exception *exc_obj,
62         struct _Unwind_Context *context, void *stop_parameter);
63 /* unwind library pointers */
64 static _Unwind_Reason_Code (*uwl_forcedunwind)(struct _Unwind_Exception *,
65         _Unwind_Stop_Fn, void *);
66 static _Unwind_Word (*uwl_getcfa)(struct _Unwind_Context *);
67
68 static void
69 thread_uw_init(void)
70 {
71         static int inited = 0;
72         Dl_info dlinfo;
73         void *handle;
74         void *forcedunwind, *getcfa;
75
76         if (inited)
77             return;
78         handle = RTLD_DEFAULT;
79         if ((forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind")) != NULL) {
80             if (dladdr(forcedunwind, &dlinfo)) {
81                 /*
82                  * Make sure the address is always valid by holding the library,
83                  * also assume functions are in same library.
84                  */
85                 if ((handle = dlopen(dlinfo.dli_fname, RTLD_LAZY)) != NULL) {
86                     forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind");
87                     getcfa = dlsym(handle, "_Unwind_GetCFA");
88                     if (forcedunwind != NULL && getcfa != NULL) {
89                         uwl_getcfa = getcfa;
90                         atomic_store_rel_ptr((volatile void *)&uwl_forcedunwind,
91                                 (uintptr_t)forcedunwind);
92                     } else {
93                         dlclose(handle);
94                     }
95                 }
96             }
97         }
98         inited = 1;
99 }
100
101 _Unwind_Reason_Code
102 _Unwind_ForcedUnwind(struct _Unwind_Exception *ex, _Unwind_Stop_Fn stop_func,
103         void *stop_arg)
104 {
105         return (*uwl_forcedunwind)(ex, stop_func, stop_arg);
106 }
107
108 _Unwind_Word
109 _Unwind_GetCFA(struct _Unwind_Context *context)
110 {
111         return (*uwl_getcfa)(context);
112 }
113 #else
114 #pragma weak _Unwind_GetCFA
115 #pragma weak _Unwind_ForcedUnwind
116 #endif /* PIC */
117
118 static void
119 thread_unwind_cleanup(_Unwind_Reason_Code code, struct _Unwind_Exception *e)
120 {
121         /*
122          * Specification said that _Unwind_Resume should not be used here,
123          * instead, user should rethrow the exception. For C++ user, they
124          * should put "throw" sentence in catch(...) block.
125          */
126         PANIC("exception should be rethrown");
127 }
128
129 static _Unwind_Reason_Code
130 thread_unwind_stop(int version, _Unwind_Action actions,
131         _Unwind_Exception_Class exc_class,
132         struct _Unwind_Exception *exc_obj,
133         struct _Unwind_Context *context, void *stop_parameter)
134 {
135         struct pthread *curthread = _get_curthread();
136         struct pthread_cleanup *cur;
137         uintptr_t cfa;
138         int done = 0;
139
140         /* XXX assume stack grows down to lower address */
141
142         cfa = _Unwind_GetCFA(context);
143         if (actions & _UA_END_OF_STACK ||
144             cfa >= (uintptr_t)curthread->unwind_stackend) {
145                 done = 1;
146         }
147
148         while ((cur = curthread->cleanup) != NULL &&
149                (done || (uintptr_t)cur <= cfa)) {
150                 __pthread_cleanup_pop_imp(1);
151         }
152
153         if (done)
154                 exit_thread(); /* Never return! */
155
156         return (_URC_NO_REASON);
157 }
158
159 static void
160 thread_unwind(void)
161 {
162         struct pthread  *curthread = _get_curthread();
163
164         curthread->ex.exception_class = 0;
165         curthread->ex.exception_cleanup = thread_unwind_cleanup;
166         _Unwind_ForcedUnwind(&curthread->ex, thread_unwind_stop, NULL);
167         PANIC("_Unwind_ForcedUnwind returned");
168 }
169
170 #endif
171
172 void
173 _thread_exit(const char *fname, int lineno, const char *msg)
174 {
175
176         /* Write an error message to the standard error file descriptor: */
177         _thread_printf(2,
178             "Fatal error '%s' at line %d in file %s (errno = %d)\n",
179             msg, lineno, fname, errno);
180
181         abort();
182 }
183
184 void
185 _pthread_exit(void *status)
186 {
187         _pthread_exit_mask(status, NULL);
188 }
189
190 void
191 _pthread_exit_mask(void *status, sigset_t *mask)
192 {
193         struct pthread *curthread = _get_curthread();
194
195         /* Check if this thread is already in the process of exiting: */
196         if (curthread->cancelling) {
197                 char msg[128];
198                 snprintf(msg, sizeof(msg), "Thread %p has called "
199                     "pthread_exit() from a destructor. POSIX 1003.1 "
200                     "1996 s16.2.5.2 does not allow this!", curthread);
201                 PANIC(msg);
202         }
203
204         /* Flag this thread as exiting. */
205         curthread->cancelling = 1;
206         curthread->no_cancel = 1;
207         curthread->cancel_async = 0;
208         curthread->cancel_point = 0;
209         if (mask != NULL)
210                 __sys_sigprocmask(SIG_SETMASK, mask, NULL);
211         if (curthread->unblock_sigcancel) {
212                 sigset_t set;
213
214                 curthread->unblock_sigcancel = 0;
215                 SIGEMPTYSET(set);
216                 SIGADDSET(set, SIGCANCEL);
217                 __sys_sigprocmask(SIG_UNBLOCK, mask, NULL);
218         }
219         
220         /* Save the return value: */
221         curthread->ret = status;
222 #ifdef _PTHREAD_FORCED_UNWIND
223 #ifdef PIC
224         thread_uw_init();
225         if (uwl_forcedunwind != NULL) {
226                 thread_unwind();
227         }
228 #else
229         if (_Unwind_ForcedUnwind != NULL) {
230                 thread_unwind();
231         }
232 #endif /* PIC */
233
234         else {
235                 while (curthread->cleanup != NULL) {
236                         __pthread_cleanup_pop_imp(1);
237                 }
238                 exit_thread();
239         }
240
241 #else
242         while (curthread->cleanup != NULL) {
243                 __pthread_cleanup_pop_imp(1);
244         }
245
246         exit_thread();
247 #endif /* _PTHREAD_FORCED_UNWIND */
248 }
249
250 static void
251 exit_thread(void)
252 {
253         struct pthread *curthread = _get_curthread();
254
255         /* Check if there is thread specific data: */
256         if (curthread->specific != NULL) {
257                 /* Run the thread-specific data destructors: */
258                 _thread_cleanupspecific();
259         }
260
261         if (!_thr_isthreaded())
262                 exit(0);
263
264         if (atomic_fetchadd_int(&_thread_active_threads, -1) == 1) {
265                 exit(0);
266                 /* Never reach! */
267         }
268
269         /* Tell malloc that the thread is exiting. */
270         _malloc_thread_cleanup();
271
272         THR_LOCK(curthread);
273         curthread->state = PS_DEAD;
274         if (curthread->flags & THR_FLAGS_NEED_SUSPEND) {
275                 curthread->cycle++;
276                 _thr_umtx_wake(&curthread->cycle, INT_MAX, 0);
277         }
278         /*
279          * Thread was created with initial refcount 1, we drop the
280          * reference count to allow it to be garbage collected.
281          */
282         curthread->refcount--;
283         _thr_try_gc(curthread, curthread); /* thread lock released */
284
285         if (!curthread->force_exit && SHOULD_REPORT_EVENT(curthread, TD_DEATH))
286                 _thr_report_death(curthread);
287
288 #if defined(_PTHREADS_INVARIANTS)
289         if (THR_IN_CRITICAL(curthread))
290                 PANIC("thread exits with resources held!");
291 #endif
292         /*
293          * Kernel will do wakeup at the address, so joiner thread
294          * will be resumed if it is sleeping at the address.
295          */
296         thr_exit(&curthread->tid);
297         PANIC("thr_exit() returned");
298         /* Never reach! */
299 }