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