]>
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_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: ipc/ipc_pset.c
63 * Functions to manipulate IPC port sets.
66 #include <mach/port.h>
67 #include <mach/kern_return.h>
68 #include <mach/message.h>
69 #include <ipc/ipc_mqueue.h>
70 #include <ipc/ipc_object.h>
71 #include <ipc/ipc_pset.h>
72 #include <ipc/ipc_right.h>
73 #include <ipc/ipc_space.h>
74 #include <ipc/ipc_port.h>
75 #include <ipc/ipc_print.h>
77 #include <kern/kern_types.h>
80 * Routine: ipc_pset_alloc
82 * Allocate a port set.
84 * Nothing locked. If successful, the port set is returned
85 * locked. (The caller doesn't have a reference.)
87 * KERN_SUCCESS The port set is allocated.
88 * KERN_INVALID_TASK The space is dead.
89 * KERN_NO_SPACE No room for an entry in the space.
90 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
96 mach_port_name_t
*namep
,
100 mach_port_name_t name
;
103 kr
= ipc_object_alloc(space
, IOT_PORT_SET
,
104 MACH_PORT_TYPE_PORT_SET
, 0,
105 &name
, (ipc_object_t
*) &pset
);
106 if (kr
!= KERN_SUCCESS
)
110 pset
->ips_local_name
= name
;
111 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
119 * Routine: ipc_pset_alloc_name
121 * Allocate a port set, with a specific name.
123 * Nothing locked. If successful, the port set is returned
124 * locked. (The caller doesn't have a reference.)
126 * KERN_SUCCESS The port set is allocated.
127 * KERN_INVALID_TASK The space is dead.
128 * KERN_NAME_EXISTS The name already denotes a right.
129 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
135 mach_port_name_t name
,
142 kr
= ipc_object_alloc_name(space
, IOT_PORT_SET
,
143 MACH_PORT_TYPE_PORT_SET
, 0,
144 name
, (ipc_object_t
*) &pset
);
145 if (kr
!= KERN_SUCCESS
)
149 pset
->ips_local_name
= name
;
150 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
157 * Routine: ipc_pset_member
159 * Checks to see if a port is a member of a pset
161 * Both port and port set are locked.
162 * The port must be active.
169 assert(ip_active(port
));
171 return (ipc_mqueue_member(&port
->ip_messages
, &pset
->ips_messages
));
176 * Routine: ipc_pset_add
178 * Puts a port into a port set.
180 * Both port and port set are locked and active.
181 * The owner of the port set is also receiver for the port.
191 assert(ips_active(pset
));
192 assert(ip_active(port
));
194 kr
= ipc_mqueue_add(&port
->ip_messages
, &pset
->ips_messages
);
196 if (kr
== KERN_SUCCESS
)
197 port
->ip_pset_count
++;
205 * Routine: ipc_pset_remove
207 * Removes a port from a port set.
208 * The port set loses a reference.
210 * Both port and port set are locked.
211 * The port must be active.
221 assert(ip_active(port
));
223 if (port
->ip_pset_count
== 0)
224 return KERN_NOT_IN_SET
;
226 kr
= ipc_mqueue_remove(&port
->ip_messages
, &pset
->ips_messages
);
228 if (kr
== KERN_SUCCESS
)
229 port
->ip_pset_count
--;
235 * Routine: ipc_pset_remove_from_all
237 * Removes a port from all it's port sets.
239 * port is locked and active.
243 ipc_pset_remove_from_all(
246 assert(ip_active(port
));
248 if (port
->ip_pset_count
== 0)
249 return KERN_NOT_IN_SET
;
252 * Remove the port's mqueue from all sets
254 ipc_mqueue_remove_from_all(&port
->ip_messages
);
255 port
->ip_pset_count
= 0;
261 * Routine: ipc_pset_destroy
263 * Destroys a port_set.
265 * The port_set is locked and alive.
266 * The caller has a reference, which is consumed.
267 * Afterwards, the port_set is unlocked and dead.
276 assert(ips_active(pset
));
278 pset
->ips_object
.io_bits
&= ~IO_BITS_ACTIVE
;
281 * remove all the member message queues
283 ipc_mqueue_remove_all(&pset
->ips_messages
);
286 imq_lock(&pset
->ips_messages
);
287 ipc_mqueue_changed(&pset
->ips_messages
);
288 imq_unlock(&pset
->ips_messages
);
291 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
293 ips_release(pset
); /* consume the ref our caller gave us */
294 ips_check_unlock(pset
);
297 #include <mach_kdb.h>
300 #include <ddb/db_output.h>
302 #define printf kdbprintf
306 struct ipc_kmsg
*base
)
308 register int count
= 0;
311 struct ipc_kmsg
*kmsg
= base
;
314 while (kmsg
&& kmsg
->ikm_next
!= base
315 && kmsg
->ikm_next
!= IKM_BOGUS
){
316 kmsg
= kmsg
->ikm_next
;
324 * Routine: ipc_pset_print
326 * Pretty-print a port set for kdb.
332 printf("pset 0x%x\n", pset
);
336 ipc_object_print(&pset
->ips_object
);
337 iprintf("local_name = 0x%x\n", pset
->ips_local_name
);
338 iprintf("%d kmsgs => 0x%x",
339 ipc_list_count(pset
->ips_messages
.imq_messages
.ikmq_base
),
340 pset
->ips_messages
.imq_messages
.ikmq_base
);
341 printf(",rcvrs queue= 0x%x\n", &pset
->ips_messages
.imq_wait_queue
);
346 #endif /* MACH_KDB */