win32select.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright 2007-2012 Niels Provos and Nick Mathewson
  3. * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
  4. * Copyright 2003 Michael A. Davis <mike@datanerds.net>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "event2/event-config.h"
  29. #include "evconfig-private.h"
  30. #ifdef _WIN32
  31. #include <winsock2.h>
  32. #include <windows.h>
  33. #include <sys/types.h>
  34. #include <sys/queue.h>
  35. #include <limits.h>
  36. #include <signal.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include "event2/util.h"
  42. #include "util-internal.h"
  43. #include "log-internal.h"
  44. #include "event2/event.h"
  45. #include "event-internal.h"
  46. #include "evmap-internal.h"
  47. #include "event2/thread.h"
  48. #include "evthread-internal.h"
  49. #include "time-internal.h"
  50. #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
  51. extern struct event_list timequeue;
  52. extern struct event_list addqueue;
  53. struct win_fd_set {
  54. unsigned int fd_count;
  55. SOCKET fd_array[1];
  56. };
  57. /* MSDN says this is required to handle SIGFPE */
  58. volatile double SIGFPE_REQ = 0.0f;
  59. struct idx_info {
  60. int read_pos_plus1;
  61. int write_pos_plus1;
  62. };
  63. struct win32op {
  64. unsigned num_fds_in_fd_sets;
  65. int resize_out_sets;
  66. struct win_fd_set *readset_in;
  67. struct win_fd_set *writeset_in;
  68. struct win_fd_set *readset_out;
  69. struct win_fd_set *writeset_out;
  70. struct win_fd_set *exset_out;
  71. unsigned signals_are_broken : 1;
  72. };
  73. static void *win32_init(struct event_base *);
  74. static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
  75. static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
  76. static int win32_dispatch(struct event_base *base, struct timeval *);
  77. static void win32_dealloc(struct event_base *);
  78. struct eventop win32ops = {
  79. "win32",
  80. win32_init,
  81. win32_add,
  82. win32_del,
  83. win32_dispatch,
  84. win32_dealloc,
  85. 0, /* doesn't need reinit */
  86. 0, /* No features supported. */
  87. sizeof(struct idx_info),
  88. };
  89. #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))
  90. static int
  91. grow_fd_sets(struct win32op *op, unsigned new_num_fds)
  92. {
  93. size_t size;
  94. EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count &&
  95. new_num_fds >= op->writeset_in->fd_count);
  96. EVUTIL_ASSERT(new_num_fds >= 1);
  97. size = FD_SET_ALLOC_SIZE(new_num_fds);
  98. if (!(op->readset_in = mm_realloc(op->readset_in, size)))
  99. return (-1);
  100. if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
  101. return (-1);
  102. op->resize_out_sets = 1;
  103. op->num_fds_in_fd_sets = new_num_fds;
  104. return (0);
  105. }
  106. static int
  107. do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
  108. {
  109. struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
  110. if (read) {
  111. if (ent->read_pos_plus1 > 0)
  112. return (0);
  113. } else {
  114. if (ent->write_pos_plus1 > 0)
  115. return (0);
  116. }
  117. if (set->fd_count == op->num_fds_in_fd_sets) {
  118. if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
  119. return (-1);
  120. /* set pointer will have changed and needs reiniting! */
  121. set = read ? op->readset_in : op->writeset_in;
  122. }
  123. set->fd_array[set->fd_count] = s;
  124. if (read)
  125. ent->read_pos_plus1 = set->fd_count+1;
  126. else
  127. ent->write_pos_plus1 = set->fd_count+1;
  128. return (set->fd_count++);
  129. }
  130. static int
  131. do_fd_clear(struct event_base *base,
  132. struct win32op *op, struct idx_info *ent, int read)
  133. {
  134. int i;
  135. struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
  136. if (read) {
  137. i = ent->read_pos_plus1 - 1;
  138. ent->read_pos_plus1 = 0;
  139. } else {
  140. i = ent->write_pos_plus1 - 1;
  141. ent->write_pos_plus1 = 0;
  142. }
  143. if (i < 0)
  144. return (0);
  145. if (--set->fd_count != (unsigned)i) {
  146. struct idx_info *ent2;
  147. SOCKET s2;
  148. s2 = set->fd_array[i] = set->fd_array[set->fd_count];
  149. ent2 = evmap_io_get_fdinfo_(&base->io, s2);
  150. if (!ent2) /* This indicates a bug. */
  151. return (0);
  152. if (read)
  153. ent2->read_pos_plus1 = i+1;
  154. else
  155. ent2->write_pos_plus1 = i+1;
  156. }
  157. return (0);
  158. }
  159. #define NEVENT 32
  160. void *
  161. win32_init(struct event_base *base)
  162. {
  163. struct win32op *winop;
  164. size_t size;
  165. if (!(winop = mm_calloc(1, sizeof(struct win32op))))
  166. return NULL;
  167. winop->num_fds_in_fd_sets = NEVENT;
  168. size = FD_SET_ALLOC_SIZE(NEVENT);
  169. if (!(winop->readset_in = mm_malloc(size)))
  170. goto err;
  171. if (!(winop->writeset_in = mm_malloc(size)))
  172. goto err;
  173. if (!(winop->readset_out = mm_malloc(size)))
  174. goto err;
  175. if (!(winop->writeset_out = mm_malloc(size)))
  176. goto err;
  177. if (!(winop->exset_out = mm_malloc(size)))
  178. goto err;
  179. winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
  180. winop->readset_out->fd_count = winop->writeset_out->fd_count
  181. = winop->exset_out->fd_count = 0;
  182. if (evsig_init_(base) < 0)
  183. winop->signals_are_broken = 1;
  184. evutil_weakrand_seed_(&base->weakrand_seed, 0);
  185. return (winop);
  186. err:
  187. XFREE(winop->readset_in);
  188. XFREE(winop->writeset_in);
  189. XFREE(winop->readset_out);
  190. XFREE(winop->writeset_out);
  191. XFREE(winop->exset_out);
  192. XFREE(winop);
  193. return (NULL);
  194. }
  195. int
  196. win32_add(struct event_base *base, evutil_socket_t fd,
  197. short old, short events, void *idx_)
  198. {
  199. struct win32op *win32op = base->evbase;
  200. struct idx_info *idx = idx_;
  201. if ((events & EV_SIGNAL) && win32op->signals_are_broken)
  202. return (-1);
  203. if (!(events & (EV_READ|EV_WRITE)))
  204. return (0);
  205. event_debug(("%s: adding event for %d", __func__, (int)fd));
  206. if (events & EV_READ) {
  207. if (do_fd_set(win32op, idx, fd, 1)<0)
  208. return (-1);
  209. }
  210. if (events & EV_WRITE) {
  211. if (do_fd_set(win32op, idx, fd, 0)<0)
  212. return (-1);
  213. }
  214. return (0);
  215. }
  216. int
  217. win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
  218. void *idx_)
  219. {
  220. struct win32op *win32op = base->evbase;
  221. struct idx_info *idx = idx_;
  222. event_debug(("%s: Removing event for "EV_SOCK_FMT,
  223. __func__, EV_SOCK_ARG(fd)));
  224. if (events & EV_READ)
  225. do_fd_clear(base, win32op, idx, 1);
  226. if (events & EV_WRITE)
  227. do_fd_clear(base, win32op, idx, 0);
  228. return 0;
  229. }
  230. static void
  231. fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
  232. {
  233. out->fd_count = in->fd_count;
  234. memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
  235. }
  236. /*
  237. static void dump_fd_set(struct win_fd_set *s)
  238. {
  239. unsigned int i;
  240. printf("[ ");
  241. for(i=0;i<s->fd_count;++i)
  242. printf("%d ",(int)s->fd_array[i]);
  243. printf("]\n");
  244. }
  245. */
  246. int
  247. win32_dispatch(struct event_base *base, struct timeval *tv)
  248. {
  249. struct win32op *win32op = base->evbase;
  250. int res = 0;
  251. unsigned j, i;
  252. int fd_count;
  253. SOCKET s;
  254. if (win32op->resize_out_sets) {
  255. size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets);
  256. if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size)))
  257. return (-1);
  258. if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
  259. return (-1);
  260. if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
  261. return (-1);
  262. win32op->resize_out_sets = 0;
  263. }
  264. fd_set_copy(win32op->readset_out, win32op->readset_in);
  265. fd_set_copy(win32op->exset_out, win32op->writeset_in);
  266. fd_set_copy(win32op->writeset_out, win32op->writeset_in);
  267. fd_count =
  268. (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
  269. win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
  270. if (!fd_count) {
  271. long msec = tv ? evutil_tv_to_msec_(tv) : LONG_MAX;
  272. /* Sleep's DWORD argument is unsigned long */
  273. if (msec < 0)
  274. msec = LONG_MAX;
  275. /* Windows doesn't like you to call select() with no sockets */
  276. Sleep(msec);
  277. return (0);
  278. }
  279. EVBASE_RELEASE_LOCK(base, th_base_lock);
  280. res = select(fd_count,
  281. (struct fd_set*)win32op->readset_out,
  282. (struct fd_set*)win32op->writeset_out,
  283. (struct fd_set*)win32op->exset_out, tv);
  284. EVBASE_ACQUIRE_LOCK(base, th_base_lock);
  285. event_debug(("%s: select returned %d", __func__, res));
  286. if (res <= 0) {
  287. event_debug(("%s: %s", __func__,
  288. evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())));
  289. return res;
  290. }
  291. if (win32op->readset_out->fd_count) {
  292. i = evutil_weakrand_range_(&base->weakrand_seed,
  293. win32op->readset_out->fd_count);
  294. for (j=0; j<win32op->readset_out->fd_count; ++j) {
  295. if (++i >= win32op->readset_out->fd_count)
  296. i = 0;
  297. s = win32op->readset_out->fd_array[i];
  298. evmap_io_active_(base, s, EV_READ);
  299. }
  300. }
  301. if (win32op->exset_out->fd_count) {
  302. i = evutil_weakrand_range_(&base->weakrand_seed,
  303. win32op->exset_out->fd_count);
  304. for (j=0; j<win32op->exset_out->fd_count; ++j) {
  305. if (++i >= win32op->exset_out->fd_count)
  306. i = 0;
  307. s = win32op->exset_out->fd_array[i];
  308. evmap_io_active_(base, s, EV_WRITE);
  309. }
  310. }
  311. if (win32op->writeset_out->fd_count) {
  312. i = evutil_weakrand_range_(&base->weakrand_seed,
  313. win32op->writeset_out->fd_count);
  314. for (j=0; j<win32op->writeset_out->fd_count; ++j) {
  315. if (++i >= win32op->writeset_out->fd_count)
  316. i = 0;
  317. s = win32op->writeset_out->fd_array[i];
  318. evmap_io_active_(base, s, EV_WRITE);
  319. }
  320. }
  321. return (0);
  322. }
  323. void
  324. win32_dealloc(struct event_base *base)
  325. {
  326. struct win32op *win32op = base->evbase;
  327. evsig_dealloc_(base);
  328. if (win32op->readset_in)
  329. mm_free(win32op->readset_in);
  330. if (win32op->writeset_in)
  331. mm_free(win32op->writeset_in);
  332. if (win32op->readset_out)
  333. mm_free(win32op->readset_out);
  334. if (win32op->writeset_out)
  335. mm_free(win32op->writeset_out);
  336. if (win32op->exset_out)
  337. mm_free(win32op->exset_out);
  338. /* XXXXX free the tree. */
  339. memset(win32op, 0, sizeof(*win32op));
  340. mm_free(win32op);
  341. }
  342. #endif