event_iocp.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2009-2012 Niels Provos, Nick Mathewson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "evconfig-private.h"
  27. #ifndef _WIN32_WINNT
  28. /* Minimum required for InitializeCriticalSectionAndSpinCount */
  29. #define _WIN32_WINNT 0x0403
  30. #endif
  31. #include <winsock2.h>
  32. #include <windows.h>
  33. #include <process.h>
  34. #include <stdio.h>
  35. #include <mswsock.h>
  36. #include "event2/util.h"
  37. #include "util-internal.h"
  38. #include "iocp-internal.h"
  39. #include "log-internal.h"
  40. #include "mm-internal.h"
  41. #include "event-internal.h"
  42. #include "evthread-internal.h"
  43. #define NOTIFICATION_KEY ((ULONG_PTR)-1)
  44. void
  45. event_overlapped_init_(struct event_overlapped *o, iocp_callback cb)
  46. {
  47. memset(o, 0, sizeof(struct event_overlapped));
  48. o->cb = cb;
  49. }
  50. static void
  51. handle_entry(OVERLAPPED *o, ULONG_PTR completion_key, DWORD nBytes, int ok)
  52. {
  53. struct event_overlapped *eo =
  54. EVUTIL_UPCAST(o, struct event_overlapped, overlapped);
  55. eo->cb(eo, completion_key, nBytes, ok);
  56. }
  57. static void
  58. loop(void *port_)
  59. {
  60. struct event_iocp_port *port = port_;
  61. long ms = port->ms;
  62. HANDLE p = port->port;
  63. if (ms <= 0)
  64. ms = INFINITE;
  65. while (1) {
  66. OVERLAPPED *overlapped=NULL;
  67. ULONG_PTR key=0;
  68. DWORD bytes=0;
  69. int ok = GetQueuedCompletionStatus(p, &bytes, &key,
  70. &overlapped, ms);
  71. EnterCriticalSection(&port->lock);
  72. if (port->shutdown) {
  73. if (--port->n_live_threads == 0)
  74. ReleaseSemaphore(port->shutdownSemaphore, 1,
  75. NULL);
  76. LeaveCriticalSection(&port->lock);
  77. return;
  78. }
  79. LeaveCriticalSection(&port->lock);
  80. if (key != NOTIFICATION_KEY && overlapped)
  81. handle_entry(overlapped, key, bytes, ok);
  82. else if (!overlapped)
  83. break;
  84. }
  85. event_warnx("GetQueuedCompletionStatus exited with no event.");
  86. EnterCriticalSection(&port->lock);
  87. if (--port->n_live_threads == 0)
  88. ReleaseSemaphore(port->shutdownSemaphore, 1, NULL);
  89. LeaveCriticalSection(&port->lock);
  90. }
  91. int
  92. event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
  93. ev_uintptr_t key)
  94. {
  95. HANDLE h;
  96. h = CreateIoCompletionPort((HANDLE)fd, port->port, key, port->n_threads);
  97. if (!h)
  98. return -1;
  99. return 0;
  100. }
  101. static void *
  102. get_extension_function(SOCKET s, const GUID *which_fn)
  103. {
  104. void *ptr = NULL;
  105. DWORD bytes=0;
  106. WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
  107. (GUID*)which_fn, sizeof(*which_fn),
  108. &ptr, sizeof(ptr),
  109. &bytes, NULL, NULL);
  110. /* No need to detect errors here: if ptr is set, then we have a good
  111. function pointer. Otherwise, we should behave as if we had no
  112. function pointer.
  113. */
  114. return ptr;
  115. }
  116. /* Mingw doesn't have these in its mswsock.h. The values are copied from
  117. wine.h. Perhaps if we copy them exactly, the cargo will come again.
  118. */
  119. #ifndef WSAID_ACCEPTEX
  120. #define WSAID_ACCEPTEX \
  121. {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
  122. #endif
  123. #ifndef WSAID_CONNECTEX
  124. #define WSAID_CONNECTEX \
  125. {0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}
  126. #endif
  127. #ifndef WSAID_GETACCEPTEXSOCKADDRS
  128. #define WSAID_GETACCEPTEXSOCKADDRS \
  129. {0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
  130. #endif
  131. static int extension_fns_initialized = 0;
  132. static void
  133. init_extension_functions(struct win32_extension_fns *ext)
  134. {
  135. const GUID acceptex = WSAID_ACCEPTEX;
  136. const GUID connectex = WSAID_CONNECTEX;
  137. const GUID getacceptexsockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
  138. SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
  139. if (s == EVUTIL_INVALID_SOCKET)
  140. return;
  141. ext->AcceptEx = get_extension_function(s, &acceptex);
  142. ext->ConnectEx = get_extension_function(s, &connectex);
  143. ext->GetAcceptExSockaddrs = get_extension_function(s,
  144. &getacceptexsockaddrs);
  145. closesocket(s);
  146. extension_fns_initialized = 1;
  147. }
  148. static struct win32_extension_fns the_extension_fns;
  149. const struct win32_extension_fns *
  150. event_get_win32_extension_fns_(void)
  151. {
  152. return &the_extension_fns;
  153. }
  154. #define N_CPUS_DEFAULT 2
  155. struct event_iocp_port *
  156. event_iocp_port_launch_(int n_cpus)
  157. {
  158. struct event_iocp_port *port;
  159. int i;
  160. if (!extension_fns_initialized)
  161. init_extension_functions(&the_extension_fns);
  162. if (!(port = mm_calloc(1, sizeof(struct event_iocp_port))))
  163. return NULL;
  164. if (n_cpus <= 0)
  165. n_cpus = N_CPUS_DEFAULT;
  166. port->n_threads = n_cpus * 2;
  167. port->threads = mm_calloc(port->n_threads, sizeof(HANDLE));
  168. if (!port->threads)
  169. goto err;
  170. port->port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0,
  171. n_cpus);
  172. port->ms = -1;
  173. if (!port->port)
  174. goto err;
  175. port->shutdownSemaphore = CreateSemaphore(NULL, 0, 1, NULL);
  176. if (!port->shutdownSemaphore)
  177. goto err;
  178. for (i=0; i<port->n_threads; ++i) {
  179. ev_uintptr_t th = _beginthread(loop, 0, port);
  180. if (th == (ev_uintptr_t)-1)
  181. goto err;
  182. port->threads[i] = (HANDLE)th;
  183. ++port->n_live_threads;
  184. }
  185. InitializeCriticalSectionAndSpinCount(&port->lock, 1000);
  186. return port;
  187. err:
  188. if (port->port)
  189. CloseHandle(port->port);
  190. if (port->threads)
  191. mm_free(port->threads);
  192. if (port->shutdownSemaphore)
  193. CloseHandle(port->shutdownSemaphore);
  194. mm_free(port);
  195. return NULL;
  196. }
  197. static void
  198. event_iocp_port_unlock_and_free_(struct event_iocp_port *port)
  199. {
  200. DeleteCriticalSection(&port->lock);
  201. CloseHandle(port->port);
  202. CloseHandle(port->shutdownSemaphore);
  203. mm_free(port->threads);
  204. mm_free(port);
  205. }
  206. static int
  207. event_iocp_notify_all(struct event_iocp_port *port)
  208. {
  209. int i, r, ok=1;
  210. for (i=0; i<port->n_threads; ++i) {
  211. r = PostQueuedCompletionStatus(port->port, 0, NOTIFICATION_KEY,
  212. NULL);
  213. if (!r)
  214. ok = 0;
  215. }
  216. return ok ? 0 : -1;
  217. }
  218. int
  219. event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec)
  220. {
  221. DWORD ms = INFINITE;
  222. int n;
  223. EnterCriticalSection(&port->lock);
  224. port->shutdown = 1;
  225. LeaveCriticalSection(&port->lock);
  226. event_iocp_notify_all(port);
  227. if (waitMsec >= 0)
  228. ms = waitMsec;
  229. WaitForSingleObject(port->shutdownSemaphore, ms);
  230. EnterCriticalSection(&port->lock);
  231. n = port->n_live_threads;
  232. LeaveCriticalSection(&port->lock);
  233. if (n == 0) {
  234. event_iocp_port_unlock_and_free_(port);
  235. return 0;
  236. } else {
  237. return -1;
  238. }
  239. }
  240. int
  241. event_iocp_activate_overlapped_(
  242. struct event_iocp_port *port, struct event_overlapped *o,
  243. ev_uintptr_t key, ev_uint32_t n)
  244. {
  245. BOOL r;
  246. r = PostQueuedCompletionStatus(port->port, n, key, &o->overlapped);
  247. return (r==0) ? -1 : 0;
  248. }
  249. struct event_iocp_port *
  250. event_base_get_iocp_(struct event_base *base)
  251. {
  252. #ifdef _WIN32
  253. return base->iocp;
  254. #else
  255. return NULL;
  256. #endif
  257. }