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