libc/stdlib: Add __cxa_thread_atexit_impl() hook.
[dragonfly.git] / lib / libc_r / uthread / uthread_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. 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: src/lib/libc_r/uthread/uthread_exit.c,v 1.16.2.8 2002/10/22 14:44:03 fjoe Exp $
33  */
34 #include <errno.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <pthread.h>
41 #include "libc_private.h"
42 #include "pthread_private.h"
43
44 #define FLAGS_IN_SCHEDQ \
45         (PTHREAD_FLAGS_IN_PRIOQ|PTHREAD_FLAGS_IN_WAITQ|PTHREAD_FLAGS_IN_WORKQ)
46
47 void __exit(int status)
48 {
49         int             flags;
50         int             i;
51         struct itimerval itimer;
52
53         /* Disable the interval timer: */
54         itimer.it_interval.tv_sec  = 0;
55         itimer.it_interval.tv_usec = 0;
56         itimer.it_value.tv_sec     = 0;
57         itimer.it_value.tv_usec    = 0;
58         setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL);
59
60         /* Close the pthread kernel pipe: */
61         __sys_close(_thread_kern_pipe[0]);
62         __sys_close(_thread_kern_pipe[1]);
63
64         /*
65          * Enter a loop to set all file descriptors to blocking
66          * if they were not created as non-blocking:
67          */
68         for (i = 0; i < _thread_dtablesize; i++) {
69                 /* Check if this file descriptor is in use: */
70                 if (_thread_fd_table[i] != NULL &&
71                     (_thread_fd_getflags(i) & O_NONBLOCK) == 0) {
72                         /* Get the current flags: */
73                         flags = __sys_fcntl(i, F_GETFL, NULL);
74                         /* Clear the nonblocking file descriptor flag: */
75                         __sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK);
76                 }
77         }
78
79         /* Call the _exit syscall: */
80         __sys_exit(status);
81 }
82
83 __strong_reference(__exit, _exit);
84
85 void
86 _thread_exit(char *fname, int lineno, char *string)
87 {
88         char            s[256];
89
90         /* Prepare an error message string: */
91         strcpy(s, "Fatal error '");
92         strcat(s, string);
93         strcat(s, "' at line ? ");
94         strcat(s, "in file ");
95         strcat(s, fname);
96         strcat(s, " (errno = ?");
97         strcat(s, ")\n");
98
99         /* Write the string to the standard error file descriptor: */
100         /* XXX should this be non-blocking ? */
101         __sys_extpwrite(2, s, strlen(s), O_FNONBLOCKING, -1);
102
103         /* Force this process to exit: */
104         /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */
105 #if defined(_PTHREADS_INVARIANTS)
106         abort();
107 #else
108         _exit(1);
109 #endif
110 }
111
112 /*
113  * Only called when a thread is cancelled.  It may be more useful
114  * to call it from pthread_exit() if other ways of asynchronous or
115  * abnormal thread termination can be found.
116  */
117 void
118 _thread_exit_cleanup(void)
119 {
120         struct pthread  *curthread = _get_curthread();
121
122         /*
123          * POSIX states that cancellation/termination of a thread should
124          * not release any visible resources (such as mutexes) and that
125          * it is the applications responsibility.  Resources that are
126          * internal to the threads library, including file and fd locks,
127          * are not visible to the application and need to be released.
128          */
129         /* Unlock all owned fd locks: */
130         _thread_fd_unlock_owned(curthread);
131
132         /* Unlock all private mutexes: */
133         _mutex_unlock_private(curthread);
134
135         /*
136          * This still isn't quite correct because we don't account
137          * for held spinlocks (see libc/stdlib/malloc.c).
138          */
139 }
140
141 void
142 _pthread_exit(void *status)
143 {
144         struct pthread  *curthread = _get_curthread();
145         pthread_t pthread;
146
147         /* Check if this thread is already in the process of exiting: */
148         if ((curthread->flags & PTHREAD_EXITING) != 0) {
149                 char msg[128];
150                 snprintf(msg, sizeof(msg), "Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",curthread);
151                 PANIC(msg);
152         }
153
154         /* Flag this thread as exiting: */
155         curthread->flags |= PTHREAD_EXITING;
156
157         /* Save the return value: */
158         curthread->ret = status;
159
160         while (curthread->cleanup != NULL) {
161                 pthread_cleanup_pop(1);
162         }
163         if (curthread->attr.cleanup_attr != NULL) {
164                 curthread->attr.cleanup_attr(curthread->attr.arg_attr);
165         }
166         /* Check if there is thread specific data: */
167         if (curthread->specific_data != NULL) {
168                 /* Run the thread-specific data destructors: */
169                 _thread_cleanupspecific();
170         }
171
172         /* Call TLS destructors, if any */
173         _thread_finalize();
174
175         /* Free thread-specific poll_data structure, if allocated: */
176         if (curthread->poll_data.fds != NULL) {
177                 free(curthread->poll_data.fds);
178                 curthread->poll_data.fds = NULL;
179         }
180
181         /*
182          * Lock the garbage collector mutex to ensure that the garbage
183          * collector is not using the dead thread list.
184          */
185         if (pthread_mutex_lock(&_gc_mutex) != 0)
186                 PANIC("Cannot lock gc mutex");
187
188         /* Add this thread to the list of dead threads. */
189         TAILQ_INSERT_HEAD(&_dead_list, curthread, dle);
190
191         /*
192          * Signal the garbage collector thread that there is something
193          * to clean up.
194          */
195         if (pthread_cond_signal(&_gc_cond) != 0)
196                 PANIC("Cannot signal gc cond");
197
198         /*
199          * Avoid a race condition where a scheduling signal can occur
200          * causing the garbage collector thread to run.  If this happens,
201          * the current thread can be cleaned out from under us.
202          */
203         _thread_kern_sig_defer();
204
205         /* Unlock the garbage collector mutex: */
206         if (pthread_mutex_unlock(&_gc_mutex) != 0)
207                 PANIC("Cannot unlock gc mutex");
208
209         /* Check if there is a thread joining this one: */
210         if (curthread->joiner != NULL) {
211                 pthread = curthread->joiner;
212                 curthread->joiner = NULL;
213
214                 /* Make the joining thread runnable: */
215                 PTHREAD_NEW_STATE(pthread, PS_RUNNING);
216
217                 /* Set the return value for the joining thread: */
218                 pthread->join_status.ret = curthread->ret;
219                 pthread->join_status.error = 0;
220                 pthread->join_status.thread = NULL;
221
222                 /* Make this thread collectable by the garbage collector. */
223                 PTHREAD_ASSERT(((curthread->attr.flags & PTHREAD_DETACHED) ==
224                     0), "Cannot join a detached thread");
225                 curthread->attr.flags |= PTHREAD_DETACHED;
226         }
227
228         /* Remove this thread from the thread list: */
229         TAILQ_REMOVE(&_thread_list, curthread, tle);
230
231         /* This thread will never be re-scheduled. */
232         _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__);
233
234         /* This point should not be reached. */
235         PANIC("Dead thread has resumed");
236 }
237
238 __strong_reference(_pthread_exit, pthread_exit);