]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/linuxthreads.old/ptfork.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / linuxthreads.old / ptfork.c
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.                 */
14
15 /* mods for uClibc: removed strong alias and defined funcs properly */
16
17 /* The "atfork" stuff */
18
19 #include <errno.h>
20
21 #ifdef __ARCH_USE_MMU__
22
23 #include <bits/uClibc_mutex.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include "pthread.h"
28 #include "internals.h"
29
30 struct handler_list {
31   void (*handler)(void);
32   struct handler_list * next;
33 };
34
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;
39
40 #ifdef __MALLOC__
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);
45 #endif
46 #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
47 __UCLIBC_MUTEX_EXTERN(__malloc_lock);
48 #endif
49
50 static void pthread_insert_list(struct handler_list ** list,
51                                 void (*handler)(void),
52                                 struct handler_list * newlist,
53                                 int at_end)
54 {
55   if (handler == NULL) return;
56   if (at_end) {
57     while(*list != NULL) list = &((*list)->next);
58   }
59   newlist->handler = handler;
60   newlist->next = *list;
61   *list = newlist;
62 }
63
64 struct handler_list_block {
65   struct handler_list prepare, parent, child;
66 };
67
68 int pthread_atfork(void (*prepare)(void),
69                      void (*parent)(void),
70                      void (*child)(void))
71 {
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);
83   return 0;
84 }
85 /*strong_alias (__pthread_atfork, pthread_atfork)*/
86
87 static __inline__ void pthread_call_handlers(struct handler_list * list)
88 {
89   for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
90 }
91
92 void __pthread_once_fork_prepare(void);
93 void __pthread_once_fork_child(void);
94 void __pthread_once_fork_parent(void);
95
96 static pid_t __fork(void)
97 {
98   pid_t pid;
99   struct handler_list * prepare, * child, * parent;
100
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);
106
107   __pthread_once_fork_prepare();
108 #ifdef __MALLOC__
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);
113 #endif
114 #elif defined(__MALLOC_STANDARD__) || defined(__MALLOC_SIMPLE__)
115   __pthread_mutex_lock(&__malloc_lock);
116 #endif
117
118   pid = __libc_fork();
119   if (pid == 0) {
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);
125 #endif
126     __libc_lock_init_adaptive(__malloc_heap_lock);
127     __libc_lock_init(__malloc_sbrk_lock);
128 #endif
129     __libc_lock_init_adaptive(pthread_atfork_lock);
130     __pthread_reset_main_thread();
131     __fresetlockfiles();
132     __pthread_once_fork_child();
133     pthread_call_handlers(child);
134   } else {
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);
140 #endif
141     __pthread_mutex_unlock(&__malloc_heap_lock);
142     __pthread_mutex_unlock(&__malloc_sbrk_lock);
143 #endif
144     __pthread_mutex_unlock(&pthread_atfork_lock);
145     __pthread_once_fork_parent();
146     pthread_call_handlers(parent);
147   }
148   return pid;
149 }
150 strong_alias(__fork,fork)
151 strong_alias(__fork,vfork)
152
153 #else
154
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 int pthread_atfork(void (*prepare)(void),
158                    void (*parent)(void),
159                    void (*child)(void))
160 {
161   /* ENOMEM is probably pushing it a little bit.
162      Take it as `no *virtual* memory' :-)  */
163   errno = ENOMEM;
164   return -1;
165 }
166
167 #endif