Start flushing out the IPI routines and do some clean-up.
[dragonfly/vkernel-mp.git] / lib / libc_r / uthread / uthread_fork.c
blob01c6cf51df31b1eb48a2f563e79b4044143c7c56
1 /*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
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.
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.
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.5 2006/08/03 18:40:46 swildner Exp $
35 #include <errno.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <pthread.h>
41 #include "pthread_private.h"
43 pid_t
44 _fork(void)
46 struct pthread *curthread = _get_curthread();
47 int i, flags;
48 pid_t ret;
49 pthread_t pthread;
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. */
61 } else {
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);
69 _thread_mksigpipe();
71 /* Reinitialize the GC mutex: */
72 if (_mutex_reinit(&_gc_mutex) != 0) {
73 /* Abort this application: */
74 PANIC("Cannot initialize GC mutex for forked process");
76 /* Reinitialize the GC condition variable: */
77 else if (_cond_reinit(&_gc_cond) != 0) {
78 /* Abort this application: */
79 PANIC("Cannot initialize GC condvar for forked process");
81 /* Initialize the ready queue: */
82 else if (_pq_init(&_readyq) != 0) {
83 /* Abort this application: */
84 PANIC("Cannot initialize priority ready queue.");
85 } else {
87 * Enter a loop to remove all threads other than
88 * the running thread from the thread list:
90 pthread = TAILQ_FIRST(&_thread_list);
91 while (pthread != NULL) {
92 /* Save the thread to be freed: */
93 pthread_save = pthread;
96 * Advance to the next thread before
97 * destroying the current thread:
99 pthread = TAILQ_NEXT(pthread, tle);
101 /* Make sure this isn't the running thread: */
102 if (pthread_save != curthread) {
103 /* Remove this thread from the list: */
104 TAILQ_REMOVE(&_thread_list,
105 pthread_save, tle);
107 if (pthread_save->attr.stackaddr_attr ==
108 NULL && pthread_save->stack != NULL) {
109 if (pthread_save->attr.stacksize_attr
110 == PTHREAD_STACK_DEFAULT) {
112 * Default-size stack.
113 * Cache it:
115 struct stack *spare_stack;
117 spare_stack
118 = (pthread_save->stack
119 + PTHREAD_STACK_DEFAULT
120 - sizeof(struct stack));
121 SLIST_INSERT_HEAD(&_stackq,
122 spare_stack, qe);
123 } else
125 * Free the stack of
126 * the dead thread:
128 free(pthread_save->stack);
131 if (pthread_save->specific_data != NULL)
132 free(pthread_save->specific_data);
134 if (pthread_save->poll_data.fds != NULL)
135 free(pthread_save->poll_data.fds);
137 _rtld_free_tls(pthread_save->tcb);
138 free(pthread_save);
142 /* Treat the current thread as the initial thread: */
143 _thread_initial = curthread;
145 /* Re-init the dead thread list: */
146 TAILQ_INIT(&_dead_list);
148 /* Re-init the waiting and work queues. */
149 TAILQ_INIT(&_waitingq);
150 TAILQ_INIT(&_workq);
152 /* Re-init the threads mutex queue: */
153 TAILQ_INIT(&curthread->mutexq);
155 /* No spinlocks yet: */
156 _spinblock_count = 0;
158 /* Don't queue signals yet: */
159 _queue_signals = 0;
161 /* Initialize the scheduling switch hook routine: */
162 _sched_switch_hook = NULL;
164 /* Clear out any locks in the file descriptor table: */
165 for (i = 0; i < _thread_dtablesize; i++) {
166 if (_thread_fd_table[i] != NULL) {
167 /* Initialise the file locks: */
168 memset(&_thread_fd_table[i]->lock, 0,
169 sizeof(_thread_fd_table[i]->lock));
170 _thread_fd_table[i]->r_owner = NULL;
171 _thread_fd_table[i]->w_owner = NULL;
172 _thread_fd_table[i]->r_fname = NULL;
173 _thread_fd_table[i]->w_fname = NULL;
174 _thread_fd_table[i]->r_lineno = 0;
175 _thread_fd_table[i]->w_lineno = 0;
176 _thread_fd_table[i]->r_lockcount = 0;
177 _thread_fd_table[i]->w_lockcount = 0;
179 /* Initialise the read/write queues: */
180 TAILQ_INIT(&_thread_fd_table[i]->r_queue);
181 TAILQ_INIT(&_thread_fd_table[i]->w_queue);
188 * Undefer and handle pending signals, yielding if necessary:
190 _thread_kern_sig_undefer();
192 /* Return the process ID: */
193 return (ret);
196 __strong_reference(_fork, fork);