]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ipc/ipc_pset.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 * File: ipc/ipc_pset.c
57 * Functions to manipulate IPC port sets.
60 #include <mach/port.h>
61 #include <mach/kern_return.h>
62 #include <mach/message.h>
63 #include <ipc/ipc_mqueue.h>
64 #include <ipc/ipc_object.h>
65 #include <ipc/ipc_pset.h>
66 #include <ipc/ipc_right.h>
67 #include <ipc/ipc_space.h>
68 #include <ipc/ipc_port.h>
69 #include <ipc/ipc_print.h>
71 #include <kern/kern_types.h>
74 * Routine: ipc_pset_alloc
76 * Allocate a port set.
78 * Nothing locked. If successful, the port set is returned
79 * locked. (The caller doesn't have a reference.)
81 * KERN_SUCCESS The port set is allocated.
82 * KERN_INVALID_TASK The space is dead.
83 * KERN_NO_SPACE No room for an entry in the space.
84 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
90 mach_port_name_t
*namep
,
94 mach_port_name_t name
;
97 kr
= ipc_object_alloc(space
, IOT_PORT_SET
,
98 MACH_PORT_TYPE_PORT_SET
, 0,
99 &name
, (ipc_object_t
*) &pset
);
100 if (kr
!= KERN_SUCCESS
)
104 pset
->ips_local_name
= name
;
105 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
113 * Routine: ipc_pset_alloc_name
115 * Allocate a port set, with a specific name.
117 * Nothing locked. If successful, the port set is returned
118 * locked. (The caller doesn't have a reference.)
120 * KERN_SUCCESS The port set is allocated.
121 * KERN_INVALID_TASK The space is dead.
122 * KERN_NAME_EXISTS The name already denotes a right.
123 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
129 mach_port_name_t name
,
136 kr
= ipc_object_alloc_name(space
, IOT_PORT_SET
,
137 MACH_PORT_TYPE_PORT_SET
, 0,
138 name
, (ipc_object_t
*) &pset
);
139 if (kr
!= KERN_SUCCESS
)
143 pset
->ips_local_name
= name
;
144 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
151 * Routine: ipc_pset_member
153 * Checks to see if a port is a member of a pset
155 * Both port and port set are locked.
156 * The port must be active.
163 assert(ip_active(port
));
165 return (ipc_mqueue_member(&port
->ip_messages
, &pset
->ips_messages
));
170 * Routine: ipc_pset_add
172 * Puts a port into a port set.
174 * Both port and port set are locked and active.
175 * The owner of the port set is also receiver for the port.
185 assert(ips_active(pset
));
186 assert(ip_active(port
));
188 kr
= ipc_mqueue_add(&port
->ip_messages
, &pset
->ips_messages
);
190 if (kr
== KERN_SUCCESS
)
191 port
->ip_pset_count
++;
199 * Routine: ipc_pset_remove
201 * Removes a port from a port set.
202 * The port set loses a reference.
204 * Both port and port set are locked.
205 * The port must be active.
215 assert(ip_active(port
));
217 if (port
->ip_pset_count
== 0)
218 return KERN_NOT_IN_SET
;
220 kr
= ipc_mqueue_remove(&port
->ip_messages
, &pset
->ips_messages
);
222 if (kr
== KERN_SUCCESS
)
223 port
->ip_pset_count
--;
229 * Routine: ipc_pset_remove_from_all
231 * Removes a port from all it's port sets.
233 * port is locked and active.
237 ipc_pset_remove_from_all(
240 assert(ip_active(port
));
242 if (port
->ip_pset_count
== 0)
243 return KERN_NOT_IN_SET
;
246 * Remove the port's mqueue from all sets
248 ipc_mqueue_remove_from_all(&port
->ip_messages
);
249 port
->ip_pset_count
= 0;
255 * Routine: ipc_pset_destroy
257 * Destroys a port_set.
259 * The port_set is locked and alive.
260 * The caller has a reference, which is consumed.
261 * Afterwards, the port_set is unlocked and dead.
270 assert(ips_active(pset
));
272 pset
->ips_object
.io_bits
&= ~IO_BITS_ACTIVE
;
275 * remove all the member message queues
277 ipc_mqueue_remove_all(&pset
->ips_messages
);
280 imq_lock(&pset
->ips_messages
);
281 ipc_mqueue_changed(&pset
->ips_messages
);
282 imq_unlock(&pset
->ips_messages
);
285 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
287 ips_release(pset
); /* consume the ref our caller gave us */
288 ips_check_unlock(pset
);
291 #include <mach_kdb.h>
294 #include <ddb/db_output.h>
296 #define printf kdbprintf
300 struct ipc_kmsg
*base
)
302 register int count
= 0;
305 struct ipc_kmsg
*kmsg
= base
;
308 while (kmsg
&& kmsg
->ikm_next
!= base
309 && kmsg
->ikm_next
!= IKM_BOGUS
){
310 kmsg
= kmsg
->ikm_next
;
318 * Routine: ipc_pset_print
320 * Pretty-print a port set for kdb.
326 printf("pset 0x%x\n", pset
);
330 ipc_object_print(&pset
->ips_object
);
331 iprintf("local_name = 0x%x\n", pset
->ips_local_name
);
332 iprintf("%d kmsgs => 0x%x",
333 ipc_list_count(pset
->ips_messages
.imq_messages
.ikmq_base
),
334 pset
->ips_messages
.imq_messages
.ikmq_base
);
335 printf(",rcvrs queue= 0x%x\n", &pset
->ips_messages
.imq_wait_queue
);
340 #endif /* MACH_KDB */