]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_mqueue.h
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_mqueue.h
CommitLineData
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
84typedef 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;
cb323159
A
93#ifdef __LP64__
94 uint32_t qcontext;
95#endif
39037602 96#if MACH_FLIPC
0a7de745 97 struct flipc_port *fport; // Null for local port, or ptr to flipc port
39037602 98#endif
813fb2f6 99 } port;
b0d623f7 100 struct {
0a7de745 101 struct waitq_set setq;
813fb2f6 102 } pset;
1c79356b 103 } data;
d9a64523 104 union {
cb323159
A
105 /*
106 * Port Sets:
107 * only use imq_klist
108 *
109 * Special Reply Ports (ip_specialreply == true):
110 * only use imq_srp_owner_thread
111 *
112 * Ports, based on ip_sync_link_state, use:
113 * - PORT_SYNC_LINK_ANY: imq_klist
114 * - PORT_SYNC_LINK_WORKLOOP_KNOTE: imq_inheritor_knote
115 * - PORT_SYNC_LINK_WORKLOOP_STASH: imq_inheritor_turnstile
116 * - PORT_SYNC_LINK_RCV_THREAD: imq_inheritor_thread_ref
117 */
d9a64523 118 struct klist imq_klist;
cb323159
A
119 struct knote *imq_inheritor_knote;
120 struct turnstile *imq_inheritor_turnstile;
121 thread_t imq_inheritor_thread_ref;
122 thread_t imq_srp_owner_thread;
d9a64523 123 };
cb323159
A
124#ifndef __LP64__
125 uint32_t qcontext;
126#endif
1c79356b
A
127} *ipc_mqueue_t;
128
0a7de745 129#define IMQ_NULL ((ipc_mqueue_t) 0)
1c79356b 130
0a7de745
A
131#define imq_wait_queue data.port.waitq
132#define imq_messages data.port.messages
133#define imq_msgcount data.port.msgcount
134#define imq_qlimit data.port.qlimit
135#define imq_seqno data.port.seqno
136#define imq_receiver_name data.port.receiver_name
39037602 137#if MACH_FLIPC
0a7de745 138#define imq_fport data.port.fport
39037602 139#endif
1c79356b 140
cb323159
A
141/*
142 * The qcontext structure member fills in a 32-bit padding gap in ipc_mqueue.
143 * However, the 32-bits are in slightly different places on 32 and 64 bit systems.
144 */
145#ifdef __LP64__
146#define imq_context data.port.qcontext
147#else
148#define imq_context qcontext
149#endif
150
3e170ce0
A
151/*
152 * we can use the 'eventmask' bits of the waitq b/c
153 * they are only used by global queues
154 */
0a7de745
A
155#define imq_fullwaiters data.port.waitq.waitq_eventmask
156#define imq_in_pset data.port.waitq.waitq_set_id
157#define imq_preposts data.port.waitq.waitq_prepost_id
3e170ce0 158
0a7de745
A
159#define imq_set_queue data.pset.setq
160#define imq_is_set(mq) waitqs_is_set(&(mq)->imq_set_queue)
161#define imq_is_queue(mq) waitq_is_queue(&(mq)->imq_wait_queue)
162#define imq_is_valid(mq) waitq_is_valid(&(mq)->imq_wait_queue)
3e170ce0 163
0a7de745
A
164#define imq_unlock(mq) waitq_unlock(&(mq)->imq_wait_queue)
165#define imq_held(mq) waitq_held(&(mq)->imq_wait_queue)
166#define imq_valid(mq) waitq_valid(&(mq)->imq_wait_queue)
1c79356b 167
cb323159
A
168extern void imq_lock(ipc_mqueue_t mq);
169extern unsigned int imq_lock_try(ipc_mqueue_t mq);
170
813fb2f6
A
171/*
172 * Get an ipc_mqueue pointer from a waitq pointer. These are traditionally the
173 * same pointer, but this conversion makes no assumptions on union structure
174 * member positions - it should allow the waitq to move around in either the
175 * port-set mqueue or the port mqueue independently.
176 */
cb323159
A
177#define imq_from_waitq(waitq) (waitq_is_set(waitq) ? \
178 __container_of(waitq, struct ipc_mqueue, imq_set_queue.wqset_q) : \
179 __container_of(waitq, struct ipc_mqueue, imq_wait_queue))
180
181#define imq_to_object(mq) ip_to_object(ip_from_mq(mq))
813fb2f6 182
3e170ce0 183extern void imq_reserve_and_lock(ipc_mqueue_t mq,
0a7de745 184 uint64_t *reserved_prepost);
3e170ce0
A
185
186extern void imq_release_and_unlock(ipc_mqueue_t mq,
0a7de745 187 uint64_t reserved_prepost);
1c79356b 188
0a7de745
A
189#define imq_full(mq) ((mq)->imq_msgcount >= (mq)->imq_qlimit)
190#define imq_full_kernel(mq) ((mq)->imq_msgcount >= MACH_PORT_QLIMIT_KERNEL)
1c79356b
A
191
192extern int ipc_mqueue_full;
b0d623f7 193// extern int ipc_mqueue_rcv;
1c79356b 194
0a7de745
A
195#define IPC_MQUEUE_FULL CAST_EVENT64_T(&ipc_mqueue_full)
196#define IPC_MQUEUE_RECEIVE NO_EVENT64
1c79356b
A
197
198/*
199 * Exported interfaces
200 */
201
202/* Initialize a newly-allocated message queue */
203extern void ipc_mqueue_init(
0a7de745
A
204 ipc_mqueue_t mqueue,
205 boolean_t is_set);
3e170ce0
A
206
207/* de-initialize / cleanup an mqueue (specifically waitq resources) */
208extern void ipc_mqueue_deinit(
0a7de745 209 ipc_mqueue_t mqueue);
91447636
A
210
211/* destroy an mqueue */
39037602 212extern boolean_t ipc_mqueue_destroy_locked(
0a7de745 213 ipc_mqueue_t mqueue);
1c79356b 214
1c79356b
A
215/* Wake up receivers waiting in a message queue */
216extern void ipc_mqueue_changed(
0a7de745
A
217 ipc_space_t space,
218 ipc_mqueue_t mqueue);
1c79356b 219
91447636
A
220/* Add the specific mqueue as a member of the set */
221extern kern_return_t ipc_mqueue_add(
0a7de745
A
222 ipc_mqueue_t mqueue,
223 ipc_mqueue_t set_mqueue,
224 uint64_t *reserved_link,
225 uint64_t *reserved_prepost);
91447636
A
226
227/* Check to see if mqueue is member of set_mqueue */
228extern boolean_t ipc_mqueue_member(
0a7de745
A
229 ipc_mqueue_t mqueue,
230 ipc_mqueue_t set_mqueue);
91447636
A
231
232/* Remove an mqueue from a specific set */
233extern kern_return_t ipc_mqueue_remove(
0a7de745
A
234 ipc_mqueue_t mqueue,
235 ipc_mqueue_t set_mqueue);
91447636
A
236
237/* Remove an mqueue from all sets */
238extern void ipc_mqueue_remove_from_all(
0a7de745 239 ipc_mqueue_t mqueue);
91447636
A
240
241/* Remove all the members of the specifiied set */
242extern void ipc_mqueue_remove_all(
0a7de745 243 ipc_mqueue_t mqueue);
91447636 244
1c79356b
A
245/* Send a message to a port */
246extern mach_msg_return_t ipc_mqueue_send(
0a7de745
A
247 ipc_mqueue_t mqueue,
248 ipc_kmsg_t kmsg,
249 mach_msg_option_t option,
39037602 250 mach_msg_timeout_t timeout_val);
1c79356b 251
39236c6e
A
252/* check for queue send queue full of a port */
253extern mach_msg_return_t ipc_mqueue_preflight_send(
0a7de745
A
254 ipc_mqueue_t mqueue,
255 ipc_kmsg_t kmsg,
256 mach_msg_option_t option,
257 mach_msg_timeout_t timeout_val);
39236c6e 258
39037602
A
259/* Set a [send-possible] override on the mqueue */
260extern void ipc_mqueue_override_send(
261 ipc_mqueue_t mqueue,
262 mach_msg_priority_t override);
263
1c79356b
A
264/* Deliver message to message queue or waiting receiver */
265extern void ipc_mqueue_post(
0a7de745
A
266 ipc_mqueue_t mqueue,
267 ipc_kmsg_t kmsg,
268 mach_msg_option_t option);
1c79356b
A
269
270/* Receive a message from a message queue */
271extern void ipc_mqueue_receive(
0a7de745
A
272 ipc_mqueue_t mqueue,
273 mach_msg_option_t option,
274 mach_msg_size_t max_size,
275 mach_msg_timeout_t timeout_val,
1c79356b
A
276 int interruptible);
277
b0d623f7
A
278/* Receive a message from a message queue using a specified thread */
279extern wait_result_t ipc_mqueue_receive_on_thread(
0a7de745 280 ipc_mqueue_t mqueue,
b0d623f7
A
281 mach_msg_option_t option,
282 mach_msg_size_t max_size,
283 mach_msg_timeout_t rcv_timeout,
284 int interruptible,
285 thread_t thread);
286
1c79356b 287/* Continuation routine for message receive */
91447636 288extern void ipc_mqueue_receive_continue(
0a7de745
A
289 void *param,
290 wait_result_t wresult);
1c79356b
A
291
292/* Select a message from a queue and try to post it to ourself */
b0d623f7 293extern void ipc_mqueue_select_on_thread(
0a7de745
A
294 ipc_mqueue_t port_mq,
295 ipc_mqueue_t set_mq,
296 mach_msg_option_t option,
297 mach_msg_size_t max_size,
b0d623f7
A
298 thread_t thread);
299
300/* Peek into a messaqe queue to see if there are messages */
6d2010ae 301extern unsigned ipc_mqueue_peek(
0a7de745
A
302 ipc_mqueue_t mqueue,
303 mach_port_seqno_t *msg_seqnop,
304 mach_msg_size_t *msg_sizep,
305 mach_msg_id_t *msg_idp,
306 mach_msg_max_trailer_t *msg_trailerp,
307 ipc_kmsg_t *kmsgp);
39037602
A
308
309/* Peek into a locked messaqe queue to see if there are messages */
310extern unsigned ipc_mqueue_peek_locked(
0a7de745
A
311 ipc_mqueue_t mqueue,
312 mach_port_seqno_t *msg_seqnop,
313 mach_msg_size_t *msg_sizep,
314 mach_msg_id_t *msg_idp,
315 mach_msg_max_trailer_t *msg_trailerp,
316 ipc_kmsg_t *kmsgp);
39236c6e
A
317
318/* Peek into a messaqe queue set to see if there are queues with messages */
319extern unsigned ipc_mqueue_set_peek(
0a7de745 320 ipc_mqueue_t mqueue);
1c79356b 321
39037602
A
322/* Release an mqueue/port reference that was granted by MACH_PEEK_MSG */
323extern void ipc_mqueue_release_peek_ref(
0a7de745 324 ipc_mqueue_t mqueue);
39037602 325
39236c6e
A
326/* Gather the names of member port for a given set */
327extern void ipc_mqueue_set_gather_member_names(
0a7de745
A
328 ipc_space_t space,
329 ipc_mqueue_t set_mq,
330 ipc_entry_num_t maxnames,
331 mach_port_name_t *names,
332 ipc_entry_num_t *actualp);
39236c6e 333
1c79356b
A
334/* Clear a message count reservation */
335extern void ipc_mqueue_release_msgcount(
0a7de745
A
336 ipc_mqueue_t port_mq,
337 ipc_mqueue_t set_mq);
1c79356b
A
338
339/* Change a queue limit */
340extern void ipc_mqueue_set_qlimit(
0a7de745
A
341 ipc_mqueue_t mqueue,
342 mach_port_msgcount_t qlimit);
1c79356b
A
343
344/* Change a queue's sequence number */
345extern void ipc_mqueue_set_seqno(
0a7de745
A
346 ipc_mqueue_t mqueue,
347 mach_port_seqno_t seqno);
1c79356b
A
348
349/* Convert a name in a space to a message queue */
350extern mach_msg_return_t ipc_mqueue_copyin(
0a7de745
A
351 ipc_space_t space,
352 mach_port_name_t name,
353 ipc_mqueue_t *mqueuep,
354 ipc_object_t *objectp);
1c79356b 355
0a7de745 356#endif /* _IPC_IPC_MQUEUE_H_ */