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