xref: /netbsd-src/external/bsd/libevent/dist/iocp-internal.h (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1 /*	$NetBSD: iocp-internal.h,v 1.1.1.3 2021/04/07 02:43:12 christos Exp $	*/
2 /*
3  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
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  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef IOCP_INTERNAL_H_INCLUDED_
29 #define IOCP_INTERNAL_H_INCLUDED_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct event_overlapped;
36 struct event_iocp_port;
37 struct evbuffer;
38 typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success);
39 
40 /* This whole file is actually win32 only. We wrap the structures in a win32
41  * ifdef so that we can test-compile code that uses these interfaces on
42  * non-win32 platforms. */
43 #ifdef _WIN32
44 
45 /**
46    Internal use only.  Wraps an OVERLAPPED that we're using for libevent
47    functionality.  Whenever an event_iocp_port gets an event for a given
48    OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the
49    iocp_callback function with the event_overlapped, the iocp key, and the
50    number of bytes transferred as arguments.
51  */
52 struct event_overlapped {
53 	OVERLAPPED overlapped;
54 	iocp_callback cb;
55 };
56 
57 /* Mingw's headers don't define LPFN_ACCEPTEX. */
58 
59 typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
60 typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED);
61 typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT);
62 
63 /** Internal use only. Holds pointers to functions that only some versions of
64     Windows provide.
65  */
66 struct win32_extension_fns {
67 	AcceptExPtr AcceptEx;
68 	ConnectExPtr ConnectEx;
69 	GetAcceptExSockaddrsPtr GetAcceptExSockaddrs;
70 };
71 
72 /**
73     Internal use only. Stores a Windows IO Completion port, along with
74     related data.
75  */
76 struct event_iocp_port {
77 	/** The port itself */
78 	HANDLE port;
79 	/* A lock to cover internal structures. */
80 	CRITICAL_SECTION lock;
81 	/** Number of threads ever open on the port. */
82 	short n_threads;
83 	/** True iff we're shutting down all the threads on this port */
84 	short shutdown;
85 	/** How often the threads on this port check for shutdown and other
86 	 * conditions */
87 	long ms;
88 	/* The threads that are waiting for events. */
89 	HANDLE *threads;
90 	/** Number of threads currently open on this port. */
91 	short n_live_threads;
92 	/** A semaphore to signal when we are done shutting down. */
93 	HANDLE *shutdownSemaphore;
94 };
95 
96 EVENT2_EXPORT_SYMBOL
97 const struct win32_extension_fns *event_get_win32_extension_fns_(void);
98 #else
99 /* Dummy definition so we can test-compile more things on unix. */
100 struct event_overlapped {
101 	iocp_callback cb;
102 };
103 #endif
104 
105 /** Initialize the fields in an event_overlapped.
106 
107     @param overlapped The struct event_overlapped to initialize
108     @param cb The callback that should be invoked once the IO operation has
109 	finished.
110  */
111 EVENT2_EXPORT_SYMBOL
112 void event_overlapped_init_(struct event_overlapped *, iocp_callback cb);
113 
114 /** Allocate and return a new evbuffer that supports overlapped IO on a given
115     socket.  The socket must be associated with an IO completion port using
116     event_iocp_port_associate_.
117 */
118 EVENT2_EXPORT_SYMBOL
119 struct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd);
120 
121 /** XXXX Document (nickm) */
122 evutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf);
123 
124 void evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd);
125 
126 /** Start reading data onto the end of an overlapped evbuffer.
127 
128     An evbuffer can only have one read pending at a time.  While the read
129     is in progress, no other data may be added to the end of the buffer.
130     The buffer must be created with event_overlapped_init_().
131     evbuffer_commit_read_() must be called in the completion callback.
132 
133     @param buf The buffer to read onto
134     @param n The number of bytes to try to read.
135     @param ol Overlapped object with associated completion callback.
136     @return 0 on success, -1 on error.
137  */
138 EVENT2_EXPORT_SYMBOL
139 int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
140 
141 /** Start writing data from the start of an evbuffer.
142 
143     An evbuffer can only have one write pending at a time.  While the write is
144     in progress, no other data may be removed from the front of the buffer.
145     The buffer must be created with event_overlapped_init_().
146     evbuffer_commit_write_() must be called in the completion callback.
147 
148     @param buf The buffer to read onto
149     @param n The number of bytes to try to read.
150     @param ol Overlapped object with associated completion callback.
151     @return 0 on success, -1 on error.
152  */
153 EVENT2_EXPORT_SYMBOL
154 int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
155 
156 /** XXX document */
157 EVENT2_EXPORT_SYMBOL
158 void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t);
159 EVENT2_EXPORT_SYMBOL
160 void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t);
161 
162 /** Create an IOCP, and launch its worker threads.  Internal use only.
163 
164     This interface is unstable, and will change.
165  */
166 EVENT2_EXPORT_SYMBOL
167 struct event_iocp_port *event_iocp_port_launch_(int n_cpus);
168 
169 /** Associate a file descriptor with an iocp, such that overlapped IO on the
170     fd will happen on one of the iocp's worker threads.
171 */
172 EVENT2_EXPORT_SYMBOL
173 int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
174     ev_uintptr_t key);
175 
176 /** Tell all threads serving an iocp to stop.  Wait for up to waitMsec for all
177     the threads to finish whatever they're doing.  If waitMsec is -1, wait
178     as long as required.  If all the threads are done, free the port and return
179     0. Otherwise, return -1.  If you get a -1 return value, it is safe to call
180     this function again.
181 */
182 EVENT2_EXPORT_SYMBOL
183 int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec);
184 
185 /* FIXME document. */
186 EVENT2_EXPORT_SYMBOL
187 int event_iocp_activate_overlapped_(struct event_iocp_port *port,
188     struct event_overlapped *o,
189     ev_uintptr_t key, ev_uint32_t n_bytes);
190 
191 struct event_base;
192 /* FIXME document. */
193 EVENT2_EXPORT_SYMBOL
194 struct event_iocp_port *event_base_get_iocp_(struct event_base *base);
195 
196 /* FIXME document. */
197 EVENT2_EXPORT_SYMBOL
198 int event_base_start_iocp_(struct event_base *base, int n_cpus);
199 void event_base_stop_iocp_(struct event_base *base);
200 
201 /* FIXME document. */
202 EVENT2_EXPORT_SYMBOL
203 struct bufferevent *bufferevent_async_new_(struct event_base *base,
204     evutil_socket_t fd, int options);
205 
206 /* FIXME document. */
207 void bufferevent_async_set_connected_(struct bufferevent *bev);
208 int bufferevent_async_can_connect_(struct bufferevent *bev);
209 int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
210 	const struct sockaddr *sa, int socklen);
211 
212 #ifdef __cplusplus
213 }
214 #endif
215 
216 #endif
217