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