xref: /illumos-gate/usr/src/uts/common/sys/msg_impl.h (revision eb9fe4ca)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_MSG_IMPL_H
27 #define	_SYS_MSG_IMPL_H
28 
29 #include <sys/ipc_impl.h>
30 #if defined(_KERNEL) || defined(_KMEMUSER)
31 #include <sys/msg.h>
32 #include <sys/t_lock.h>
33 #include <sys/list.h>
34 #endif
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Argument vectors for the various flavors of msgsys().
42  */
43 
44 #define	MSGGET	0
45 #define	MSGCTL	1
46 #define	MSGRCV	2
47 #define	MSGSND	3
48 #define	MSGIDS	4
49 #define	MSGSNAP	5
50 
51 #if defined(_KERNEL) || defined(_KMEMUSER)
52 
53 typedef struct  msgq_wakeup {
54 	list_node_t	msgw_list;
55 	long		msgw_type;	/* Message type request. */
56 	long		msgw_snd_wake;	/* Type of msg from msgsnd */
57 	size_t		msgw_snd_size;	/* Designates size of the msg sending */
58 	kthread_t	*msgw_thrd;	/* Thread waiting */
59 	kcondvar_t	msgw_wake_cv;	/* waiting on this */
60 } msgq_wakeup_t;
61 
62 
63 typedef struct msg_select {
64 	msgq_wakeup_t *(*selection)();
65 	struct msg_select *next_selection;
66 } msg_select_t;
67 
68 /*
69  * There is one msg structure for each message in the system.
70  */
71 struct msg {
72 	list_node_t	msg_node;	/* message list node */
73 	long		msg_type;	/* message type */
74 	size_t		msg_size;	/* message text size */
75 	void		*msg_addr;	/* message text address */
76 	long		msg_flags;	/* message flags */
77 	long		msg_copycnt;	/* current # of copyouts on message */
78 };
79 
80 /*
81  * Per message flags
82  */
83 #define	MSG_RCVCOPY	00001	/* msgrcv is copying out this message */
84 #define	MSG_UNLINKED	00002	/* msg has been unlinked from queue */
85 
86 /*
87  * msg_rcv_cv is now an array of kcondvar_t for performance reason.
88  * We use multiple condition variables (kcondvar_t) to avoid needing
89  * to wake all readers when sending a single message.
90  */
91 
92 #define	MSG_NEG_INTERVAL 8
93 #define	MSG_MAX_QNUM	64
94 #define	MSG_MAX_QNUM_CV	65
95 
96 typedef struct kmsqid {
97 	kipc_perm_t	msg_perm;	/* operation permission struct */
98 	list_t		msg_list;	/* list of messages on q */
99 	msglen_t	msg_cbytes;	/* current # bytes on q */
100 	msgqnum_t	msg_qnum;	/* # of messages on q */
101 	msgqnum_t	msg_qmax;	/* max # of messages on q */
102 	msglen_t	msg_qbytes;	/* max # of bytes on q */
103 	pid_t		msg_lspid;	/* pid of last msgsnd */
104 	pid_t		msg_lrpid;	/* pid of last msgrcv */
105 	time_t		msg_stime;	/* last msgsnd time */
106 	time_t		msg_rtime;	/* last msgrcv time */
107 	time_t		msg_ctime;	/* last change time */
108 	uint_t		msg_snd_cnt;	/* # of waiting senders */
109 	uint_t		msg_rcv_cnt;	/* # of waiting receivers */
110 	uint64_t	msg_lowest_type; /* Smallest type on queue */
111 	/*
112 	 * linked list of routines used to determine what to wake up next.
113 	 * 	msg_fnd_sndr:	Routines for waking up readers waiting
114 	 *			for a message from the sender.
115 	 *	msg_fnd_rdr:	Routines for waking up readers waiting
116 	 *			for a copyout to finish.
117 	 */
118 	msg_select_t	*msg_fnd_sndr;
119 	msg_select_t	*msg_fnd_rdr;
120 	/*
121 	 * Various counts and queues for controlling the sleeping
122 	 * and waking up of processes that are waiting for various
123 	 * message queue events.
124 	 *
125 	 * msg_cpy_block:   List of receiving threads that are blocked because
126 	 *		    the message of choice is being copied out.
127 	 * msg_wait_snd:    List of receiving threads whose type specifier
128 	 *		    is positive or 0 but are blocked because there
129 	 *		    are no matches.
130 	 * msg_wait_snd_ngt:
131 	 *		    List of receiving threads whose type specifier is
132 	 *		    negative message type but are blocked because
133 	 *		    there are no types that qualify.
134 	 * msg_wait_rcv:    List of sending threads that are blocked because
135 	 *		    there is no room left on the message queue.
136 	 */
137 	kcondvar_t	msg_snd_cv;
138 	list_t		msg_cpy_block;
139 	list_t		msg_wait_snd[MSG_MAX_QNUM_CV];
140 	list_t		msg_wait_snd_ngt[MSG_MAX_QNUM_CV];
141 	list_t		msg_wait_rcv;
142 	size_t		msg_snd_smallest; /* Smallest msg on send wait list */
143 	int		msg_ngt_cnt;	/* # of negative receivers blocked */
144 	char		msg_neg_copy;	/* Neg type copy underway */
145 } kmsqid_t;
146 
147 #endif	/* _KERNEL */
148 
149 #if defined(_SYSCALL32)
150 /*
151  * LP64 view of the ILP32 msgbuf structure
152  */
153 struct ipcmsgbuf32 {
154 	int32_t	mtype;		/* message type */
155 	char	mtext[1];	/* message text */
156 };
157 
158 /*
159  * LP64 view of the ILP32 msgsnap_head and msgsnap_mhead structures
160  */
161 struct msgsnap_head32 {
162 	size32_t msgsnap_size;	/* bytes consumed/required in the buffer */
163 	size32_t msgsnap_nmsg;	/* number of messages in the buffer */
164 };
165 
166 struct msgsnap_mhead32 {
167 	size32_t msgsnap_mlen;	/* number of bytes in message that follows */
168 	int32_t	msgsnap_mtype;	/* message type */
169 };
170 
171 /*
172  * LP64 view of the ILP32 msqid_ds structure
173  */
174 struct msqid_ds32 {
175 	struct ipc_perm32 msg_perm;	/* operation permission struct */
176 	caddr32_t	msg_first;	/* ptr to first message on q */
177 	caddr32_t	msg_last;	/* ptr to last message on q */
178 	uint32_t	msg_cbytes;	/* current # bytes on q */
179 	uint32_t	msg_qnum;	/* # of messages on q */
180 	uint32_t	msg_qbytes;	/* max # of bytes on q */
181 	pid32_t		msg_lspid;	/* pid of last msgsnd */
182 	pid32_t		msg_lrpid;	/* pid of last msgrcv */
183 	time32_t	msg_stime;	/* last msgsnd time */
184 	int32_t		msg_pad1;	/* reserved for time_t expansion */
185 	time32_t	msg_rtime;	/* last msgrcv time */
186 	int32_t		msg_pad2;	/* time_t expansion */
187 	time32_t	msg_ctime;	/* last change time */
188 	int32_t		msg_pad3;	/* time expansion */
189 	int16_t		msg_cv;
190 	int16_t		msg_qnum_cv;
191 	int32_t		msg_pad4[3];	/* reserve area */
192 };
193 #endif	/* _SYSCALL32 */
194 
195 #ifdef	__cplusplus
196 }
197 #endif
198 
199 #endif	/* _SYS_MSG_IMPL_H */
200