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