Updated to fedora-glibc-20070317T2130
[glibc.git] / hurd / hurd / port.h
bloba1803c36a59672037cffbf7610fb1fa8e5358431
1 /* Lightweight user references for ports.
2 Copyright (C) 1993, 1994, 1995, 1997, 1999, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _HURD_PORT_H
23 #define _HURD_PORT_H 1
24 #include <features.h>
26 #include <mach.h>
27 #include <hurd/userlink.h>
28 #include <spin-lock.h>
29 #include <hurd/signal.h>
32 /* Structure describing a cell containing a port. With the lock held, a
33 user extracts PORT, and attaches his own link (in local storage) to the
34 USERS chain. PORT can then safely be used. When PORT is no longer
35 needed, with the lock held, the user removes his link from the chain.
36 If his link is the last, and PORT has changed since he fetched it, the
37 user deallocates the port he used. See <hurd/userlink.h>. */
39 struct hurd_port
41 spin_lock_t lock; /* Locks rest. */
42 struct hurd_userlink *users; /* Chain of users; see below. */
43 mach_port_t port; /* Port. */
47 /* Evaluate EXPR with the variable `port' bound to the port in PORTCELL. */
49 #define HURD_PORT_USE(portcell, expr) \
50 ({ struct hurd_port *const __p = (portcell); \
51 struct hurd_userlink __link; \
52 const mach_port_t port = _hurd_port_get (__p, &__link); \
53 __typeof(expr) __result = (expr); \
54 _hurd_port_free (__p, &__link, port); \
55 __result; })
58 #ifndef _HURD_PORT_H_EXTERN_INLINE
59 #define _HURD_PORT_H_EXTERN_INLINE __extern_inline
60 #endif
63 /* Initialize *PORT to INIT. */
65 _HURD_PORT_H_EXTERN_INLINE void
66 _hurd_port_init (struct hurd_port *port, mach_port_t init)
68 __spin_lock_init (&port->lock);
69 port->users = NULL;
70 port->port = init;
74 /* Cleanup function for non-local exits. */
75 extern void _hurd_port_cleanup (void *, jmp_buf, int);
77 /* Get a reference to *PORT, which is locked.
78 Pass return value and LINK to _hurd_port_free when done. */
80 _HURD_PORT_H_EXTERN_INLINE mach_port_t
81 _hurd_port_locked_get (struct hurd_port *port,
82 struct hurd_userlink *link)
84 mach_port_t result;
85 result = port->port;
86 if (result != MACH_PORT_NULL)
88 link->cleanup = &_hurd_port_cleanup;
89 link->cleanup_data = (void *) result;
90 _hurd_userlink_link (&port->users, link);
92 __spin_unlock (&port->lock);
93 return result;
96 /* Same, but locks PORT first. */
98 _HURD_PORT_H_EXTERN_INLINE mach_port_t
99 _hurd_port_get (struct hurd_port *port,
100 struct hurd_userlink *link)
102 mach_port_t result;
103 HURD_CRITICAL_BEGIN;
104 __spin_lock (&port->lock);
105 result = _hurd_port_locked_get (port, link);
106 HURD_CRITICAL_END;
107 return result;
111 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
113 _HURD_PORT_H_EXTERN_INLINE void
114 _hurd_port_free (struct hurd_port *port,
115 struct hurd_userlink *link,
116 mach_port_t used_port)
118 int dealloc;
119 if (used_port == MACH_PORT_NULL)
120 /* When we fetch an empty port cell with _hurd_port_get,
121 it does not link us on the users chain, since there is
122 no shared resource. */
123 return;
124 HURD_CRITICAL_BEGIN;
125 __spin_lock (&port->lock);
126 dealloc = _hurd_userlink_unlink (link);
127 __spin_unlock (&port->lock);
128 HURD_CRITICAL_END;
129 if (dealloc)
130 __mach_port_deallocate (__mach_task_self (), used_port);
134 /* Set *PORT's port to NEWPORT. NEWPORT's reference is consumed by PORT->port.
135 PORT->lock is locked. */
137 _HURD_PORT_H_EXTERN_INLINE void
138 _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
140 mach_port_t old;
141 old = _hurd_userlink_clear (&port->users) ? port->port : MACH_PORT_NULL;
142 port->port = newport;
143 __spin_unlock (&port->lock);
144 if (old != MACH_PORT_NULL)
145 __mach_port_deallocate (__mach_task_self (), old);
148 /* Same, but locks PORT first. */
150 _HURD_PORT_H_EXTERN_INLINE void
151 _hurd_port_set (struct hurd_port *port, mach_port_t newport)
153 HURD_CRITICAL_BEGIN;
154 __spin_lock (&port->lock);
155 _hurd_port_locked_set (port, newport);
156 HURD_CRITICAL_END;
160 #endif /* hurd/port.h */