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