Prevent check-local-headers.sh hang.
[glibc.git] / nptl / register-atfork.c
blob681bb574c8df36aa6f43aa2a4ff9d3538f2c76c1
1 /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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/>. */
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fork.h>
23 #include <atomic.h>
26 struct fork_handler *__fork_handlers;
28 /* Lock to protect allocation and deallocation of fork handlers. */
29 int __fork_lock = LLL_LOCK_INITIALIZER;
32 /* Number of pre-allocated handler entries. */
33 #define NHANDLER 48
35 /* Memory pool for fork handler structures. */
36 static struct fork_handler_pool
38 struct fork_handler_pool *next;
39 struct fork_handler mem[NHANDLER];
40 } fork_handler_pool;
43 static struct fork_handler *
44 fork_handler_alloc (void)
46 struct fork_handler_pool *runp = &fork_handler_pool;
47 struct fork_handler *result = NULL;
48 unsigned int i;
52 /* Search for an empty entry. */
53 for (i = 0; i < NHANDLER; ++i)
54 if (runp->mem[i].refcntr == 0)
55 goto found;
57 while ((runp = runp->next) != NULL);
59 /* We have to allocate a new entry. */
60 runp = (struct fork_handler_pool *) calloc (1, sizeof (*runp));
61 if (runp != NULL)
63 /* Enqueue the new memory pool into the list. */
64 runp->next = fork_handler_pool.next;
65 fork_handler_pool.next = runp;
67 /* We use the last entry on the page. This means when we start
68 searching from the front the next time we will find the first
69 entry unused. */
70 i = NHANDLER - 1;
72 found:
73 result = &runp->mem[i];
74 result->refcntr = 1;
75 result->need_signal = 0;
78 return result;
82 int
83 __register_atfork (prepare, parent, child, dso_handle)
84 void (*prepare) (void);
85 void (*parent) (void);
86 void (*child) (void);
87 void *dso_handle;
89 /* Get the lock to not conflict with other allocations. */
90 lll_lock (__fork_lock, LLL_PRIVATE);
92 struct fork_handler *newp = fork_handler_alloc ();
94 if (newp != NULL)
96 /* Initialize the new record. */
97 newp->prepare_handler = prepare;
98 newp->parent_handler = parent;
99 newp->child_handler = child;
100 newp->dso_handle = dso_handle;
102 __linkin_atfork (newp);
105 /* Release the lock. */
106 lll_unlock (__fork_lock, LLL_PRIVATE);
108 return newp == NULL ? ENOMEM : 0;
110 libc_hidden_def (__register_atfork)
113 void
114 attribute_hidden
115 __linkin_atfork (struct fork_handler *newp)
118 newp->next = __fork_handlers;
119 while (catomic_compare_and_exchange_bool_acq (&__fork_handlers,
120 newp, newp->next) != 0);
124 libc_freeres_fn (free_mem)
126 /* Get the lock to not conflict with running forks. */
127 lll_lock (__fork_lock, LLL_PRIVATE);
129 /* No more fork handlers. */
130 __fork_handlers = NULL;
132 /* Free eventually allocated memory blocks for the object pool. */
133 struct fork_handler_pool *runp = fork_handler_pool.next;
135 memset (&fork_handler_pool, '\0', sizeof (fork_handler_pool));
137 /* Release the lock. */
138 lll_unlock (__fork_lock, LLL_PRIVATE);
140 /* We can free the memory after releasing the lock. */
141 while (runp != NULL)
143 struct fork_handler_pool *oldp = runp;
144 runp = runp->next;
145 free (oldp);