1/*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
49 *  School of Computer Science
50 *  Carnegie Mellon University
51 *  Pittsburgh PA 15213-3890
52 *
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
66#ifndef	_IPC_IPC_MQUEUE_H_
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>
75#include <kern/kern_types.h>
76#include <kern/spl.h>
77#include <kern/wait_queue.h>
78
79#include <ipc/ipc_kmsg.h>
80#include <ipc/ipc_object.h>
81#include <ipc/ipc_types.h>
82
83#include <sys/event.h>
84
85typedef struct ipc_mqueue {
86	union {
87		struct {
88			struct  wait_queue	wait_queue;
89			struct ipc_kmsg_queue	messages;
90			mach_port_msgcount_t	msgcount;
91			mach_port_msgcount_t	qlimit;
92			mach_port_seqno_t 	seqno;
93			mach_port_name_t	receiver_name;
94			boolean_t		fullwaiters;
95		} port;
96		struct {
97			struct wait_queue_set	set_queue;
98			mach_port_name_t	local_name;
99		} pset;
100	} data;
101} *ipc_mqueue_t;
102
103#define	IMQ_NULL		((ipc_mqueue_t) 0)
104
105#define imq_wait_queue		data.port.wait_queue
106#define imq_messages		data.port.messages
107#define imq_msgcount		data.port.msgcount
108#define imq_qlimit		data.port.qlimit
109#define imq_seqno		data.port.seqno
110#define imq_receiver_name	data.port.receiver_name
111#define imq_fullwaiters		data.port.fullwaiters
112
113#define imq_set_queue		data.pset.set_queue
114#define imq_setlinks		data.pset.set_queue.wqs_setlinks
115#define imq_preposts		data.pset.set_queue.wqs_preposts
116#define imq_local_name		data.pset.local_name
117#define imq_is_set(mq)		wait_queue_is_set(&(mq)->imq_set_queue)
118
119#define	imq_lock(mq)		wait_queue_lock(&(mq)->imq_wait_queue)
120#define	imq_lock_try(mq)	wait_queue_lock_try(&(mq)->imq_wait_queue)
121#define	imq_unlock(mq)		wait_queue_unlock(&(mq)->imq_wait_queue)
122#define imq_held(mq)		wait_queue_held(&(mq)->imq_wait_queue)
123
124#define imq_full(mq)		((mq)->imq_msgcount >= (mq)->imq_qlimit)
125#define imq_full_kernel(mq)	((mq)->imq_msgcount >= MACH_PORT_QLIMIT_KERNEL)
126
127extern int ipc_mqueue_full;
128// extern int ipc_mqueue_rcv;
129
130#define IPC_MQUEUE_FULL		CAST_EVENT64_T(&ipc_mqueue_full)
131#define IPC_MQUEUE_RECEIVE	NO_EVENT64
132
133/*
134 * Exported interfaces
135 */
136
137/* Initialize a newly-allocated message queue */
138extern void ipc_mqueue_init(
139	ipc_mqueue_t		mqueue,
140	boolean_t		is_set);
141
142/* destroy an mqueue */
143extern void ipc_mqueue_destroy(
144	ipc_mqueue_t		mqueue);
145
146/* Wake up receivers waiting in a message queue */
147extern void ipc_mqueue_changed(
148	ipc_mqueue_t		mqueue);
149
150/* Add the specific mqueue as a member of the set */
151extern kern_return_t ipc_mqueue_add(
152	ipc_mqueue_t		mqueue,
153	ipc_mqueue_t	 	set_mqueue,
154	wait_queue_link_t	wql);
155
156/* Check to see if mqueue is member of set_mqueue */
157extern boolean_t ipc_mqueue_member(
158	ipc_mqueue_t		mqueue,
159	ipc_mqueue_t		set_mqueue);
160
161/* Remove an mqueue from a specific set */
162extern kern_return_t ipc_mqueue_remove(
163	ipc_mqueue_t	 	mqueue,
164	ipc_mqueue_t		set_mqueue,
165	wait_queue_link_t 	*wqlp);
166
167/* Remove an mqueue from all sets */
168extern void ipc_mqueue_remove_from_all(
169	ipc_mqueue_t		mqueue,
170	queue_t 		links);
171
172/* Remove all the members of the specifiied set */
173extern void ipc_mqueue_remove_all(
174	ipc_mqueue_t		mqueue,
175	queue_t			links);
176
177/* Send a message to a port */
178extern mach_msg_return_t ipc_mqueue_send(
179	ipc_mqueue_t		mqueue,
180	ipc_kmsg_t		kmsg,
181	mach_msg_option_t	option,
182	mach_msg_timeout_t	timeout_val,
183	spl_t			s);
184
185/* Deliver message to message queue or waiting receiver */
186extern void ipc_mqueue_post(
187	ipc_mqueue_t		mqueue,
188	ipc_kmsg_t		kmsg);
189
190/* Receive a message from a message queue */
191extern void ipc_mqueue_receive(
192	ipc_mqueue_t		mqueue,
193	mach_msg_option_t	option,
194	mach_msg_size_t		max_size,
195	mach_msg_timeout_t	timeout_val,
196	int                     interruptible);
197
198/* Receive a message from a message queue using a specified thread */
199extern wait_result_t ipc_mqueue_receive_on_thread(
200        ipc_mqueue_t            mqueue,
201	mach_msg_option_t       option,
202	mach_msg_size_t         max_size,
203	mach_msg_timeout_t      rcv_timeout,
204	int                     interruptible,
205	thread_t                thread);
206
207/* Continuation routine for message receive */
208extern void ipc_mqueue_receive_continue(
209	void			*param,
210	wait_result_t		wresult);
211
212/* Select a message from a queue and try to post it to ourself */
213extern void ipc_mqueue_select_on_thread(
214	ipc_mqueue_t		mqueue,
215	mach_msg_option_t	option,
216	mach_msg_size_t		max_size,
217	thread_t                thread);
218
219/* Peek into a messaqe queue to see if there are messages */
220extern unsigned ipc_mqueue_peek(
221	ipc_mqueue_t		mqueue);
222
223/* Clear a message count reservation */
224extern void ipc_mqueue_release_msgcount(
225	ipc_mqueue_t		mqueue);
226
227/* Change a queue limit */
228extern void ipc_mqueue_set_qlimit(
229	ipc_mqueue_t		mqueue,
230	mach_port_msgcount_t	qlimit);
231
232/* Change a queue's sequence number */
233extern void ipc_mqueue_set_seqno(
234	ipc_mqueue_t		mqueue,
235	mach_port_seqno_t 	seqno);
236
237/* Convert a name in a space to a message queue */
238extern mach_msg_return_t ipc_mqueue_copyin(
239	ipc_space_t		space,
240	mach_port_name_t	name,
241	ipc_mqueue_t		*mqueuep,
242	ipc_object_t		*objectp);
243
244#endif	/* _IPC_IPC_MQUEUE_H_ */
245