xref: /netbsd-src/external/bsd/libevent/dist/event_iocp.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1*657871a7Schristos /*	$NetBSD: event_iocp.c,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $	*/
26ecf6635Schristos /*
36ecf6635Schristos  * Copyright (c) 2009-2012 Niels Provos, Nick Mathewson
46ecf6635Schristos  *
56ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
66ecf6635Schristos  * modification, are permitted provided that the following conditions
76ecf6635Schristos  * are met:
86ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
96ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
106ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
116ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
126ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
136ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
146ecf6635Schristos  *    derived from this software without specific prior written permission.
156ecf6635Schristos  *
166ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
256ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266ecf6635Schristos  */
27805a1ce9Schristos #include "evconfig-private.h"
286ecf6635Schristos 
296ecf6635Schristos #ifndef _WIN32_WINNT
306ecf6635Schristos /* Minimum required for InitializeCriticalSectionAndSpinCount */
316ecf6635Schristos #define _WIN32_WINNT 0x0403
326ecf6635Schristos #endif
336ecf6635Schristos #include <winsock2.h>
346ecf6635Schristos #include <windows.h>
356ecf6635Schristos #include <process.h>
366ecf6635Schristos #include <stdio.h>
376ecf6635Schristos #include <mswsock.h>
386ecf6635Schristos 
396ecf6635Schristos #include "event2/util.h"
406ecf6635Schristos #include "util-internal.h"
416ecf6635Schristos #include "iocp-internal.h"
426ecf6635Schristos #include "log-internal.h"
436ecf6635Schristos #include "mm-internal.h"
446ecf6635Schristos #include "event-internal.h"
456ecf6635Schristos #include "evthread-internal.h"
466ecf6635Schristos 
476ecf6635Schristos #define NOTIFICATION_KEY ((ULONG_PTR)-1)
486ecf6635Schristos 
496ecf6635Schristos void
event_overlapped_init_(struct event_overlapped * o,iocp_callback cb)50805a1ce9Schristos event_overlapped_init_(struct event_overlapped *o, iocp_callback cb)
516ecf6635Schristos {
526ecf6635Schristos 	memset(o, 0, sizeof(struct event_overlapped));
536ecf6635Schristos 	o->cb = cb;
546ecf6635Schristos }
556ecf6635Schristos 
566ecf6635Schristos static void
handle_entry(OVERLAPPED * o,ULONG_PTR completion_key,DWORD nBytes,int ok)576ecf6635Schristos handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok)
586ecf6635Schristos {
596ecf6635Schristos 	struct event_overlapped *eo =
606ecf6635Schristos 	    EVUTIL_UPCAST(o, struct event_overlapped, overlapped);
616ecf6635Schristos 	eo->cb(eo, completion_key, nBytes, ok);
626ecf6635Schristos }
636ecf6635Schristos 
646ecf6635Schristos static void
loop(void * port_)65805a1ce9Schristos loop(void *port_)
666ecf6635Schristos {
67805a1ce9Schristos 	struct event_iocp_port *port = port_;
686ecf6635Schristos 	long ms = port->ms;
696ecf6635Schristos 	HANDLE p = port->port;
706ecf6635Schristos 
716ecf6635Schristos 	if (ms <= 0)
726ecf6635Schristos 		ms = INFINITE;
736ecf6635Schristos 
746ecf6635Schristos 	while (1) {
756ecf6635Schristos 		OVERLAPPED *overlapped=NULL;
766ecf6635Schristos 		ULONG_PTR key=0;
776ecf6635Schristos 		DWORD bytes=0;
786ecf6635Schristos 		int ok = GetQueuedCompletionStatus(p, &bytes, &key,
796ecf6635Schristos 			&overlapped, ms);
806ecf6635Schristos 		EnterCriticalSection(&port->lock);
816ecf6635Schristos 		if (port->shutdown) {
826ecf6635Schristos 			if (--port->n_live_threads == 0)
836ecf6635Schristos 				ReleaseSemaphore(port->shutdownSemaphore, 1,
846ecf6635Schristos 						NULL);
856ecf6635Schristos 			LeaveCriticalSection(&port->lock);
866ecf6635Schristos 			return;
876ecf6635Schristos 		}
886ecf6635Schristos 		LeaveCriticalSection(&port->lock);
896ecf6635Schristos 
906ecf6635Schristos 		if (key != NOTIFICATION_KEY && overlapped)
916ecf6635Schristos 			handle_entry(overlapped, key, bytes, ok);
926ecf6635Schristos 		else if (!overlapped)
936ecf6635Schristos 			break;
946ecf6635Schristos 	}
956ecf6635Schristos 	event_warnx("GetQueuedCompletionStatus exited with no event.");
966ecf6635Schristos 	EnterCriticalSection(&port->lock);
976ecf6635Schristos 	if (--port->n_live_threads == 0)
986ecf6635Schristos 		ReleaseSemaphore(port->shutdownSemaphore, 1, NULL);
996ecf6635Schristos 	LeaveCriticalSection(&port->lock);
1006ecf6635Schristos }
1016ecf6635Schristos 
1026ecf6635Schristos int
event_iocp_port_associate_(struct event_iocp_port * port,evutil_socket_t fd,ev_uintptr_t key)103805a1ce9Schristos event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
1046ecf6635Schristos     ev_uintptr_t key)
1056ecf6635Schristos {
1066ecf6635Schristos 	HANDLE h;
1076ecf6635Schristos 	h = CreateIoCompletionPort((HANDLE)fd, port->port, key, port->n_threads);
1086ecf6635Schristos 	if (!h)
1096ecf6635Schristos 		return -1;
1106ecf6635Schristos 	return 0;
1116ecf6635Schristos }
1126ecf6635Schristos 
1136ecf6635Schristos static void *
get_extension_function(SOCKET s,const GUID * which_fn)1146ecf6635Schristos get_extension_function(SOCKET s, const GUID *which_fn)
1156ecf6635Schristos {
1166ecf6635Schristos 	void *ptr = NULL;
1176ecf6635Schristos 	DWORD bytes=0;
1186ecf6635Schristos 	WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
1196ecf6635Schristos 	    (GUID*)which_fn, sizeof(*which_fn),
1206ecf6635Schristos 	    &ptr, sizeof(ptr),
1216ecf6635Schristos 	    &bytes, NULL, NULL);
1226ecf6635Schristos 
1236ecf6635Schristos 	/* No need to detect errors here: if ptr is set, then we have a good
1246ecf6635Schristos 	   function pointer.  Otherwise, we should behave as if we had no
1256ecf6635Schristos 	   function pointer.
1266ecf6635Schristos 	*/
1276ecf6635Schristos 	return ptr;
1286ecf6635Schristos }
1296ecf6635Schristos 
1306ecf6635Schristos /* Mingw doesn't have these in its mswsock.h.  The values are copied from
1316ecf6635Schristos    wine.h.   Perhaps if we copy them exactly, the cargo will come again.
1326ecf6635Schristos */
1336ecf6635Schristos #ifndef WSAID_ACCEPTEX
1346ecf6635Schristos #define WSAID_ACCEPTEX \
1356ecf6635Schristos 	{0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1366ecf6635Schristos #endif
1376ecf6635Schristos #ifndef WSAID_CONNECTEX
1386ecf6635Schristos #define WSAID_CONNECTEX \
1396ecf6635Schristos 	{0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}
1406ecf6635Schristos #endif
1416ecf6635Schristos #ifndef WSAID_GETACCEPTEXSOCKADDRS
1426ecf6635Schristos #define WSAID_GETACCEPTEXSOCKADDRS \
1436ecf6635Schristos 	{0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
1446ecf6635Schristos #endif
1456ecf6635Schristos 
146447d9fdcSspz static int extension_fns_initialized = 0;
147447d9fdcSspz 
1486ecf6635Schristos static void
init_extension_functions(struct win32_extension_fns * ext)1496ecf6635Schristos init_extension_functions(struct win32_extension_fns *ext)
1506ecf6635Schristos {
1516ecf6635Schristos 	const GUID acceptex = WSAID_ACCEPTEX;
1526ecf6635Schristos 	const GUID connectex = WSAID_CONNECTEX;
1536ecf6635Schristos 	const GUID getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
1546ecf6635Schristos 	SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
155*657871a7Schristos 	if (s == EVUTIL_INVALID_SOCKET)
1566ecf6635Schristos 		return;
1576ecf6635Schristos 	ext->AcceptEx = get_extension_function(s, &acceptex);
1586ecf6635Schristos 	ext->ConnectEx = get_extension_function(s, &connectex);
1596ecf6635Schristos 	ext->GetAcceptExSockaddrs = get_extension_function(s,
1606ecf6635Schristos 	    &getacceptexsockaddrs);
1616ecf6635Schristos 	closesocket(s);
162447d9fdcSspz 
163447d9fdcSspz 	extension_fns_initialized = 1;
1646ecf6635Schristos }
1656ecf6635Schristos 
1666ecf6635Schristos static struct win32_extension_fns the_extension_fns;
1676ecf6635Schristos 
1686ecf6635Schristos const struct win32_extension_fns *
event_get_win32_extension_fns_(void)169805a1ce9Schristos event_get_win32_extension_fns_(void)
1706ecf6635Schristos {
1716ecf6635Schristos 	return &the_extension_fns;
1726ecf6635Schristos }
1736ecf6635Schristos 
1746ecf6635Schristos #define N_CPUS_DEFAULT 2
1756ecf6635Schristos 
1766ecf6635Schristos struct event_iocp_port *
event_iocp_port_launch_(int n_cpus)177805a1ce9Schristos event_iocp_port_launch_(int n_cpus)
1786ecf6635Schristos {
1796ecf6635Schristos 	struct event_iocp_port *port;
1806ecf6635Schristos 	int i;
1816ecf6635Schristos 
1826ecf6635Schristos 	if (!extension_fns_initialized)
1836ecf6635Schristos 		init_extension_functions(&the_extension_fns);
1846ecf6635Schristos 
1856ecf6635Schristos 	if (!(port = mm_calloc(1, sizeof(struct event_iocp_port))))
1866ecf6635Schristos 		return NULL;
1876ecf6635Schristos 
1886ecf6635Schristos 	if (n_cpus <= 0)
1896ecf6635Schristos 		n_cpus = N_CPUS_DEFAULT;
1906ecf6635Schristos 	port->n_threads = n_cpus * 2;
1916ecf6635Schristos 	port->threads = mm_calloc(port->n_threads, sizeof(HANDLE));
1926ecf6635Schristos 	if (!port->threads)
1936ecf6635Schristos 		goto err;
1946ecf6635Schristos 
1956ecf6635Schristos 	port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0,
1966ecf6635Schristos 			n_cpus);
1976ecf6635Schristos 	port->ms = -1;
1986ecf6635Schristos 	if (!port->port)
1996ecf6635Schristos 		goto err;
2006ecf6635Schristos 
2016ecf6635Schristos 	port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
2026ecf6635Schristos 	if (!port->shutdownSemaphore)
2036ecf6635Schristos 		goto err;
2046ecf6635Schristos 
2056ecf6635Schristos 	for (i=0; i<port->n_threads; ++i) {
2066ecf6635Schristos 		ev_uintptr_t th = _beginthread(loop, 0, port);
2076ecf6635Schristos 		if (th == (ev_uintptr_t)-1)
2086ecf6635Schristos 			goto err;
2096ecf6635Schristos 		port->threads[i] = (HANDLE)th;
2106ecf6635Schristos 		++port->n_live_threads;
2116ecf6635Schristos 	}
2126ecf6635Schristos 
2136ecf6635Schristos 	InitializeCriticalSectionAndSpinCount(&port->lock, 1000);
2146ecf6635Schristos 
2156ecf6635Schristos 	return port;
2166ecf6635Schristos err:
2176ecf6635Schristos 	if (port->port)
2186ecf6635Schristos 		CloseHandle(port->port);
2196ecf6635Schristos 	if (port->threads)
2206ecf6635Schristos 		mm_free(port->threads);
2216ecf6635Schristos 	if (port->shutdownSemaphore)
2226ecf6635Schristos 		CloseHandle(port->shutdownSemaphore);
2236ecf6635Schristos 	mm_free(port);
2246ecf6635Schristos 	return NULL;
2256ecf6635Schristos }
2266ecf6635Schristos 
2276ecf6635Schristos static void
event_iocp_port_unlock_and_free_(struct event_iocp_port * port)228805a1ce9Schristos event_iocp_port_unlock_and_free_(struct event_iocp_port *port)
2296ecf6635Schristos {
2306ecf6635Schristos 	DeleteCriticalSection(&port->lock);
2316ecf6635Schristos 	CloseHandle(port->port);
2326ecf6635Schristos 	CloseHandle(port->shutdownSemaphore);
2336ecf6635Schristos 	mm_free(port->threads);
2346ecf6635Schristos 	mm_free(port);
2356ecf6635Schristos }
2366ecf6635Schristos 
2376ecf6635Schristos static int
event_iocp_notify_all(struct event_iocp_port * port)2386ecf6635Schristos event_iocp_notify_all(struct event_iocp_port *port)
2396ecf6635Schristos {
2406ecf6635Schristos 	int i, r, ok=1;
2416ecf6635Schristos 	for (i=0; i<port->n_threads; ++i) {
2426ecf6635Schristos 		r = PostQueuedCompletionStatus(port->port, 0, NOTIFICATION_KEY,
2436ecf6635Schristos 		    NULL);
2446ecf6635Schristos 		if (!r)
2456ecf6635Schristos 			ok = 0;
2466ecf6635Schristos 	}
2476ecf6635Schristos 	return ok ? 0 : -1;
2486ecf6635Schristos }
2496ecf6635Schristos 
2506ecf6635Schristos int
event_iocp_shutdown_(struct event_iocp_port * port,long waitMsec)251805a1ce9Schristos event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec)
2526ecf6635Schristos {
2536ecf6635Schristos 	DWORD ms = INFINITE;
2546ecf6635Schristos 	int n;
2556ecf6635Schristos 
2566ecf6635Schristos 	EnterCriticalSection(&port->lock);
2576ecf6635Schristos 	port->shutdown = 1;
2586ecf6635Schristos 	LeaveCriticalSection(&port->lock);
2596ecf6635Schristos 	event_iocp_notify_all(port);
2606ecf6635Schristos 
2616ecf6635Schristos 	if (waitMsec >= 0)
2626ecf6635Schristos 		ms = waitMsec;
2636ecf6635Schristos 
2646ecf6635Schristos 	WaitForSingleObject(port->shutdownSemaphore, ms);
2656ecf6635Schristos 	EnterCriticalSection(&port->lock);
2666ecf6635Schristos 	n = port->n_live_threads;
2676ecf6635Schristos 	LeaveCriticalSection(&port->lock);
2686ecf6635Schristos 	if (n == 0) {
269805a1ce9Schristos 		event_iocp_port_unlock_and_free_(port);
2706ecf6635Schristos 		return 0;
2716ecf6635Schristos 	} else {
2726ecf6635Schristos 		return -1;
2736ecf6635Schristos 	}
2746ecf6635Schristos }
2756ecf6635Schristos 
2766ecf6635Schristos int
event_iocp_activate_overlapped_(struct event_iocp_port * port,struct event_overlapped * o,ev_uintptr_t key,ev_uint32_t n)277805a1ce9Schristos event_iocp_activate_overlapped_(
2786ecf6635Schristos     struct event_iocp_port *port, struct event_overlapped *o,
2796ecf6635Schristos     ev_uintptr_t key, ev_uint32_t n)
2806ecf6635Schristos {
2816ecf6635Schristos 	BOOL r;
2826ecf6635Schristos 
2836ecf6635Schristos 	r = PostQueuedCompletionStatus(port->port, n, key, &o->overlapped);
2846ecf6635Schristos 	return (r==0) ? -1 : 0;
2856ecf6635Schristos }
2866ecf6635Schristos 
2876ecf6635Schristos struct event_iocp_port *
event_base_get_iocp_(struct event_base * base)288805a1ce9Schristos event_base_get_iocp_(struct event_base *base)
2896ecf6635Schristos {
290805a1ce9Schristos #ifdef _WIN32
2916ecf6635Schristos 	return base->iocp;
2926ecf6635Schristos #else
2936ecf6635Schristos 	return NULL;
2946ecf6635Schristos #endif
2956ecf6635Schristos }
296