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
32 * $FreeBSD: src/lib/libc_r/uthread/uthread_fork.c,v 1.19.2.7 2002/10/22 14:44:03 fjoe Exp $
33 * $DragonFly: src/lib/libc_r/uthread/uthread_fork.c,v 1.2 2003/06/17 04:26:48 dillon Exp $
41 #include "pthread_private.h"
46 struct pthread *curthread = _get_curthread();
50 pthread_t pthread_save;
53 * Defer signals to protect the scheduling queues from access
54 * by the signal handler:
56 _thread_kern_sig_defer();
58 /* Fork a new process: */
59 if ((ret = __sys_fork()) != 0) {
60 /* Parent process or error. Nothing to do here. */
62 /* Close the pthread kernel pipe: */
63 __sys_close(_thread_kern_pipe[0]);
64 __sys_close(_thread_kern_pipe[1]);
66 /* Reset signals pending for the running thread: */
67 sigemptyset(&curthread->sigpend);
70 * Create a pipe that is written to by the signal handler to
71 * prevent signals being missed in calls to
74 if (__sys_pipe(_thread_kern_pipe) != 0) {
75 /* Cannot create pipe, so abort: */
76 PANIC("Cannot create pthread kernel pipe for forked process");
78 /* Get the flags for the read pipe: */
79 else if ((flags = __sys_fcntl(_thread_kern_pipe[0], F_GETFL, NULL)) == -1) {
80 /* Abort this application: */
83 /* Make the read pipe non-blocking: */
84 else if (__sys_fcntl(_thread_kern_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1) {
85 /* Abort this application: */
88 /* Get the flags for the write pipe: */
89 else if ((flags = __sys_fcntl(_thread_kern_pipe[1], F_GETFL, NULL)) == -1) {
90 /* Abort this application: */
93 /* Make the write pipe non-blocking: */
94 else if (__sys_fcntl(_thread_kern_pipe[1], F_SETFL, flags | O_NONBLOCK) == -1) {
95 /* Abort this application: */
98 /* Reinitialize the GC mutex: */
99 else if (_mutex_reinit(&_gc_mutex) != 0) {
100 /* Abort this application: */
101 PANIC("Cannot initialize GC mutex for forked process");
103 /* Reinitialize the GC condition variable: */
104 else if (_cond_reinit(&_gc_cond) != 0) {
105 /* Abort this application: */
106 PANIC("Cannot initialize GC condvar for forked process");
108 /* Initialize the ready queue: */
109 else if (_pq_init(&_readyq) != 0) {
110 /* Abort this application: */
111 PANIC("Cannot initialize priority ready queue.");
114 * Enter a loop to remove all threads other than
115 * the running thread from the thread list:
117 pthread = TAILQ_FIRST(&_thread_list);
118 while (pthread != NULL) {
119 /* Save the thread to be freed: */
120 pthread_save = pthread;
123 * Advance to the next thread before
124 * destroying the current thread:
126 pthread = TAILQ_NEXT(pthread, tle);
128 /* Make sure this isn't the running thread: */
129 if (pthread_save != curthread) {
130 /* Remove this thread from the list: */
131 TAILQ_REMOVE(&_thread_list,
134 if (pthread_save->attr.stackaddr_attr ==
135 NULL && pthread_save->stack != NULL) {
136 if (pthread_save->attr.stacksize_attr
137 == PTHREAD_STACK_DEFAULT) {
139 * Default-size stack.
142 struct stack *spare_stack;
145 = (pthread_save->stack
146 + PTHREAD_STACK_DEFAULT
147 - sizeof(struct stack));
148 SLIST_INSERT_HEAD(&_stackq,
155 free(pthread_save->stack);
158 if (pthread_save->specific_data != NULL)
159 free(pthread_save->specific_data);
161 if (pthread_save->poll_data.fds != NULL)
162 free(pthread_save->poll_data.fds);
168 /* Treat the current thread as the initial thread: */
169 _thread_initial = curthread;
171 /* Re-init the dead thread list: */
172 TAILQ_INIT(&_dead_list);
174 /* Re-init the waiting and work queues. */
175 TAILQ_INIT(&_waitingq);
178 /* Re-init the threads mutex queue: */
179 TAILQ_INIT(&curthread->mutexq);
181 /* No spinlocks yet: */
182 _spinblock_count = 0;
184 /* Don't queue signals yet: */
187 /* Initialize the scheduling switch hook routine: */
188 _sched_switch_hook = NULL;
190 /* Clear out any locks in the file descriptor table: */
191 for (i = 0; i < _thread_dtablesize; i++) {
192 if (_thread_fd_table[i] != NULL) {
193 /* Initialise the file locks: */
194 memset(&_thread_fd_table[i]->lock, 0,
195 sizeof(_thread_fd_table[i]->lock));
196 _thread_fd_table[i]->r_owner = NULL;
197 _thread_fd_table[i]->w_owner = NULL;
198 _thread_fd_table[i]->r_fname = NULL;
199 _thread_fd_table[i]->w_fname = NULL;
200 _thread_fd_table[i]->r_lineno = 0;;
201 _thread_fd_table[i]->w_lineno = 0;;
202 _thread_fd_table[i]->r_lockcount = 0;;
203 _thread_fd_table[i]->w_lockcount = 0;;
205 /* Initialise the read/write queues: */
206 TAILQ_INIT(&_thread_fd_table[i]->r_queue);
207 TAILQ_INIT(&_thread_fd_table[i]->w_queue);
214 * Undefer and handle pending signals, yielding if necessary:
216 _thread_kern_sig_undefer();
218 /* Return the process ID: */
222 __strong_reference(_fork, fork);