1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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 /* mods for uClibc: removed strong alias and defined funcs properly */
17 /* The "atfork" stuff */
21 #ifdef __ARCH_USE_MMU__
27 #include "internals.h"
30 void (*handler)(void);
31 struct handler_list * next;
34 static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
35 static struct handler_list * pthread_atfork_prepare = NULL;
36 static struct handler_list * pthread_atfork_parent = NULL;
37 static struct handler_list * pthread_atfork_child = NULL;
39 static void pthread_insert_list(struct handler_list ** list,
40 void (*handler)(void),
41 struct handler_list * newlist,
44 if (handler == NULL) return;
46 while(*list != NULL) list = &((*list)->next);
48 newlist->handler = handler;
49 newlist->next = *list;
53 struct handler_list_block {
54 struct handler_list prepare, parent, child;
57 int pthread_atfork(void (*prepare)(void),
61 struct handler_list_block * block =
62 (struct handler_list_block *) malloc(sizeof(struct handler_list_block));
63 if (block == NULL) return ENOMEM;
64 __pthread_mutex_lock(&pthread_atfork_lock);
65 /* "prepare" handlers are called in LIFO */
66 pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
67 /* "parent" handlers are called in FIFO */
68 pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
69 /* "child" handlers are called in FIFO */
70 pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
71 __pthread_mutex_unlock(&pthread_atfork_lock);
74 //strong_alias (__pthread_atfork, pthread_atfork)
76 static inline void pthread_call_handlers(struct handler_list * list)
78 for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
81 extern __typeof(fork) __libc_fork;
83 pid_t __fork(void) attribute_hidden;
87 struct handler_list * prepare, * child, * parent;
89 __pthread_mutex_lock(&pthread_atfork_lock);
90 prepare = pthread_atfork_prepare;
91 child = pthread_atfork_child;
92 parent = pthread_atfork_parent;
93 __pthread_mutex_unlock(&pthread_atfork_lock);
94 pthread_call_handlers(prepare);
97 __pthread_reset_main_thread();
99 pthread_call_handlers(child);
101 pthread_call_handlers(parent);
105 strong_alias(__fork,fork)
114 /* We can't support pthread_atfork without MMU, since we don't have
115 fork(), and we can't offer the correct semantics for vfork(). */
116 int pthread_atfork(void (*prepare)(void),
117 void (*parent)(void),
120 /* ENOMEM is probably pushing it a little bit.
121 Take it as `no *virtual* memory' :-) */