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