(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / unix / sysv / linux / unregister-atfork.c
blob72c8d615eb06c3885f4a3341a7008e0c24b399ba
1 /* Copyright (C) 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include "fork.h"
23 #include <atomic.h>
26 void
27 __unregister_atfork (dso_handle)
28 void *dso_handle;
30 /* Check whether there is any entry in the list which we have to
31 remove. It is likely that this is not the case so don't bother
32 getting the lock.
34 We do not worry about other threads adding entries for this DSO
35 right this moment. If this happens this is a race and we can do
36 whatever we please. The program will crash anyway seen. */
37 struct fork_handler *runp = __fork_handlers;
38 struct fork_handler *lastp = NULL;
40 while (runp != NULL)
41 if (runp->dso_handle == dso_handle)
42 break;
43 else
45 lastp = runp;
46 runp = runp->next;
49 if (runp == NULL)
50 /* Nothing to do. */
51 return;
53 /* Get the lock to not conflict with additions or deletions. Note
54 that there couldn't have been another thread deleting something.
55 The __unregister_atfork function is only called from the
56 dlclose() code which itself serializes the operations. */
57 lll_lock (__fork_lock);
59 /* We have to create a new list with all the entries we don't remove. */
60 struct deleted_handler
62 struct fork_handler *handler;
63 struct deleted_handler *next;
64 } *deleted = NULL;
66 /* Remove the entries for the DSO which is unloaded from the list.
67 It's a single linked list so readers are. */
70 if (runp->dso_handle == dso_handle)
72 if (lastp == NULL)
73 __fork_handlers = runp->next;
74 else
75 lastp->next = runp->next;
77 /* We cannot overwrite the ->next element now. Put the deleted
78 entries in a separate list. */
79 struct deleted_handler *newp = alloca (sizeof (*newp));
80 newp->handler = runp;
81 newp->next = deleted;
82 deleted = newp;
84 else
85 lastp = runp;
87 runp = runp->next;
89 while (runp != NULL);
91 /* Release the lock. */
92 lll_unlock (__fork_lock);
94 /* Walk the list of all entries which have to be deleted. */
95 while (deleted != NULL)
97 /* We need to be informed by possible current users. */
98 deleted->handler->need_signal = 1;
99 /* Make sure this gets written out first. */
100 atomic_write_barrier ();
102 /* Decrement the reference counter. If it does not reach zero
103 wait for the last user. */
104 atomic_decrement (&deleted->handler->refcntr);
105 unsigned int val;
106 while ((val = deleted->handler->refcntr) != 0)
107 lll_futex_wait (&deleted->handler->refcntr, val);
109 deleted = deleted->next;