michael@0: /* michael@0: * Copyright 2007-2012 Niels Provos and Nick Mathewson michael@0: * Copyright 2000-2007 Niels Provos michael@0: * Copyright 2003 Michael A. Davis michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/util.h" michael@0: #include "event2/event-config.h" michael@0: #include "util-internal.h" michael@0: #include "log-internal.h" michael@0: #include "event2/event.h" michael@0: #include "event-internal.h" michael@0: #include "evmap-internal.h" michael@0: #include "event2/thread.h" michael@0: #include "evthread-internal.h" michael@0: michael@0: #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0) michael@0: michael@0: extern struct event_list timequeue; michael@0: extern struct event_list addqueue; michael@0: michael@0: struct win_fd_set { michael@0: u_int fd_count; michael@0: SOCKET fd_array[1]; michael@0: }; michael@0: michael@0: /* MSDN says this is required to handle SIGFPE */ michael@0: volatile double SIGFPE_REQ = 0.0f; michael@0: michael@0: struct idx_info { michael@0: int read_pos_plus1; michael@0: int write_pos_plus1; michael@0: }; michael@0: michael@0: struct win32op { michael@0: unsigned num_fds_in_fd_sets; michael@0: int resize_out_sets; michael@0: struct win_fd_set *readset_in; michael@0: struct win_fd_set *writeset_in; michael@0: struct win_fd_set *readset_out; michael@0: struct win_fd_set *writeset_out; michael@0: struct win_fd_set *exset_out; michael@0: unsigned signals_are_broken : 1; michael@0: }; michael@0: michael@0: static void *win32_init(struct event_base *); michael@0: static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *_idx); michael@0: static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *_idx); michael@0: static int win32_dispatch(struct event_base *base, struct timeval *); michael@0: static void win32_dealloc(struct event_base *); michael@0: michael@0: struct eventop win32ops = { michael@0: "win32", michael@0: win32_init, michael@0: win32_add, michael@0: win32_del, michael@0: win32_dispatch, michael@0: win32_dealloc, michael@0: 0, /* doesn't need reinit */ michael@0: 0, /* No features supported. */ michael@0: sizeof(struct idx_info), michael@0: }; michael@0: michael@0: #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET))) michael@0: michael@0: static int michael@0: grow_fd_sets(struct win32op *op, unsigned new_num_fds) michael@0: { michael@0: size_t size; michael@0: michael@0: EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count && michael@0: new_num_fds >= op->writeset_in->fd_count); michael@0: EVUTIL_ASSERT(new_num_fds >= 1); michael@0: michael@0: size = FD_SET_ALLOC_SIZE(new_num_fds); michael@0: if (!(op->readset_in = mm_realloc(op->readset_in, size))) michael@0: return (-1); michael@0: if (!(op->writeset_in = mm_realloc(op->writeset_in, size))) michael@0: return (-1); michael@0: op->resize_out_sets = 1; michael@0: op->num_fds_in_fd_sets = new_num_fds; michael@0: return (0); michael@0: } michael@0: michael@0: static int michael@0: do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read) michael@0: { michael@0: struct win_fd_set *set = read ? op->readset_in : op->writeset_in; michael@0: if (read) { michael@0: if (ent->read_pos_plus1 > 0) michael@0: return (0); michael@0: } else { michael@0: if (ent->write_pos_plus1 > 0) michael@0: return (0); michael@0: } michael@0: if (set->fd_count == op->num_fds_in_fd_sets) { michael@0: if (grow_fd_sets(op, op->num_fds_in_fd_sets*2)) michael@0: return (-1); michael@0: /* set pointer will have changed and needs reiniting! */ michael@0: set = read ? op->readset_in : op->writeset_in; michael@0: } michael@0: set->fd_array[set->fd_count] = s; michael@0: if (read) michael@0: ent->read_pos_plus1 = set->fd_count+1; michael@0: else michael@0: ent->write_pos_plus1 = set->fd_count+1; michael@0: return (set->fd_count++); michael@0: } michael@0: michael@0: static int michael@0: do_fd_clear(struct event_base *base, michael@0: struct win32op *op, struct idx_info *ent, int read) michael@0: { michael@0: int i; michael@0: struct win_fd_set *set = read ? op->readset_in : op->writeset_in; michael@0: if (read) { michael@0: i = ent->read_pos_plus1 - 1; michael@0: ent->read_pos_plus1 = 0; michael@0: } else { michael@0: i = ent->write_pos_plus1 - 1; michael@0: ent->write_pos_plus1 = 0; michael@0: } michael@0: if (i < 0) michael@0: return (0); michael@0: if (--set->fd_count != (unsigned)i) { michael@0: struct idx_info *ent2; michael@0: SOCKET s2; michael@0: s2 = set->fd_array[i] = set->fd_array[set->fd_count]; michael@0: michael@0: ent2 = evmap_io_get_fdinfo(&base->io, s2); michael@0: michael@0: if (!ent2) /* This indicates a bug. */ michael@0: return (0); michael@0: if (read) michael@0: ent2->read_pos_plus1 = i+1; michael@0: else michael@0: ent2->write_pos_plus1 = i+1; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: #define NEVENT 32 michael@0: void * michael@0: win32_init(struct event_base *_base) michael@0: { michael@0: struct win32op *winop; michael@0: size_t size; michael@0: if (!(winop = mm_calloc(1, sizeof(struct win32op)))) michael@0: return NULL; michael@0: winop->num_fds_in_fd_sets = NEVENT; michael@0: size = FD_SET_ALLOC_SIZE(NEVENT); michael@0: if (!(winop->readset_in = mm_malloc(size))) michael@0: goto err; michael@0: if (!(winop->writeset_in = mm_malloc(size))) michael@0: goto err; michael@0: if (!(winop->readset_out = mm_malloc(size))) michael@0: goto err; michael@0: if (!(winop->writeset_out = mm_malloc(size))) michael@0: goto err; michael@0: if (!(winop->exset_out = mm_malloc(size))) michael@0: goto err; michael@0: winop->readset_in->fd_count = winop->writeset_in->fd_count = 0; michael@0: winop->readset_out->fd_count = winop->writeset_out->fd_count michael@0: = winop->exset_out->fd_count = 0; michael@0: michael@0: if (evsig_init(_base) < 0) michael@0: winop->signals_are_broken = 1; michael@0: michael@0: return (winop); michael@0: err: michael@0: XFREE(winop->readset_in); michael@0: XFREE(winop->writeset_in); michael@0: XFREE(winop->readset_out); michael@0: XFREE(winop->writeset_out); michael@0: XFREE(winop->exset_out); michael@0: XFREE(winop); michael@0: return (NULL); michael@0: } michael@0: michael@0: int michael@0: win32_add(struct event_base *base, evutil_socket_t fd, michael@0: short old, short events, void *_idx) michael@0: { michael@0: struct win32op *win32op = base->evbase; michael@0: struct idx_info *idx = _idx; michael@0: michael@0: if ((events & EV_SIGNAL) && win32op->signals_are_broken) michael@0: return (-1); michael@0: michael@0: if (!(events & (EV_READ|EV_WRITE))) michael@0: return (0); michael@0: michael@0: event_debug(("%s: adding event for %d", __func__, (int)fd)); michael@0: if (events & EV_READ) { michael@0: if (do_fd_set(win32op, idx, fd, 1)<0) michael@0: return (-1); michael@0: } michael@0: if (events & EV_WRITE) { michael@0: if (do_fd_set(win32op, idx, fd, 0)<0) michael@0: return (-1); michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: win32_del(struct event_base *base, evutil_socket_t fd, short old, short events, michael@0: void *_idx) michael@0: { michael@0: struct win32op *win32op = base->evbase; michael@0: struct idx_info *idx = _idx; michael@0: michael@0: event_debug(("%s: Removing event for "EV_SOCK_FMT, michael@0: __func__, EV_SOCK_ARG(fd))); michael@0: if (events & EV_READ) michael@0: do_fd_clear(base, win32op, idx, 1); michael@0: if (events & EV_WRITE) michael@0: do_fd_clear(base, win32op, idx, 0); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in) michael@0: { michael@0: out->fd_count = in->fd_count; michael@0: memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET))); michael@0: } michael@0: michael@0: /* michael@0: static void dump_fd_set(struct win_fd_set *s) michael@0: { michael@0: unsigned int i; michael@0: printf("[ "); michael@0: for(i=0;ifd_count;++i) michael@0: printf("%d ",(int)s->fd_array[i]); michael@0: printf("]\n"); michael@0: } michael@0: */ michael@0: michael@0: int michael@0: win32_dispatch(struct event_base *base, struct timeval *tv) michael@0: { michael@0: struct win32op *win32op = base->evbase; michael@0: int res = 0; michael@0: unsigned j, i; michael@0: int fd_count; michael@0: SOCKET s; michael@0: michael@0: if (win32op->resize_out_sets) { michael@0: size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets); michael@0: if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size))) michael@0: return (-1); michael@0: if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size))) michael@0: return (-1); michael@0: if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size))) michael@0: return (-1); michael@0: win32op->resize_out_sets = 0; michael@0: } michael@0: michael@0: fd_set_copy(win32op->readset_out, win32op->readset_in); michael@0: fd_set_copy(win32op->exset_out, win32op->writeset_in); michael@0: fd_set_copy(win32op->writeset_out, win32op->writeset_in); michael@0: michael@0: fd_count = michael@0: (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ? michael@0: win32op->readset_out->fd_count : win32op->writeset_out->fd_count; michael@0: michael@0: if (!fd_count) { michael@0: long msec = tv ? evutil_tv_to_msec(tv) : LONG_MAX; michael@0: /* Sleep's DWORD argument is unsigned long */ michael@0: if (msec < 0) michael@0: msec = LONG_MAX; michael@0: /* Windows doesn't like you to call select() with no sockets */ michael@0: Sleep(msec); michael@0: return (0); michael@0: } michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: michael@0: res = select(fd_count, michael@0: (struct fd_set*)win32op->readset_out, michael@0: (struct fd_set*)win32op->writeset_out, michael@0: (struct fd_set*)win32op->exset_out, tv); michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: michael@0: event_debug(("%s: select returned %d", __func__, res)); michael@0: michael@0: if (res <= 0) { michael@0: return res; michael@0: } michael@0: michael@0: if (win32op->readset_out->fd_count) { michael@0: i = rand() % win32op->readset_out->fd_count; michael@0: for (j=0; jreadset_out->fd_count; ++j) { michael@0: if (++i >= win32op->readset_out->fd_count) michael@0: i = 0; michael@0: s = win32op->readset_out->fd_array[i]; michael@0: evmap_io_active(base, s, EV_READ); michael@0: } michael@0: } michael@0: if (win32op->exset_out->fd_count) { michael@0: i = rand() % win32op->exset_out->fd_count; michael@0: for (j=0; jexset_out->fd_count; ++j) { michael@0: if (++i >= win32op->exset_out->fd_count) michael@0: i = 0; michael@0: s = win32op->exset_out->fd_array[i]; michael@0: evmap_io_active(base, s, EV_WRITE); michael@0: } michael@0: } michael@0: if (win32op->writeset_out->fd_count) { michael@0: SOCKET s; michael@0: i = rand() % win32op->writeset_out->fd_count; michael@0: for (j=0; jwriteset_out->fd_count; ++j) { michael@0: if (++i >= win32op->writeset_out->fd_count) michael@0: i = 0; michael@0: s = win32op->writeset_out->fd_array[i]; michael@0: evmap_io_active(base, s, EV_WRITE); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: win32_dealloc(struct event_base *_base) michael@0: { michael@0: struct win32op *win32op = _base->evbase; michael@0: michael@0: evsig_dealloc(_base); michael@0: if (win32op->readset_in) michael@0: mm_free(win32op->readset_in); michael@0: if (win32op->writeset_in) michael@0: mm_free(win32op->writeset_in); michael@0: if (win32op->readset_out) michael@0: mm_free(win32op->readset_out); michael@0: if (win32op->writeset_out) michael@0: mm_free(win32op->writeset_out); michael@0: if (win32op->exset_out) michael@0: mm_free(win32op->exset_out); michael@0: /* XXXXX free the tree. */ michael@0: michael@0: memset(win32op, 0, sizeof(win32op)); michael@0: mm_free(win32op); michael@0: }