2 * Copyright (c) 2000 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>
70 #include <kern/thread_pool.h>
73 * Routine: ipc_pset_alloc
75 * Allocate a port set.
77 * Nothing locked. If successful, the port set is returned
78 * locked. (The caller doesn't have a reference.)
80 * KERN_SUCCESS The port set is allocated.
81 * KERN_INVALID_TASK The space is dead.
82 * KERN_NO_SPACE No room for an entry in the space.
83 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
89 mach_port_name_t
*namep
,
93 mach_port_name_t name
;
96 kr
= ipc_object_alloc(space
, IOT_PORT_SET
,
97 MACH_PORT_TYPE_PORT_SET
, 0,
98 &name
, (ipc_object_t
*) &pset
);
99 if (kr
!= KERN_SUCCESS
)
103 pset
->ips_local_name
= name
;
104 pset
->ips_pset_self
= pset
;
105 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
106 thread_pool_init(&pset
->ips_thread_pool
);
114 * Routine: ipc_pset_alloc_name
116 * Allocate a port set, with a specific name.
118 * Nothing locked. If successful, the port set is returned
119 * locked. (The caller doesn't have a reference.)
121 * KERN_SUCCESS The port set is allocated.
122 * KERN_INVALID_TASK The space is dead.
123 * KERN_NAME_EXISTS The name already denotes a right.
124 * KERN_RESOURCE_SHORTAGE Couldn't allocate memory.
130 mach_port_name_t name
,
137 kr
= ipc_object_alloc_name(space
, IOT_PORT_SET
,
138 MACH_PORT_TYPE_PORT_SET
, 0,
139 name
, (ipc_object_t
*) &pset
);
140 if (kr
!= KERN_SUCCESS
)
144 pset
->ips_local_name
= name
;
145 pset
->ips_pset_self
= pset
;
146 ipc_mqueue_init(&pset
->ips_messages
, TRUE
/* set */);
147 thread_pool_init(&pset
->ips_thread_pool
);
154 * Routine: ipc_pset_member
156 * Checks to see if a port is a member of a pset
158 * Both port and port set are locked.
159 * The port must be active.
166 assert(ip_active(port
));
168 return (ipc_mqueue_member(&port
->ip_messages
, &pset
->ips_messages
));
173 * Routine: ipc_pset_add
175 * Puts a port into a port set.
176 * The port set gains a reference.
178 * Both port and port set are locked and active.
179 * The owner of the port set is also receiver for the port.
187 assert(ips_active(pset
));
188 assert(ip_active(port
));
190 if (ipc_pset_member(pset
, port
))
191 return KERN_ALREADY_IN_SET
;
194 port
->ip_pset_count
++;
196 ipc_mqueue_add(&port
->ip_messages
, &pset
->ips_messages
);
204 * Routine: ipc_pset_remove
206 * Removes a port from a port set.
207 * The port set loses a reference.
209 * Both port and port set are locked.
210 * The port must be active.
218 mach_msg_return_t mr
;
220 assert(ip_active(port
));
222 if (port
->ip_pset_count
== 0)
223 return KERN_NOT_IN_SET
;
225 mr
= ipc_mqueue_remove(&port
->ip_messages
, &pset
->ips_messages
);
227 if (mr
== MACH_MSG_SUCCESS
) {
228 port
->ip_pset_count
--;
235 * Routine: ipc_pset_remove_all
237 * Removes a port from all it's port sets.
238 * Each port set loses a reference.
240 * port is locked and active.
247 ipc_pset_mqueue_t pset_mqueue
;
250 assert(ip_active(port
));
252 if (port
->ip_pset_count
== 0)
253 return KERN_NOT_IN_SET
;
256 * Remove each port set's mqueue from the port's (one at a time).
258 while (port
->ip_pset_count
> 0) {
259 ipc_mqueue_remove_one(&port
->ip_messages
,
260 (ipc_mqueue_t
)&pset_mqueue
);
261 assert(pset_mqueue
!= (ipc_pset_mqueue_t
)0);
262 port
->ip_pset_count
--;
264 pset
= pset_mqueue
->ipsm_pset
;
265 ipc_pset_release(pset
); /* locks and unlocks pset */
269 assert(port
->ip_pset_count
== 0);
275 * Routine: ipc_pset_destroy
277 * Destroys a port_set.
279 * Doesn't remove members from the port set;
280 * that happens lazily.
282 * The port_set is locked and alive.
283 * The caller has a reference, which is consumed.
284 * Afterwards, the port_set is unlocked and dead.
293 assert(ips_active(pset
));
295 pset
->ips_object
.io_bits
&= ~IO_BITS_ACTIVE
;
298 imq_lock(&pset
->ips_messages
);
299 ipc_mqueue_changed(&pset
->ips_messages
);
300 imq_unlock(&pset
->ips_messages
);
303 /* XXXX Perhaps ought to verify ips_thread_pool is empty */
305 ips_release(pset
); /* consume the ref our caller gave us */
306 ips_check_unlock(pset
);
309 #include <mach_kdb.h>
312 #include <ddb/db_output.h>
314 #define printf kdbprintf
318 struct ipc_kmsg
*base
)
320 register int count
= 0;
323 struct ipc_kmsg
*kmsg
= base
;
326 while (kmsg
&& kmsg
->ikm_next
!= base
327 && kmsg
->ikm_next
!= IKM_BOGUS
){
328 kmsg
= kmsg
->ikm_next
;
336 * Routine: ipc_pset_print
338 * Pretty-print a port set for kdb.
345 extern int db_indent
;
347 printf("pset 0x%x\n", pset
);
351 ipc_object_print(&pset
->ips_object
);
352 iprintf("local_name = 0x%x\n", pset
->ips_local_name
);
353 iprintf("%d kmsgs => 0x%x",
354 ipc_list_count(pset
->ips_messages
.imq_messages
.ikmq_base
),
355 pset
->ips_messages
.imq_messages
.ikmq_base
);
356 printf(",rcvrs queue= 0x%x\n", &pset
->ips_messages
.imq_wait_queue
);
361 #endif /* MACH_KDB */