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