1 /*****************************************************************************
2 * poll.c: I/O event multiplexing
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * Author: Rémi Denis-Courmont
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
31 # include <vlc_network.h>
36 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
38 struct timespec tsbuf, *ts;
44 div_t d = div (timeout, 1000);
45 tsbuf.tv_sec = d.quot;
46 tsbuf.tv_nsec = d.rem * 1000000;
52 pthread_sigmask (SIG_BLOCK, NULL, &set);
53 sigdelset (&set, SIGRTMIN);
55 canc = vlc_savecancel ();
56 ret = ppoll (fds, nfds, ts, &set);
57 vlc_restorecancel (canc);
63 #elif defined (HAVE_POLL)
64 # include <vlc_network.h>
68 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
70 (void)fds; (void)nfds; (void)timeout;
80 /* No, it's not as simple as #undef FD_SETSIZE */
81 # error Header inclusion order compromised!
84 #include <vlc_network.h>
86 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
88 size_t setsize = sizeof (fd_set) + nfds * sizeof (SOCKET);
89 fd_set *rdset = malloc (setsize);
90 fd_set *wrset = malloc (setsize);
91 fd_set *exset = malloc (setsize);
92 struct timeval tv = { 0, 0 };
95 if (unlikely(rdset == NULL || wrset == NULL || exset == NULL))
104 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
106 #define FD_SETSIZE (nfds)
115 for (unsigned i = 0; i < nfds; i++)
121 /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
122 * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
123 * uses poll() rather than select(). Most POSIX systems implement
124 * fd_set has a bit field with no sanity checks. This is especially bad
125 * on systems (such as BSD) that have no process open files limit by
126 * default, such that it is quite feasible to get fd >= FD_SETSIZE.
127 * The next instructions will result in a buffer overflow if run on
128 * a POSIX system, and the later FD_ISSET would perform an undefined
131 * With Winsock, fd_set is a table of integers. This is awfully slow.
132 * However, FD_SET and FD_ISSET silently and safely discard excess
133 * entries. Here, overflow cannot happen anyway: fd_set of adequate
134 * size are allocated.
135 * Note that Vista has a much nicer WSAPoll(), but Mingw does not
138 if (fds[i].events & POLLIN)
139 FD_SET ((SOCKET)fd, rdset);
140 if (fds[i].events & POLLOUT)
141 FD_SET ((SOCKET)fd, wrset);
142 if (fds[i].events & POLLPRI)
143 FD_SET ((SOCKET)fd, exset);
146 #ifndef HAVE_ALERTABLE_SELECT
147 # warning FIXME! Fix cancellation and remove this crap.
148 if ((timeout < 0) || (timeout > 50))
157 div_t d = div (timeout, 1000);
159 tv.tv_usec = d.rem * 1000;
162 val = select (val + 1, rdset, wrset, exset,
163 /*(timeout >= 0) ?*/ &tv /*: NULL*/);
165 #ifndef HAVE_ALERTABLE_SELECT
169 timeout -= (timeout > 50) ? 50 : timeout;
178 for (unsigned i = 0; i < nfds; i++)
181 fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
182 | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
183 | (FD_ISSET (fd, exset) ? POLLPRI : 0);
192 # error poll() not implemented!