]> git.sesse.net Git - vlc/blob - src/network/poll.c
vlc_poll: no conditional symbols
[vlc] / src / network / poll.c
1 /*****************************************************************************
2  * poll.c: I/O event multiplexing
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Author: Rémi Denis-Courmont
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29
30 #ifdef HAVE_POLL
31 struct pollfd;
32
33 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
34 {
35     return poll (fds, nfds, timeout);
36 }
37 #else /* !HAVE_POLL */
38 #include <string.h>
39 #include <stdlib.h>
40 #include <vlc_network.h>
41
42 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
43 {
44     fd_set rdset, wrset, exset;
45     struct timeval tv = { 0, 0 };
46     int val = -1;
47
48     FD_ZERO (&rdset);
49     FD_ZERO (&wrset);
50     FD_ZERO (&exset);
51     for (unsigned i = 0; i < nfds; i++)
52     {
53         int fd = fds[i].fd;
54         if (val < fd)
55             val = fd;
56
57         /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
58          * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
59          * uses poll() rather than select(). Most POSIX systems implement
60          * fd_set has a bit field with no sanity checks. This is especially bad
61          * on systems (such as BSD) that have no process open files limit by
62          * default, such that it is quite feasible to get fd >= FD_SETSIZE.
63          * The next instructions will result in a buffer overflow if run on
64          * a POSIX system, and the later FD_ISSET will do undefined memory
65          * access.
66          *
67          * With Winsock, fd_set is a table of integers. This is awfully slow.
68          * However, FD_SET and FD_ISSET silently and safely discard
69          * overflows. If it happens we will loose socket events. Note that
70          * most (if not all) Winsock SOCKET handles are actually bigger than
71          * FD_SETSIZE in terms of absolute value - they are not POSIX file
72          * descriptors. From Vista, there is a much nicer WSAPoll(), but Mingw
73          * is yet to support it.
74          *
75          * With BeOS, the situation is unknown (FIXME: document).
76          */
77         if (fds[i].events & POLLIN)
78             FD_SET (fd, &rdset);
79         if (fds[i].events & POLLOUT)
80             FD_SET (fd, &wrset);
81         if (fds[i].events & POLLPRI)
82             FD_SET (fd, &exset);
83     }
84
85     if (timeout >= 0)
86     {
87         div_t d = div (timeout, 1000);
88         tv.tv_sec = d.quot;
89         tv.tv_usec = d.rem * 1000;
90     }
91
92     val = select (val + 1, &rdset, &wrset, &exset,
93                   (timeout >= 0) ? &tv : NULL);
94     if (val == -1)
95         return -1;
96
97     for (unsigned i = 0; i < nfds; i++)
98     {
99         int fd = fds[i].fd;
100         fds[i].revents = (FD_ISSET (fd, &rdset) ? POLLIN : 0)
101                        | (FD_ISSET (fd, &wrset) ? POLLOUT : 0)
102                        | (FD_ISSET (fd, &exset) ? POLLPRI : 0);
103     }
104     return val;
105 }
106 #endif /* !HAVE_POLL */