]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_sync.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_sync.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 *
28 */
29
30 #include <kern/sync_sema.h>
31 #include <kern/sync_lock.h>
32 #include <kern/ipc_kobject.h>
33 #include <kern/ipc_sync.h>
34 #include <ipc/port.h>
35 #include <ipc/ipc_space.h>
36 #include <ipc/ipc_port.h>
37 #include <mach/semaphore.h>
38 #include <mach/lock_set_server.h>
39 #include <mach/mach_port_server.h>
40 #include <mach/port.h>
41
42
43 kern_return_t
44 port_name_to_semaphore(
45 mach_port_name_t name,
46 semaphore_t *semaphorep)
47 {
48 semaphore_t semaphore;
49 ipc_port_t kern_port;
50 kern_return_t kr;
51
52 if (!MACH_PORT_VALID(name)) {
53 *semaphorep = SEMAPHORE_NULL;
54 return KERN_INVALID_NAME;
55 }
56
57 kr = ipc_object_translate(current_space(), name, MACH_PORT_RIGHT_SEND,
58 (ipc_object_t *) &kern_port);
59 if (kr != KERN_SUCCESS) {
60 *semaphorep = SEMAPHORE_NULL;
61 return kr;
62 }
63 /* have the port locked */
64 assert(IP_VALID(kern_port));
65
66 if (!ip_active(kern_port) || (ip_kotype(kern_port) != IKOT_SEMAPHORE)) {
67 ip_unlock(kern_port);
68 *semaphorep = SEMAPHORE_NULL;
69 return KERN_INVALID_ARGUMENT;
70 }
71
72 semaphore = (semaphore_t) kern_port->ip_kobject;
73 assert(semaphore != SEMAPHORE_NULL);
74 semaphore_reference(semaphore);
75 ip_unlock(kern_port);
76
77 *semaphorep = semaphore;
78 return KERN_SUCCESS;
79 }
80
81 semaphore_t
82 convert_port_to_semaphore (ipc_port_t port)
83 {
84 semaphore_t semaphore = SEMAPHORE_NULL;
85
86 if (IP_VALID (port)) {
87 ip_lock(port);
88 if (ip_active(port) && (ip_kotype(port) == IKOT_SEMAPHORE)) {
89 semaphore = (semaphore_t) port->ip_kobject;
90 semaphore_reference(semaphore);
91 }
92 ip_unlock(port);
93 }
94
95 return (semaphore);
96 }
97
98
99 ipc_port_t
100 convert_semaphore_to_port (semaphore_t semaphore)
101 {
102 ipc_port_t port;
103
104 if (semaphore != SEMAPHORE_NULL)
105 port = ipc_port_make_send(semaphore->port);
106 else
107 port = IP_NULL;
108
109 return (port);
110 }
111
112 lock_set_t
113 convert_port_to_lock_set (ipc_port_t port)
114 {
115 lock_set_t lock_set = LOCK_SET_NULL;
116
117 if (IP_VALID (port)) {
118 ip_lock(port);
119 if (ip_active(port) && (ip_kotype(port) == IKOT_LOCK_SET)) {
120 lock_set = (lock_set_t) port->ip_kobject;
121 lock_set_reference(lock_set);
122 }
123 ip_unlock(port);
124 }
125
126 return (lock_set);
127 }
128
129 ipc_port_t
130 convert_lock_set_to_port (lock_set_t lock_set)
131 {
132 ipc_port_t port;
133
134 if (lock_set != LOCK_SET_NULL)
135 port = ipc_port_make_send(lock_set->port);
136 else
137 port = IP_NULL;
138
139 return (port);
140 }
141