Do some minor critical path performance improvements in the scheduler
[dragonfly.git] / sys / kern / lwkt_msgport.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * NOTE! This file may be compiled for userland libraries as well as for
27  * the kernel.
28  *
29  * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.18 2004/04/10 20:55:23 dillon Exp $
30  */
31
32 #ifdef _KERNEL
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/rtprio.h>
39 #include <sys/queue.h>
40 #include <sys/sysctl.h>
41 #include <sys/kthread.h>
42 #include <machine/cpu.h>
43 #include <sys/lock.h>
44
45 #include <vm/vm.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_page.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_zone.h>
54
55 #include <sys/thread2.h>
56 #include <sys/msgport2.h>
57
58 #include <machine/stdarg.h>
59 #include <machine/ipl.h>
60 #ifdef SMP
61 #include <machine/smp.h>
62 #endif
63
64 #include <sys/malloc.h>
65 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
66
67 #else
68
69 #include <sys/stdint.h>
70 #include <libcaps/thread.h>
71 #include <sys/thread.h>
72 #include <sys/msgport.h>
73 #include <sys/errno.h>
74 #include <libcaps/globaldata.h>
75 #include <sys/thread2.h>
76 #include <sys/msgport2.h>
77 #include <string.h>
78
79 #endif /* _KERNEL */
80
81
82 /************************************************************************
83  *                              MESSAGE FUNCTIONS                       *
84  ************************************************************************/
85
86 static void lwkt_replyport_remote(lwkt_msg_t msg);
87 static void lwkt_putport_remote(lwkt_msg_t msg);
88
89 /*
90  * lwkt_sendmsg()
91  *
92  *      Send a message asynchronously.  This function requests asynchronous
93  *      completion and calls lwkt_beginmsg().  If the target port decides to
94  *      run the message synchronously this function will automatically queue
95  *      the message to the current thread's message queue to present a
96  *      consistent interface to the caller. 
97  *
98  *      The message's ms_cmd must be initialized and its ms_flags must be
99  *      at least zero'd out.  lwkt_sendmsg() will initialize the message's
100  *      reply port to the current thread's built-in reply port.
101  */
102 void
103 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
104 {
105     int error;
106
107     msg->ms_flags |= MSGF_ASYNC;
108     msg->ms_flags &= ~(MSGF_REPLY | MSGF_QUEUED);
109     KKASSERT(msg->ms_reply_port != NULL);
110     if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
111         lwkt_replymsg(msg, error);
112     }
113 }
114
115 /*
116  * lwkt_domsg()
117  *
118  *      Send a message synchronously.  This function requests synchronous
119  *      completion and calls lwkt_beginmsg().  If the target port decides to
120  *      run the message asynchronously this function will block waiting for
121  *      the message to complete.  Since MSGF_ASYNC is not set the target
122  *      will not attempt to queue the reply to a reply port but will simply
123  *      wake up anyone waiting on the message.
124  *
125  *      A synchronous error code is always returned.
126  *
127  *      The message's ms_cmd must be initialized and its ms_flags must be
128  *      at least zero'd out.  lwkt_domsg() will initialize the message's
129  *      reply port to the current thread's built-in reply port.
130  */
131 int
132 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg)
133 {
134     int error;
135
136     msg->ms_flags &= ~(MSGF_ASYNC | MSGF_REPLY | MSGF_QUEUED);
137     KKASSERT(msg->ms_reply_port != NULL);
138     if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
139         error = lwkt_waitmsg(msg);
140     }
141     return(error);
142 }
143
144 /************************************************************************
145  *                              PORT FUNCTIONS                          *
146  ************************************************************************/
147
148 /*
149  * lwkt_initport()
150  *
151  *      Initialize a port for use and assign it to the specified thread.
152  */
153 void
154 lwkt_initport(lwkt_port_t port, thread_t td)
155 {
156     bzero(port, sizeof(*port));
157     TAILQ_INIT(&port->mp_msgq);
158     port->mp_td = td;
159     port->mp_putport = lwkt_default_putport;
160     port->mp_waitport =  lwkt_default_waitport;
161     port->mp_replyport = lwkt_default_replyport;
162     port->mp_abortport = lwkt_default_abortport;
163 }
164
165 /*
166  * lwkt_getport()
167  *
168  *      Retrieve the next message from the port's message queue, return NULL
169  *      if no messages are pending.
170  *
171  *      The calling thread MUST own the port.
172  */
173 void *
174 lwkt_getport(lwkt_port_t port)
175 {
176     lwkt_msg_t msg;
177
178     KKASSERT(port->mp_td == curthread);
179
180     crit_enter();
181     if ((msg = TAILQ_FIRST(&port->mp_msgq)) != NULL) {
182         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
183         msg->ms_flags &= ~MSGF_QUEUED;
184     }
185     crit_exit();
186     return(msg);
187 }
188
189 /*
190  * lwkt_default_replyport()
191  *
192  *      This function is typically assigned to the mp_replyport port vector.
193  *
194  *      The message is being returned to the specified port.  The port is
195  *      owned by the mp_td thread.  If we are on the same cpu as the mp_td
196  *      thread we can trivially queue the message to the messageq, otherwise
197  *      we have to send an ipi message to the correct cpu.   We then schedule
198  *      the target thread.
199  *
200  *      If MSGF_ASYNC is not set we do not bother queueing the message, we
201  *      just set the DONE bit.  
202  *
203  *      This inline must be entered with a critical section already held.
204  *      Note that the IPIQ callback function (*_remote) is entered with a
205  *      critical section already held, and we obtain one in lwkt_replyport().
206  */
207 static __inline
208 void
209 _lwkt_replyport(lwkt_port_t port, lwkt_msg_t msg)
210 {
211     thread_t td = port->mp_td;
212
213     if (td->td_gd == mycpu) {
214         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
215         msg->ms_flags |= MSGF_DONE | MSGF_REPLY | MSGF_QUEUED;
216         if (port->mp_flags & MSGPORTF_WAITING)
217             lwkt_schedule(td);
218     } else {
219         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_replyport_remote, msg);
220     }
221 }
222
223 static
224 void
225 lwkt_replyport_remote(lwkt_msg_t msg)
226 {
227     _lwkt_replyport(msg->ms_reply_port, msg);
228 }
229
230 void
231 lwkt_default_replyport(lwkt_port_t port, lwkt_msg_t msg)
232 {
233     crit_enter();
234     if (msg->ms_flags & MSGF_ASYNC) {
235         _lwkt_replyport(port, msg);
236     } else {
237         msg->ms_flags |= MSGF_DONE;
238         if (port->mp_flags & MSGPORTF_WAITING)
239             lwkt_schedule(port->mp_td);
240     }
241     crit_exit();
242 }
243
244 /*
245  * lwkt_default_putport()
246  *
247  *      This function is typically assigned to the mp_putport port vector.
248  *
249  *      Queue a message to the target port and wakeup the thread owning it.
250  *      This function always returns EASYNC and may be assigned to a
251  *      message port's mp_putport function vector.
252  *
253  *      You must already be in a critical section when calling
254  *      the inline function.  The _remote function will be in a critical
255  *      section due to being called from the IPI, and lwkt_default_putport() 
256  *      enters a critical section.
257  */
258 static
259 __inline
260 void
261 _lwkt_putport(lwkt_port_t port, lwkt_msg_t msg)
262 {
263     thread_t td = port->mp_td;
264
265     if (td->td_gd == mycpu) {
266         TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
267         msg->ms_flags |= MSGF_QUEUED;
268         if (port->mp_flags & MSGPORTF_WAITING)
269             lwkt_schedule(td);
270     } else {
271         msg->ms_target_port = port;
272         lwkt_send_ipiq(td->td_gd, (ipifunc_t)lwkt_putport_remote, msg);
273     }
274 }
275
276 static
277 void
278 lwkt_putport_remote(lwkt_msg_t msg)
279 {
280     _lwkt_putport(msg->ms_target_port, msg);
281 }
282
283 int
284 lwkt_default_putport(lwkt_port_t port, lwkt_msg_t msg)
285 {
286     crit_enter();
287     msg->ms_flags &= ~MSGF_DONE;
288     _lwkt_putport(port, msg);
289     crit_exit();
290     return(EASYNC);
291 }
292
293 /*
294  * lwkt_default_abortport()
295  *
296  *      This function is typically assigned to the mp_abortport port vector.
297  *
298  *      This vector is typically called via the message's ms_target_port
299  *      pointer.  It should be noted that ms_target_port may race against
300  *      a forwarding operation run on a different cpu.  Any implementation
301  *      of lwkt_abortport() must deal with potential races by following
302  *      the message to the next appropriate port.
303  *
304  *      This function is a NOP.  by defaults message ports have no abort
305  *      capabilities.  Remember that aborts are always optional so doing 
306  *      nothing is perfectly reasonable.
307  */
308 void
309 lwkt_default_abortport(lwkt_port_t port, lwkt_msg_t msg)
310 {
311     /* NOP */
312 }
313
314 /*
315  * lwkt_default_waitport()
316  *
317  *      If msg is NULL, dequeue the next message from the port's message
318  *      queue, block until a message is ready.  This function never
319  *      returns NULL.
320  *
321  *      If msg is non-NULL, block until the requested message has been returned
322  *      to the port then dequeue and return it.
323  *
324  *      Note that the API does not currently support multiple threads waiting
325  *      on a port.  By virtue of owning the port it is controlled by our
326  *      cpu and we can safely manipulate it's contents.
327  */
328 void *
329 lwkt_default_waitport(lwkt_port_t port, lwkt_msg_t msg)
330 {
331     thread_t td = curthread;
332
333     KKASSERT(port->mp_td == td);
334     crit_enter_quick(td);
335     if (msg == NULL) {
336         if ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL) {
337             port->mp_flags |= MSGPORTF_WAITING;
338             do {
339                 lwkt_deschedule_self(td);
340                 lwkt_switch();
341             } while ((msg = TAILQ_FIRST(&port->mp_msgq)) == NULL);
342             port->mp_flags &= ~MSGPORTF_WAITING;
343         }
344         TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
345         msg->ms_flags &= ~MSGF_QUEUED;
346     } else {
347         /*
348          * If the message is marked done by not queued it has already been
349          * pulled off the port and returned and we do not have to do anything.
350          * Otherwise we do not own the message have to wait for message
351          * completion.  Beware of cpu races if MSGF_DONE is not foudn to be
352          * set!
353          */
354         if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY)) != MSGF_DONE) {
355             /*
356              * We must own the reply port to safely mess with it's contents.
357              */
358             port = msg->ms_reply_port;
359             KKASSERT(port->mp_td == curthread);
360
361             if ((msg->ms_flags & MSGF_DONE) == 0) {
362                 port->mp_flags |= MSGPORTF_WAITING; /* saved by the BGL */
363                 do {
364                     lwkt_deschedule_self(td);
365                     lwkt_switch();
366                 } while ((msg->ms_flags & MSGF_DONE) == 0);
367                 port->mp_flags &= ~MSGPORTF_WAITING; /* saved by the BGL */
368             }
369             /*
370              * We own the message now.
371              */
372             if (msg->ms_flags & MSGF_QUEUED) {
373                 msg->ms_flags &= ~MSGF_QUEUED;
374                 TAILQ_REMOVE(&port->mp_msgq, msg, ms_node);
375             }
376         }
377     }
378     crit_exit_quick(td);
379     return(msg);
380 }
381