]> git.sesse.net Git - vlc/blob - src/network/poll.c
cf9554a30ab687c453425985cf52f6a025b24820
[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 #include <stdlib.h>
30 #include <vlc_network.h>
31
32
33 #ifdef HAVE_POLL
34 struct pollfd;
35
36 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
37 {
38     (void)fds; (void)nfds; (void)timeout;
39     abort ();
40 }
41 #else /* !HAVE_POLL */
42
43 #include <string.h>
44
45 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
46 {
47     fd_set rdset, wrset, exset;
48     struct timeval tv = { 0, 0 };
49     int val;
50
51 resume:
52     val = -1;
53     vlc_testcancel ();
54
55     FD_ZERO (&rdset);
56     FD_ZERO (&wrset);
57     FD_ZERO (&exset);
58     for (unsigned i = 0; i < nfds; i++)
59     {
60         int fd = fds[i].fd;
61         if (val < fd)
62             val = fd;
63
64         /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
65          * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
66          * uses poll() rather than select(). Most POSIX systems implement
67          * fd_set has a bit field with no sanity checks. This is especially bad
68          * on systems (such as BSD) that have no process open files limit by
69          * default, such that it is quite feasible to get fd >= FD_SETSIZE.
70          * The next instructions will result in a buffer overflow if run on
71          * a POSIX system, and the later FD_ISSET will do undefined memory
72          * access.
73          *
74          * With Winsock, fd_set is a table of integers. This is awfully slow.
75          * However, FD_SET and FD_ISSET silently and safely discard
76          * overflows. If it happens we will loose socket events. Note that
77          * most (if not all) Winsock SOCKET handles are actually bigger than
78          * FD_SETSIZE in terms of absolute value - they are not POSIX file
79          * descriptors. From Vista, there is a much nicer WSAPoll(), but Mingw
80          * is yet to support it.
81          *
82          * With BeOS, the situation is unknown (FIXME: document).
83          */
84         if (fds[i].events & POLLIN)
85             FD_SET (fd, &rdset);
86         if (fds[i].events & POLLOUT)
87             FD_SET (fd, &wrset);
88         if (fds[i].events & POLLPRI)
89             FD_SET (fd, &exset);
90     }
91
92 #ifndef HAVE_ALERTABLE_SELECT
93 # warning FIXME! Fix cancellation and remove this crap.
94     if ((timeout < 0) || (timeout > 50))
95     {
96         tv.tv_sec = 0;
97         tv.tv_usec = 50000;
98     }
99     else
100 #endif
101     if (timeout >= 0)
102     {
103         div_t d = div (timeout, 1000);
104         tv.tv_sec = d.quot;
105         tv.tv_usec = d.rem * 1000;
106     }
107
108     val = select (val + 1, &rdset, &wrset, &exset,
109                   /*(timeout >= 0) ?*/ &tv /*: NULL*/);
110
111 #ifndef HAVE_ALERTABLE_SELECT
112     if (val == 0)
113     {
114         if (timeout > 0)
115             timeout -= (timeout > 50) ? 50 : timeout;
116         if (timeout != 0)
117             goto resume;
118     }
119 #endif
120
121     if (val == -1)
122         return -1;
123
124     for (unsigned i = 0; i < nfds; i++)
125     {
126         int fd = fds[i].fd;
127         fds[i].revents = (FD_ISSET (fd, &rdset) ? POLLIN : 0)
128                        | (FD_ISSET (fd, &wrset) ? POLLOUT : 0)
129                        | (FD_ISSET (fd, &exset) ? POLLPRI : 0);
130     }
131     return val;
132 }
133 #endif /* !HAVE_POLL */