2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * NOTE! This file may be compiled for userland libraries as well as for
37 * $DragonFly: src/sys/kern/lwkt_msgport.c,v 1.54 2008/11/26 15:05:42 sephe Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/rtprio.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/kthread.h>
48 #include <sys/signalvar.h>
49 #include <sys/signal2.h>
50 #include <machine/cpu.h>
54 #include <vm/vm_param.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_pager.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_zone.h>
63 #include <sys/thread2.h>
64 #include <sys/msgport2.h>
65 #include <sys/spinlock2.h>
66 #include <sys/serialize.h>
68 #include <machine/stdarg.h>
69 #include <machine/cpufunc.h>
71 #include <machine/smp.h>
74 #include <sys/malloc.h>
75 MALLOC_DEFINE(M_LWKTMSG, "lwkt message", "lwkt message");
77 /************************************************************************
79 ************************************************************************/
84 * Request asynchronous completion and call lwkt_beginmsg(). The
85 * target port can opt to execute the message synchronously or
86 * asynchronously and this function will automatically queue the
87 * response if the target executes the message synchronously.
89 * NOTE: The message is in an indeterminant state until this call
90 * returns. The caller should not mess with it (e.g. try to abort it)
94 lwkt_sendmsg(lwkt_port_t port, lwkt_msg_t msg)
98 KKASSERT(msg->ms_reply_port != NULL &&
99 (msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) == MSGF_DONE);
100 msg->ms_flags &= ~(MSGF_REPLY | MSGF_SYNC | MSGF_DONE);
101 if ((error = lwkt_beginmsg(port, msg)) != EASYNC) {
102 lwkt_replymsg(msg, error);
109 * Request asynchronous completion and call lwkt_beginmsg(). The
110 * target port can opt to execute the message synchronously or
111 * asynchronously and this function will automatically queue the
112 * response if the target executes the message synchronously.
115 lwkt_domsg(lwkt_port_t port, lwkt_msg_t msg, int flags)
119 KKASSERT(msg->ms_reply_port != NULL &&
120 (msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) == MSGF_DONE);
121 msg->ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
122 msg->ms_flags |= MSGF_SYNC;
123 if ((error = lwkt_beginmsg(port, msg)) == EASYNC) {
124 error = lwkt_waitmsg(msg, flags);
126 msg->ms_flags |= MSGF_DONE | MSGF_REPLY;
134 * Forward a message received on one port to another port.
137 lwkt_forwardmsg(lwkt_port_t port, lwkt_msg_t msg)
142 KKASSERT((msg->ms_flags & (MSGF_QUEUED|MSGF_DONE|MSGF_REPLY)) == 0);
143 if ((error = port->mp_putport(port, msg)) != EASYNC)
144 lwkt_replymsg(msg, error);
152 * Attempt to abort a message. This only works if MSGF_ABORTABLE is set.
153 * The caller must ensure that the message will not be both replied AND
154 * destroyed while the abort is in progress.
156 * This function issues a callback which might block!
159 lwkt_abortmsg(lwkt_msg_t msg)
162 * A critical section protects us from reply IPIs on this cpu.
167 * Shortcut the operation if the message has already been returned.
168 * The callback typically constructs a lwkt_msg with the abort request,
169 * issues it synchronously, and waits for completion. The callback
170 * is not required to actually abort the message and the target port,
171 * upon receiving an abort request message generated by the callback
172 * should check whether the original message has already completed or
175 if (msg->ms_flags & MSGF_ABORTABLE) {
176 if ((msg->ms_flags & (MSGF_DONE|MSGF_REPLY)) == 0)
177 msg->ms_abortfn(msg);
182 /************************************************************************
183 * PORT INITIALIZATION API *
184 ************************************************************************/
186 static void *lwkt_thread_getport(lwkt_port_t port);
187 static int lwkt_thread_putport(lwkt_port_t port, lwkt_msg_t msg);
188 static int lwkt_thread_waitmsg(lwkt_msg_t msg, int flags);
189 static void *lwkt_thread_waitport(lwkt_port_t port, int flags);
190 static void lwkt_thread_replyport(lwkt_port_t port, lwkt_msg_t msg);
191 static void lwkt_thread_dropmsg(lwkt_port_t port, lwkt_msg_t msg);
193 static void *lwkt_spin_getport(lwkt_port_t port);
194 static int lwkt_spin_putport(lwkt_port_t port, lwkt_msg_t msg);
195 static int lwkt_spin_waitmsg(lwkt_msg_t msg, int flags);
196 static void *lwkt_spin_waitport(lwkt_port_t port, int flags);
197 static void lwkt_spin_replyport(lwkt_port_t port, lwkt_msg_t msg);
199 static void *lwkt_serialize_getport(lwkt_port_t port);
200 static int lwkt_serialize_putport(lwkt_port_t port, lwkt_msg_t msg);
201 static int lwkt_serialize_waitmsg(lwkt_msg_t msg, int flags);
202 static void *lwkt_serialize_waitport(lwkt_port_t port, int flags);
203 static void lwkt_serialize_replyport(lwkt_port_t port, lwkt_msg_t msg);
205 static void lwkt_null_replyport(lwkt_port_t port, lwkt_msg_t msg);
206 static void *lwkt_panic_getport(lwkt_port_t port);
207 static int lwkt_panic_putport(lwkt_port_t port, lwkt_msg_t msg);
208 static int lwkt_panic_waitmsg(lwkt_msg_t msg, int flags);
209 static void *lwkt_panic_waitport(lwkt_port_t port, int flags);
210 static void lwkt_panic_replyport(lwkt_port_t port, lwkt_msg_t msg);
211 static void lwkt_panic_dropmsg(lwkt_port_t port, lwkt_msg_t msg);
214 * Core port initialization (internal)
218 _lwkt_initport(lwkt_port_t port,
219 void *(*gportfn)(lwkt_port_t),
220 int (*pportfn)(lwkt_port_t, lwkt_msg_t),
221 int (*wmsgfn)(lwkt_msg_t, int),
222 void *(*wportfn)(lwkt_port_t, int),
223 void (*rportfn)(lwkt_port_t, lwkt_msg_t),
224 void (*dmsgfn)(lwkt_port_t, lwkt_msg_t))
226 bzero(port, sizeof(*port));
227 TAILQ_INIT(&port->mp_msgq);
228 TAILQ_INIT(&port->mp_msgq_prio);
229 port->mp_getport = gportfn;
230 port->mp_putport = pportfn;
231 port->mp_waitmsg = wmsgfn;
232 port->mp_waitport = wportfn;
233 port->mp_replyport = rportfn;
234 port->mp_dropmsg = dmsgfn;
238 * Schedule the target thread. If the message flags contains MSGF_NORESCHED
239 * we tell the scheduler not to reschedule if td is at a higher priority.
241 * This routine is called even if the thread is already scheduled.
245 _lwkt_schedule_msg(thread_t td, int flags)
251 * lwkt_initport_thread()
253 * Initialize a port for use by a particular thread. The port may
254 * only be used by <td>.
257 lwkt_initport_thread(lwkt_port_t port, thread_t td)
263 lwkt_thread_waitport,
264 lwkt_thread_replyport,
265 lwkt_thread_dropmsg);
270 * lwkt_initport_spin()
272 * Initialize a port for use with descriptors that might be accessed
273 * via multiple LWPs, processes, or threads. Has somewhat more
274 * overhead then thread ports.
277 lwkt_initport_spin(lwkt_port_t port)
286 spin_init(&port->mpu_spin);
290 * lwkt_initport_serialize()
292 * Initialize a port for use with descriptors that might be accessed
293 * via multiple LWPs, processes, or threads. Callers are assumed to
294 * have held the serializer (slz).
297 lwkt_initport_serialize(lwkt_port_t port, struct lwkt_serialize *slz)
300 lwkt_serialize_getport,
301 lwkt_serialize_putport,
302 lwkt_serialize_waitmsg,
303 lwkt_serialize_waitport,
304 lwkt_serialize_replyport,
306 port->mpu_serialize = slz;
310 * Similar to the standard initport, this function simply marks the message
311 * as being done and does not attempt to return it to an originating port.
314 lwkt_initport_replyonly_null(lwkt_port_t port)
326 * Initialize a reply-only port, typically used as a message sink. Such
327 * ports can only be used as a reply port.
330 lwkt_initport_replyonly(lwkt_port_t port,
331 void (*rportfn)(lwkt_port_t, lwkt_msg_t))
333 _lwkt_initport(port, lwkt_panic_getport, lwkt_panic_putport,
334 lwkt_panic_waitmsg, lwkt_panic_waitport,
335 rportfn, lwkt_panic_dropmsg);
339 lwkt_initport_putonly(lwkt_port_t port,
340 int (*pportfn)(lwkt_port_t, lwkt_msg_t))
342 _lwkt_initport(port, lwkt_panic_getport, pportfn,
343 lwkt_panic_waitmsg, lwkt_panic_waitport,
344 lwkt_panic_replyport, lwkt_panic_dropmsg);
348 lwkt_initport_panic(lwkt_port_t port)
351 lwkt_panic_getport, lwkt_panic_putport,
352 lwkt_panic_waitmsg, lwkt_panic_waitport,
353 lwkt_panic_replyport, lwkt_panic_dropmsg);
358 _lwkt_pullmsg(lwkt_port_t port, lwkt_msg_t msg)
360 lwkt_msg_queue *queue;
363 * normal case, remove and return the message.
365 if (__predict_false(msg->ms_flags & MSGF_PRIORITY))
366 queue = &port->mp_msgq_prio;
368 queue = &port->mp_msgq;
369 TAILQ_REMOVE(queue, msg, ms_node);
372 * atomic op needed for spin ports
374 atomic_clear_int(&msg->ms_flags, MSGF_QUEUED);
379 _lwkt_pushmsg(lwkt_port_t port, lwkt_msg_t msg)
381 lwkt_msg_queue *queue;
384 * atomic op needed for spin ports
386 atomic_set_int(&msg->ms_flags, MSGF_QUEUED);
387 if (__predict_false(msg->ms_flags & MSGF_PRIORITY))
388 queue = &port->mp_msgq_prio;
390 queue = &port->mp_msgq;
391 TAILQ_INSERT_TAIL(queue, msg, ms_node);
396 _lwkt_pollmsg(lwkt_port_t port)
400 msg = TAILQ_FIRST(&port->mp_msgq_prio);
401 if (__predict_false(msg != NULL))
405 * Priority queue has no message, fallback to non-priority queue.
407 return TAILQ_FIRST(&port->mp_msgq);
412 _lwkt_enqueue_reply(lwkt_port_t port, lwkt_msg_t msg)
415 * atomic op needed for spin ports
417 _lwkt_pushmsg(port, msg);
418 atomic_set_int(&msg->ms_flags, MSGF_REPLY | MSGF_DONE);
421 /************************************************************************
422 * THREAD PORT BACKEND *
423 ************************************************************************
425 * This backend is used when the port a message is retrieved from is owned
426 * by a single thread (the calling thread). Messages are IPId to the
427 * correct cpu before being enqueued to a port. Note that this is fairly
428 * optimal since scheduling would have had to do an IPI anyway if the
429 * message were headed to a different cpu.
435 * This function completes reply processing for the default case in the
436 * context of the originating cpu.
440 lwkt_thread_replyport_remote(lwkt_msg_t msg)
442 lwkt_port_t port = msg->ms_reply_port;
446 * Chase any thread migration that occurs
448 if (port->mpu_td->td_gd != mycpu) {
449 lwkt_send_ipiq(port->mpu_td->td_gd,
450 (ipifunc1_t)lwkt_thread_replyport_remote, msg);
458 KKASSERT(msg->ms_flags & MSGF_INTRANSIT);
459 msg->ms_flags &= ~MSGF_INTRANSIT;
461 flags = msg->ms_flags;
462 if (msg->ms_flags & MSGF_SYNC) {
464 msg->ms_flags |= MSGF_REPLY | MSGF_DONE;
466 _lwkt_enqueue_reply(port, msg);
468 if (port->mp_flags & MSGPORTF_WAITING)
469 _lwkt_schedule_msg(port->mpu_td, flags);
475 * lwkt_thread_replyport() - Backend to lwkt_replymsg()
477 * Called with the reply port as an argument but in the context of the
478 * original target port. Completion must occur on the target port's
481 * The critical section protects us from IPIs on the this CPU.
485 lwkt_thread_replyport(lwkt_port_t port, lwkt_msg_t msg)
489 KKASSERT((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED|MSGF_INTRANSIT)) == 0);
491 if (msg->ms_flags & MSGF_SYNC) {
493 * If a synchronous completion has been requested, just wakeup
494 * the message without bothering to queue it to the target port.
496 * Assume the target thread is non-preemptive, so no critical
497 * section is required.
500 if (port->mpu_td->td_gd == mycpu) {
502 flags = msg->ms_flags;
504 msg->ms_flags |= MSGF_DONE | MSGF_REPLY;
505 if (port->mp_flags & MSGPORTF_WAITING)
506 _lwkt_schedule_msg(port->mpu_td, flags);
510 msg->ms_flags |= MSGF_INTRANSIT;
512 msg->ms_flags |= MSGF_REPLY;
513 lwkt_send_ipiq(port->mpu_td->td_gd,
514 (ipifunc1_t)lwkt_thread_replyport_remote, msg);
519 * If an asynchronous completion has been requested the message
520 * must be queued to the reply port.
522 * A critical section is required to interlock the port queue.
525 if (port->mpu_td->td_gd == mycpu) {
528 _lwkt_enqueue_reply(port, msg);
529 if (port->mp_flags & MSGPORTF_WAITING)
530 _lwkt_schedule_msg(port->mpu_td, msg->ms_flags);
535 msg->ms_flags |= MSGF_INTRANSIT;
537 msg->ms_flags |= MSGF_REPLY;
538 lwkt_send_ipiq(port->mpu_td->td_gd,
539 (ipifunc1_t)lwkt_thread_replyport_remote, msg);
546 * lwkt_thread_dropmsg() - Backend to lwkt_dropmsg()
548 * This function could _only_ be used when caller is in the same thread
549 * as the message's target port owner thread.
552 lwkt_thread_dropmsg(lwkt_port_t port, lwkt_msg_t msg)
554 KASSERT(port->mpu_td == curthread,
555 ("message could only be dropped in the same thread "
556 "as the message target port thread\n"));
557 crit_enter_quick(port->mpu_td);
558 _lwkt_pullmsg(port, msg);
559 msg->ms_flags |= MSGF_DONE;
560 crit_exit_quick(port->mpu_td);
564 * lwkt_thread_putport() - Backend to lwkt_beginmsg()
566 * Called with the target port as an argument but in the context of the
567 * reply port. This function always implements an asynchronous put to
568 * the target message port, and thus returns EASYNC.
570 * The message must already have cleared MSGF_DONE and MSGF_REPLY
577 lwkt_thread_putport_remote(lwkt_msg_t msg)
579 lwkt_port_t port = msg->ms_target_port;
582 * Chase any thread migration that occurs
584 if (port->mpu_td->td_gd != mycpu) {
585 lwkt_send_ipiq(port->mpu_td->td_gd,
586 (ipifunc1_t)lwkt_thread_putport_remote, msg);
594 KKASSERT(msg->ms_flags & MSGF_INTRANSIT);
595 msg->ms_flags &= ~MSGF_INTRANSIT;
597 _lwkt_pushmsg(port, msg);
598 if (port->mp_flags & MSGPORTF_WAITING)
599 _lwkt_schedule_msg(port->mpu_td, msg->ms_flags);
606 lwkt_thread_putport(lwkt_port_t port, lwkt_msg_t msg)
608 KKASSERT((msg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0);
610 msg->ms_target_port = port;
612 if (port->mpu_td->td_gd == mycpu) {
615 _lwkt_pushmsg(port, msg);
616 if (port->mp_flags & MSGPORTF_WAITING)
617 _lwkt_schedule_msg(port->mpu_td, msg->ms_flags);
622 msg->ms_flags |= MSGF_INTRANSIT;
624 lwkt_send_ipiq(port->mpu_td->td_gd,
625 (ipifunc1_t)lwkt_thread_putport_remote, msg);
632 * lwkt_thread_getport()
634 * Retrieve the next message from the port or NULL if no messages
639 lwkt_thread_getport(lwkt_port_t port)
643 KKASSERT(port->mpu_td == curthread);
645 crit_enter_quick(port->mpu_td);
646 if ((msg = _lwkt_pollmsg(port)) != NULL)
647 _lwkt_pullmsg(port, msg);
648 crit_exit_quick(port->mpu_td);
653 * lwkt_thread_waitmsg()
655 * Wait for a particular message to be replied. We must be the only
656 * thread waiting on the message. The port must be owned by the
661 lwkt_thread_waitmsg(lwkt_msg_t msg, int flags)
663 KASSERT((msg->ms_flags & MSGF_DROPABLE) == 0,
664 ("can't wait dropable message\n"));
666 if ((msg->ms_flags & MSGF_DONE) == 0) {
668 * If the done bit was not set we have to block until it is.
670 lwkt_port_t port = msg->ms_reply_port;
671 thread_t td = curthread;
674 KKASSERT(port->mpu_td == td);
675 crit_enter_quick(td);
678 while ((msg->ms_flags & MSGF_DONE) == 0) {
679 port->mp_flags |= MSGPORTF_WAITING;
680 if (sentabort == 0) {
681 if ((sentabort = lwkt_sleep("waitmsg", flags)) != 0) {
685 lwkt_sleep("waitabt", 0);
687 port->mp_flags &= ~MSGPORTF_WAITING;
689 if (msg->ms_flags & MSGF_QUEUED)
690 _lwkt_pullmsg(port, msg);
694 * If the done bit was set we only have to mess around with the
695 * message if it is queued on the reply port.
697 if (msg->ms_flags & MSGF_QUEUED) {
698 lwkt_port_t port = msg->ms_reply_port;
699 thread_t td = curthread;
701 KKASSERT(port->mpu_td == td);
702 crit_enter_quick(td);
703 _lwkt_pullmsg(port, msg);
707 return(msg->ms_error);
712 lwkt_thread_waitport(lwkt_port_t port, int flags)
714 thread_t td = curthread;
718 KKASSERT(port->mpu_td == td);
719 crit_enter_quick(td);
720 while ((msg = _lwkt_pollmsg(port)) == NULL) {
721 port->mp_flags |= MSGPORTF_WAITING;
722 error = lwkt_sleep("waitport", flags);
723 port->mp_flags &= ~MSGPORTF_WAITING;
727 _lwkt_pullmsg(port, msg);
733 /************************************************************************
734 * SPIN PORT BACKEND *
735 ************************************************************************
737 * This backend uses spinlocks instead of making assumptions about which
738 * thread is accessing the port. It must be used when a port is not owned
739 * by a particular thread. This is less optimal then thread ports but
740 * you don't have a choice if there are multiple threads accessing the port.
742 * Note on MSGPORTF_WAITING - because there may be multiple threads blocked
743 * on the message port, it is the responsibility of the code doing the
744 * wakeup to clear this flag rather then the blocked threads. Some
745 * superfluous wakeups may occur, which is ok.
747 * XXX synchronous message wakeups are not current optimized.
752 lwkt_spin_getport(lwkt_port_t port)
756 spin_lock(&port->mpu_spin);
757 if ((msg = _lwkt_pollmsg(port)) != NULL)
758 _lwkt_pullmsg(port, msg);
759 spin_unlock(&port->mpu_spin);
765 lwkt_spin_putport(lwkt_port_t port, lwkt_msg_t msg)
769 KKASSERT((msg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0);
771 msg->ms_target_port = port;
772 spin_lock(&port->mpu_spin);
773 _lwkt_pushmsg(port, msg);
775 if (port->mp_flags & MSGPORTF_WAITING) {
776 port->mp_flags &= ~MSGPORTF_WAITING;
779 spin_unlock(&port->mpu_spin);
787 lwkt_spin_waitmsg(lwkt_msg_t msg, int flags)
793 KASSERT((msg->ms_flags & MSGF_DROPABLE) == 0,
794 ("can't wait dropable message\n"));
796 if ((msg->ms_flags & MSGF_DONE) == 0) {
797 port = msg->ms_reply_port;
799 spin_lock(&port->mpu_spin);
800 while ((msg->ms_flags & MSGF_DONE) == 0) {
804 * If message was sent synchronously from the beginning
805 * the wakeup will be on the message structure, else it
806 * will be on the port structure.
808 if (msg->ms_flags & MSGF_SYNC) {
810 atomic_set_int(&msg->ms_flags, MSGF_WAITING);
813 port->mp_flags |= MSGPORTF_WAITING;
817 * Only messages which support abort can be interrupted.
818 * We must still wait for message completion regardless.
820 if ((flags & PCATCH) && sentabort == 0) {
821 error = ssleep(won, &port->mpu_spin, PCATCH, "waitmsg", 0);
824 spin_unlock(&port->mpu_spin);
826 spin_lock(&port->mpu_spin);
829 error = ssleep(won, &port->mpu_spin, 0, "waitmsg", 0);
831 /* see note at the top on the MSGPORTF_WAITING flag */
834 * Turn EINTR into ERESTART if the signal indicates.
836 if (sentabort && msg->ms_error == EINTR)
837 msg->ms_error = sentabort;
838 if (msg->ms_flags & MSGF_QUEUED)
839 _lwkt_pullmsg(port, msg);
840 spin_unlock(&port->mpu_spin);
842 if (msg->ms_flags & MSGF_QUEUED) {
843 port = msg->ms_reply_port;
844 spin_lock(&port->mpu_spin);
845 _lwkt_pullmsg(port, msg);
846 spin_unlock(&port->mpu_spin);
849 return(msg->ms_error);
854 lwkt_spin_waitport(lwkt_port_t port, int flags)
859 spin_lock(&port->mpu_spin);
860 while ((msg = _lwkt_pollmsg(port)) == NULL) {
861 port->mp_flags |= MSGPORTF_WAITING;
862 error = ssleep(port, &port->mpu_spin, flags, "waitport", 0);
863 /* see note at the top on the MSGPORTF_WAITING flag */
865 spin_unlock(&port->mpu_spin);
869 _lwkt_pullmsg(port, msg);
870 spin_unlock(&port->mpu_spin);
876 lwkt_spin_replyport(lwkt_port_t port, lwkt_msg_t msg)
880 KKASSERT((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) == 0);
882 if (msg->ms_flags & MSGF_SYNC) {
884 * If a synchronous completion has been requested, just wakeup
885 * the message without bothering to queue it to the target port.
887 spin_lock(&port->mpu_spin);
888 msg->ms_flags |= MSGF_DONE | MSGF_REPLY;
890 if (msg->ms_flags & MSGF_WAITING) {
891 atomic_clear_int(&msg->ms_flags, MSGF_WAITING);
894 spin_unlock(&port->mpu_spin);
899 * If an asynchronous completion has been requested the message
900 * must be queued to the reply port.
902 spin_lock(&port->mpu_spin);
903 _lwkt_enqueue_reply(port, msg);
905 if (port->mp_flags & MSGPORTF_WAITING) {
906 port->mp_flags &= ~MSGPORTF_WAITING;
909 spin_unlock(&port->mpu_spin);
915 /************************************************************************
916 * SERIALIZER PORT BACKEND *
917 ************************************************************************
919 * This backend uses serializer to protect port accessing. Callers are
920 * assumed to have serializer held. This kind of port is usually created
921 * by network device driver along with _one_ lwkt thread to pipeline
922 * operations which may temporarily release serializer.
924 * Implementation is based on SPIN PORT BACKEND.
929 lwkt_serialize_getport(lwkt_port_t port)
933 ASSERT_SERIALIZED(port->mpu_serialize);
935 if ((msg = _lwkt_pollmsg(port)) != NULL)
936 _lwkt_pullmsg(port, msg);
942 lwkt_serialize_putport(lwkt_port_t port, lwkt_msg_t msg)
944 KKASSERT((msg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0);
945 ASSERT_SERIALIZED(port->mpu_serialize);
947 msg->ms_target_port = port;
948 _lwkt_pushmsg(port, msg);
949 if (port->mp_flags & MSGPORTF_WAITING) {
950 port->mp_flags &= ~MSGPORTF_WAITING;
958 lwkt_serialize_waitmsg(lwkt_msg_t msg, int flags)
964 KASSERT((msg->ms_flags & MSGF_DROPABLE) == 0,
965 ("can't wait dropable message\n"));
967 if ((msg->ms_flags & MSGF_DONE) == 0) {
968 port = msg->ms_reply_port;
970 ASSERT_SERIALIZED(port->mpu_serialize);
973 while ((msg->ms_flags & MSGF_DONE) == 0) {
977 * If message was sent synchronously from the beginning
978 * the wakeup will be on the message structure, else it
979 * will be on the port structure.
981 if (msg->ms_flags & MSGF_SYNC) {
985 port->mp_flags |= MSGPORTF_WAITING;
989 * Only messages which support abort can be interrupted.
990 * We must still wait for message completion regardless.
992 if ((flags & PCATCH) && sentabort == 0) {
993 error = zsleep(won, port->mpu_serialize, PCATCH, "waitmsg", 0);
996 lwkt_serialize_exit(port->mpu_serialize);
998 lwkt_serialize_enter(port->mpu_serialize);
1001 error = zsleep(won, port->mpu_serialize, 0, "waitmsg", 0);
1003 /* see note at the top on the MSGPORTF_WAITING flag */
1006 * Turn EINTR into ERESTART if the signal indicates.
1008 if (sentabort && msg->ms_error == EINTR)
1009 msg->ms_error = sentabort;
1010 if (msg->ms_flags & MSGF_QUEUED)
1011 _lwkt_pullmsg(port, msg);
1013 if (msg->ms_flags & MSGF_QUEUED) {
1014 port = msg->ms_reply_port;
1016 ASSERT_SERIALIZED(port->mpu_serialize);
1017 _lwkt_pullmsg(port, msg);
1020 return(msg->ms_error);
1025 lwkt_serialize_waitport(lwkt_port_t port, int flags)
1030 ASSERT_SERIALIZED(port->mpu_serialize);
1032 while ((msg = _lwkt_pollmsg(port)) == NULL) {
1033 port->mp_flags |= MSGPORTF_WAITING;
1034 error = zsleep(port, port->mpu_serialize, flags, "waitport", 0);
1035 /* see note at the top on the MSGPORTF_WAITING flag */
1039 _lwkt_pullmsg(port, msg);
1045 lwkt_serialize_replyport(lwkt_port_t port, lwkt_msg_t msg)
1047 KKASSERT((msg->ms_flags & (MSGF_DONE|MSGF_QUEUED)) == 0);
1048 ASSERT_SERIALIZED(port->mpu_serialize);
1050 if (msg->ms_flags & MSGF_SYNC) {
1052 * If a synchronous completion has been requested, just wakeup
1053 * the message without bothering to queue it to the target port.
1055 msg->ms_flags |= MSGF_DONE | MSGF_REPLY;
1059 * If an asynchronous completion has been requested the message
1060 * must be queued to the reply port.
1062 _lwkt_enqueue_reply(port, msg);
1063 if (port->mp_flags & MSGPORTF_WAITING) {
1064 port->mp_flags &= ~MSGPORTF_WAITING;
1070 /************************************************************************
1071 * PANIC AND SPECIAL PORT FUNCTIONS *
1072 ************************************************************************/
1075 * You can point a port's reply vector at this function if you just want
1076 * the message marked done, without any queueing or signaling. This is
1077 * often used for structure-embedded messages.
1081 lwkt_null_replyport(lwkt_port_t port, lwkt_msg_t msg)
1083 msg->ms_flags |= MSGF_DONE | MSGF_REPLY;
1088 lwkt_panic_getport(lwkt_port_t port)
1090 panic("lwkt_getport() illegal on port %p", port);
1095 lwkt_panic_putport(lwkt_port_t port, lwkt_msg_t msg)
1097 panic("lwkt_begin/do/sendmsg() illegal on port %p msg %p", port, msg);
1102 lwkt_panic_waitmsg(lwkt_msg_t msg, int flags)
1104 panic("port %p msg %p cannot be waited on", msg->ms_reply_port, msg);
1109 lwkt_panic_waitport(lwkt_port_t port, int flags)
1111 panic("port %p cannot be waited on", port);
1116 lwkt_panic_replyport(lwkt_port_t port, lwkt_msg_t msg)
1118 panic("lwkt_replymsg() is illegal on port %p msg %p", port, msg);
1123 lwkt_panic_dropmsg(lwkt_port_t port, lwkt_msg_t msg)
1125 panic("lwkt_dropmsg() is illegal on port %p msg %p", port, msg);