OSDN Git Service

Merge remote branch 'origin/master' into nptl
[uclinux-h8/uClibc.git] / libpthread / nptl / sysdeps / unix / sysv / linux / unregister-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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <fork.h>
23 #include <atomic.h>
24 #include <tls.h>
25
26
27 void
28 __unregister_atfork (
29      void *dso_handle)
30 {
31   /* Check whether there is any entry in the list which we have to
32      remove.  It is likely that this is not the case so don't bother
33      getting the lock.
34
35      We do not worry about other threads adding entries for this DSO
36      right this moment.  If this happens this is a race and we can do
37      whatever we please.  The program will crash anyway seen.  */
38   struct fork_handler *runp = __fork_handlers;
39   struct fork_handler *lastp = NULL;
40
41   while (runp != NULL)
42     if (runp->dso_handle == dso_handle)
43       break;
44     else
45       {
46         lastp = runp;
47         runp = runp->next;
48       }
49
50   if (runp == NULL)
51     /* Nothing to do.  */
52     return;
53
54   /* Get the lock to not conflict with additions or deletions.  Note
55      that there couldn't have been another thread deleting something.
56      The __unregister_atfork function is only called from the
57      dlclose() code which itself serializes the operations.  */
58   lll_lock (__fork_lock, LLL_PRIVATE);
59
60   /* We have to create a new list with all the entries we don't remove.  */
61   struct deleted_handler
62   {
63     struct fork_handler *handler;
64     struct deleted_handler *next;
65   } *deleted = NULL;
66
67   /* Remove the entries for the DSO which is unloaded from the list.
68      It's a single linked list so readers are.  */
69   do
70     {
71     again:
72       if (runp->dso_handle == dso_handle)
73         {
74           if (lastp == NULL)
75             {
76               /* We have to use an atomic operation here because
77                  __linkin_atfork also uses one.  */
78               if (catomic_compare_and_exchange_bool_acq (&__fork_handlers,
79                                                          runp->next, runp)
80                   != 0)
81                 {
82                   runp = __fork_handlers;
83                   goto again;
84                 }
85             }
86           else
87             lastp->next = runp->next;
88
89           /* We cannot overwrite the ->next element now.  Put the deleted
90              entries in a separate list.  */
91           struct deleted_handler *newp = alloca (sizeof (*newp));
92           newp->handler = runp;
93           newp->next = deleted;
94           deleted = newp;
95         }
96       else
97         lastp = runp;
98
99       runp = runp->next;
100     }
101   while (runp != NULL);
102
103   /* Release the lock.  */
104   lll_unlock (__fork_lock, LLL_PRIVATE);
105
106   /* Walk the list of all entries which have to be deleted.  */
107   while (deleted != NULL)
108     {
109       /* We need to be informed by possible current users.  */
110       deleted->handler->need_signal = 1;
111       /* Make sure this gets written out first.  */
112       atomic_write_barrier ();
113
114       /* Decrement the reference counter.  If it does not reach zero
115          wait for the last user.  */
116       atomic_decrement (&deleted->handler->refcntr);
117       unsigned int val;
118       while ((val = deleted->handler->refcntr) != 0)
119         lll_futex_wait (&deleted->handler->refcntr, val, LLL_PRIVATE);
120
121       deleted = deleted->next;
122     }
123 }