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 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
34 #include <sys/param.h>
41 #include "pthread_private.h"
43 static void free_thread_resources(struct pthread *thread);
45 __weak_reference(_fork, fork);
50 struct pthread *curthread = _get_curthread();
51 struct pthread_atfork *af;
52 int i, flags, use_deadlist = 0;
55 pthread_t pthread_save;
58 * Defer signals to protect the scheduling queues from access
59 * by the signal handler:
61 _thread_kern_sig_defer();
63 _pthread_mutex_lock(&_atfork_mutex);
65 /* Run down atfork prepare handlers. */
66 TAILQ_FOREACH_REVERSE(af, &_atfork_list, atfork_head, qe) {
67 if (af->prepare != NULL)
71 /* Fork a new process: */
72 if ((ret = __sys_fork()) != 0) {
73 /* Run down atfork parent handlers. */
74 TAILQ_FOREACH(af, &_atfork_list, qe) {
75 if (af->parent != NULL)
78 _pthread_mutex_unlock(&_atfork_mutex);
81 /* Close the pthread kernel pipe: */
82 __sys_close(_thread_kern_pipe[0]);
83 __sys_close(_thread_kern_pipe[1]);
85 /* Reset signals pending for the running thread: */
86 sigemptyset(&curthread->sigpend);
89 * Create a pipe that is written to by the signal handler to
90 * prevent signals being missed in calls to
93 if (__sys_pipe(_thread_kern_pipe) != 0) {
94 /* Cannot create pipe, so abort: */
95 PANIC("Cannot create pthread kernel pipe for forked process");
97 /* Get the flags for the read pipe: */
98 else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) {
99 /* Abort this application: */
102 /* Make the read pipe non-blocking: */
103 else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) {
104 /* Abort this application: */
107 /* Get the flags for the write pipe: */
108 else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) {
109 /* Abort this application: */
112 /* Make the write pipe non-blocking: */
113 else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) {
114 /* Abort this application: */
117 /* Reinitialize the GC mutex: */
118 else if (_mutex_reinit(&_gc_mutex) != 0) {
119 /* Abort this application: */
120 PANIC("Cannot initialize GC mutex for forked process");
122 /* Reinitialize the GC condition variable: */
123 else if (_cond_reinit(&_gc_cond) != 0) {
124 /* Abort this application: */
125 PANIC("Cannot initialize GC condvar for forked process");
127 /* Initialize the ready queue: */
128 else if (_pq_init(&_readyq) != 0) {
129 /* Abort this application: */
130 PANIC("Cannot initialize priority ready queue.");
133 * Enter a loop to remove all threads other than
134 * the running thread from the thread list:
136 if ((pthread = TAILQ_FIRST(&_thread_list)) == NULL) {
137 pthread = TAILQ_FIRST(&_dead_list);
140 while (pthread != NULL) {
141 /* Save the thread to be freed: */
142 pthread_save = pthread;
145 * Advance to the next thread before
146 * destroying the current thread:
148 if (use_deadlist != 0)
149 pthread = TAILQ_NEXT(pthread, dle);
151 pthread = TAILQ_NEXT(pthread, tle);
153 /* Make sure this isn't the running thread: */
154 if (pthread_save != curthread) {
156 * Remove this thread from the
159 if (use_deadlist != 0)
160 TAILQ_REMOVE(&_thread_list,
163 TAILQ_REMOVE(&_thread_list,
166 free_thread_resources(pthread_save);
170 * Switch to the deadlist when the active
171 * thread list has been consumed. This can't
172 * be at the top of the loop because it is
173 * used to determine to which list the thread
174 * belongs (when it is removed from the list).
176 if (pthread == NULL) {
177 pthread = TAILQ_FIRST(&_dead_list);
182 /* Treat the current thread as the initial thread: */
183 _thread_initial = curthread;
185 /* Re-init the dead thread list: */
186 TAILQ_INIT(&_dead_list);
188 /* Re-init the waiting and work queues. */
189 TAILQ_INIT(&_waitingq);
192 /* Re-init the threads mutex queue: */
193 TAILQ_INIT(&curthread->mutexq);
195 /* No spinlocks yet: */
196 _spinblock_count = 0;
198 /* Don't queue signals yet: */
201 /* Initialize the scheduling switch hook routine: */
202 _sched_switch_hook = NULL;
204 /* Clear out any locks in the file descriptor table: */
205 for (i = 0; i < _thread_dtablesize; i++) {
206 if (_thread_fd_table[i] != NULL) {
207 /* Initialise the file locks: */
208 memset(&_thread_fd_table[i]->lock, 0,
209 sizeof(_thread_fd_table[i]->lock));
210 _thread_fd_table[i]->r_owner = NULL;
211 _thread_fd_table[i]->w_owner = NULL;
212 _thread_fd_table[i]->r_fname = NULL;
213 _thread_fd_table[i]->w_fname = NULL;
214 _thread_fd_table[i]->r_lineno = 0;;
215 _thread_fd_table[i]->w_lineno = 0;;
216 _thread_fd_table[i]->r_lockcount = 0;;
217 _thread_fd_table[i]->w_lockcount = 0;;
219 /* Initialise the read/write queues: */
220 TAILQ_INIT(&_thread_fd_table[i]->r_queue);
221 TAILQ_INIT(&_thread_fd_table[i]->w_queue);
225 /* Run down atfork child handlers. */
226 TAILQ_FOREACH(af, &_atfork_list, qe) {
227 if (af->child != NULL)
230 _mutex_reinit(&_atfork_mutex);
235 * Undefer and handle pending signals, yielding if necessary:
237 _thread_kern_sig_undefer();
239 /* Return the process ID: */
244 free_thread_resources(struct pthread *thread)
247 /* Check to see if the threads library allocated the stack. */
248 if ((thread->attr.stackaddr_attr == NULL) && (thread->stack != NULL)) {
250 * Since this is being called from fork, we are currently single
251 * threaded so there is no need to protect the call to
252 * _thread_stack_free() with _gc_mutex.
254 _thread_stack_free(thread->stack, thread->attr.stacksize_attr,
255 thread->attr.guardsize_attr);
258 if (thread->specific != NULL)
259 free(thread->specific);
261 if (thread->poll_data.fds != NULL)
262 free(thread->poll_data.fds);