]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_exit.c
This commit was generated by cvs2svn to compensate for changes in r47145,
[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 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
30  * SUCH DAMAGE.
31  *
32  */
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #ifdef _THREAD_SAFE
38 #include <pthread.h>
39 #include "pthread_private.h"
40
41 void _exit(int status)
42 {
43         int             flags;
44         int             i;
45         struct itimerval itimer;
46
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_SCHED_TIMER, &itimer, NULL);
53
54         /* Close the pthread kernel pipe: */
55         _thread_sys_close(_thread_kern_pipe[0]);
56         _thread_sys_close(_thread_kern_pipe[1]);
57
58         /*
59          * Enter a loop to set all file descriptors to blocking
60          * if they were not created as non-blocking:
61          */
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);
70                 }
71         }
72
73         /* Call the _exit syscall: */
74         _thread_sys__exit(status);
75 }
76
77 void
78 _thread_exit(char *fname, int lineno, char *string)
79 {
80         char            s[256];
81
82         /* Prepare an error message string: */
83         strcpy(s, "Fatal error '");
84         strcat(s, string);
85         strcat(s, "' at line ? ");
86         strcat(s, "in file ");
87         strcat(s, fname);
88         strcat(s, " (errno = ?");
89         strcat(s, ")\n");
90
91         /* Write the string to the standard error file descriptor: */
92         _thread_sys_write(2, s, strlen(s));
93
94         /* Force this process to exit: */
95         _exit(1);
96 }
97
98 void
99 pthread_exit(void *status)
100 {
101         int             sig;
102         long            l;
103         pthread_t       pthread;
104
105         /* Check if this thread is already in the process of exiting: */
106         if ((_thread_run->flags & PTHREAD_EXITING) != 0) {
107                 char msg[128];
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);
109                 PANIC(msg);
110         }
111
112         /* Flag this thread as exiting: */
113         _thread_run->flags |= PTHREAD_EXITING;
114
115         /* Save the return value: */
116         _thread_run->ret = status;
117
118         while (_thread_run->cleanup != NULL) {
119                 pthread_cleanup_pop(1);
120         }
121
122         if (_thread_run->attr.cleanup_attr != NULL) {
123                 _thread_run->attr.cleanup_attr(_thread_run->attr.arg_attr);
124         }
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();
129         }
130
131         /*
132          * Guard against preemption by a scheduling signal.  A change of
133          * thread state modifies the waiting and priority queues.
134          */
135         _thread_kern_sched_defer();
136
137         /* Check if there are any threads joined to this one: */
138         while ((pthread = _thread_queue_deq(&(_thread_run->join_queue))) != NULL) {
139                 /* Wake the joined thread and let it detach this thread: */
140                 PTHREAD_NEW_STATE(pthread,PS_RUNNING);
141         }
142
143         /*
144          * Reenable preemption and yield if a scheduling signal
145          * occurred while in the critical region.
146          */
147         _thread_kern_sched_undefer();
148
149         /*
150          * Lock the garbage collector mutex to ensure that the garbage
151          * collector is not using the dead thread list.
152          */
153         if (pthread_mutex_lock(&_gc_mutex) != 0)
154                 PANIC("Cannot lock gc mutex");
155
156         /* Add this thread to the list of dead threads. */
157         _thread_run->nxt_dead = _thread_dead;
158         _thread_dead = _thread_run;
159
160         /*
161          * Signal the garbage collector thread that there is something
162          * to clean up.
163          */
164         if (pthread_cond_signal(&_gc_cond) != 0)
165                 PANIC("Cannot signal gc cond");
166
167         /* Unlock the garbage collector mutex: */
168         if (pthread_mutex_unlock(&_gc_mutex) != 0)
169                 PANIC("Cannot lock gc mutex");
170
171         /* This this thread will never be re-scheduled. */
172         _thread_kern_sched_state(PS_DEAD, __FILE__, __LINE__);
173
174         /* This point should not be reached. */
175         PANIC("Dead thread has resumed");
176 }
177 #endif