1 /* $NetBSD: iocp-internal.h,v 1.1.1.1 2013/04/11 16:43:29 christos Exp $ */
3 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef _EVENT_IOCP_INTERNAL_H
29 #define _EVENT_IOCP_INTERNAL_H
35 struct event_overlapped
;
36 struct event_iocp_port
;
38 typedef void (*iocp_callback
)(struct event_overlapped
*, ev_uintptr_t
, ev_ssize_t
, int success
);
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. */
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.
52 struct event_overlapped
{
53 OVERLAPPED overlapped
;
57 /* Mingw's headers don't define LPFN_ACCEPTEX. */
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
);
63 /** Internal use only. Holds pointers to functions that only some versions of
66 struct win32_extension_fns
{
68 ConnectExPtr ConnectEx
;
69 GetAcceptExSockaddrsPtr GetAcceptExSockaddrs
;
73 Internal use only. Stores a Windows IO Completion port, along with
76 struct event_iocp_port
{
77 /** The port itself */
79 /* A lock to cover internal structures. */
80 CRITICAL_SECTION lock
;
81 /** Number of threads ever open on the port. */
83 /** True iff we're shutting down all the threads on this port */
85 /** How often the threads on this port check for shutdown and other
88 /* The threads that are waiting for events. */
90 /** Number of threads currently open on this port. */
92 /** A semaphore to signal when we are done shutting down. */
93 HANDLE
*shutdownSemaphore
;
96 const struct win32_extension_fns
*event_get_win32_extension_fns(void);
98 /* Dummy definition so we can test-compile more things on unix. */
99 struct event_overlapped
{
104 /** Initialize the fields in an event_overlapped.
106 @param overlapped The struct event_overlapped to initialize
107 @param cb The callback that should be invoked once the IO operation has
110 void event_overlapped_init(struct event_overlapped
*, iocp_callback cb
);
112 /** Allocate and return a new evbuffer that supports overlapped IO on a given
113 socket. The socket must be associated with an IO completion port using
114 event_iocp_port_associate.
116 struct evbuffer
*evbuffer_overlapped_new(evutil_socket_t fd
);
118 /** XXXX Document (nickm) */
119 evutil_socket_t
_evbuffer_overlapped_get_fd(struct evbuffer
*buf
);
121 void _evbuffer_overlapped_set_fd(struct evbuffer
*buf
, evutil_socket_t fd
);
123 /** Start reading data onto the end of an overlapped evbuffer.
125 An evbuffer can only have one read pending at a time. While the read
126 is in progress, no other data may be added to the end of the buffer.
127 The buffer must be created with event_overlapped_init().
128 evbuffer_commit_read() must be called in the completion callback.
130 @param buf The buffer to read onto
131 @param n The number of bytes to try to read.
132 @param ol Overlapped object with associated completion callback.
133 @return 0 on success, -1 on error.
135 int evbuffer_launch_read(struct evbuffer
*buf
, size_t n
, struct event_overlapped
*ol
);
137 /** Start writing data from the start of an evbuffer.
139 An evbuffer can only have one write pending at a time. While the write is
140 in progress, no other data may be removed from the front of the buffer.
141 The buffer must be created with event_overlapped_init().
142 evbuffer_commit_write() must be called in the completion callback.
144 @param buf The buffer to read onto
145 @param n The number of bytes to try to read.
146 @param ol Overlapped object with associated completion callback.
147 @return 0 on success, -1 on error.
149 int evbuffer_launch_write(struct evbuffer
*buf
, ev_ssize_t n
, struct event_overlapped
*ol
);
152 void evbuffer_commit_read(struct evbuffer
*, ev_ssize_t
);
153 void evbuffer_commit_write(struct evbuffer
*, ev_ssize_t
);
155 /** Create an IOCP, and launch its worker threads. Internal use only.
157 This interface is unstable, and will change.
159 struct event_iocp_port
*event_iocp_port_launch(int n_cpus
);
161 /** Associate a file descriptor with an iocp, such that overlapped IO on the
162 fd will happen on one of the iocp's worker threads.
164 int event_iocp_port_associate(struct event_iocp_port
*port
, evutil_socket_t fd
,
167 /** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all
168 the threads to finish whatever they're doing. If waitMsec is -1, wait
169 as long as required. If all the threads are done, free the port and return
170 0. Otherwise, return -1. If you get a -1 return value, it is safe to call
173 int event_iocp_shutdown(struct event_iocp_port
*port
, long waitMsec
);
175 /* FIXME document. */
176 int event_iocp_activate_overlapped(struct event_iocp_port
*port
,
177 struct event_overlapped
*o
,
178 ev_uintptr_t key
, ev_uint32_t n_bytes
);
181 /* FIXME document. */
182 struct event_iocp_port
*event_base_get_iocp(struct event_base
*base
);
184 /* FIXME document. */
185 int event_base_start_iocp(struct event_base
*base
, int n_cpus
);
186 void event_base_stop_iocp(struct event_base
*base
);
188 /* FIXME document. */
189 struct bufferevent
*bufferevent_async_new(struct event_base
*base
,
190 evutil_socket_t fd
, int options
);
192 /* FIXME document. */
193 void bufferevent_async_set_connected(struct bufferevent
*bev
);
194 int bufferevent_async_can_connect(struct bufferevent
*bev
);
195 int bufferevent_async_connect(struct bufferevent
*bev
, evutil_socket_t fd
,
196 const struct sockaddr
*sa
, int socklen
);