]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_exit.c
This commit was generated by cvs2svn to compensate for changes in r53469,
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_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$
33  */
34 #include <errno.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #ifdef _THREAD_SAFE
39 #include <pthread.h>
40 #include "pthread_private.h"
41
42 void _exit(int status)
43 {
44         int             flags;
45         int             i;
46         struct itimerval itimer;
47
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);
54
55         /* Close the pthread kernel pipe: */
56         _thread_sys_close(_thread_kern_pipe[0]);
57         _thread_sys_close(_thread_kern_pipe[1]);
58
59         /*
60          * Enter a loop to set all file descriptors to blocking
61          * if they were not created as non-blocking:
62          */
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);
71                 }
72         }
73
74         /* Call the _exit syscall: */
75         _thread_sys__exit(status);
76 }
77
78 void
79 _thread_exit(char *fname, int lineno, char *string)
80 {
81         char            s[256];
82
83         /* Prepare an error message string: */
84         strcpy(s, "Fatal error '");
85         strcat(s, string);
86         strcat(s, "' at line ? ");
87         strcat(s, "in file ");
88         strcat(s, fname);
89         strcat(s, " (errno = ?");
90         strcat(s, ")\n");
91
92         /* Write the string to the standard error file descriptor: */
93         _thread_sys_write(2, s, strlen(s));
94
95         /* Force this process to exit: */
96         /* XXX - Do we want abort to be conditional on _PTHREADS_INVARIANTS? */
97 #if defined(_PTHREADS_INVARIANTS)
98         abort();
99 #else
100         _exit(1);
101 #endif
102 }
103
104 void
105 pthread_exit(void *status)
106 {
107         int             sig;
108         long            l;
109         pthread_t       pthread;
110
111         /* Check if this thread is already in the process of exiting: */
112         if ((_thread_run->flags & PTHREAD_EXITING) != 0) {
113                 char msg[128];
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);
115                 PANIC(msg);
116         }
117
118         /* Flag this thread as exiting: */
119         _thread_run->flags |= PTHREAD_EXITING;
120
121         /* Save the return value: */
122         _thread_run->ret = status;
123
124         while (_thread_run->cleanup != NULL) {
125                 pthread_cleanup_pop(1);
126         }
127
128         if (_thread_run->attr.cleanup_attr != NULL) {
129                 _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr);
130         }
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();
135         }
136
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;
141         }
142
143         /*
144          * Defer signals to protect the scheduling queues from access
145          * by the signal handler:
146          */
147         _thread_kern_sig_defer();
148
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);
153
154                 /* Wake the joined thread and let it detach this thread: */
155                 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
156         }
157
158         /*
159          * Undefer and handle pending signals, yielding if necessary:
160          */
161         _thread_kern_sig_undefer();
162
163         /*
164          * Lock the garbage collector mutex to ensure that the garbage
165          * collector is not using the dead thread list.
166          */
167         if (pthread_mutex_lock(&_gc_mutex) != 0)
168                 PANIC("Cannot lock gc mutex");
169
170         /* Add this thread to the list of dead threads. */
171         TAILQ_INSERT_HEAD(&_dead_list, _thread_run, dle);
172
173         /*
174          * Defer signals to protect the scheduling queues from access
175          * by the signal handler:
176          */
177         _thread_kern_sig_defer();
178
179         /* Remove this thread from the thread list: */
180         TAILQ_REMOVE(&_thread_list, _thread_run, tle);
181
182         /*
183          * Undefer and handle pending signals, yielding if necessary:
184          */
185         _thread_kern_sig_undefer();
186
187         /*
188          * Signal the garbage collector thread that there is something
189          * to clean up.
190          */
191         if (pthread_cond_signal(&_gc_cond) != 0)
192                 PANIC("Cannot signal gc cond");
193
194         /*
195          * Mark the thread as dead so it will not return if it
196          * gets context switched out when the mutex is unlocked.
197          */
198         PTHREAD_SET_STATE(_thread_run, PS_DEAD);
199
200         /* Unlock the garbage collector mutex: */
201         if (pthread_mutex_unlock(&_gc_mutex) != 0)
202                 PANIC("Cannot lock gc mutex");
203
204         /* This this thread will never be re-scheduled. */
205         _thread_kern_sched(NULL);
206
207         /* This point should not be reached. */
208         PANIC("Dead thread has resumed");
209 }
210 #endif