]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | * | |
31 | */ | |
32 | ||
33 | #include <kern/sync_sema.h> | |
34 | #include <kern/sync_lock.h> | |
35 | #include <kern/ipc_kobject.h> | |
36 | #include <kern/ipc_sync.h> | |
37 | #include <ipc/port.h> | |
38 | #include <ipc/ipc_space.h> | |
39 | #include <ipc/ipc_port.h> | |
3e170ce0 | 40 | #include <mach/mach_types.h> |
1c79356b A |
41 | #include <mach/semaphore.h> |
42 | #include <mach/lock_set_server.h> | |
43 | #include <mach/mach_port_server.h> | |
44 | #include <mach/port.h> | |
45 | ||
46 | ||
3e170ce0 A |
47 | /* |
48 | * Routine: port_name_to_semaphore | |
49 | * Purpose: | |
50 | * Convert from a port name in the current space to a semaphore. | |
51 | * Produces a semaphore ref, which may be null. | |
52 | * Conditions: | |
53 | * Nothing locked. | |
54 | */ | |
1c79356b A |
55 | kern_return_t |
56 | port_name_to_semaphore( | |
57 | mach_port_name_t name, | |
58 | semaphore_t *semaphorep) | |
59 | { | |
1c79356b A |
60 | ipc_port_t kern_port; |
61 | kern_return_t kr; | |
62 | ||
63 | if (!MACH_PORT_VALID(name)) { | |
64 | *semaphorep = SEMAPHORE_NULL; | |
65 | return KERN_INVALID_NAME; | |
66 | } | |
3e170ce0 | 67 | |
1c79356b A |
68 | kr = ipc_object_translate(current_space(), name, MACH_PORT_RIGHT_SEND, |
69 | (ipc_object_t *) &kern_port); | |
70 | if (kr != KERN_SUCCESS) { | |
71 | *semaphorep = SEMAPHORE_NULL; | |
72 | return kr; | |
73 | } | |
74 | /* have the port locked */ | |
75 | assert(IP_VALID(kern_port)); | |
76 | ||
3e170ce0 | 77 | *semaphorep = convert_port_to_semaphore(kern_port); |
1c79356b A |
78 | ip_unlock(kern_port); |
79 | ||
1c79356b A |
80 | return KERN_SUCCESS; |
81 | } | |
3e170ce0 A |
82 | |
83 | /* | |
84 | * Routine: convert_port_to_semaphore | |
85 | * Purpose: | |
86 | * Convert from a port to a semaphore. | |
87 | * Doesn't consume the port [send-right] ref; | |
88 | * produces a semaphore ref, which may be null. | |
89 | * Conditions: | |
90 | * Caller has a send-right reference to port. | |
91 | * Port may or may not be locked. | |
92 | */ | |
1c79356b A |
93 | semaphore_t |
94 | convert_port_to_semaphore (ipc_port_t port) | |
95 | { | |
1c79356b | 96 | |
3e170ce0 A |
97 | if (IP_VALID(port)) { |
98 | semaphore_t semaphore; | |
99 | ||
100 | /* | |
101 | * No need to lock because we have a reference on the | |
102 | * port, and if it is a true semaphore port, that reference | |
103 | * keeps the semaphore bound to the port (and active). | |
104 | */ | |
105 | if (ip_kotype(port) == IKOT_SEMAPHORE) { | |
106 | assert(ip_active(port)); | |
1c79356b A |
107 | semaphore = (semaphore_t) port->ip_kobject; |
108 | semaphore_reference(semaphore); | |
3e170ce0 | 109 | return (semaphore); |
1c79356b | 110 | } |
1c79356b | 111 | } |
3e170ce0 | 112 | return SEMAPHORE_NULL; |
1c79356b A |
113 | } |
114 | ||
115 | ||
3e170ce0 A |
116 | /* |
117 | * Routine: convert_semaphore_to_port | |
118 | * Purpose: | |
119 | * Convert a semaphore reference to a send right to a | |
120 | * semaphore port. | |
121 | * | |
122 | * Consumes the semaphore reference. If the semaphore | |
123 | * port currently has no send rights (or doesn't exist | |
124 | * yet), the reference is donated to the port to represent | |
125 | * all extant send rights collectively. | |
126 | */ | |
1c79356b A |
127 | ipc_port_t |
128 | convert_semaphore_to_port (semaphore_t semaphore) | |
129 | { | |
3e170ce0 | 130 | ipc_port_t port, send; |
1c79356b | 131 | |
b0d623f7 A |
132 | if (semaphore == SEMAPHORE_NULL) |
133 | return (IP_NULL); | |
1c79356b | 134 | |
b0d623f7 | 135 | /* caller is donating a reference */ |
3e170ce0 A |
136 | port = semaphore->port; |
137 | ||
138 | if (!IP_VALID(port)) { | |
139 | port = ipc_port_alloc_kernel(); | |
140 | assert(IP_VALID(port)); | |
141 | ipc_kobject_set_atomically(port, (ipc_kobject_t) semaphore, IKOT_SEMAPHORE); | |
142 | ||
143 | /* If we lose the race, deallocate and pick up the other guy's port */ | |
144 | if (!OSCompareAndSwapPtr(IP_NULL, port, &semaphore->port)) { | |
145 | ipc_port_dealloc_kernel(port); | |
146 | port = semaphore->port; | |
147 | assert(ip_kotype(port) == IKOT_SEMAPHORE); | |
148 | assert(port->ip_kobject == (ipc_kobject_t)semaphore); | |
149 | } | |
150 | } | |
151 | ||
152 | ip_lock(port); | |
153 | assert(ip_active(port)); | |
154 | send = ipc_port_make_send_locked(port); | |
155 | ||
156 | if (1 == port->ip_srights) { | |
157 | ipc_port_t old_notify; | |
158 | ||
159 | /* transfer our ref to the port, and arm the no-senders notification */ | |
160 | assert(IP_NULL == port->ip_nsrequest); | |
161 | ipc_port_nsrequest(port, port->ip_mscount, ipc_port_make_sonce_locked(port), &old_notify); | |
162 | /* port unlocked */ | |
163 | assert(IP_NULL == old_notify); | |
164 | } else { | |
165 | /* piggyback on the existing port reference, so consume ours */ | |
166 | ip_unlock(port); | |
167 | semaphore_dereference(semaphore); | |
168 | } | |
169 | return (send); | |
170 | } | |
171 | ||
172 | /* | |
173 | * Routine: semaphore_notify | |
174 | * Purpose: | |
175 | * Called whenever the Mach port system detects no-senders | |
176 | * on the semaphore port. | |
177 | * | |
178 | * When a send-right is first created, a no-senders | |
179 | * notification is armed (and a semaphore reference is donated). | |
180 | * | |
181 | * A no-senders notification will be posted when no one else holds a | |
182 | * send-right (reference) to the semaphore's port. This notification function | |
183 | * will consume the semaphore reference donated to the extant collection of | |
184 | * send-rights. | |
185 | */ | |
186 | void | |
187 | semaphore_notify(mach_msg_header_t *msg) | |
188 | { | |
189 | mach_no_senders_notification_t *notification = (void *)msg; | |
190 | ipc_port_t port = notification->not_header.msgh_remote_port; | |
191 | semaphore_t semaphore; | |
192 | ||
193 | assert(ip_active(port)); | |
194 | assert(IKOT_SEMAPHORE == ip_kotype(port)); | |
195 | semaphore = (semaphore_t)port->ip_kobject; | |
196 | ||
b0d623f7 | 197 | semaphore_dereference(semaphore); |
1c79356b A |
198 | } |
199 | ||
200 | lock_set_t | |
39236c6e | 201 | convert_port_to_lock_set (__unused ipc_port_t port) |
1c79356b | 202 | { |
39236c6e | 203 | return (LOCK_SET_NULL); |
1c79356b A |
204 | } |
205 | ||
206 | ipc_port_t | |
39236c6e | 207 | convert_lock_set_to_port (__unused lock_set_t lock_set) |
1c79356b | 208 | { |
39236c6e | 209 | return (IP_NULL); |
1c79356b A |
210 | } |
211 |