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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
35 #include <sys/types.h>
36 #include <sys/queue.h>
44 #include "event2/util.h"
45 #include "util-internal.h"
46 #include "log-internal.h"
47 #include "event2/event.h"
48 #include "event-internal.h"
49 #include "evmap-internal.h"
50 #include "event2/thread.h"
51 #include "evthread-internal.h"
52 #include "time-internal.h"
54 #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
56 extern struct event_list timequeue;
57 extern struct event_list addqueue;
60 unsigned int fd_count;
64 /* MSDN says this is required to handle SIGFPE */
65 volatile double SIGFPE_REQ = 0.0f;
73 unsigned num_fds_in_fd_sets;
75 struct win_fd_set *readset_in;
76 struct win_fd_set *writeset_in;
77 struct win_fd_set *readset_out;
78 struct win_fd_set *writeset_out;
79 struct win_fd_set *exset_out;
80 unsigned signals_are_broken : 1;
83 static void *win32_init(struct event_base *);
84 static int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
85 static int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
86 static int win32_dispatch(struct event_base *base, struct timeval *);
87 static void win32_dealloc(struct event_base *);
89 struct eventop win32ops = {
96 0, /* doesn't need reinit */
97 0, /* No features supported. */
98 sizeof(struct idx_info),
101 #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))
104 grow_fd_sets(struct win32op *op, unsigned new_num_fds)
108 EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count &&
109 new_num_fds >= op->writeset_in->fd_count);
110 EVUTIL_ASSERT(new_num_fds >= 1);
112 size = FD_SET_ALLOC_SIZE(new_num_fds);
113 if (!(op->readset_in = mm_realloc(op->readset_in, size)))
115 if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
117 op->resize_out_sets = 1;
118 op->num_fds_in_fd_sets = new_num_fds;
123 do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
125 struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
127 if (ent->read_pos_plus1 > 0)
130 if (ent->write_pos_plus1 > 0)
133 if (set->fd_count == op->num_fds_in_fd_sets) {
134 if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
136 /* set pointer will have changed and needs reiniting! */
137 set = read ? op->readset_in : op->writeset_in;
139 set->fd_array[set->fd_count] = s;
141 ent->read_pos_plus1 = set->fd_count+1;
143 ent->write_pos_plus1 = set->fd_count+1;
144 return (set->fd_count++);
148 do_fd_clear(struct event_base *base,
149 struct win32op *op, struct idx_info *ent, int read)
152 struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
154 i = ent->read_pos_plus1 - 1;
155 ent->read_pos_plus1 = 0;
157 i = ent->write_pos_plus1 - 1;
158 ent->write_pos_plus1 = 0;
162 if (--set->fd_count != (unsigned)i) {
163 struct idx_info *ent2;
165 s2 = set->fd_array[i] = set->fd_array[set->fd_count];
167 ent2 = evmap_io_get_fdinfo_(&base->io, s2);
169 if (!ent2) /* This indicates a bug. */
172 ent2->read_pos_plus1 = i+1;
174 ent2->write_pos_plus1 = i+1;
181 win32_init(struct event_base *base)
183 struct win32op *winop;
185 if (!(winop = mm_calloc(1, sizeof(struct win32op))))
187 winop->num_fds_in_fd_sets = NEVENT;
188 size = FD_SET_ALLOC_SIZE(NEVENT);
189 if (!(winop->readset_in = mm_malloc(size)))
191 if (!(winop->writeset_in = mm_malloc(size)))
193 if (!(winop->readset_out = mm_malloc(size)))
195 if (!(winop->writeset_out = mm_malloc(size)))
197 if (!(winop->exset_out = mm_malloc(size)))
199 winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
200 winop->readset_out->fd_count = winop->writeset_out->fd_count
201 = winop->exset_out->fd_count = 0;
203 if (evsig_init_(base) < 0)
204 winop->signals_are_broken = 1;
206 evutil_weakrand_seed_(&base->weakrand_seed, 0);
210 XFREE(winop->readset_in);
211 XFREE(winop->writeset_in);
212 XFREE(winop->readset_out);
213 XFREE(winop->writeset_out);
214 XFREE(winop->exset_out);
220 win32_add(struct event_base *base, evutil_socket_t fd,
221 short old, short events, void *idx_)
223 struct win32op *win32op = base->evbase;
224 struct idx_info *idx = idx_;
226 if ((events & EV_SIGNAL) && win32op->signals_are_broken)
229 if (!(events & (EV_READ|EV_WRITE)))
232 event_debug(("%s: adding event for %d", __func__, (int)fd));
233 if (events & EV_READ) {
234 if (do_fd_set(win32op, idx, fd, 1)<0)
237 if (events & EV_WRITE) {
238 if (do_fd_set(win32op, idx, fd, 0)<0)
245 win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
248 struct win32op *win32op = base->evbase;
249 struct idx_info *idx = idx_;
251 event_debug(("%s: Removing event for "EV_SOCK_FMT,
252 __func__, EV_SOCK_ARG(fd)));
253 if (events & EV_READ)
254 do_fd_clear(base, win32op, idx, 1);
255 if (events & EV_WRITE)
256 do_fd_clear(base, win32op, idx, 0);
262 fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
264 out->fd_count = in->fd_count;
265 memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
269 static void dump_fd_set(struct win_fd_set *s)
273 for(i=0;i<s->fd_count;++i)
274 printf("%d ",(int)s->fd_array[i]);
280 win32_dispatch(struct event_base *base, struct timeval *tv)
282 struct win32op *win32op = base->evbase;
288 if (win32op->resize_out_sets) {
289 size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets);
290 if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size)))
292 if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
294 if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
296 win32op->resize_out_sets = 0;
299 fd_set_copy(win32op->readset_out, win32op->readset_in);
300 fd_set_copy(win32op->exset_out, win32op->writeset_in);
301 fd_set_copy(win32op->writeset_out, win32op->writeset_in);
304 (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
305 win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
308 long msec = tv ? evutil_tv_to_msec_(tv) : LONG_MAX;
309 /* Sleep's DWORD argument is unsigned long */
312 /* Windows doesn't like you to call select() with no sockets */
317 EVBASE_RELEASE_LOCK(base, th_base_lock);
319 res = select(fd_count,
320 (struct fd_set*)win32op->readset_out,
321 (struct fd_set*)win32op->writeset_out,
322 (struct fd_set*)win32op->exset_out, tv);
324 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
326 event_debug(("%s: select returned %d", __func__, res));
332 if (win32op->readset_out->fd_count) {
333 i = evutil_weakrand_range_(&base->weakrand_seed,
334 win32op->readset_out->fd_count);
335 for (j=0; j<win32op->readset_out->fd_count; ++j) {
336 if (++i >= win32op->readset_out->fd_count)
338 s = win32op->readset_out->fd_array[i];
339 evmap_io_active_(base, s, EV_READ);
342 if (win32op->exset_out->fd_count) {
343 i = evutil_weakrand_range_(&base->weakrand_seed,
344 win32op->exset_out->fd_count);
345 for (j=0; j<win32op->exset_out->fd_count; ++j) {
346 if (++i >= win32op->exset_out->fd_count)
348 s = win32op->exset_out->fd_array[i];
349 evmap_io_active_(base, s, EV_WRITE);
352 if (win32op->writeset_out->fd_count) {
354 i = evutil_weakrand_range_(&base->weakrand_seed,
355 win32op->writeset_out->fd_count);
356 for (j=0; j<win32op->writeset_out->fd_count; ++j) {
357 if (++i >= win32op->writeset_out->fd_count)
359 s = win32op->writeset_out->fd_array[i];
360 evmap_io_active_(base, s, EV_WRITE);
367 win32_dealloc(struct event_base *base)
369 struct win32op *win32op = base->evbase;
371 evsig_dealloc_(base);
372 if (win32op->readset_in)
373 mm_free(win32op->readset_in);
374 if (win32op->writeset_in)
375 mm_free(win32op->writeset_in);
376 if (win32op->readset_out)
377 mm_free(win32op->readset_out);
378 if (win32op->writeset_out)
379 mm_free(win32op->writeset_out);
380 if (win32op->exset_out)
381 mm_free(win32op->exset_out);
382 /* XXXXX free the tree. */
384 memset(win32op, 0, sizeof(*win32op));