OSDN Git Service

tweak the idea between having a MMU and actually using it
[uclinux-h8/uClibc.git] / 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 <stddef.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include "pthread.h"
27 #include "internals.h"
28
29 struct handler_list {
30   void (*handler)(void);
31   struct handler_list * next;
32 };
33
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;
38
39 static void pthread_insert_list(struct handler_list ** list,
40                                 void (*handler)(void),
41                                 struct handler_list * newlist,
42                                 int at_end)
43 {
44   if (handler == NULL) return;
45   if (at_end) {
46     while(*list != NULL) list = &((*list)->next);
47   }
48   newlist->handler = handler;
49   newlist->next = *list;
50   *list = newlist;
51 }
52
53 struct handler_list_block {
54   struct handler_list prepare, parent, child;
55 };
56
57 int pthread_atfork(void (*prepare)(void),
58                      void (*parent)(void),
59                      void (*child)(void))
60 {
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);
72   return 0;
73 }
74 //strong_alias (__pthread_atfork, pthread_atfork)
75
76 static inline void pthread_call_handlers(struct handler_list * list)
77 {
78   for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
79 }
80
81 extern __typeof(fork) __libc_fork;
82
83 pid_t __fork(void) attribute_hidden;
84 pid_t __fork(void)
85 {
86   pid_t pid;
87   struct handler_list * prepare, * child, * parent;
88
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);
95   pid = __libc_fork();
96   if (pid == 0) {
97     __pthread_reset_main_thread();
98     __fresetlockfiles();
99     pthread_call_handlers(child);
100   } else {
101     pthread_call_handlers(parent);
102   }
103   return pid;
104 }
105 strong_alias(__fork,fork)
106
107 pid_t vfork(void)
108 {
109   return __fork();
110 }
111
112 #else
113
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),
118                    void (*child)(void))
119 {
120   /* ENOMEM is probably pushing it a little bit.
121      Take it as `no *virtual* memory' :-)  */
122   errno = ENOMEM;
123   return -1;
124 }
125
126 #endif