]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
0a7de745 | 31 | /* |
1c79356b A |
32 | * Mach Operating System |
33 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
0a7de745 | 35 | * |
1c79356b A |
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. | |
0a7de745 | 41 | * |
1c79356b A |
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. | |
0a7de745 | 45 | * |
1c79356b | 46 | * Carnegie Mellon requests users of this software to return to |
0a7de745 | 47 | * |
1c79356b A |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
0a7de745 | 52 | * |
1c79356b A |
53 | * any improvements or extensions that they make and grant Carnegie Mellon |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | /* | |
59 | * File: ipc/ipc_mqueue.h | |
60 | * Author: Rich Draves | |
61 | * Date: 1989 | |
62 | * | |
63 | * Definitions for message queues. | |
64 | */ | |
65 | ||
0a7de745 | 66 | #ifndef _IPC_IPC_MQUEUE_H_ |
1c79356b A |
67 | #define _IPC_IPC_MQUEUE_H_ |
68 | ||
69 | #include <mach_assert.h> | |
70 | ||
71 | #include <mach/message.h> | |
72 | ||
73 | #include <kern/assert.h> | |
74 | #include <kern/macro_help.h> | |
91447636 | 75 | #include <kern/kern_types.h> |
3e170ce0 | 76 | #include <kern/waitq.h> |
1c79356b A |
77 | |
78 | #include <ipc/ipc_kmsg.h> | |
79 | #include <ipc/ipc_object.h> | |
80 | #include <ipc/ipc_types.h> | |
81 | ||
b0d623f7 A |
82 | #include <sys/event.h> |
83 | ||
1c79356b A |
84 | typedef struct ipc_mqueue { |
85 | union { | |
86 | struct { | |
0a7de745 A |
87 | struct waitq waitq; |
88 | struct ipc_kmsg_queue messages; | |
89 | mach_port_seqno_t seqno; | |
90 | mach_port_name_t receiver_name; | |
91 | uint16_t msgcount; | |
92 | uint16_t qlimit; | |
39037602 | 93 | #if MACH_FLIPC |
0a7de745 | 94 | struct flipc_port *fport; // Null for local port, or ptr to flipc port |
39037602 | 95 | #endif |
813fb2f6 | 96 | } port; |
b0d623f7 | 97 | struct { |
0a7de745 | 98 | struct waitq_set setq; |
813fb2f6 | 99 | } pset; |
1c79356b | 100 | } data; |
d9a64523 A |
101 | union { |
102 | struct klist imq_klist; | |
103 | uintptr_t imq_inheritor; | |
104 | }; | |
1c79356b A |
105 | } *ipc_mqueue_t; |
106 | ||
0a7de745 | 107 | #define IMQ_NULL ((ipc_mqueue_t) 0) |
1c79356b | 108 | |
d9a64523 A |
109 | /* |
110 | * When a receive right is in flight, before it can ever be registered with | |
111 | * a new knote, its imq_klist field can be overloaded to hold a pointer | |
112 | * to the knote that the port is pushing on through his turnstile. | |
113 | * | |
114 | * if IMQ_KLIST_VALID() returns true, then the imq_klist field can be used, | |
115 | * else IMQ_INHERITOR() can be used to get the pointer to the knote currently | |
116 | * being the port turnstile inheritor. | |
117 | */ | |
118 | #define IMQ_KLIST_VALID(imq) (((imq)->imq_inheritor & 1) == 0) | |
119 | #define IMQ_INHERITOR(imq) ((struct turnstile *)((imq)->imq_inheritor ^ 1)) | |
120 | #define IMQ_SET_INHERITOR(imq, inheritor) \ | |
121 | MACRO_BEGIN \ | |
0a7de745 A |
122 | assert(((imq)->imq_inheritor & 1) || SLIST_EMPTY(&(imq)->imq_klist)); \ |
123 | ((imq)->imq_inheritor = (uintptr_t)(inheritor) | 1); \ | |
d9a64523 A |
124 | MACRO_END |
125 | ||
0a7de745 A |
126 | #define imq_wait_queue data.port.waitq |
127 | #define imq_messages data.port.messages | |
128 | #define imq_msgcount data.port.msgcount | |
129 | #define imq_qlimit data.port.qlimit | |
130 | #define imq_seqno data.port.seqno | |
131 | #define imq_receiver_name data.port.receiver_name | |
39037602 | 132 | #if MACH_FLIPC |
0a7de745 | 133 | #define imq_fport data.port.fport |
39037602 | 134 | #endif |
1c79356b | 135 | |
3e170ce0 A |
136 | /* |
137 | * we can use the 'eventmask' bits of the waitq b/c | |
138 | * they are only used by global queues | |
139 | */ | |
0a7de745 A |
140 | #define imq_fullwaiters data.port.waitq.waitq_eventmask |
141 | #define imq_in_pset data.port.waitq.waitq_set_id | |
142 | #define imq_preposts data.port.waitq.waitq_prepost_id | |
3e170ce0 | 143 | |
0a7de745 A |
144 | #define imq_set_queue data.pset.setq |
145 | #define imq_is_set(mq) waitqs_is_set(&(mq)->imq_set_queue) | |
146 | #define imq_is_queue(mq) waitq_is_queue(&(mq)->imq_wait_queue) | |
147 | #define imq_is_valid(mq) waitq_is_valid(&(mq)->imq_wait_queue) | |
3e170ce0 | 148 | |
0a7de745 A |
149 | #define imq_lock(mq) waitq_lock(&(mq)->imq_wait_queue) |
150 | #define imq_lock_try(mq) waitq_lock_try(&(mq)->imq_wait_queue) | |
151 | #define imq_unlock(mq) waitq_unlock(&(mq)->imq_wait_queue) | |
152 | #define imq_held(mq) waitq_held(&(mq)->imq_wait_queue) | |
153 | #define imq_valid(mq) waitq_valid(&(mq)->imq_wait_queue) | |
1c79356b | 154 | |
813fb2f6 A |
155 | /* |
156 | * Get an ipc_mqueue pointer from a waitq pointer. These are traditionally the | |
157 | * same pointer, but this conversion makes no assumptions on union structure | |
158 | * member positions - it should allow the waitq to move around in either the | |
159 | * port-set mqueue or the port mqueue independently. | |
160 | */ | |
0a7de745 A |
161 | #define imq_from_waitq(waitq) (waitq_is_set(waitq) ? \ |
162 | ((struct ipc_mqueue *)((void *)( \ | |
163 | (uintptr_t)(waitq) - \ | |
164 | __offsetof(struct ipc_mqueue, imq_set_queue)) \ | |
165 | )) : \ | |
166 | ((struct ipc_mqueue *)((void *)( \ | |
167 | (uintptr_t)(waitq) - \ | |
168 | __offsetof(struct ipc_mqueue, imq_wait_queue)) \ | |
169 | )) \ | |
170 | ) | |
813fb2f6 | 171 | |
3e170ce0 | 172 | extern void imq_reserve_and_lock(ipc_mqueue_t mq, |
0a7de745 | 173 | uint64_t *reserved_prepost); |
3e170ce0 A |
174 | |
175 | extern void imq_release_and_unlock(ipc_mqueue_t mq, | |
0a7de745 | 176 | uint64_t reserved_prepost); |
1c79356b | 177 | |
0a7de745 A |
178 | #define imq_full(mq) ((mq)->imq_msgcount >= (mq)->imq_qlimit) |
179 | #define imq_full_kernel(mq) ((mq)->imq_msgcount >= MACH_PORT_QLIMIT_KERNEL) | |
1c79356b A |
180 | |
181 | extern int ipc_mqueue_full; | |
b0d623f7 | 182 | // extern int ipc_mqueue_rcv; |
1c79356b | 183 | |
0a7de745 A |
184 | #define IPC_MQUEUE_FULL CAST_EVENT64_T(&ipc_mqueue_full) |
185 | #define IPC_MQUEUE_RECEIVE NO_EVENT64 | |
1c79356b A |
186 | |
187 | /* | |
188 | * Exported interfaces | |
189 | */ | |
190 | ||
191 | /* Initialize a newly-allocated message queue */ | |
192 | extern void ipc_mqueue_init( | |
0a7de745 A |
193 | ipc_mqueue_t mqueue, |
194 | boolean_t is_set); | |
3e170ce0 A |
195 | |
196 | /* de-initialize / cleanup an mqueue (specifically waitq resources) */ | |
197 | extern void ipc_mqueue_deinit( | |
0a7de745 | 198 | ipc_mqueue_t mqueue); |
91447636 A |
199 | |
200 | /* destroy an mqueue */ | |
39037602 | 201 | extern boolean_t ipc_mqueue_destroy_locked( |
0a7de745 | 202 | ipc_mqueue_t mqueue); |
1c79356b | 203 | |
1c79356b A |
204 | /* Wake up receivers waiting in a message queue */ |
205 | extern void ipc_mqueue_changed( | |
0a7de745 A |
206 | ipc_space_t space, |
207 | ipc_mqueue_t mqueue); | |
1c79356b | 208 | |
91447636 A |
209 | /* Add the specific mqueue as a member of the set */ |
210 | extern kern_return_t ipc_mqueue_add( | |
0a7de745 A |
211 | ipc_mqueue_t mqueue, |
212 | ipc_mqueue_t set_mqueue, | |
213 | uint64_t *reserved_link, | |
214 | uint64_t *reserved_prepost); | |
91447636 A |
215 | |
216 | /* Check to see if mqueue is member of set_mqueue */ | |
217 | extern boolean_t ipc_mqueue_member( | |
0a7de745 A |
218 | ipc_mqueue_t mqueue, |
219 | ipc_mqueue_t set_mqueue); | |
91447636 A |
220 | |
221 | /* Remove an mqueue from a specific set */ | |
222 | extern kern_return_t ipc_mqueue_remove( | |
0a7de745 A |
223 | ipc_mqueue_t mqueue, |
224 | ipc_mqueue_t set_mqueue); | |
91447636 A |
225 | |
226 | /* Remove an mqueue from all sets */ | |
227 | extern void ipc_mqueue_remove_from_all( | |
0a7de745 | 228 | ipc_mqueue_t mqueue); |
91447636 A |
229 | |
230 | /* Remove all the members of the specifiied set */ | |
231 | extern void ipc_mqueue_remove_all( | |
0a7de745 | 232 | ipc_mqueue_t mqueue); |
91447636 | 233 | |
1c79356b A |
234 | /* Send a message to a port */ |
235 | extern mach_msg_return_t ipc_mqueue_send( | |
0a7de745 A |
236 | ipc_mqueue_t mqueue, |
237 | ipc_kmsg_t kmsg, | |
238 | mach_msg_option_t option, | |
39037602 | 239 | mach_msg_timeout_t timeout_val); |
1c79356b | 240 | |
39236c6e A |
241 | /* check for queue send queue full of a port */ |
242 | extern mach_msg_return_t ipc_mqueue_preflight_send( | |
0a7de745 A |
243 | ipc_mqueue_t mqueue, |
244 | ipc_kmsg_t kmsg, | |
245 | mach_msg_option_t option, | |
246 | mach_msg_timeout_t timeout_val); | |
39236c6e | 247 | |
39037602 A |
248 | /* Set a [send-possible] override on the mqueue */ |
249 | extern void ipc_mqueue_override_send( | |
250 | ipc_mqueue_t mqueue, | |
251 | mach_msg_priority_t override); | |
252 | ||
1c79356b A |
253 | /* Deliver message to message queue or waiting receiver */ |
254 | extern void ipc_mqueue_post( | |
0a7de745 A |
255 | ipc_mqueue_t mqueue, |
256 | ipc_kmsg_t kmsg, | |
257 | mach_msg_option_t option); | |
1c79356b A |
258 | |
259 | /* Receive a message from a message queue */ | |
260 | extern void ipc_mqueue_receive( | |
0a7de745 A |
261 | ipc_mqueue_t mqueue, |
262 | mach_msg_option_t option, | |
263 | mach_msg_size_t max_size, | |
264 | mach_msg_timeout_t timeout_val, | |
1c79356b A |
265 | int interruptible); |
266 | ||
b0d623f7 A |
267 | /* Receive a message from a message queue using a specified thread */ |
268 | extern wait_result_t ipc_mqueue_receive_on_thread( | |
0a7de745 | 269 | ipc_mqueue_t mqueue, |
b0d623f7 A |
270 | mach_msg_option_t option, |
271 | mach_msg_size_t max_size, | |
272 | mach_msg_timeout_t rcv_timeout, | |
273 | int interruptible, | |
274 | thread_t thread); | |
275 | ||
1c79356b | 276 | /* Continuation routine for message receive */ |
91447636 | 277 | extern void ipc_mqueue_receive_continue( |
0a7de745 A |
278 | void *param, |
279 | wait_result_t wresult); | |
1c79356b A |
280 | |
281 | /* Select a message from a queue and try to post it to ourself */ | |
b0d623f7 | 282 | extern void ipc_mqueue_select_on_thread( |
0a7de745 A |
283 | ipc_mqueue_t port_mq, |
284 | ipc_mqueue_t set_mq, | |
285 | mach_msg_option_t option, | |
286 | mach_msg_size_t max_size, | |
b0d623f7 A |
287 | thread_t thread); |
288 | ||
289 | /* Peek into a messaqe queue to see if there are messages */ | |
6d2010ae | 290 | extern unsigned ipc_mqueue_peek( |
0a7de745 A |
291 | ipc_mqueue_t mqueue, |
292 | mach_port_seqno_t *msg_seqnop, | |
293 | mach_msg_size_t *msg_sizep, | |
294 | mach_msg_id_t *msg_idp, | |
295 | mach_msg_max_trailer_t *msg_trailerp, | |
296 | ipc_kmsg_t *kmsgp); | |
39037602 A |
297 | |
298 | /* Peek into a locked messaqe queue to see if there are messages */ | |
299 | extern unsigned ipc_mqueue_peek_locked( | |
0a7de745 A |
300 | ipc_mqueue_t mqueue, |
301 | mach_port_seqno_t *msg_seqnop, | |
302 | mach_msg_size_t *msg_sizep, | |
303 | mach_msg_id_t *msg_idp, | |
304 | mach_msg_max_trailer_t *msg_trailerp, | |
305 | ipc_kmsg_t *kmsgp); | |
39236c6e A |
306 | |
307 | /* Peek into a messaqe queue set to see if there are queues with messages */ | |
308 | extern unsigned ipc_mqueue_set_peek( | |
0a7de745 | 309 | ipc_mqueue_t mqueue); |
1c79356b | 310 | |
39037602 A |
311 | /* Release an mqueue/port reference that was granted by MACH_PEEK_MSG */ |
312 | extern void ipc_mqueue_release_peek_ref( | |
0a7de745 | 313 | ipc_mqueue_t mqueue); |
39037602 | 314 | |
39236c6e A |
315 | /* Gather the names of member port for a given set */ |
316 | extern void ipc_mqueue_set_gather_member_names( | |
0a7de745 A |
317 | ipc_space_t space, |
318 | ipc_mqueue_t set_mq, | |
319 | ipc_entry_num_t maxnames, | |
320 | mach_port_name_t *names, | |
321 | ipc_entry_num_t *actualp); | |
39236c6e | 322 | |
1c79356b A |
323 | /* Clear a message count reservation */ |
324 | extern void ipc_mqueue_release_msgcount( | |
0a7de745 A |
325 | ipc_mqueue_t port_mq, |
326 | ipc_mqueue_t set_mq); | |
1c79356b A |
327 | |
328 | /* Change a queue limit */ | |
329 | extern void ipc_mqueue_set_qlimit( | |
0a7de745 A |
330 | ipc_mqueue_t mqueue, |
331 | mach_port_msgcount_t qlimit); | |
1c79356b A |
332 | |
333 | /* Change a queue's sequence number */ | |
334 | extern void ipc_mqueue_set_seqno( | |
0a7de745 A |
335 | ipc_mqueue_t mqueue, |
336 | mach_port_seqno_t seqno); | |
1c79356b A |
337 | |
338 | /* Convert a name in a space to a message queue */ | |
339 | extern mach_msg_return_t ipc_mqueue_copyin( | |
0a7de745 A |
340 | ipc_space_t space, |
341 | mach_port_name_t name, | |
342 | ipc_mqueue_t *mqueuep, | |
343 | ipc_object_t *objectp); | |
1c79356b | 344 | |
0a7de745 | 345 | #endif /* _IPC_IPC_MQUEUE_H_ */ |