From 95a4332f329a015c10f8e8223f3e5d72446b6ec8 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Mon, 1 May 2000 16:19:40 +0000 Subject: [PATCH] Added atfork support. --- scheduler/pthread.c | 48 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/scheduler/pthread.c b/scheduler/pthread.c index a394e9f3385..7c086c7cb00 100644 --- a/scheduler/pthread.c +++ b/scheduler/pthread.c @@ -9,6 +9,7 @@ #include "config.h" +#include #include #include #include @@ -103,26 +104,51 @@ void __pthread_kill_other_threads_np(void) } strong_alias(__pthread_kill_other_threads_np, pthread_kill_other_threads_np); +/***** atfork *****/ + +#define MAX_ATFORK 8 /* libc doesn't need that many anyway */ + +static CRITICAL_SECTION atfork_section = CRITICAL_SECTION_INIT; +typedef void (*atfork_handler)(); +static atfork_handler atfork_prepare[MAX_ATFORK]; +static atfork_handler atfork_parent[MAX_ATFORK]; +static atfork_handler atfork_child[MAX_ATFORK]; +static int atfork_count; + int __pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)) { - P_OUTPUT("fixme:pthread_atfork\n"); - return 0; + EnterCriticalSection( &atfork_section ); + assert( atfork_count < MAX_ATFORK ); + atfork_prepare[atfork_count] = prepare; + atfork_parent[atfork_count] = parent; + atfork_child[atfork_count] = child; + atfork_count++; + LeaveCriticalSection( &atfork_section ); + return 0; } strong_alias(__pthread_atfork, pthread_atfork); pid_t PTHREAD_FORK(void) { - pid_t pid; - /* call prepare handlers */ - pid = LIBC_FORK(); - if (pid == 0) { - /* call child handlers */ - } else { - /* call parent handlers */ - } - return pid; + pid_t pid; + int i; + + EnterCriticalSection( &atfork_section ); + /* prepare handlers are called in reverse insertion order */ + for (i = atfork_count - 1; i >= 0; i--) atfork_prepare[i](); + if (!(pid = LIBC_FORK())) + { + InitializeCriticalSection( &atfork_section ); + for (i = 0; i < atfork_count; i++) atfork_child[i](); + } + else + { + for (i = 0; i < atfork_count; i++) atfork_parent[i](); + LeaveCriticalSection( &atfork_section ); + } + return pid; } #ifdef ALIAS_FORK strong_alias(PTHREAD_FORK, fork);