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);
136 /* Lock the thread list: */
139 /* Check if the running thread is at the head of the linked list: */
140 if (_thread_link_list == _thread_run) {
141 /* There is no previous thread: */
142 _thread_link_list = _thread_run->nxt;
144 /* Point to the first thread in the list: */
145 pthread = _thread_link_list;
148 * Enter a loop to find the thread in the linked list before
149 * the running thread:
151 while (pthread != NULL && pthread->nxt != _thread_run) {
152 /* Point to the next thread: */
153 pthread = pthread->nxt;
156 /* Check that a previous thread was found: */
157 if (pthread != NULL) {
159 * Point the previous thread to the one after the
162 pthread->nxt = _thread_run->nxt;
166 /* Unlock the thread list: */
167 _unlock_thread_list();
169 /* Lock the dead thread list: */
170 _lock_dead_thread_list();
173 * This thread will never run again. Add it to the list of dead
176 _thread_run->nxt = _thread_dead;
177 _thread_dead = _thread_run;
179 /* Unlock the dead thread list: */
180 _unlock_dead_thread_list();
183 * The running thread is no longer in the thread link list so it will
186 _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__);
188 /* This point should not be reached. */
189 PANIC("Dead thread has resumed");