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. 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.
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
40 #include "pthread_private.h"
42 void _exit(int status)
46 struct itimerval itimer;
48 /* Disable the interval timer: */
49 itimer.it_interval.tv_sec = 0;
50 itimer.it_interval.tv_usec = 0;
51 itimer.it_value.tv_sec = 0;
52 itimer.it_value.tv_usec = 0;
53 setitimer(_ITIMER_SCHED_TIMER, &itimer, NULL);
55 /* Close the pthread kernel pipe: */
56 _thread_sys_close(_thread_kern_pipe[0]);
57 _thread_sys_close(_thread_kern_pipe[1]);
60 * Enter a loop to set all file descriptors to blocking
61 * if they were not created as non-blocking:
63 for (i = 0; i < _thread_dtablesize; i++) {
64 /* Check if this file descriptor is in use: */
65 if (_thread_fd_table[i] != NULL &&
66 !(_thread_fd_table[i]->flags & O_NONBLOCK)) {
67 /* Get the current flags: */
68 flags = _thread_sys_fcntl(i, F_GETFL, NULL);
69 /* Clear the nonblocking file descriptor flag: */
70 _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK);
74 /* Call the _exit syscall: */
75 _thread_sys__exit(status);
79 _thread_exit(char *fname, int lineno, char *string)
83 /* Prepare an error message string: */
84 strcpy(s, "Fatal error '");
86 strcat(s, "' at line ? ");
87 strcat(s, "in file ");
89 strcat(s, " (errno = ?");
92 /* Write the string to the standard error file descriptor: */
93 _thread_sys_write(2, s, strlen(s));
95 /* Force this process to exit: */
96 /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */
97 #if defined(_PTHREADS_INVARIANTS)
105 pthread_exit(void *status)
111 /* Check if this thread is already in the process of exiting: */
112 if ((_thread_run->flags & PTHREAD_EXITING) != 0) {
114 snprintf(msg,"Thread %p has called pthread_exit() from a destructor. POSIX 1003.1 1996 s16.2.5.2 does not allow this!",_thread_run);
118 /* Flag this thread as exiting: */
119 _thread_run->flags |= PTHREAD_EXITING;
121 /* Save the return value: */
122 _thread_run->ret = status;
124 while (_thread_run->cleanup != NULL) {
125 pthread_cleanup_pop(1);
128 if (_thread_run->attr.cleanup_attr != NULL) {
129 _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr);
131 /* Check if there is thread specific data: */
132 if (_thread_run->specific_data != NULL) {
133 /* Run the thread-specific data destructors: */
134 _thread_cleanupspecific();
137 /* Free thread-specific poll_data structure, if allocated */
138 if (_thread_run->poll_data.fds != NULL) {
139 free(_thread_run->poll_data.fds);
140 _thread_run->poll_data.fds = NULL;
144 * Defer signals to protect the scheduling queues from access
145 * by the signal handler:
147 _thread_kern_sig_defer();
149 /* Check if there are any threads joined to this one: */
150 while ((pthread = TAILQ_FIRST(&(_thread_run->join_queue))) != NULL) {
151 /* Remove the thread from the queue: */
152 TAILQ_REMOVE(&_thread_run->join_queue, pthread, qe);
154 /* Wake the joined thread and let it detach this thread: */
155 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
159 * Undefer and handle pending signals, yielding if necessary:
161 _thread_kern_sig_undefer();
164 * Lock the garbage collector mutex to ensure that the garbage
165 * collector is not using the dead thread list.
167 if (pthread_mutex_lock(&_gc_mutex) != 0)
168 PANIC("Cannot lock gc mutex");
170 /* Add this thread to the list of dead threads. */
171 TAILQ_INSERT_HEAD(&_dead_list, _thread_run, dle);
174 * Defer signals to protect the scheduling queues from access
175 * by the signal handler:
177 _thread_kern_sig_defer();
179 /* Remove this thread from the thread list: */
180 TAILQ_REMOVE(&_thread_list, _thread_run, tle);
183 * Undefer and handle pending signals, yielding if necessary:
185 _thread_kern_sig_undefer();
188 * Signal the garbage collector thread that there is something
191 if (pthread_cond_signal(&_gc_cond) != 0)
192 PANIC("Cannot signal gc cond");
195 * Mark the thread as dead so it will not return if it
196 * gets context switched out when the mutex is unlocked.
198 PTHREAD_SET_STATE(_thread_run, PS_DEAD);
200 /* Unlock the garbage collector mutex: */
201 if (pthread_mutex_unlock(&_gc_mutex) != 0)
202 PANIC("Cannot lock gc mutex");
204 /* This this thread will never be re-scheduled. */
205 _thread_kern_sched(NULL);
207 /* This point should not be reached. */
208 PANIC("Dead thread has resumed");