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