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__
23 #include <bits/uClibc_mutex.h>
28 #include "internals.h"
31 void (*handler
)(void);
32 struct handler_list
* next
;
35 static pthread_mutex_t pthread_atfork_lock
= PTHREAD_MUTEX_INITIALIZER
;
36 static struct handler_list
* pthread_atfork_prepare
= NULL
;
37 static struct handler_list
* pthread_atfork_parent
= NULL
;
38 static struct handler_list
* pthread_atfork_child
= NULL
;
41 __UCLIBC_MUTEX_EXTERN(__malloc_heap_lock
);
42 __UCLIBC_MUTEX_EXTERN(__malloc_sbrk_lock
);
43 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
44 __UCLIBC_MUTEX_EXTERN(__malloc_mmb_heap_lock
);
46 #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
47 __UCLIBC_MUTEX_EXTERN(__malloc_lock
);
50 static void pthread_insert_list(struct handler_list
** list
,
51 void (*handler
)(void),
52 struct handler_list
* newlist
,
55 if (handler
== NULL
) return;
57 while(*list
!= NULL
) list
= &((*list
)->next
);
59 newlist
->handler
= handler
;
60 newlist
->next
= *list
;
64 struct handler_list_block
{
65 struct handler_list prepare
, parent
, child
;
68 int pthread_atfork(void (*prepare
)(void),
72 struct handler_list_block
* block
=
73 (struct handler_list_block
*) malloc(sizeof(struct handler_list_block
));
74 if (block
== NULL
) return ENOMEM
;
75 __pthread_mutex_lock(&pthread_atfork_lock
);
76 /* "prepare" handlers are called in LIFO */
77 pthread_insert_list(&pthread_atfork_prepare
, prepare
, &block
->prepare
, 0);
78 /* "parent" handlers are called in FIFO */
79 pthread_insert_list(&pthread_atfork_parent
, parent
, &block
->parent
, 1);
80 /* "child" handlers are called in FIFO */
81 pthread_insert_list(&pthread_atfork_child
, child
, &block
->child
, 1);
82 __pthread_mutex_unlock(&pthread_atfork_lock
);
85 /*strong_alias (__pthread_atfork, pthread_atfork)*/
87 static __inline__
void pthread_call_handlers(struct handler_list
* list
)
89 for (/*nothing*/; list
!= NULL
; list
= list
->next
) (list
->handler
)();
92 void __pthread_once_fork_prepare(void);
93 void __pthread_once_fork_child(void);
94 void __pthread_once_fork_parent(void);
96 static pid_t
__fork(void)
99 struct handler_list
* prepare
, * child
, * parent
;
101 __pthread_mutex_lock(&pthread_atfork_lock
);
102 prepare
= pthread_atfork_prepare
;
103 child
= pthread_atfork_child
;
104 parent
= pthread_atfork_parent
;
105 pthread_call_handlers(prepare
);
107 __pthread_once_fork_prepare();
109 __pthread_mutex_lock(&__malloc_sbrk_lock
);
110 __pthread_mutex_lock(&__malloc_heap_lock
);
111 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
112 __pthread_mutex_lock(&__malloc_mmb_heap_lock
);
114 #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
115 __pthread_mutex_lock(&__malloc_lock
);
120 #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
121 __libc_lock_init_recursive(__malloc_lock
);
122 #elif defined(__MALLOC__)
123 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
124 __libc_lock_init_adaptive(__malloc_mmb_heap_lock
);
126 __libc_lock_init_adaptive(__malloc_heap_lock
);
127 __libc_lock_init(__malloc_sbrk_lock
);
129 __libc_lock_init_adaptive(pthread_atfork_lock
);
130 __pthread_reset_main_thread();
132 __pthread_once_fork_child();
133 pthread_call_handlers(child
);
135 #if defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
136 __pthread_mutex_unlock(&__malloc_lock
);
137 #elif defined(__MALLOC__)
138 #ifdef __UCLIBC_UCLINUX_BROKEN_MUNMAP__
139 __pthread_mutex_unlock(&__malloc_mmb_heap_lock
);
141 __pthread_mutex_unlock(&__malloc_heap_lock
);
142 __pthread_mutex_unlock(&__malloc_sbrk_lock
);
144 __pthread_mutex_unlock(&pthread_atfork_lock
);
145 __pthread_once_fork_parent();
146 pthread_call_handlers(parent
);
150 strong_alias(__fork
,fork
)
151 strong_alias(__fork
,vfork
)
155 /* We can't support pthread_atfork without MMU, since we don't have
156 fork(), and we can't offer the correct semantics for vfork().
157 But instead of failing, just provide a dummy */
158 int pthread_atfork(void (*prepare
)(void),
159 void (*parent
)(void),