1 /* Lightweight user references for ports.
2 Copyright (C) 1993-2018 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 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
133 _hurd_port_free (struct hurd_port
*port
,
134 struct hurd_userlink
*link
,
135 mach_port_t used_port
);
137 #if defined __USE_EXTERN_INLINES && defined _LIBC
139 _HURD_PORT_H_EXTERN_INLINE
void
140 _hurd_port_free (struct hurd_port
*port
,
141 struct hurd_userlink
*link
,
142 mach_port_t used_port
)
145 if (used_port
== MACH_PORT_NULL
)
146 /* When we fetch an empty port cell with _hurd_port_get,
147 it does not link us on the users chain, since there is
148 no shared resource. */
151 __spin_lock (&port
->lock
);
152 dealloc
= _hurd_userlink_unlink (link
);
153 __spin_unlock (&port
->lock
);
156 __mach_port_deallocate (__mach_task_self (), used_port
);
162 /* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port.
163 PORT->lock is locked. */
165 extern void _hurd_port_locked_set (struct hurd_port
*port
, mach_port_t newport
);
167 #if defined __USE_EXTERN_INLINES && defined _LIBC
169 _HURD_PORT_H_EXTERN_INLINE
void
170 _hurd_port_locked_set (struct hurd_port
*port
, mach_port_t newport
)
173 old
= _hurd_userlink_clear (&port
->users
) ? port
->port
: MACH_PORT_NULL
;
174 port
->port
= newport
;
175 __spin_unlock (&port
->lock
);
176 if (old
!= MACH_PORT_NULL
)
177 __mach_port_deallocate (__mach_task_self (), old
);
182 /* Same, but locks PORT first. */
184 extern void _hurd_port_set (struct hurd_port
*port
, mach_port_t newport
);
186 #if defined __USE_EXTERN_INLINES && defined _LIBC
188 _HURD_PORT_H_EXTERN_INLINE
void
189 _hurd_port_set (struct hurd_port
*port
, mach_port_t newport
)
192 __spin_lock (&port
->lock
);
193 _hurd_port_locked_set (port
, newport
);
200 #endif /* hurd/port.h */