* hurd.h, hurd/hurd/fd.h, hurd/hurd/port.h, hurd/hurd/signal.h,
[platform/upstream/glibc.git] / hurd / hurd / port.h
1 /* Lightweight user references for ports.
2    Copyright (C) 1993, 1994, 1995, 1997, 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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.  */
19
20 #ifndef _HURD_PORT_H
21
22 #define _HURD_PORT_H    1
23 #include <features.h>
24
25 #include <mach.h>
26 #include <hurd/userlink.h>
27 #include <spin-lock.h>
28 #include <hurd/signal.h>
29
30
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>.  */
37
38 struct hurd_port
39   {
40     spin_lock_t lock;           /* Locks rest.  */
41     struct hurd_userlink *users; /* Chain of users; see below.  */
42     mach_port_t port;           /* Port. */
43   };
44
45
46 /* Evaluate EXPR with the variable `port' bound to the port in PORTCELL.  */
47
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; })
55
56
57 #ifndef _HURD_PORT_H_EXTERN_INLINE
58 #define _HURD_PORT_H_EXTERN_INLINE extern __inline
59 #endif
60
61
62 /* Initialize *PORT to INIT.  */
63
64 _HURD_PORT_H_EXTERN_INLINE void
65 _hurd_port_init (struct hurd_port *port, mach_port_t init)
66 {
67   __spin_lock_init (&port->lock);
68   port->users = NULL;
69   port->port = init;
70 }
71
72
73 /* Cleanup function for non-local exits.  */
74 extern void _hurd_port_cleanup (void *, jmp_buf, int);
75
76 /* Get a reference to *PORT, which is locked.
77    Pass return value and LINK to _hurd_port_free when done.  */
78
79 _HURD_PORT_H_EXTERN_INLINE mach_port_t
80 _hurd_port_locked_get (struct hurd_port *port,
81                        struct hurd_userlink *link)
82 {
83   mach_port_t result;
84   result = port->port;
85   if (result != MACH_PORT_NULL)
86     {
87       link->cleanup = &_hurd_port_cleanup;
88       link->cleanup_data = (void *) result;
89       _hurd_userlink_link (&port->users, link);
90     }
91   __spin_unlock (&port->lock);
92   return result;
93 }
94
95 /* Same, but locks PORT first.  */
96
97 _HURD_PORT_H_EXTERN_INLINE mach_port_t
98 _hurd_port_get (struct hurd_port *port,
99                 struct hurd_userlink *link)
100 {
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;
107 }
108
109
110 /* Free a reference gotten with `USED_PORT = _hurd_port_get (PORT, LINK);' */
111
112 _HURD_PORT_H_EXTERN_INLINE void
113 _hurd_port_free (struct hurd_port *port,
114                  struct hurd_userlink *link,
115                  mach_port_t used_port)
116 {
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);
130 }
131
132
133 /* Set *PORT's port to NEWPORT.  NEWPORT's reference is consumed by PORT->port.
134    PORT->lock is locked.  */
135
136 _HURD_PORT_H_EXTERN_INLINE void
137 _hurd_port_locked_set (struct hurd_port *port, mach_port_t newport)
138 {
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);
145 }
146
147 /* Same, but locks PORT first.  */
148
149 _HURD_PORT_H_EXTERN_INLINE void
150 _hurd_port_set (struct hurd_port *port, mach_port_t newport)
151 {
152   HURD_CRITICAL_BEGIN;
153   __spin_lock (&port->lock);
154   _hurd_port_locked_set (port, newport);
155   HURD_CRITICAL_END;
156 }
157
158
159 #endif  /* hurd/port.h */