]>
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_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
61 * File: ipc/ipc_pset.c
65 * Functions to manipulate IPC port sets.
68 #include <mach/port.h>
69 #include <mach/kern_return.h>
70 #include <mach/message.h>
71 #include <ipc/ipc_mqueue.h>
72 #include <ipc/ipc_object.h>
73 #include <ipc/ipc_pset.h>
74 #include <ipc/ipc_right.h>
75 #include <ipc/ipc_space.h>
76 #include <ipc/ipc_port.h>
77 #include <ipc/ipc_print.h>
79 #include <kern/kern_types.h>
82 * Routine: ipc_pset_alloc
84 * Allocate a port set.
86 * Nothing locked. If successful, the port set is returned
87 * locked. (The caller doesn't have a reference.)
89 * KERN_SUCCESS The port set is allocated.
90 * KERN_INVALID_TASK The space is dead.
91 * KERN_NO_SPACE No room for an entry in the space.
92 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
98 mach_port_name_t
*namep
,
102 mach_port_name_t name
;
105 kr
= ipc_object_alloc(space
, IOT_PORT_SET
,
106 MACH_PORT_TYPE_PORT_SET
, 0,
107 &name
, (ipc_object_t
*) &pset
);
108 if (kr
!= KERN_SUCCESS
)
112 pset
->ips_local_name
= name
;
113 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
121 * Routine: ipc_pset_alloc_name
123 * Allocate a port set, with a specific name.
125 * Nothing locked. If successful, the port set is returned
126 * locked. (The caller doesn't have a reference.)
128 * KERN_SUCCESS The port set is allocated.
129 * KERN_INVALID_TASK The space is dead.
130 * KERN_NAME_EXISTS The name already denotes a right.
131 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
137 mach_port_name_t name
,
144 kr
= ipc_object_alloc_name(space
, IOT_PORT_SET
,
145 MACH_PORT_TYPE_PORT_SET
, 0,
146 name
, (ipc_object_t
*) &pset
);
147 if (kr
!= KERN_SUCCESS
)
151 pset
->ips_local_name
= name
;
152 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
159 * Routine: ipc_pset_member
161 * Checks to see if a port is a member of a pset
163 * Both port and port set are locked.
164 * The port must be active.
171 assert(ip_active(port
));
173 return (ipc_mqueue_member(&port
->ip_messages
, &pset
->ips_messages
));
178 * Routine: ipc_pset_add
180 * Puts a port into a port set.
182 * Both port and port set are locked and active.
183 * The owner of the port set is also receiver for the port.
193 assert(ips_active(pset
));
194 assert(ip_active(port
));
196 kr
= ipc_mqueue_add(&port
->ip_messages
, &pset
->ips_messages
);
198 if (kr
== KERN_SUCCESS
)
199 port
->ip_pset_count
++;
207 * Routine: ipc_pset_remove
209 * Removes a port from a port set.
210 * The port set loses a reference.
212 * Both port and port set are locked.
213 * The port must be active.
223 assert(ip_active(port
));
225 if (port
->ip_pset_count
== 0)
226 return KERN_NOT_IN_SET
;
228 kr
= ipc_mqueue_remove(&port
->ip_messages
, &pset
->ips_messages
);
230 if (kr
== KERN_SUCCESS
)
231 port
->ip_pset_count
--;
237 * Routine: ipc_pset_remove_from_all
239 * Removes a port from all it's port sets.
241 * port is locked and active.
245 ipc_pset_remove_from_all(
248 assert(ip_active(port
));
250 if (port
->ip_pset_count
== 0)
251 return KERN_NOT_IN_SET
;
254 * Remove the port's mqueue from all sets
256 ipc_mqueue_remove_from_all(&port
->ip_messages
);
257 port
->ip_pset_count
= 0;
263 * Routine: ipc_pset_destroy
265 * Destroys a port_set.
267 * The port_set is locked and alive.
268 * The caller has a reference, which is consumed.
269 * Afterwards, the port_set is unlocked and dead.
278 assert(ips_active(pset
));
280 pset
->ips_object
.io_bits
&= ~IO_BITS_ACTIVE
;
283 * remove all the member message queues
285 ipc_mqueue_remove_all(&pset
->ips_messages
);
288 imq_lock(&pset
->ips_messages
);
289 ipc_mqueue_changed(&pset
->ips_messages
);
290 imq_unlock(&pset
->ips_messages
);
293 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
295 ips_release(pset
); /* consume the ref our caller gave us */
296 ips_check_unlock(pset
);
299 #include <mach_kdb.h>
302 #include <ddb/db_output.h>
304 #define printf kdbprintf
308 struct ipc_kmsg
*base
)
310 register int count
= 0;
313 struct ipc_kmsg
*kmsg
= base
;
316 while (kmsg
&& kmsg
->ikm_next
!= base
317 && kmsg
->ikm_next
!= IKM_BOGUS
){
318 kmsg
= kmsg
->ikm_next
;
326 * Routine: ipc_pset_print
328 * Pretty-print a port set for kdb.
334 printf("pset 0x%x\n", pset
);
338 ipc_object_print(&pset
->ips_object
);
339 iprintf("local_name = 0x%x\n", pset
->ips_local_name
);
340 iprintf("%d kmsgs => 0x%x",
341 ipc_list_count(pset
->ips_messages
.imq_messages
.ikmq_base
),
342 pset
->ips_messages
.imq_messages
.ikmq_base
);
343 printf(",rcvrs queue= 0x%x\n", &pset
->ips_messages
.imq_wait_queue
);
348 #endif /* MACH_KDB */