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/>. */
21 #define _HURD_PORT_H 1
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>. */
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); \
55 #ifndef _HURD_PORT_H_EXTERN_INLINE
56 #define _HURD_PORT_H_EXTERN_INLINE __extern_inline
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
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
);
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. */
84 _hurd_port_locked_get (struct hurd_port
*port
,
85 struct hurd_userlink
*link
);
87 #if defined __USE_EXTERN_INLINES && defined _LIBC
89 _HURD_PORT_H_EXTERN_INLINE mach_port_t
90 _hurd_port_locked_get (struct hurd_port
*port
,
91 struct hurd_userlink
*link
)
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
);
107 /* Same, but locks PORT first. */
110 _hurd_port_get (struct hurd_port
*port
,
111 struct hurd_userlink
*link
);
113 #if defined __USE_EXTERN_INLINES && defined _LIBC
115 _HURD_PORT_H_EXTERN_INLINE mach_port_t
116 _hurd_port_get (struct hurd_port
*port
,
117 struct hurd_userlink
*link
)
121 __spin_lock (&port
->lock
);
122 result
= _hurd_port_locked_get (port
, link
);
130 /* Relocate LINK to NEW_LINK.
131 To be used when e.g. reallocating a link array. */
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
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
)
146 __spin_lock (&port
->lock
);
147 _hurd_userlink_move (new_link
, link
);
148 __spin_unlock (&port
->lock
);
155 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
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
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
)
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. */
176 __spin_lock (&port
->lock
);
177 dealloc
= _hurd_userlink_unlink (link
);
178 __spin_unlock (&port
->lock
);
181 __mach_port_deallocate (__mach_task_self (), used_port
);
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
194 _HURD_PORT_H_EXTERN_INLINE
void
195 _hurd_port_locked_set (struct hurd_port
*port
, mach_port_t newport
)
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
);
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
213 _HURD_PORT_H_EXTERN_INLINE
void
214 _hurd_port_set (struct hurd_port
*port
, mach_port_t newport
)
217 __spin_lock (&port
->lock
);
218 _hurd_port_locked_set (port
, newport
);
225 #endif /* hurd/port.h */