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 REGENTS 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
39 #include "pthread_private.h"
41 void _exit(int status)
45 struct itimerval itimer;
47 /* Disable the interval timer: */
48 itimer.it_interval.tv_sec = 0;
49 itimer.it_interval.tv_usec = 0;
50 itimer.it_value.tv_sec = 0;
51 itimer.it_value.tv_usec = 0;
52 setitimer(ITIMER_VIRTUAL, &itimer, NULL);
54 /* Close the pthread kernel pipe: */
55 _thread_sys_close(_thread_kern_pipe[0]);
56 _thread_sys_close(_thread_kern_pipe[1]);
59 * Enter a loop to set all file descriptors to blocking
60 * if they were not created as non-blocking:
62 for (i = 0; i < _thread_dtablesize; i++) {
63 /* Check if this file descriptor is in use: */
64 if (_thread_fd_table[i] != NULL &&
65 !(_thread_fd_table[i]->flags & O_NONBLOCK)) {
66 /* Get the current flags: */
67 flags = _thread_sys_fcntl(i, F_GETFL, NULL);
68 /* Clear the nonblocking file descriptor flag: */
69 _thread_sys_fcntl(i, F_SETFL, flags & ~O_NONBLOCK);
73 /* Call the _exit syscall: */
74 _thread_sys__exit(status);
78 _thread_exit(char *fname, int lineno, char *string)
82 /* Prepare an error message string: */
83 strcpy(s, "Fatal error '");
85 strcat(s, "' at line ? ");
86 strcat(s, "in file ");
88 strcat(s, " (errno = ?");
91 /* Write the string to the standard error file descriptor: */
92 _thread_sys_write(2, s, strlen(s));
94 /* Force this process to exit: */
99 pthread_exit(void *status)
105 /* Check if this thread is already in the process of exiting: */
106 if ((_thread_run->flags & PTHREAD_EXITING) != 0) {
108 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);
112 /* Flag this thread as exiting: */
113 _thread_run->flags |= PTHREAD_EXITING;
115 /* Save the return value: */
116 _thread_run->ret = status;
118 while (_thread_run->cleanup != NULL) {
119 pthread_cleanup_pop(1);
122 if (_thread_run->attr.cleanup_attr != NULL) {
123 _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr);
125 /* Check if there is thread specific data: */
126 if (_thread_run->specific_data != NULL) {
127 /* Run the thread-specific data destructors: */
128 _thread_cleanupspecific();
130 /* Check if there are any threads joined to this one: */
131 while ((pthread = _thread_queue_deq(&(_thread_run->join_queue))) != NULL) {
132 /* Wake the joined thread and let it detach this thread: */
133 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
137 * Lock the garbage collector mutex to ensure that the garbage
138 * collector is not using the dead thread list.
140 if (pthread_mutex_lock(&_gc_mutex) != 0)
141 PANIC("Cannot lock gc mutex");
143 /* Add this thread to the list of dead threads. */
144 _thread_run->nxt_dead = _thread_dead;
145 _thread_dead = _thread_run;
148 * Signal the garbage collector thread that there is something
151 if (pthread_cond_signal(&_gc_cond) != 0)
152 PANIC("Cannot signal gc cond");
154 /* Unlock the garbage collector mutex: */
155 if (pthread_mutex_unlock(&_gc_mutex) != 0)
156 PANIC("Cannot lock gc mutex");
158 /* This this thread will never be re-scheduled. */
159 _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__);
161 /* This point should not be reached. */
162 PANIC("Dead thread has resumed");