xref: /freebsd/contrib/libevent/win32select.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 
31 #ifdef _WIN32
32 
33 #include <winsock2.h>
34 #include <windows.h>
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <limits.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <errno.h>
43 
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"
53 
54 #define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
55 
56 extern struct event_list timequeue;
57 extern struct event_list addqueue;
58 
59 struct win_fd_set {
60 	unsigned int fd_count;
61 	SOCKET fd_array[1];
62 };
63 
64 /* MSDN says this is required to handle SIGFPE */
65 volatile double SIGFPE_REQ = 0.0f;
66 
67 struct idx_info {
68 	int read_pos_plus1;
69 	int write_pos_plus1;
70 };
71 
72 struct win32op {
73 	unsigned num_fds_in_fd_sets;
74 	int resize_out_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;
81 };
82 
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 *);
88 
89 struct eventop win32ops = {
90 	"win32",
91 	win32_init,
92 	win32_add,
93 	win32_del,
94 	win32_dispatch,
95 	win32_dealloc,
96 	0, /* doesn't need reinit */
97 	0, /* No features supported. */
98 	sizeof(struct idx_info),
99 };
100 
101 #define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))
102 
103 static int
104 grow_fd_sets(struct win32op *op, unsigned new_num_fds)
105 {
106 	size_t size;
107 
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);
111 
112 	size = FD_SET_ALLOC_SIZE(new_num_fds);
113 	if (!(op->readset_in = mm_realloc(op->readset_in, size)))
114 		return (-1);
115 	if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
116 		return (-1);
117 	op->resize_out_sets = 1;
118 	op->num_fds_in_fd_sets = new_num_fds;
119 	return (0);
120 }
121 
122 static int
123 do_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
124 {
125 	struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
126 	if (read) {
127 		if (ent->read_pos_plus1 > 0)
128 			return (0);
129 	} else {
130 		if (ent->write_pos_plus1 > 0)
131 			return (0);
132 	}
133 	if (set->fd_count == op->num_fds_in_fd_sets) {
134 		if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
135 			return (-1);
136 		/* set pointer will have changed and needs reiniting! */
137 		set = read ? op->readset_in : op->writeset_in;
138 	}
139 	set->fd_array[set->fd_count] = s;
140 	if (read)
141 		ent->read_pos_plus1 = set->fd_count+1;
142 	else
143 		ent->write_pos_plus1 = set->fd_count+1;
144 	return (set->fd_count++);
145 }
146 
147 static int
148 do_fd_clear(struct event_base *base,
149 			struct win32op *op, struct idx_info *ent, int read)
150 {
151 	int i;
152 	struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
153 	if (read) {
154 		i = ent->read_pos_plus1 - 1;
155 		ent->read_pos_plus1 = 0;
156 	} else {
157 		i = ent->write_pos_plus1 - 1;
158 		ent->write_pos_plus1 = 0;
159 	}
160 	if (i < 0)
161 		return (0);
162 	if (--set->fd_count != (unsigned)i) {
163 		struct idx_info *ent2;
164 		SOCKET s2;
165 		s2 = set->fd_array[i] = set->fd_array[set->fd_count];
166 
167 		ent2 = evmap_io_get_fdinfo_(&base->io, s2);
168 
169 		if (!ent2) /* This indicates a bug. */
170 			return (0);
171 		if (read)
172 			ent2->read_pos_plus1 = i+1;
173 		else
174 			ent2->write_pos_plus1 = i+1;
175 	}
176 	return (0);
177 }
178 
179 #define NEVENT 32
180 void *
181 win32_init(struct event_base *base)
182 {
183 	struct win32op *winop;
184 	size_t size;
185 	if (!(winop = mm_calloc(1, sizeof(struct win32op))))
186 		return NULL;
187 	winop->num_fds_in_fd_sets = NEVENT;
188 	size = FD_SET_ALLOC_SIZE(NEVENT);
189 	if (!(winop->readset_in = mm_malloc(size)))
190 		goto err;
191 	if (!(winop->writeset_in = mm_malloc(size)))
192 		goto err;
193 	if (!(winop->readset_out = mm_malloc(size)))
194 		goto err;
195 	if (!(winop->writeset_out = mm_malloc(size)))
196 		goto err;
197 	if (!(winop->exset_out = mm_malloc(size)))
198 		goto err;
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;
202 
203 	if (evsig_init_(base) < 0)
204 		winop->signals_are_broken = 1;
205 
206 	evutil_weakrand_seed_(&base->weakrand_seed, 0);
207 
208 	return (winop);
209  err:
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);
215 	XFREE(winop);
216 	return (NULL);
217 }
218 
219 int
220 win32_add(struct event_base *base, evutil_socket_t fd,
221 			 short old, short events, void *idx_)
222 {
223 	struct win32op *win32op = base->evbase;
224 	struct idx_info *idx = idx_;
225 
226 	if ((events & EV_SIGNAL) && win32op->signals_are_broken)
227 		return (-1);
228 
229 	if (!(events & (EV_READ|EV_WRITE)))
230 		return (0);
231 
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)
235 			return (-1);
236 	}
237 	if (events & EV_WRITE) {
238 		if (do_fd_set(win32op, idx, fd, 0)<0)
239 			return (-1);
240 	}
241 	return (0);
242 }
243 
244 int
245 win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
246 		  void *idx_)
247 {
248 	struct win32op *win32op = base->evbase;
249 	struct idx_info *idx = idx_;
250 
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);
257 
258 	return 0;
259 }
260 
261 static void
262 fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
263 {
264 	out->fd_count = in->fd_count;
265 	memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
266 }
267 
268 /*
269   static void dump_fd_set(struct win_fd_set *s)
270   {
271   unsigned int i;
272   printf("[ ");
273   for(i=0;i<s->fd_count;++i)
274   printf("%d ",(int)s->fd_array[i]);
275   printf("]\n");
276   }
277 */
278 
279 int
280 win32_dispatch(struct event_base *base, struct timeval *tv)
281 {
282 	struct win32op *win32op = base->evbase;
283 	int res = 0;
284 	unsigned j, i;
285 	int fd_count;
286 	SOCKET s;
287 
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)))
291 			return (-1);
292 		if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
293 			return (-1);
294 		if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
295 			return (-1);
296 		win32op->resize_out_sets = 0;
297 	}
298 
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);
302 
303 	fd_count =
304 	    (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
305 	    win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
306 
307 	if (!fd_count) {
308 		long msec = tv ? evutil_tv_to_msec_(tv) : LONG_MAX;
309 		/* Sleep's DWORD argument is unsigned long */
310 		if (msec < 0)
311 			msec = LONG_MAX;
312 		/* Windows doesn't like you to call select() with no sockets */
313 		Sleep(msec);
314 		return (0);
315 	}
316 
317 	EVBASE_RELEASE_LOCK(base, th_base_lock);
318 
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);
323 
324 	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
325 
326 	event_debug(("%s: select returned %d", __func__, res));
327 
328 	if (res <= 0) {
329 		return res;
330 	}
331 
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)
337 				i = 0;
338 			s = win32op->readset_out->fd_array[i];
339 			evmap_io_active_(base, s, EV_READ);
340 		}
341 	}
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)
347 				i = 0;
348 			s = win32op->exset_out->fd_array[i];
349 			evmap_io_active_(base, s, EV_WRITE);
350 		}
351 	}
352 	if (win32op->writeset_out->fd_count) {
353 		SOCKET s;
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)
358 				i = 0;
359 			s = win32op->writeset_out->fd_array[i];
360 			evmap_io_active_(base, s, EV_WRITE);
361 		}
362 	}
363 	return (0);
364 }
365 
366 void
367 win32_dealloc(struct event_base *base)
368 {
369 	struct win32op *win32op = base->evbase;
370 
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. */
383 
384 	memset(win32op, 0, sizeof(*win32op));
385 	mm_free(win32op);
386 }
387 
388 #endif
389