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