Add glibc.malloc.mxfast tunable
[glibc.git] / hurd / hurd / port.h
blob75e51ff7365ed1b1568415cc8e7fce828187a038
1 /* Lightweight user references for ports.
2 Copyright (C) 1993-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 #ifndef _HURD_PORT_H
21 #define _HURD_PORT_H 1
22 #include <features.h>
24 #include <mach.h>
25 #include <hurd/userlink.h>
26 #include <spin-lock.h>
29 /* Structure describing a cell containing a port. With the lock held, a
30 user extracts PORT, and attaches his own link (in local storage) to the
31 USERS chain. PORT can then safely be used. When PORT is no longer
32 needed, with the lock held, the user removes his link from the chain.
33 If his link is the last, and PORT has changed since he fetched it, the
34 user deallocates the port he used. See <hurd/userlink.h>. */
36 struct hurd_port
38 spin_lock_t lock; /* Locks rest. */
39 struct hurd_userlink *users; /* Chain of users; see below. */
40 mach_port_t port; /* Port. */
44 /* Evaluate EXPR with the variable `port' bound to the port in PORTCELL. */
46 #define HURD_PORT_USE(portcell, expr) \
47 ({ struct hurd_port *const __p = (portcell); \
48 struct hurd_userlink __link; \
49 const mach_port_t port = _hurd_port_get (__p, &__link); \
50 __typeof(expr) __result = (expr); \
51 _hurd_port_free (__p, &__link, port); \
52 __result; })
55 #ifndef _HURD_PORT_H_EXTERN_INLINE
56 #define _HURD_PORT_H_EXTERN_INLINE __extern_inline
57 #endif
60 /* Initialize *PORT to INIT. */
62 extern void _hurd_port_init (struct hurd_port *port, mach_port_t init);
64 #if defined __USE_EXTERN_INLINES && defined _LIBC
65 # if IS_IN (libc)
66 _HURD_PORT_H_EXTERN_INLINE void
67 _hurd_port_init (struct hurd_port *port, mach_port_t init)
69 __spin_lock_init (&port->lock);
70 port->users = NULL;
71 port->port = init;
73 # endif
74 #endif
77 /* Cleanup function for non-local exits. */
78 extern void _hurd_port_cleanup (void *, jmp_buf, int);
80 /* Get a reference to *PORT, which is locked.
81 Pass return value and LINK to _hurd_port_free when done. */
83 extern mach_port_t
84 _hurd_port_locked_get (struct hurd_port *port,
85 struct hurd_userlink *link);
87 #if defined __USE_EXTERN_INLINES && defined _LIBC
88 # if IS_IN (libc)
89 _HURD_PORT_H_EXTERN_INLINE mach_port_t
90 _hurd_port_locked_get (struct hurd_port *port,
91 struct hurd_userlink *link)
93 mach_port_t result;
94 result = port->port;
95 if (result != MACH_PORT_NULL)
97 link->cleanup = &_hurd_port_cleanup;
98 link->cleanup_data = (void *) result;
99 _hurd_userlink_link (&port->users, link);
101 __spin_unlock (&port->lock);
102 return result;
104 # endif
105 #endif
107 /* Same, but locks PORT first. */
109 extern mach_port_t
110 _hurd_port_get (struct hurd_port *port,
111 struct hurd_userlink *link);
113 #if defined __USE_EXTERN_INLINES && defined _LIBC
114 # if IS_IN (libc)
115 _HURD_PORT_H_EXTERN_INLINE mach_port_t
116 _hurd_port_get (struct hurd_port *port,
117 struct hurd_userlink *link)
119 mach_port_t result;
120 HURD_CRITICAL_BEGIN;
121 __spin_lock (&port->lock);
122 result = _hurd_port_locked_get (port, link);
123 HURD_CRITICAL_END;
124 return result;
126 # endif
127 #endif
130 /* Relocate LINK to NEW_LINK.
131 To be used when e.g. reallocating a link array. */
133 extern void
134 _hurd_port_move (struct hurd_port *port,
135 struct hurd_userlink *new_link,
136 struct hurd_userlink *link);
138 #if defined __USE_EXTERN_INLINES && defined _LIBC
139 # if IS_IN (libc)
140 _HURD_PORT_H_EXTERN_INLINE void
141 _hurd_port_move (struct hurd_port *port,
142 struct hurd_userlink *new_link,
143 struct hurd_userlink *link)
145 HURD_CRITICAL_BEGIN;
146 __spin_lock (&port->lock);
147 _hurd_userlink_move (new_link, link);
148 __spin_unlock (&port->lock);
149 HURD_CRITICAL_END;
151 # endif
152 #endif
155 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
157 extern void
158 _hurd_port_free (struct hurd_port *port,
159 struct hurd_userlink *link,
160 mach_port_t used_port);
162 #if defined __USE_EXTERN_INLINES && defined _LIBC
163 # if IS_IN (libc)
164 _HURD_PORT_H_EXTERN_INLINE void
165 _hurd_port_free (struct hurd_port *port,
166 struct hurd_userlink *link,
167 mach_port_t used_port)
169 int dealloc;
170 if (used_port == MACH_PORT_NULL)
171 /* When we fetch an empty port cell with _hurd_port_get,
172 it does not link us on the users chain, since there is
173 no shared resource. */
174 return;
175 HURD_CRITICAL_BEGIN;
176 __spin_lock (&port->lock);
177 dealloc = _hurd_userlink_unlink (link);
178 __spin_unlock (&port->lock);
179 HURD_CRITICAL_END;
180 if (dealloc)
181 __mach_port_deallocate (__mach_task_self (), used_port);
183 # endif
184 #endif
187 /* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port.
188 PORT->lock is locked. */
190 extern void _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport);
192 #if defined __USE_EXTERN_INLINES && defined _LIBC
193 # if IS_IN (libc)
194 _HURD_PORT_H_EXTERN_INLINE void
195 _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
197 mach_port_t old;
198 old = _hurd_userlink_clear (&port->users) ? port->port : MACH_PORT_NULL;
199 port->port = newport;
200 __spin_unlock (&port->lock);
201 if (old != MACH_PORT_NULL)
202 __mach_port_deallocate (__mach_task_self (), old);
204 # endif
205 #endif
207 /* Same, but locks PORT first. */
209 extern void _hurd_port_set (struct hurd_port *port, mach_port_t newport);
211 #if defined __USE_EXTERN_INLINES && defined _LIBC
212 # if IS_IN (libc)
213 _HURD_PORT_H_EXTERN_INLINE void
214 _hurd_port_set (struct hurd_port *port, mach_port_t newport)
216 HURD_CRITICAL_BEGIN;
217 __spin_lock (&port->lock);
218 _hurd_port_locked_set (port, newport);
219 HURD_CRITICAL_END;
221 # endif
222 #endif
225 #endif /* hurd/port.h */