2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Neither the name of the author nor the names of any co-contributors
12 * may be used to endorse or promote products derived from this software
13 * without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/lib/libpthread/thread/thr_atfork.c,v 1.1 2003/11/05 03:42:10 davidxu Exp $
31 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by John Birrell.
45 * 4. Neither the name of the author nor the names of any co-contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * $FreeBSD: src/lib/libpthread/thread/thr_fork.c,v 1.34 2003/11/05 18:18:45 deischen Exp $
62 * $DragonFly: src/lib/libthread_xu/thread/thr_fork.c,v 1.3 2005/03/29 19:26:20 joerg Exp $
65 #include <machine/tls.h>
75 #include "libc_private.h"
76 #include "thr_private.h"
78 __weak_reference(_pthread_atfork, pthread_atfork);
81 _pthread_atfork(void (*prepare)(void), void (*parent)(void),
84 struct pthread *curthread;
85 struct pthread_atfork *af;
89 if ((af = malloc(sizeof(struct pthread_atfork))) == NULL)
92 curthread = tls_get_curthread();
93 af->prepare = prepare;
96 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
97 TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe);
98 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
103 * For a while, allow libpthread to work with a libc that doesn't
104 * export the malloc lock.
106 #pragma weak __malloc_lock
108 __weak_reference(_fork, fork);
113 static umtx_t inprogress;
117 struct pthread *curthread;
118 struct pthread_atfork *af;
121 #ifndef __DragonFly__
125 if (!_thr_is_inited())
126 return (__sys_fork());
128 curthread = tls_get_curthread();
130 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
134 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
135 _thr_umtx_wait(&inprogress, tmp, NULL, 0);
136 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
141 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
143 /* Run down atfork prepare handlers. */
144 TAILQ_FOREACH_REVERSE(af, &_thr_atfork_list, atfork_head, qe) {
145 if (af->prepare != NULL)
149 #ifndef __DragonFly__
151 * Try our best to protect memory from being corrupted in
152 * child process because another thread in malloc code will
153 * simply be kill by fork().
155 if ((_thr_isthreaded() != 0) && (__malloc_lock != NULL)) {
157 _spinlock(__malloc_lock);
164 * Block all signals until we reach a safe point.
166 _thr_signal_block(curthread);
168 /* Fork a new process: */
169 if ((ret = __sys_fork()) == 0) {
173 curthread->cancelflags &= ~THR_CANCEL_NEEDED;
175 * Thread list will be reinitialized, and later we call
176 * _libpthread_init(), it will add us back to list.
178 curthread->tlflags &= ~(TLFLAGS_IN_TDLIST | TLFLAGS_DETACHED);
180 /* child is a new kernel thread. */
181 curthread->tid = _thr_get_tid();
183 /* clear other threads locked us. */
184 _thr_umtx_init(&curthread->lock);
185 _thr_umtx_init(&_thr_atfork_lock);
188 /* reinitialize libc spinlocks, this includes __malloc_lock. */
189 _thr_spinlock_init();
190 _mutex_fork(curthread);
192 /* reinitalize library. */
193 _libpthread_init(curthread);
195 /* Ready to continue, unblock signals. */
196 _thr_signal_unblock(curthread);
198 /* Run down atfork child handlers. */
199 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
200 if (af->child != NULL)
207 #ifndef __DragonFly__
209 _spinunlock(__malloc_lock);
212 /* Ready to continue, unblock signals. */
213 _thr_signal_unblock(curthread);
215 /* Run down atfork parent handlers. */
216 TAILQ_FOREACH(af, &_thr_atfork_list, qe) {
217 if (af->parent != NULL)
221 THR_UMTX_LOCK(curthread, &_thr_atfork_lock);
224 _thr_umtx_wake(&inprogress, waiters);
225 THR_UMTX_UNLOCK(curthread, &_thr_atfork_lock);
229 /* Return the process ID: */