]> git.sesse.net Git - vlc/blob - src/network/poll.c
Enable vlc_poll() on OS/2
[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_MAEMO
31 # include <vlc_network.h>
32 # include <signal.h>
33 # include <errno.h>
34 # include <poll.h>
35
36 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
37 {
38     struct timespec tsbuf, *ts;
39     sigset_t set;
40     int canc, ret;
41
42     if (timeout != -1)
43     {
44         div_t d = div (timeout, 1000);
45         tsbuf.tv_sec = d.quot;
46         tsbuf.tv_nsec = d.rem * 1000000;
47         ts = &tsbuf;
48     }
49     else
50         ts = NULL;
51
52     pthread_sigmask (SIG_BLOCK, NULL, &set);
53     sigdelset (&set, SIGRTMIN);
54
55     canc = vlc_savecancel ();
56     ret = ppoll (fds, nfds, ts, &set);
57     vlc_restorecancel (canc);
58
59     vlc_testcancel ();
60     return ret;
61 }
62
63 #elif defined (HAVE_POLL)
64 # include <vlc_network.h>
65
66 struct pollfd;
67
68 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
69 {
70     (void)fds; (void)nfds; (void)timeout;
71     abort ();
72 }
73
74 #elif defined (WIN32) || defined(__OS2__)
75
76 #include <stdlib.h>
77 #include <string.h>
78 #include <errno.h>
79 #ifndef __OS2__
80 #ifdef FD_SETSIZE
81 /* No, it's not as simple as #undef FD_SETSIZE */
82 # error Header inclusion order compromised!
83 #endif
84 #define FD_SETSIZE 0
85 #endif
86 #include <vlc_network.h>
87
88 #ifdef __OS2__
89 #include <sys/time.h>
90 #include <sys/select.h>
91
92 #define SOCKET unsigned
93 #endif
94
95 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
96 {
97     size_t setsize = sizeof (fd_set) + nfds * sizeof (SOCKET);
98     fd_set *rdset = malloc (setsize);
99     fd_set *wrset = malloc (setsize);
100     fd_set *exset = malloc (setsize);
101     struct timeval tv = { 0, 0 };
102     int val;
103
104     if (unlikely(rdset == NULL || wrset == NULL || exset == NULL))
105     {
106         free (rdset);
107         free (wrset);
108         free (exset);
109         errno = ENOMEM;
110         return -1;
111     }
112
113 /* Winsock FD_SET uses FD_SETSIZE in its expansion */
114 #undef FD_SETSIZE
115 #define FD_SETSIZE (nfds)
116
117 resume:
118     val = -1;
119     vlc_testcancel ();
120
121     FD_ZERO (rdset);
122     FD_ZERO (wrset);
123     FD_ZERO (exset);
124     for (unsigned i = 0; i < nfds; i++)
125     {
126         int fd = fds[i].fd;
127         if (val < fd)
128             val = fd;
129
130         /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
131          * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
132          * uses poll() rather than select(). Most POSIX systems implement
133          * fd_set has a bit field with no sanity checks. This is especially bad
134          * on systems (such as BSD) that have no process open files limit by
135          * default, such that it is quite feasible to get fd >= FD_SETSIZE.
136          * The next instructions will result in a buffer overflow if run on
137          * a POSIX system, and the later FD_ISSET would perform an undefined
138          * memory read.
139          *
140          * With Winsock, fd_set is a table of integers. This is awfully slow.
141          * However, FD_SET and FD_ISSET silently and safely discard excess
142          * entries. Here, overflow cannot happen anyway: fd_set of adequate
143          * size are allocated.
144          * Note that Vista has a much nicer WSAPoll(), but Mingw does not
145          * support it yet.
146          */
147         if (fds[i].events & POLLIN)
148             FD_SET ((SOCKET)fd, rdset);
149         if (fds[i].events & POLLOUT)
150             FD_SET ((SOCKET)fd, wrset);
151         if (fds[i].events & POLLPRI)
152             FD_SET ((SOCKET)fd, exset);
153     }
154
155 #ifndef HAVE_ALERTABLE_SELECT
156 # warning FIXME! Fix cancellation and remove this crap.
157     if ((timeout < 0) || (timeout > 50))
158     {
159         tv.tv_sec = 0;
160         tv.tv_usec = 50000;
161     }
162     else
163 #endif
164     if (timeout >= 0)
165     {
166         div_t d = div (timeout, 1000);
167         tv.tv_sec = d.quot;
168         tv.tv_usec = d.rem * 1000;
169     }
170
171     val = select (val + 1, rdset, wrset, exset,
172                   /*(timeout >= 0) ?*/ &tv /*: NULL*/);
173
174 #ifndef HAVE_ALERTABLE_SELECT
175     if (val == 0)
176     {
177         if (timeout > 0)
178             timeout -= (timeout > 50) ? 50 : timeout;
179         if (timeout != 0)
180             goto resume;
181     }
182 #endif
183
184     if (val == -1)
185         return -1;
186
187     for (unsigned i = 0; i < nfds; i++)
188     {
189         int fd = fds[i].fd;
190         fds[i].revents = (FD_ISSET (fd, rdset) ? POLLIN : 0)
191                        | (FD_ISSET (fd, wrset) ? POLLOUT : 0)
192                        | (FD_ISSET (fd, exset) ? POLLPRI : 0);
193     }
194     free (exset);
195     free (wrset);
196     free (rdset);
197     return val;
198 }
199
200 #else
201 # error poll() not implemented!
202 #endif