summaryrefslogtreecommitdiff
path: root/lib/libpthread/uthread/uthread_fork.c
diff options
context:
space:
mode:
authorBrad Smith <brad@cvs.openbsd.org>2005-10-30 03:37:35 +0000
committerBrad Smith <brad@cvs.openbsd.org>2005-10-30 03:37:35 +0000
commit62b1ec6e3667b903b262c213211453eadd3f1f93 (patch)
treeab5296a9073f4c6c817bd4ab531b69b2d76522c2 /lib/libpthread/uthread/uthread_fork.c
parentdec824965a7de11d9973b93c5288156da4541f74 (diff)
Add pthread_atfork(3)
From FreeBSD 'looks ok' fgsch@ miod@ man page reviewed by jmc@
Diffstat (limited to 'lib/libpthread/uthread/uthread_fork.c')
-rw-r--r--lib/libpthread/uthread/uthread_fork.c24
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/libpthread/uthread/uthread_fork.c b/lib/libpthread/uthread/uthread_fork.c
index 02f7656859b..09a3279392a 100644
--- a/lib/libpthread/uthread/uthread_fork.c
+++ b/lib/libpthread/uthread/uthread_fork.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uthread_fork.c,v 1.11 2003/08/06 21:08:05 millert Exp $ */
+/* $OpenBSD: uthread_fork.c,v 1.12 2005/10/30 03:37:34 brad Exp $ */
/*
* Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
* All rights reserved.
@@ -45,6 +45,7 @@ pid_t
fork(void)
{
struct pthread *curthread = _get_curthread();
+ struct pthread_atfork *af;
int i, flags;
pid_t ret;
pthread_t pthread;
@@ -55,9 +56,22 @@ fork(void)
*/
_thread_kern_sig_defer();
+ pthread_mutex_lock(&_atfork_mutex);
+
+ /* Run down atfork prepare handlers. */
+ TAILQ_FOREACH_REVERSE(af, &_atfork_list, atfork_head, qe) {
+ if (af->prepare != NULL)
+ af->prepare();
+ }
+
/* Fork a new process: */
if ((ret = _thread_sys_fork()) != 0) {
- /* Parent process or error. Nothing to do here. */
+ /* Run down atfork parent handlers. */
+ TAILQ_FOREACH(af, &_atfork_list, qe) {
+ if (af->parent != NULL)
+ af->parent();
+ }
+ pthread_mutex_unlock(&_atfork_mutex);
} else {
/* Close the pthread kernel pipe: */
_thread_sys_close(_thread_kern_pipe[0]);
@@ -178,6 +192,12 @@ fork(void)
}
}
}
+ /* Run down atfork child handlers. */
+ TAILQ_FOREACH(af, &_atfork_list, qe) {
+ if (af->child != NULL)
+ af->child();
+ }
+ _mutex_reinit(&_atfork_mutex);
}
/*