chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / linuxthreads / 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 /* The "atfork" stuff */
16
17 #include <errno.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include "pthread.h"
22 #include "internals.h"
23 #include <bits/libc-lock.h>
24 #include "fork.h"
25
26 extern int __libc_fork (void);
27
28 pid_t __pthread_fork (struct fork_block *b)
29 {
30   pid_t pid;
31   list_t *runp;
32
33   __libc_lock_lock (b->lock);
34
35   /* Run all the registered preparation handlers.  In reverse order.  */
36   list_for_each_prev (runp, &b->prepare_list)
37     {
38       struct fork_handler *curp;
39       curp = list_entry (runp, struct fork_handler, list);
40       curp->handler ();
41     }
42
43   __pthread_once_fork_prepare();
44   __flockfilelist();
45
46   pid = ARCH_FORK ();
47
48   if (pid == 0) {
49     __pthread_reset_main_thread();
50
51     __fresetlockfiles();
52     __pthread_once_fork_child();
53
54     /* Run the handlers registered for the child.  */
55     list_for_each (runp, &b->child_list)
56       {
57         struct fork_handler *curp;
58         curp = list_entry (runp, struct fork_handler, list);
59         curp->handler ();
60       }
61
62     __libc_lock_init (b->lock);
63   } else {
64     __funlockfilelist();
65     __pthread_once_fork_parent();
66
67     /* Run the handlers registered for the parent.  */
68     list_for_each (runp, &b->parent_list)
69       {
70         struct fork_handler *curp;
71         curp = list_entry (runp, struct fork_handler, list);
72         curp->handler ();
73       }
74
75     __libc_lock_unlock (b->lock);
76   }
77
78   return pid;
79 }
80
81 #ifdef SHARED
82 pid_t __fork (void)
83 {
84   return __libc_fork ();
85 }
86 weak_alias (__fork, fork);
87
88 pid_t __vfork(void)
89 {
90   return __libc_fork ();
91 }
92 weak_alias (__vfork, vfork);
93 #endif