xref: /dragonfly/sys/sys/msgport2.h (revision 7c4f4eee)
1 /*
2  * SYS/MSGPORT2.H
3  *
4  *	Implements Inlines for LWKT messages and ports.
5  */
6 
7 #ifndef _SYS_MSGPORT2_H_
8 #define _SYS_MSGPORT2_H_
9 
10 #ifndef _KERNEL
11 #error "This file should not be included by userland programs."
12 #endif
13 
14 #include <sys/systm.h>
15 
16 /*
17  * Initialize a LWKT message structure.  Note that if the message supports
18  * an abort MSGF_ABORTABLE must be passed in flags.
19  *
20  * Note that other areas of the LWKT msg may already be initialized, so we
21  * do not zero the message here.
22  *
23  * Messages are marked as DONE until sent.
24  */
25 static __inline
26 void
27 lwkt_initmsg(lwkt_msg_t msg, lwkt_port_t rport, int flags)
28 {
29     msg->ms_flags = MSGF_DONE | flags;
30     msg->ms_reply_port = rport;
31 }
32 
33 static __inline
34 void
35 lwkt_initmsg_abortable(lwkt_msg_t msg, lwkt_port_t rport, int flags,
36 		       void (*abortfn)(lwkt_msg_t))
37 {
38     lwkt_initmsg(msg, rport, flags | MSGF_ABORTABLE);
39     msg->ms_abortfn = abortfn;
40 }
41 
42 static __inline
43 void
44 lwkt_replymsg(lwkt_msg_t msg, int error)
45 {
46     lwkt_port_t port;
47 
48     msg->ms_error = error;
49     port = msg->ms_reply_port;
50     port->mp_replyport(port, msg);
51 }
52 
53 /*
54  * Retrieve the next message from the port's message queue, return NULL
55  * if no messages are pending.  The retrieved message will either be a
56  * request or a reply based on the MSGF_REPLY bit.
57  *
58  * If the backend port is a thread port, the the calling thread MUST
59  * own the port.
60  */
61 static __inline
62 void *
63 lwkt_getport(lwkt_port_t port)
64 {
65     return(port->mp_getport(port));
66 }
67 
68 static __inline
69 void *
70 lwkt_waitport(lwkt_port_t port, int flags)
71 {
72     return(port->mp_waitport(port, flags));
73 }
74 
75 static __inline
76 int
77 lwkt_waitmsg(lwkt_msg_t msg, int flags)
78 {
79     return(msg->ms_reply_port->mp_waitmsg(msg, flags));
80 }
81 
82 
83 static __inline
84 int
85 lwkt_checkmsg(lwkt_msg_t msg)
86 {
87     return(msg->ms_flags & MSGF_DONE);
88 }
89 
90 static __inline
91 int
92 lwkt_dropmsg(lwkt_msg_t msg)
93 {
94     lwkt_port_t port;
95     int error = ENOENT;
96 
97     KKASSERT(msg->ms_flags & MSGF_DROPABLE);
98     port = msg->ms_target_port;
99     if (port)
100 	    error = port->mp_dropmsg(port, msg);
101     return (error);
102 }
103 
104 static __inline
105 void
106 lwkt_setmsg_receipt(lwkt_msg_t msg, void (*receiptfn)(lwkt_msg_t, lwkt_port_t))
107 {
108 	msg->ms_flags |= MSGF_RECEIPT;
109 	msg->ms_receiptfn = receiptfn;
110 }
111 
112 #endif	/* _SYS_MSGPORT2_H_ */
113