Update.
[glibc.git] / linuxthreads / ptfork.c
blob8a7b7972a36cb320f9ddc0375831ce2b5f340112
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* The "atfork" stuff */
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include "pthread.h"
21 #include "internals.h"
23 struct handler_list {
24 void (*handler)(void);
25 struct handler_list * next;
28 static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
29 static struct handler_list * pthread_atfork_prepare = NULL;
30 static struct handler_list * pthread_atfork_parent = NULL;
31 static struct handler_list * pthread_atfork_child = NULL;
33 static void pthread_insert_list(struct handler_list ** list,
34 void (*handler)(void),
35 struct handler_list * newlist,
36 int at_end)
38 if (handler == NULL) return;
39 if (at_end) {
40 while(*list != NULL) list = &((*list)->next);
42 newlist->handler = handler;
43 newlist->next = *list;
44 *list = newlist;
47 struct handler_list_block {
48 struct handler_list prepare, parent, child;
51 int __pthread_atfork(void (*prepare)(void),
52 void (*parent)(void),
53 void (*child)(void))
55 struct handler_list_block * block =
56 (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
57 if (block == NULL) return ENOMEM;
58 pthread_mutex_lock(&pthread_atfork_lock);
59 /* "prepare" handlers are called in LIFO */
60 pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
61 /* "parent" handlers are called in FIFO */
62 pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
63 /* "child" handlers are called in FIFO */
64 pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
65 pthread_mutex_unlock(&pthread_atfork_lock);
66 return 0;
68 weak_alias (__pthread_atfork, pthread_atfork)
70 static inline void pthread_call_handlers(struct handler_list * list)
72 for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
75 extern int __fork(void);
77 int fork(void)
79 int pid;
80 struct handler_list * prepare, * child, * parent;
82 pthread_mutex_lock(&pthread_atfork_lock);
83 prepare = pthread_atfork_prepare;
84 child = pthread_atfork_child;
85 parent = pthread_atfork_parent;
86 pthread_mutex_unlock(&pthread_atfork_lock);
87 pthread_call_handlers(prepare);
88 pid = __fork();
89 if (pid == 0) {
90 __pthread_reset_main_thread();
91 __fresetlockfiles();
92 pthread_call_handlers(child);
93 } else {
94 pthread_call_handlers(parent);
96 return pid;