hurd: XFAIL appearance of sched_param and sched_priority from <sys/types.h>
[glibc.git] / hurd / hurd / port.h
blob0779578d0387de926a15814189e8c20a154e89db
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/>. */
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 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
132 extern void
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
138 # if IS_IN (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)
144 int dealloc;
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. */
149 return;
150 HURD_CRITICAL_BEGIN;
151 __spin_lock (&port->lock);
152 dealloc = _hurd_userlink_unlink (link);
153 __spin_unlock (&port->lock);
154 HURD_CRITICAL_END;
155 if (dealloc)
156 __mach_port_deallocate (__mach_task_self (), used_port);
158 # endif
159 #endif
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
168 # if IS_IN (libc)
169 _HURD_PORT_H_EXTERN_INLINE void
170 _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
172 mach_port_t old;
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);
179 # endif
180 #endif
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
187 # if IS_IN (libc)
188 _HURD_PORT_H_EXTERN_INLINE void
189 _hurd_port_set (struct hurd_port *port, mach_port_t newport)
191 HURD_CRITICAL_BEGIN;
192 __spin_lock (&port->lock);
193 _hurd_port_locked_set (port, newport);
194 HURD_CRITICAL_END;
196 # endif
197 #endif
200 #endif /* hurd/port.h */