]> git.sesse.net Git - vlc/blob - src/network/poll.c
Revert "poll(): ifndef HAVE_POLL means we don't use poll(), not that it doesn't exist...
[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 #ifndef HAVE_POLL
31 #include <string.h>
32 #include <stdlib.h>
33 #include <vlc_network.h>
34
35 int poll (struct pollfd *fds, unsigned nfds, int timeout)
36 {
37     fd_set rdset, wrset, exset;
38     struct timeval tv = { 0, 0 };
39     int val = -1;
40
41
42     FD_ZERO (&rdset);
43     FD_ZERO (&wrset);
44     FD_ZERO (&exset);
45     for (unsigned i = 0; i < nfds; i++)
46     {
47         int fd = fds[i].fd;
48         if (val < fd)
49             val = fd;
50
51         /* I assume the OS has a solution select overflow if it does not have
52          * poll(). If it did not, we are screwed anyway. */
53         if (fds[i].events & POLLIN)
54             FD_SET (fd, &rdset);
55         if (fds[i].events & POLLOUT)
56             FD_SET (fd, &wrset);
57         if (fds[i].events & POLLPRI)
58             FD_SET (fd, &exset);
59     }
60
61     if (timeout >= 0)
62     {
63         div_t d = div (timeout, 1000);
64         tv.tv_sec = d.quot;
65         tv.tv_usec = d.rem * 1000;
66     }
67
68     val = select (val + 1, &rdset, &wrset, &exset,
69                   (timeout >= 0) ? &tv : NULL);
70     if (val == -1)
71         return -1;
72
73     for (unsigned i = 0; i < nfds; i++)
74     {
75         int fd = fds[i].fd;
76         fds[i].revents = (FD_ISSET (fd, &rdset) ? POLLIN : 0)
77                        | (FD_ISSET (fd, &wrset) ? POLLOUT : 0)
78                        | (FD_ISSET (fd, &exset) ? POLLPRI : 0);
79     }
80     return val;
81 }
82 #endif /* !HAVE_POLL */