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