]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/nptl/sysdeps/unix/sysv/linux/register-atfork.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / nptl / sysdeps / unix / sysv / linux / register-atfork.c
1 /* Copyright (C) 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fork.h>
23 #include <atomic.h>
24 #include <tls.h>
25
26
27 /* Lock to protect allocation and deallocation of fork handlers.  */
28 int __fork_lock = LLL_LOCK_INITIALIZER;
29
30
31 /* Number of pre-allocated handler entries.  */
32 #define NHANDLER 48
33
34 /* Memory pool for fork handler structures.  */
35 static struct fork_handler_pool
36 {
37   struct fork_handler_pool *next;
38   struct fork_handler mem[NHANDLER];
39 } fork_handler_pool;
40
41
42 static struct fork_handler *
43 fork_handler_alloc (void)
44 {
45   struct fork_handler_pool *runp = &fork_handler_pool;
46   struct fork_handler *result = NULL;
47   unsigned int i;
48
49   do
50     {
51       /* Search for an empty entry.  */
52       for (i = 0; i < NHANDLER; ++i)
53         if (runp->mem[i].refcntr == 0)
54           goto found;
55     }
56   while ((runp = runp->next) != NULL);
57
58   /* We have to allocate a new entry.  */
59   runp = (struct fork_handler_pool *) calloc (1, sizeof (*runp));
60   if (runp != NULL)
61     {
62       /* Enqueue the new memory pool into the list.  */
63       runp->next = fork_handler_pool.next;
64       fork_handler_pool.next = runp;
65
66       /* We use the last entry on the page.  This means when we start
67          searching from the front the next time we will find the first
68          entry unused.  */
69       i = NHANDLER - 1;
70
71     found:
72       result = &runp->mem[i];
73       result->refcntr = 1;
74       result->need_signal = 0;
75     }
76
77   return result;
78 }
79
80
81 int
82 __register_atfork (
83      void (*prepare) (void),
84      void (*parent) (void),
85      void (*child) (void),
86      void *dso_handle)
87 {
88   /* Get the lock to not conflict with other allocations.  */
89   lll_lock (__fork_lock, LLL_PRIVATE);
90
91   struct fork_handler *newp = fork_handler_alloc ();
92
93   if (newp != NULL)
94     {
95       /* Initialize the new record.  */
96       newp->prepare_handler = prepare;
97       newp->parent_handler = parent;
98       newp->child_handler = child;
99       newp->dso_handle = dso_handle;
100
101       __linkin_atfork (newp);
102     }
103
104   /* Release the lock.  */
105   lll_unlock (__fork_lock, LLL_PRIVATE);
106
107   return newp == NULL ? ENOMEM : 0;
108 }
109 libc_hidden_def (__register_atfork)
110
111
112 void
113 attribute_hidden
114 __linkin_atfork (struct fork_handler *newp)
115 {
116   do
117     newp->next = __fork_handlers;
118   while (catomic_compare_and_exchange_bool_acq (&__fork_handlers,
119                                                 newp, newp->next) != 0);
120 }
121
122 #if 0
123 libc_freeres_fn (free_mem)
124 {
125   /* Get the lock to not conflict with running forks.  */
126   lll_lock (__fork_lock, LLL_PRIVATE);
127
128   /* No more fork handlers.  */
129   __fork_handlers = NULL;
130
131   /* Free eventually alloated memory blocks for the object pool.  */
132   struct fork_handler_pool *runp = fork_handler_pool.next;
133
134   memset (&fork_handler_pool, '\0', sizeof (fork_handler_pool));
135
136   /* Release the lock.  */
137   lll_unlock (__fork_lock, LLL_PRIVATE);
138
139   /* We can free the memory after releasing the lock.  */
140   while (runp != NULL)
141     {
142       struct fork_handler_pool *oldp = runp;
143       runp = runp->next;
144       free (oldp);
145     }
146 }
147 #endif
148