Add encoder support for Dirac using the Schroedinger library.
[vlc/asuraparaju-public.git] / src / network / poll.c
blob575bb10745ef34c26bdd5c732bebb5c1a017da8e
1 /*****************************************************************************
2 * poll.c: I/O event multiplexing
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include <stdlib.h>
30 #include <vlc_network.h>
33 #ifdef HAVE_MAEMO
34 # include <signal.h>
35 # include <errno.h>
36 # include <poll.h>
38 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
40 struct timespec tsbuf, *ts;
41 sigset_t set;
42 int canc, ret;
44 if (timeout != -1)
46 div_t d = div (timeout, 1000);
47 tsbuf.tv_sec = d.quot;
48 tsbuf.tv_nsec = d.rem * 1000000;
49 ts = &tsbuf;
51 else
52 ts = NULL;
54 pthread_sigmask (SIG_BLOCK, NULL, &set);
55 sigdelset (&set, SIGRTMIN);
57 canc = vlc_savecancel ();
58 ret = ppoll (fds, nfds, ts, &set);
59 vlc_restorecancel (canc);
61 vlc_testcancel ();
62 return ret;
65 #elif defined (HAVE_POLL)
66 struct pollfd;
68 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
70 (void)fds; (void)nfds; (void)timeout;
71 abort ();
73 #else /* !HAVE_POLL */
75 #include <string.h>
77 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
79 fd_set rdset, wrset, exset;
80 struct timeval tv = { 0, 0 };
81 int val;
83 resume:
84 val = -1;
85 vlc_testcancel ();
87 FD_ZERO (&rdset);
88 FD_ZERO (&wrset);
89 FD_ZERO (&exset);
90 for (unsigned i = 0; i < nfds; i++)
92 int fd = fds[i].fd;
93 if (val < fd)
94 val = fd;
96 /* With POSIX, FD_SET & FD_ISSET are not defined if fd is negative or
97 * bigger or equal than FD_SETSIZE. That is one of the reasons why VLC
98 * uses poll() rather than select(). Most POSIX systems implement
99 * fd_set has a bit field with no sanity checks. This is especially bad
100 * on systems (such as BSD) that have no process open files limit by
101 * default, such that it is quite feasible to get fd >= FD_SETSIZE.
102 * The next instructions will result in a buffer overflow if run on
103 * a POSIX system, and the later FD_ISSET will do undefined memory
104 * access.
106 * With Winsock, fd_set is a table of integers. This is awfully slow.
107 * However, FD_SET and FD_ISSET silently and safely discard
108 * overflows. If it happens we will loose socket events. Note that
109 * most (if not all) Winsock SOCKET handles are actually bigger than
110 * FD_SETSIZE in terms of absolute value - they are not POSIX file
111 * descriptors. From Vista, there is a much nicer WSAPoll(), but Mingw
112 * is yet to support it.
114 * With BeOS, the situation is unknown (FIXME: document).
116 if (fds[i].events & POLLIN)
117 FD_SET (fd, &rdset);
118 if (fds[i].events & POLLOUT)
119 FD_SET (fd, &wrset);
120 if (fds[i].events & POLLPRI)
121 FD_SET (fd, &exset);
124 #ifndef HAVE_ALERTABLE_SELECT
125 # warning FIXME! Fix cancellation and remove this crap.
126 if ((timeout < 0) || (timeout > 50))
128 tv.tv_sec = 0;
129 tv.tv_usec = 50000;
131 else
132 #endif
133 if (timeout >= 0)
135 div_t d = div (timeout, 1000);
136 tv.tv_sec = d.quot;
137 tv.tv_usec = d.rem * 1000;
140 val = select (val + 1, &rdset, &wrset, &exset,
141 /*(timeout >= 0) ?*/ &tv /*: NULL*/);
143 #ifndef HAVE_ALERTABLE_SELECT
144 if (val == 0)
146 if (timeout > 0)
147 timeout -= (timeout > 50) ? 50 : timeout;
148 if (timeout != 0)
149 goto resume;
151 #endif
153 if (val == -1)
154 return -1;
156 for (unsigned i = 0; i < nfds; i++)
158 int fd = fds[i].fd;
159 fds[i].revents = (FD_ISSET (fd, &rdset) ? POLLIN : 0)
160 | (FD_ISSET (fd, &wrset) ? POLLOUT : 0)
161 | (FD_ISSET (fd, &exset) ? POLLPRI : 0);
163 return val;
165 #endif /* !HAVE_POLL */