Update to 2.1.x development version
[glibc.git] / hurd / hurd / port.h
blobd58c45fb9792bb0ba1776194799503318b496fa3
1 /* Lightweight user references for ports.
2 Copyright (C) 1993, 1994, 1995, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifndef _HURD_PORT_H
22 #define _HURD_PORT_H 1
23 #include <features.h>
25 #include <mach.h>
26 #include <hurd/userlink.h>
27 #include <spin-lock.h>
28 #include <hurd/signal.h>
31 /* Structure describing a cell containing a port. With the lock held, a
32 user extracts PORT, and attaches his own link (in local storage) to the
33 USERS chain. PORT can then safely be used. When PORT is no longer
34 needed, with the lock held, the user removes his link from the chain.
35 If his link is the last, and PORT has changed since he fetched it, the
36 user deallocates the port he used. See <hurd/userlink.h>. */
38 struct hurd_port
40 spin_lock_t lock; /* Locks rest. */
41 struct hurd_userlink *users; /* Chain of users; see below. */
42 mach_port_t port; /* Port. */
46 /* Evaluate EXPR with the variable `port' bound to the port in PORTCELL. */
48 #define HURD_PORT_USE(portcell, expr) \
49 ({ struct hurd_port *const __p = (portcell); \
50 struct hurd_userlink __link; \
51 const mach_port_t port = _hurd_port_get (__p, &__link); \
52 __typeof(expr) __result = (expr); \
53 _hurd_port_free (__p, &__link, port); \
54 __result; })
57 #ifndef _EXTERN_INLINE
58 #define _EXTERN_INLINE extern __inline
59 #endif
62 /* Initialize *PORT to INIT. */
64 _EXTERN_INLINE void
65 _hurd_port_init (struct hurd_port *port, mach_port_t init)
67 __spin_lock_init (&port->lock);
68 port->users = NULL;
69 port->port = init;
73 /* Cleanup function for non-local exits. */
74 extern void _hurd_port_cleanup (void *, jmp_buf, int);
76 /* Get a reference to *PORT, which is locked.
77 Pass return value and LINK to _hurd_port_free when done. */
79 _EXTERN_INLINE mach_port_t
80 _hurd_port_locked_get (struct hurd_port *port,
81 struct hurd_userlink *link)
83 mach_port_t result;
84 result = port->port;
85 if (result != MACH_PORT_NULL)
87 link->cleanup = &_hurd_port_cleanup;
88 link->cleanup_data = (void *) result;
89 _hurd_userlink_link (&port->users, link);
91 __spin_unlock (&port->lock);
92 return result;
95 /* Same, but locks PORT first. */
97 _EXTERN_INLINE mach_port_t
98 _hurd_port_get (struct hurd_port *port,
99 struct hurd_userlink *link)
101 mach_port_t result;
102 HURD_CRITICAL_BEGIN;
103 __spin_lock (&port->lock);
104 result = _hurd_port_locked_get (port, link);
105 HURD_CRITICAL_END;
106 return result;
110 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
112 _EXTERN_INLINE void
113 _hurd_port_free (struct hurd_port *port,
114 struct hurd_userlink *link,
115 mach_port_t used_port)
117 int dealloc;
118 if (used_port == MACH_PORT_NULL)
119 /* When we fetch an empty port cell with _hurd_port_get,
120 it does not link us on the users chain, since there is
121 no shared resource. */
122 return;
123 HURD_CRITICAL_BEGIN;
124 __spin_lock (&port->lock);
125 dealloc = _hurd_userlink_unlink (link);
126 __spin_unlock (&port->lock);
127 HURD_CRITICAL_END;
128 if (dealloc)
129 __mach_port_deallocate (__mach_task_self (), used_port);
133 /* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port.
134 PORT->lock is locked. */
136 _EXTERN_INLINE void
137 _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
139 mach_port_t old;
140 old = _hurd_userlink_clear (&port->users) ? port->port : MACH_PORT_NULL;
141 port->port = newport;
142 __spin_unlock (&port->lock);
143 if (old != MACH_PORT_NULL)
144 __mach_port_deallocate (__mach_task_self (), old);
147 /* Same, but locks PORT first. */
149 _EXTERN_INLINE void
150 _hurd_port_set (struct hurd_port *port, mach_port_t newport)
152 HURD_CRITICAL_BEGIN;
153 __spin_lock (&port->lock);
154 _hurd_port_locked_set (port, newport);
155 HURD_CRITICAL_END;
159 #endif /* hurd/port.h */