(getrlimit): Set strong and weak names properly.
[glibc/history.git] / linuxthreads / ptfork.c
blob32b1d26d926d6010bae6437081c81689e2554a8f
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 <errno.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include "pthread.h"
22 #include "internals.h"
24 struct handler_list {
25 void (*handler)(void);
26 struct handler_list * next;
29 static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
30 static struct handler_list * pthread_atfork_prepare = NULL;
31 static struct handler_list * pthread_atfork_parent = NULL;
32 static struct handler_list * pthread_atfork_child = NULL;
34 static void pthread_insert_list(struct handler_list ** list,
35 void (*handler)(void),
36 struct handler_list * newlist,
37 int at_end)
39 if (handler == NULL) return;
40 if (at_end) {
41 while(*list != NULL) list = &((*list)->next);
43 newlist->handler = handler;
44 newlist->next = *list;
45 *list = newlist;
48 struct handler_list_block {
49 struct handler_list prepare, parent, child;
52 int __pthread_atfork(void (*prepare)(void),
53 void (*parent)(void),
54 void (*child)(void))
56 struct handler_list_block * block =
57 (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
58 if (block == NULL) return ENOMEM;
59 pthread_mutex_lock(&pthread_atfork_lock);
60 /* "prepare" handlers are called in LIFO */
61 pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
62 /* "parent" handlers are called in FIFO */
63 pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
64 /* "child" handlers are called in FIFO */
65 pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
66 pthread_mutex_unlock(&pthread_atfork_lock);
67 return 0;
69 weak_alias (__pthread_atfork, pthread_atfork)
71 static inline void pthread_call_handlers(struct handler_list * list)
73 for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
76 extern int __fork(void);
78 int fork(void)
80 int pid;
81 struct handler_list * prepare, * child, * parent;
83 pthread_mutex_lock(&pthread_atfork_lock);
84 prepare = pthread_atfork_prepare;
85 child = pthread_atfork_child;
86 parent = pthread_atfork_parent;
87 pthread_mutex_unlock(&pthread_atfork_lock);
88 pthread_call_handlers(prepare);
89 pid = __fork();
90 if (pid == 0) {
91 __pthread_reset_main_thread();
92 __fresetlockfiles();
93 pthread_call_handlers(child);
94 } else {
95 pthread_call_handlers(parent);
97 return pid;