Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / hurd / hurd / userlink.h
1 /* Support for chains recording users of a resource; `struct hurd_userlink'.
2    Copyright (C) 1994-2013 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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 #ifndef _HURD_USERLINK_H
20
21 #define _HURD_USERLINK_H        1
22 #include <features.h>
23
24 #define __need_NULL
25 #include <stddef.h>
26
27 #include <hurd/signal.h>
28 #include <setjmp.h>
29
30
31 /* This structure records a link in two doubly-linked lists.
32    We call these the per-resource user list and the per-thread
33    active-resource list.
34
35    Users of a given resource are recorded by their presence in a list
36    associated with that resource.  A user attaches his own link (in local
37    storage on his stack) to a shared chain at the time he begins using some
38    resource.  When finished with that resource, the user removes his link
39    from the chain.  If his link is the last (there are no other users of
40    the resource), and his chain has been detached from the shared cell (the
41    resource in the cell has been replaced), then the user deallocates the
42    resource that he used.
43
44    All uses of shared resources by a single thread are linked together by
45    its `active-resource' list; the head of this list is stored in the
46    per-thread sigstate structure.  When the thread makes a non-local exit
47    (i.e. longjmp), it will examine its active-resource list, and each link
48    residing in a stack frame being jumped out of will be unlinked from both
49    the resource's user list and the thread's active-resource list, and
50    deallocate the resource if that was the last user link for that resource.
51
52    NOTE: Access to a thread's active-resource list must always be done
53    inside a signal-proof critical section; the functions in this file
54    assume they are called inside a critical section, and do no locking of
55    their own.  Also important: the longjmp cleanup relies on all userlink
56    structures residing on the stack of the using thread.  */
57
58 struct hurd_userlink
59   {
60     struct
61       {
62         struct hurd_userlink *next, **prevp;
63       } resource, thread;
64
65     /* This function is called when a non-local exit
66        unwinds the frame containing this link.  */
67     void (*cleanup) (void *cleanup_data, jmp_buf env, int val);
68     void *cleanup_data;
69   };
70
71
72 #ifndef _HURD_USERLINK_H_EXTERN_INLINE
73 #define _HURD_USERLINK_H_EXTERN_INLINE __extern_inline
74 #endif
75
76
77 /* Attach LINK to the chain of users at *CHAINP.  */
78
79 _HURD_USERLINK_H_EXTERN_INLINE void
80 _hurd_userlink_link (struct hurd_userlink **chainp,
81                      struct hurd_userlink *link)
82 {
83   struct hurd_userlink **thread_chainp;
84
85   link->resource.next = *chainp;
86   if (link->resource.next)
87     link->resource.next->resource.prevp = &link->resource.next;
88   link->resource.prevp = chainp;
89   *chainp = link;
90
91   /* Also chain it on the current thread's list of active resources.  */
92   thread_chainp = &_hurd_self_sigstate ()->active_resources;
93   link->thread.next = *thread_chainp;
94   if (link->thread.next)
95     link->thread.next->thread.prevp = &link->thread.next;
96   link->thread.prevp = thread_chainp;
97   *thread_chainp = link;
98 }
99
100
101 /* Detach LINK from its chain.  Returns nonzero iff this was the
102    last user of the resource and it should be deallocated.  */
103
104 _HURD_USERLINK_H_EXTERN_INLINE int
105 _hurd_userlink_unlink (struct hurd_userlink *link)
106 {
107   /* We should deallocate the resource used if this chain has been detached
108      from the cell (and thus has a nil `prevp'), and there is no next link
109      representing another user reference to the same resource. */
110   int dealloc = ! link->resource.next && ! link->resource.prevp;
111
112   /* Remove our link from the chain of current users.  */
113   if (link->resource.prevp)
114     *link->resource.prevp = link->resource.next;
115   if (link->resource.next)
116     link->resource.next->resource.prevp = link->resource.prevp;
117
118   /* Remove our link from the chain of currently active resources
119      for this thread.  */
120   *link->thread.prevp = link->thread.next;
121   if (link->thread.next)
122     link->thread.next->thread.prevp = link->thread.prevp;
123
124   return dealloc;
125 }
126
127
128 /* Clear all users from *CHAINP.  Call this when the resource *CHAINP
129    protects is changing.  If the return value is nonzero, no users are on
130    the chain and the caller should deallocate the resource.  If the return
131    value is zero, someone is still using the resource and they will
132    deallocate it when they are finished.  */
133
134 _HURD_USERLINK_H_EXTERN_INLINE int
135 _hurd_userlink_clear (struct hurd_userlink **chainp)
136 {
137   if (*chainp == NULL)
138     return 1;
139
140   /* Detach the chain of current users from the cell.  The last user to
141      remove his link from that chain will deallocate the old resource.  */
142   (*chainp)->resource.prevp = NULL;
143   *chainp = NULL;
144   return 0;
145 }
146
147 #endif  /* hurd/userlink.h */