12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2009-2012 Niels Provos, Nick Mathewson
32b15cb3dSCy Schubert *
42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert * are met:
72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert * derived from this software without specific prior written permission.
142b15cb3dSCy Schubert *
152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert */
262b15cb3dSCy Schubert #include "evconfig-private.h"
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert #ifndef _WIN32_WINNT
292b15cb3dSCy Schubert /* Minimum required for InitializeCriticalSectionAndSpinCount */
302b15cb3dSCy Schubert #define _WIN32_WINNT 0x0403
312b15cb3dSCy Schubert #endif
322b15cb3dSCy Schubert #include <winsock2.h>
332b15cb3dSCy Schubert #include <windows.h>
342b15cb3dSCy Schubert #include <process.h>
352b15cb3dSCy Schubert #include <stdio.h>
362b15cb3dSCy Schubert #include <mswsock.h>
372b15cb3dSCy Schubert
382b15cb3dSCy Schubert #include "event2/util.h"
392b15cb3dSCy Schubert #include "util-internal.h"
402b15cb3dSCy Schubert #include "iocp-internal.h"
412b15cb3dSCy Schubert #include "log-internal.h"
422b15cb3dSCy Schubert #include "mm-internal.h"
432b15cb3dSCy Schubert #include "event-internal.h"
442b15cb3dSCy Schubert #include "evthread-internal.h"
452b15cb3dSCy Schubert
462b15cb3dSCy Schubert #define NOTIFICATION_KEY ((ULONG_PTR)-1)
472b15cb3dSCy Schubert
482b15cb3dSCy Schubert void
event_overlapped_init_(struct event_overlapped * o,iocp_callback cb)492b15cb3dSCy Schubert event_overlapped_init_(struct event_overlapped *o, iocp_callback cb)
502b15cb3dSCy Schubert {
512b15cb3dSCy Schubert memset(o, 0, sizeof(struct event_overlapped));
522b15cb3dSCy Schubert o->cb = cb;
532b15cb3dSCy Schubert }
542b15cb3dSCy Schubert
552b15cb3dSCy Schubert static void
handle_entry(OVERLAPPED * o,ULONG_PTR completion_key,DWORD nBytes,int ok)562b15cb3dSCy Schubert handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok)
572b15cb3dSCy Schubert {
582b15cb3dSCy Schubert struct event_overlapped *eo =
592b15cb3dSCy Schubert EVUTIL_UPCAST(o, struct event_overlapped, overlapped);
602b15cb3dSCy Schubert eo->cb(eo, completion_key, nBytes, ok);
612b15cb3dSCy Schubert }
622b15cb3dSCy Schubert
632b15cb3dSCy Schubert static void
loop(void * port_)642b15cb3dSCy Schubert loop(void *port_)
652b15cb3dSCy Schubert {
662b15cb3dSCy Schubert struct event_iocp_port *port = port_;
672b15cb3dSCy Schubert long ms = port->ms;
682b15cb3dSCy Schubert HANDLE p = port->port;
692b15cb3dSCy Schubert
702b15cb3dSCy Schubert if (ms <= 0)
712b15cb3dSCy Schubert ms = INFINITE;
722b15cb3dSCy Schubert
732b15cb3dSCy Schubert while (1) {
742b15cb3dSCy Schubert OVERLAPPED *overlapped=NULL;
752b15cb3dSCy Schubert ULONG_PTR key=0;
762b15cb3dSCy Schubert DWORD bytes=0;
772b15cb3dSCy Schubert int ok = GetQueuedCompletionStatus(p, &bytes, &key,
782b15cb3dSCy Schubert &overlapped, ms);
792b15cb3dSCy Schubert EnterCriticalSection(&port->lock);
802b15cb3dSCy Schubert if (port->shutdown) {
812b15cb3dSCy Schubert if (--port->n_live_threads == 0)
822b15cb3dSCy Schubert ReleaseSemaphore(port->shutdownSemaphore, 1,
832b15cb3dSCy Schubert NULL);
842b15cb3dSCy Schubert LeaveCriticalSection(&port->lock);
852b15cb3dSCy Schubert return;
862b15cb3dSCy Schubert }
872b15cb3dSCy Schubert LeaveCriticalSection(&port->lock);
882b15cb3dSCy Schubert
892b15cb3dSCy Schubert if (key != NOTIFICATION_KEY && overlapped)
902b15cb3dSCy Schubert handle_entry(overlapped, key, bytes, ok);
912b15cb3dSCy Schubert else if (!overlapped)
922b15cb3dSCy Schubert break;
932b15cb3dSCy Schubert }
942b15cb3dSCy Schubert event_warnx("GetQueuedCompletionStatus exited with no event.");
952b15cb3dSCy Schubert EnterCriticalSection(&port->lock);
962b15cb3dSCy Schubert if (--port->n_live_threads == 0)
972b15cb3dSCy Schubert ReleaseSemaphore(port->shutdownSemaphore, 1, NULL);
982b15cb3dSCy Schubert LeaveCriticalSection(&port->lock);
992b15cb3dSCy Schubert }
1002b15cb3dSCy Schubert
1012b15cb3dSCy Schubert int
event_iocp_port_associate_(struct event_iocp_port * port,evutil_socket_t fd,ev_uintptr_t key)1022b15cb3dSCy Schubert event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
1032b15cb3dSCy Schubert ev_uintptr_t key)
1042b15cb3dSCy Schubert {
1052b15cb3dSCy Schubert HANDLE h;
1062b15cb3dSCy Schubert h = CreateIoCompletionPort((HANDLE)fd, port->port, key, port->n_threads);
1072b15cb3dSCy Schubert if (!h)
1082b15cb3dSCy Schubert return -1;
1092b15cb3dSCy Schubert return 0;
1102b15cb3dSCy Schubert }
1112b15cb3dSCy Schubert
1122b15cb3dSCy Schubert static void *
get_extension_function(SOCKET s,const GUID * which_fn)1132b15cb3dSCy Schubert get_extension_function(SOCKET s, const GUID *which_fn)
1142b15cb3dSCy Schubert {
1152b15cb3dSCy Schubert void *ptr = NULL;
1162b15cb3dSCy Schubert DWORD bytes=0;
1172b15cb3dSCy Schubert WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
1182b15cb3dSCy Schubert (GUID*)which_fn, sizeof(*which_fn),
1192b15cb3dSCy Schubert &ptr, sizeof(ptr),
1202b15cb3dSCy Schubert &bytes, NULL, NULL);
1212b15cb3dSCy Schubert
1222b15cb3dSCy Schubert /* No need to detect errors here: if ptr is set, then we have a good
1232b15cb3dSCy Schubert function pointer. Otherwise, we should behave as if we had no
1242b15cb3dSCy Schubert function pointer.
1252b15cb3dSCy Schubert */
1262b15cb3dSCy Schubert return ptr;
1272b15cb3dSCy Schubert }
1282b15cb3dSCy Schubert
1292b15cb3dSCy Schubert /* Mingw doesn't have these in its mswsock.h. The values are copied from
1302b15cb3dSCy Schubert wine.h. Perhaps if we copy them exactly, the cargo will come again.
1312b15cb3dSCy Schubert */
1322b15cb3dSCy Schubert #ifndef WSAID_ACCEPTEX
1332b15cb3dSCy Schubert #define WSAID_ACCEPTEX \
1342b15cb3dSCy Schubert {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1352b15cb3dSCy Schubert #endif
1362b15cb3dSCy Schubert #ifndef WSAID_CONNECTEX
1372b15cb3dSCy Schubert #define WSAID_CONNECTEX \
1382b15cb3dSCy Schubert {0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}
1392b15cb3dSCy Schubert #endif
1402b15cb3dSCy Schubert #ifndef WSAID_GETACCEPTEXSOCKADDRS
1412b15cb3dSCy Schubert #define WSAID_GETACCEPTEXSOCKADDRS \
1422b15cb3dSCy Schubert {0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1432b15cb3dSCy Schubert #endif
1442b15cb3dSCy Schubert
1452b15cb3dSCy Schubert static int extension_fns_initialized = 0;
1462b15cb3dSCy Schubert
1472b15cb3dSCy Schubert static void
init_extension_functions(struct win32_extension_fns * ext)1482b15cb3dSCy Schubert init_extension_functions(struct win32_extension_fns *ext)
1492b15cb3dSCy Schubert {
1502b15cb3dSCy Schubert const GUID acceptex = WSAID_ACCEPTEX;
1512b15cb3dSCy Schubert const GUID connectex = WSAID_CONNECTEX;
1522b15cb3dSCy Schubert const GUID getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
1532b15cb3dSCy Schubert SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
154*a466cc55SCy Schubert if (s == EVUTIL_INVALID_SOCKET)
1552b15cb3dSCy Schubert return;
1562b15cb3dSCy Schubert ext->AcceptEx = get_extension_function(s, &acceptex);
1572b15cb3dSCy Schubert ext->ConnectEx = get_extension_function(s, &connectex);
1582b15cb3dSCy Schubert ext->GetAcceptExSockaddrs = get_extension_function(s,
1592b15cb3dSCy Schubert &getacceptexsockaddrs);
1602b15cb3dSCy Schubert closesocket(s);
1612b15cb3dSCy Schubert
1622b15cb3dSCy Schubert extension_fns_initialized = 1;
1632b15cb3dSCy Schubert }
1642b15cb3dSCy Schubert
1652b15cb3dSCy Schubert static struct win32_extension_fns the_extension_fns;
1662b15cb3dSCy Schubert
1672b15cb3dSCy Schubert const struct win32_extension_fns *
event_get_win32_extension_fns_(void)1682b15cb3dSCy Schubert event_get_win32_extension_fns_(void)
1692b15cb3dSCy Schubert {
1702b15cb3dSCy Schubert return &the_extension_fns;
1712b15cb3dSCy Schubert }
1722b15cb3dSCy Schubert
1732b15cb3dSCy Schubert #define N_CPUS_DEFAULT 2
1742b15cb3dSCy Schubert
1752b15cb3dSCy Schubert struct event_iocp_port *
event_iocp_port_launch_(int n_cpus)1762b15cb3dSCy Schubert event_iocp_port_launch_(int n_cpus)
1772b15cb3dSCy Schubert {
1782b15cb3dSCy Schubert struct event_iocp_port *port;
1792b15cb3dSCy Schubert int i;
1802b15cb3dSCy Schubert
1812b15cb3dSCy Schubert if (!extension_fns_initialized)
1822b15cb3dSCy Schubert init_extension_functions(&the_extension_fns);
1832b15cb3dSCy Schubert
1842b15cb3dSCy Schubert if (!(port = mm_calloc(1, sizeof(struct event_iocp_port))))
1852b15cb3dSCy Schubert return NULL;
1862b15cb3dSCy Schubert
1872b15cb3dSCy Schubert if (n_cpus <= 0)
1882b15cb3dSCy Schubert n_cpus = N_CPUS_DEFAULT;
1892b15cb3dSCy Schubert port->n_threads = n_cpus * 2;
1902b15cb3dSCy Schubert port->threads = mm_calloc(port->n_threads, sizeof(HANDLE));
1912b15cb3dSCy Schubert if (!port->threads)
1922b15cb3dSCy Schubert goto err;
1932b15cb3dSCy Schubert
1942b15cb3dSCy Schubert port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0,
1952b15cb3dSCy Schubert n_cpus);
1962b15cb3dSCy Schubert port->ms = -1;
1972b15cb3dSCy Schubert if (!port->port)
1982b15cb3dSCy Schubert goto err;
1992b15cb3dSCy Schubert
2002b15cb3dSCy Schubert port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
2012b15cb3dSCy Schubert if (!port->shutdownSemaphore)
2022b15cb3dSCy Schubert goto err;
2032b15cb3dSCy Schubert
2042b15cb3dSCy Schubert for (i=0; i<port->n_threads; ++i) {
2052b15cb3dSCy Schubert ev_uintptr_t th = _beginthread(loop, 0, port);
2062b15cb3dSCy Schubert if (th == (ev_uintptr_t)-1)
2072b15cb3dSCy Schubert goto err;
2082b15cb3dSCy Schubert port->threads[i] = (HANDLE)th;
2092b15cb3dSCy Schubert ++port->n_live_threads;
2102b15cb3dSCy Schubert }
2112b15cb3dSCy Schubert
2122b15cb3dSCy Schubert InitializeCriticalSectionAndSpinCount(&port->lock, 1000);
2132b15cb3dSCy Schubert
2142b15cb3dSCy Schubert return port;
2152b15cb3dSCy Schubert err:
2162b15cb3dSCy Schubert if (port->port)
2172b15cb3dSCy Schubert CloseHandle(port->port);
2182b15cb3dSCy Schubert if (port->threads)
2192b15cb3dSCy Schubert mm_free(port->threads);
2202b15cb3dSCy Schubert if (port->shutdownSemaphore)
2212b15cb3dSCy Schubert CloseHandle(port->shutdownSemaphore);
2222b15cb3dSCy Schubert mm_free(port);
2232b15cb3dSCy Schubert return NULL;
2242b15cb3dSCy Schubert }
2252b15cb3dSCy Schubert
2262b15cb3dSCy Schubert static void
event_iocp_port_unlock_and_free_(struct event_iocp_port * port)2272b15cb3dSCy Schubert event_iocp_port_unlock_and_free_(struct event_iocp_port *port)
2282b15cb3dSCy Schubert {
2292b15cb3dSCy Schubert DeleteCriticalSection(&port->lock);
2302b15cb3dSCy Schubert CloseHandle(port->port);
2312b15cb3dSCy Schubert CloseHandle(port->shutdownSemaphore);
2322b15cb3dSCy Schubert mm_free(port->threads);
2332b15cb3dSCy Schubert mm_free(port);
2342b15cb3dSCy Schubert }
2352b15cb3dSCy Schubert
2362b15cb3dSCy Schubert static int
event_iocp_notify_all(struct event_iocp_port * port)2372b15cb3dSCy Schubert event_iocp_notify_all(struct event_iocp_port *port)
2382b15cb3dSCy Schubert {
2392b15cb3dSCy Schubert int i, r, ok=1;
2402b15cb3dSCy Schubert for (i=0; i<port->n_threads; ++i) {
2412b15cb3dSCy Schubert r = PostQueuedCompletionStatus(port->port, 0, NOTIFICATION_KEY,
2422b15cb3dSCy Schubert NULL);
2432b15cb3dSCy Schubert if (!r)
2442b15cb3dSCy Schubert ok = 0;
2452b15cb3dSCy Schubert }
2462b15cb3dSCy Schubert return ok ? 0 : -1;
2472b15cb3dSCy Schubert }
2482b15cb3dSCy Schubert
2492b15cb3dSCy Schubert int
event_iocp_shutdown_(struct event_iocp_port * port,long waitMsec)2502b15cb3dSCy Schubert event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec)
2512b15cb3dSCy Schubert {
2522b15cb3dSCy Schubert DWORD ms = INFINITE;
2532b15cb3dSCy Schubert int n;
2542b15cb3dSCy Schubert
2552b15cb3dSCy Schubert EnterCriticalSection(&port->lock);
2562b15cb3dSCy Schubert port->shutdown = 1;
2572b15cb3dSCy Schubert LeaveCriticalSection(&port->lock);
2582b15cb3dSCy Schubert event_iocp_notify_all(port);
2592b15cb3dSCy Schubert
2602b15cb3dSCy Schubert if (waitMsec >= 0)
2612b15cb3dSCy Schubert ms = waitMsec;
2622b15cb3dSCy Schubert
2632b15cb3dSCy Schubert WaitForSingleObject(port->shutdownSemaphore, ms);
2642b15cb3dSCy Schubert EnterCriticalSection(&port->lock);
2652b15cb3dSCy Schubert n = port->n_live_threads;
2662b15cb3dSCy Schubert LeaveCriticalSection(&port->lock);
2672b15cb3dSCy Schubert if (n == 0) {
2682b15cb3dSCy Schubert event_iocp_port_unlock_and_free_(port);
2692b15cb3dSCy Schubert return 0;
2702b15cb3dSCy Schubert } else {
2712b15cb3dSCy Schubert return -1;
2722b15cb3dSCy Schubert }
2732b15cb3dSCy Schubert }
2742b15cb3dSCy Schubert
2752b15cb3dSCy Schubert int
event_iocp_activate_overlapped_(struct event_iocp_port * port,struct event_overlapped * o,ev_uintptr_t key,ev_uint32_t n)2762b15cb3dSCy Schubert event_iocp_activate_overlapped_(
2772b15cb3dSCy Schubert struct event_iocp_port *port, struct event_overlapped *o,
2782b15cb3dSCy Schubert ev_uintptr_t key, ev_uint32_t n)
2792b15cb3dSCy Schubert {
2802b15cb3dSCy Schubert BOOL r;
2812b15cb3dSCy Schubert
2822b15cb3dSCy Schubert r = PostQueuedCompletionStatus(port->port, n, key, &o->overlapped);
2832b15cb3dSCy Schubert return (r==0) ? -1 : 0;
2842b15cb3dSCy Schubert }
2852b15cb3dSCy Schubert
2862b15cb3dSCy Schubert struct event_iocp_port *
event_base_get_iocp_(struct event_base * base)2872b15cb3dSCy Schubert event_base_get_iocp_(struct event_base *base)
2882b15cb3dSCy Schubert {
2892b15cb3dSCy Schubert #ifdef _WIN32
2902b15cb3dSCy Schubert return base->iocp;
2912b15cb3dSCy Schubert #else
2922b15cb3dSCy Schubert return NULL;
2932b15cb3dSCy Schubert #endif
2942b15cb3dSCy Schubert }
295