1 /*****************************************************************************
2 * poll.c: poll() emulation
3 *****************************************************************************
4 * Copyright © 2007-2012 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
31 /* Too late for #undef FD_SETSIZE to work: fd_set is already defined. */
32 # error Header inclusion order compromised!
35 # include <winsock2.h>
37 # include <sys/time.h>
38 # include <sys/select.h>
41 int (poll) (struct pollfd *fds, unsigned nfds, int timeout)
44 size_t setsize = sizeof (fd_set) + nfds * sizeof (SOCKET);
45 fd_set *rdset = malloc (setsize);
46 fd_set *wrset = malloc (setsize);
47 fd_set *exset = malloc (setsize);
49 if (rdset == NULL || wrset == NULL || exset == NULL)
57 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
59 # define FD_SETSIZE (nfds)
61 fd_set rdset[1], wrset[1], exset[1];
63 struct timeval tv = { 0, 0 };
69 for (unsigned i = 0; i < nfds; i++)
75 /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
76 * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
77 * uses poll() rather than select(). Most POSIX systems implement
78 * fd_set has a bit field with no sanity checks. This is especially bad
79 * on systems (such as BSD) that have no process open files limit by
80 * default, such that it is quite feasible to get fd >= FD_SETSIZE.
81 * The next instructions will result in a buffer overflow if run on
82 * a POSIX system, and the later FD_ISSET would perform an undefined
85 * With Winsock, fd_set is a table of integers. This is awfully slow.
86 * However, FD_SET and FD_ISSET silently and safely discard excess
87 * entries. Here, overflow cannot happen anyway: fd_set of adequate
89 * Note that Vista has a much nicer WSAPoll(), but Mingw does not
93 if ((unsigned)fd >= FD_SETSIZE)
99 if (fds[i].events & POLLIN)
101 if (fds[i].events & POLLOUT)
103 if (fds[i].events & POLLPRI)
109 div_t d = div (timeout, 1000);
111 tv.tv_usec = d.rem * 1000;
114 val = select (val + 1, rdset, wrset, exset,
115 (timeout >= 0) ? &tv : NULL);
119 for (unsigned i = 0; i < nfds; i++)
122 fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
123 | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
124 | (FD_ISSET (fd, exset) ? POLLPRI : 0);