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, 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 _WIN32_WINNT |
michael@0 | 28 | /* Minimum required for InitializeCriticalSectionAndSpinCount */ |
michael@0 | 29 | #define _WIN32_WINNT 0x0403 |
michael@0 | 30 | #endif |
michael@0 | 31 | #include <winsock2.h> |
michael@0 | 32 | #include <windows.h> |
michael@0 | 33 | #include <process.h> |
michael@0 | 34 | #include <stdio.h> |
michael@0 | 35 | #include <mswsock.h> |
michael@0 | 36 | |
michael@0 | 37 | #include "event2/util.h" |
michael@0 | 38 | #include "util-internal.h" |
michael@0 | 39 | #include "iocp-internal.h" |
michael@0 | 40 | #include "log-internal.h" |
michael@0 | 41 | #include "mm-internal.h" |
michael@0 | 42 | #include "event-internal.h" |
michael@0 | 43 | #include "evthread-internal.h" |
michael@0 | 44 | |
michael@0 | 45 | #define NOTIFICATION_KEY ((ULONG_PTR)-1) |
michael@0 | 46 | |
michael@0 | 47 | void |
michael@0 | 48 | event_overlapped_init(struct event_overlapped *o, iocp_callback cb) |
michael@0 | 49 | { |
michael@0 | 50 | memset(o, 0, sizeof(struct event_overlapped)); |
michael@0 | 51 | o->cb = cb; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | static void |
michael@0 | 55 | handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok) |
michael@0 | 56 | { |
michael@0 | 57 | struct event_overlapped *eo = |
michael@0 | 58 | EVUTIL_UPCAST(o, struct event_overlapped, overlapped); |
michael@0 | 59 | eo->cb(eo, completion_key, nBytes, ok); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static void |
michael@0 | 63 | loop(void *_port) |
michael@0 | 64 | { |
michael@0 | 65 | struct event_iocp_port *port = _port; |
michael@0 | 66 | long ms = port->ms; |
michael@0 | 67 | HANDLE p = port->port; |
michael@0 | 68 | |
michael@0 | 69 | if (ms <= 0) |
michael@0 | 70 | ms = INFINITE; |
michael@0 | 71 | |
michael@0 | 72 | while (1) { |
michael@0 | 73 | OVERLAPPED *overlapped=NULL; |
michael@0 | 74 | ULONG_PTR key=0; |
michael@0 | 75 | DWORD bytes=0; |
michael@0 | 76 | int ok = GetQueuedCompletionStatus(p, &bytes, &key, |
michael@0 | 77 | &overlapped, ms); |
michael@0 | 78 | EnterCriticalSection(&port->lock); |
michael@0 | 79 | if (port->shutdown) { |
michael@0 | 80 | if (--port->n_live_threads == 0) |
michael@0 | 81 | ReleaseSemaphore(port->shutdownSemaphore, 1, |
michael@0 | 82 | NULL); |
michael@0 | 83 | LeaveCriticalSection(&port->lock); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | LeaveCriticalSection(&port->lock); |
michael@0 | 87 | |
michael@0 | 88 | if (key != NOTIFICATION_KEY && overlapped) |
michael@0 | 89 | handle_entry(overlapped, key, bytes, ok); |
michael@0 | 90 | else if (!overlapped) |
michael@0 | 91 | break; |
michael@0 | 92 | } |
michael@0 | 93 | event_warnx("GetQueuedCompletionStatus exited with no event."); |
michael@0 | 94 | EnterCriticalSection(&port->lock); |
michael@0 | 95 | if (--port->n_live_threads == 0) |
michael@0 | 96 | ReleaseSemaphore(port->shutdownSemaphore, 1, NULL); |
michael@0 | 97 | LeaveCriticalSection(&port->lock); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | int |
michael@0 | 101 | event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd, |
michael@0 | 102 | ev_uintptr_t key) |
michael@0 | 103 | { |
michael@0 | 104 | HANDLE h; |
michael@0 | 105 | h = CreateIoCompletionPort((HANDLE)fd, port->port, key, port->n_threads); |
michael@0 | 106 | if (!h) |
michael@0 | 107 | return -1; |
michael@0 | 108 | return 0; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | static void * |
michael@0 | 112 | get_extension_function(SOCKET s, const GUID *which_fn) |
michael@0 | 113 | { |
michael@0 | 114 | void *ptr = NULL; |
michael@0 | 115 | DWORD bytes=0; |
michael@0 | 116 | WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, |
michael@0 | 117 | (GUID*)which_fn, sizeof(*which_fn), |
michael@0 | 118 | &ptr, sizeof(ptr), |
michael@0 | 119 | &bytes, NULL, NULL); |
michael@0 | 120 | |
michael@0 | 121 | /* No need to detect errors here: if ptr is set, then we have a good |
michael@0 | 122 | function pointer. Otherwise, we should behave as if we had no |
michael@0 | 123 | function pointer. |
michael@0 | 124 | */ |
michael@0 | 125 | return ptr; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | /* Mingw doesn't have these in its mswsock.h. The values are copied from |
michael@0 | 129 | wine.h. Perhaps if we copy them exactly, the cargo will come again. |
michael@0 | 130 | */ |
michael@0 | 131 | #ifndef WSAID_ACCEPTEX |
michael@0 | 132 | #define WSAID_ACCEPTEX \ |
michael@0 | 133 | {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}} |
michael@0 | 134 | #endif |
michael@0 | 135 | #ifndef WSAID_CONNECTEX |
michael@0 | 136 | #define WSAID_CONNECTEX \ |
michael@0 | 137 | {0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}} |
michael@0 | 138 | #endif |
michael@0 | 139 | #ifndef WSAID_GETACCEPTEXSOCKADDRS |
michael@0 | 140 | #define WSAID_GETACCEPTEXSOCKADDRS \ |
michael@0 | 141 | {0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}} |
michael@0 | 142 | #endif |
michael@0 | 143 | |
michael@0 | 144 | static void |
michael@0 | 145 | init_extension_functions(struct win32_extension_fns *ext) |
michael@0 | 146 | { |
michael@0 | 147 | const GUID acceptex = WSAID_ACCEPTEX; |
michael@0 | 148 | const GUID connectex = WSAID_CONNECTEX; |
michael@0 | 149 | const GUID getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS; |
michael@0 | 150 | SOCKET s = socket(AF_INET, SOCK_STREAM, 0); |
michael@0 | 151 | if (s == INVALID_SOCKET) |
michael@0 | 152 | return; |
michael@0 | 153 | ext->AcceptEx = get_extension_function(s, &acceptex); |
michael@0 | 154 | ext->ConnectEx = get_extension_function(s, &connectex); |
michael@0 | 155 | ext->GetAcceptExSockaddrs = get_extension_function(s, |
michael@0 | 156 | &getacceptexsockaddrs); |
michael@0 | 157 | closesocket(s); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | static struct win32_extension_fns the_extension_fns; |
michael@0 | 161 | static int extension_fns_initialized = 0; |
michael@0 | 162 | |
michael@0 | 163 | const struct win32_extension_fns * |
michael@0 | 164 | event_get_win32_extension_fns(void) |
michael@0 | 165 | { |
michael@0 | 166 | return &the_extension_fns; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | #define N_CPUS_DEFAULT 2 |
michael@0 | 170 | |
michael@0 | 171 | struct event_iocp_port * |
michael@0 | 172 | event_iocp_port_launch(int n_cpus) |
michael@0 | 173 | { |
michael@0 | 174 | struct event_iocp_port *port; |
michael@0 | 175 | int i; |
michael@0 | 176 | |
michael@0 | 177 | if (!extension_fns_initialized) |
michael@0 | 178 | init_extension_functions(&the_extension_fns); |
michael@0 | 179 | |
michael@0 | 180 | if (!(port = mm_calloc(1, sizeof(struct event_iocp_port)))) |
michael@0 | 181 | return NULL; |
michael@0 | 182 | |
michael@0 | 183 | if (n_cpus <= 0) |
michael@0 | 184 | n_cpus = N_CPUS_DEFAULT; |
michael@0 | 185 | port->n_threads = n_cpus * 2; |
michael@0 | 186 | port->threads = mm_calloc(port->n_threads, sizeof(HANDLE)); |
michael@0 | 187 | if (!port->threads) |
michael@0 | 188 | goto err; |
michael@0 | 189 | |
michael@0 | 190 | port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, |
michael@0 | 191 | n_cpus); |
michael@0 | 192 | port->ms = -1; |
michael@0 | 193 | if (!port->port) |
michael@0 | 194 | goto err; |
michael@0 | 195 | |
michael@0 | 196 | port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL); |
michael@0 | 197 | if (!port->shutdownSemaphore) |
michael@0 | 198 | goto err; |
michael@0 | 199 | |
michael@0 | 200 | for (i=0; i<port->n_threads; ++i) { |
michael@0 | 201 | ev_uintptr_t th = _beginthread(loop, 0, port); |
michael@0 | 202 | if (th == (ev_uintptr_t)-1) |
michael@0 | 203 | goto err; |
michael@0 | 204 | port->threads[i] = (HANDLE)th; |
michael@0 | 205 | ++port->n_live_threads; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | InitializeCriticalSectionAndSpinCount(&port->lock, 1000); |
michael@0 | 209 | |
michael@0 | 210 | return port; |
michael@0 | 211 | err: |
michael@0 | 212 | if (port->port) |
michael@0 | 213 | CloseHandle(port->port); |
michael@0 | 214 | if (port->threads) |
michael@0 | 215 | mm_free(port->threads); |
michael@0 | 216 | if (port->shutdownSemaphore) |
michael@0 | 217 | CloseHandle(port->shutdownSemaphore); |
michael@0 | 218 | mm_free(port); |
michael@0 | 219 | return NULL; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | static void |
michael@0 | 223 | _event_iocp_port_unlock_and_free(struct event_iocp_port *port) |
michael@0 | 224 | { |
michael@0 | 225 | DeleteCriticalSection(&port->lock); |
michael@0 | 226 | CloseHandle(port->port); |
michael@0 | 227 | CloseHandle(port->shutdownSemaphore); |
michael@0 | 228 | mm_free(port->threads); |
michael@0 | 229 | mm_free(port); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | static int |
michael@0 | 233 | event_iocp_notify_all(struct event_iocp_port *port) |
michael@0 | 234 | { |
michael@0 | 235 | int i, r, ok=1; |
michael@0 | 236 | for (i=0; i<port->n_threads; ++i) { |
michael@0 | 237 | r = PostQueuedCompletionStatus(port->port, 0, NOTIFICATION_KEY, |
michael@0 | 238 | NULL); |
michael@0 | 239 | if (!r) |
michael@0 | 240 | ok = 0; |
michael@0 | 241 | } |
michael@0 | 242 | return ok ? 0 : -1; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | int |
michael@0 | 246 | event_iocp_shutdown(struct event_iocp_port *port, long waitMsec) |
michael@0 | 247 | { |
michael@0 | 248 | DWORD ms = INFINITE; |
michael@0 | 249 | int n; |
michael@0 | 250 | |
michael@0 | 251 | EnterCriticalSection(&port->lock); |
michael@0 | 252 | port->shutdown = 1; |
michael@0 | 253 | LeaveCriticalSection(&port->lock); |
michael@0 | 254 | event_iocp_notify_all(port); |
michael@0 | 255 | |
michael@0 | 256 | if (waitMsec >= 0) |
michael@0 | 257 | ms = waitMsec; |
michael@0 | 258 | |
michael@0 | 259 | WaitForSingleObject(port->shutdownSemaphore, ms); |
michael@0 | 260 | EnterCriticalSection(&port->lock); |
michael@0 | 261 | n = port->n_live_threads; |
michael@0 | 262 | LeaveCriticalSection(&port->lock); |
michael@0 | 263 | if (n == 0) { |
michael@0 | 264 | _event_iocp_port_unlock_and_free(port); |
michael@0 | 265 | return 0; |
michael@0 | 266 | } else { |
michael@0 | 267 | return -1; |
michael@0 | 268 | } |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | int |
michael@0 | 272 | event_iocp_activate_overlapped( |
michael@0 | 273 | struct event_iocp_port *port, struct event_overlapped *o, |
michael@0 | 274 | ev_uintptr_t key, ev_uint32_t n) |
michael@0 | 275 | { |
michael@0 | 276 | BOOL r; |
michael@0 | 277 | |
michael@0 | 278 | r = PostQueuedCompletionStatus(port->port, n, key, &o->overlapped); |
michael@0 | 279 | return (r==0) ? -1 : 0; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | struct event_iocp_port * |
michael@0 | 283 | event_base_get_iocp(struct event_base *base) |
michael@0 | 284 | { |
michael@0 | 285 | #ifdef WIN32 |
michael@0 | 286 | return base->iocp; |
michael@0 | 287 | #else |
michael@0 | 288 | return NULL; |
michael@0 | 289 | #endif |
michael@0 | 290 | } |