]> code.delx.au - pulseaudio/blob - src/pulsecore/poll.c
poll() is totally broken on Mac OS X
[pulseaudio] / src / pulsecore / poll.c
1
2 /***
3 This file is part of PulseAudio.
4
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 /***
24 Based on work for the GNU C Library.
25 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
26 ***/
27
28 /* Poll the file descriptors described by the NFDS structures starting at
29 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
30 an event to occur; if TIMEOUT is -1, block until an event occurs.
31 Returns the number of file descriptors with events, zero if timed out,
32 or -1 for errors. */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <errno.h>
39
40 #ifdef HAVE_SYS_SELECT_H
41 #include <sys/select.h>
42 #endif
43
44 #include "winsock.h"
45
46 #include <pulsecore/core-util.h>
47 #include <pulse/util.h>
48
49 #include "poll.h"
50
51 /* Mac OSX fails to implement poll() in a working way since 10.4. IOW, for
52 * several years. We need to enable a dirty workaround and emulate that call
53 * with select(), just like for Windows. sic! */
54
55 #if !defined(HAVE_POLL_H) || defined(OS_IS_DARWIN)
56
57 int pa_poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
58 struct timeval tv;
59 fd_set rset, wset, xset;
60 struct pollfd *f;
61 int ready;
62 int maxfd = 0;
63 char data[64];
64
65 FD_ZERO (&rset);
66 FD_ZERO (&wset);
67 FD_ZERO (&xset);
68
69 if (nfds == 0) {
70 if (timeout >= 0) {
71 pa_msleep(timeout);
72 return 0;
73 }
74
75 #ifdef OS_IS_WIN32
76 /*
77 * Windows does not support signals properly so waiting for them would
78 * mean a deadlock.
79 */
80 pa_msleep(100);
81 return 0;
82 #else
83 return select(0, NULL, NULL, NULL, NULL);
84 #endif
85 }
86
87 for (f = fds; f < &fds[nfds]; ++f) {
88 if (f->fd != -1) {
89 if (f->events & POLLIN)
90 FD_SET (f->fd, &rset);
91 if (f->events & POLLOUT)
92 FD_SET (f->fd, &wset);
93 if (f->events & POLLPRI)
94 FD_SET (f->fd, &xset);
95 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
96 maxfd = f->fd;
97 }
98 }
99
100 tv.tv_sec = timeout / 1000;
101 tv.tv_usec = (timeout % 1000) * 1000;
102
103 ready = select ((SELECT_TYPE_ARG1) maxfd + 1, SELECT_TYPE_ARG234 &rset,
104 SELECT_TYPE_ARG234 &wset, SELECT_TYPE_ARG234 &xset,
105 SELECT_TYPE_ARG5 (timeout == -1 ? NULL : &tv));
106 if ((ready == -1) && (errno == EBADF)) {
107 ready = 0;
108
109 FD_ZERO (&rset);
110 FD_ZERO (&wset);
111 FD_ZERO (&xset);
112
113 maxfd = -1;
114
115 for (f = fds; f < &fds[nfds]; ++f) {
116 if (f->fd != -1) {
117 fd_set sngl_rset, sngl_wset, sngl_xset;
118
119 FD_ZERO (&sngl_rset);
120 FD_ZERO (&sngl_wset);
121 FD_ZERO (&sngl_xset);
122
123 if (f->events & POLLIN)
124 FD_SET (f->fd, &sngl_rset);
125 if (f->events & POLLOUT)
126 FD_SET (f->fd, &sngl_wset);
127 if (f->events & POLLPRI)
128 FD_SET (f->fd, &sngl_xset);
129 if (f->events & (POLLIN|POLLOUT|POLLPRI)) {
130 struct timeval singl_tv;
131
132 singl_tv.tv_sec = 0;
133 singl_tv.tv_usec = 0;
134
135 if (select((SELECT_TYPE_ARG1) f->fd, SELECT_TYPE_ARG234 &rset,
136 SELECT_TYPE_ARG234 &wset, SELECT_TYPE_ARG234 &xset,
137 SELECT_TYPE_ARG5 &singl_tv) != -1) {
138 if (f->events & POLLIN)
139 FD_SET (f->fd, &rset);
140 if (f->events & POLLOUT)
141 FD_SET (f->fd, &wset);
142 if (f->events & POLLPRI)
143 FD_SET (f->fd, &xset);
144 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
145 maxfd = f->fd;
146 ++ready;
147 } else if (errno == EBADF)
148 f->revents |= POLLNVAL;
149 }
150 }
151 }
152
153 if (ready) {
154 /* Linux alters the tv struct... but it shouldn't matter here ...
155 * as we're going to be a little bit out anyway as we've just eaten
156 * more than a couple of cpu cycles above */
157 ready = select ((SELECT_TYPE_ARG1) maxfd + 1, SELECT_TYPE_ARG234 &rset,
158 SELECT_TYPE_ARG234 &wset, SELECT_TYPE_ARG234 &xset,
159 SELECT_TYPE_ARG5 (timeout == -1 ? NULL : &tv));
160 }
161 }
162
163 #ifdef OS_IS_WIN32
164 errno = WSAGetLastError();
165 #endif
166
167 if (ready > 0) {
168 ready = 0;
169 for (f = fds; f < &fds[nfds]; ++f) {
170 f->revents = 0;
171 if (f->fd != -1) {
172 if (FD_ISSET (f->fd, &rset)) {
173 /* support for POLLHUP. An hung up descriptor does not
174 increase the return value! */
175 if (recv (f->fd, data, 64, MSG_PEEK) == -1) {
176 if (errno == ESHUTDOWN || errno == ECONNRESET ||
177 errno == ECONNABORTED || errno == ENETRESET) {
178 fprintf(stderr, "Hangup\n");
179 f->revents |= POLLHUP;
180 }
181 }
182
183 if (f->revents == 0)
184 f->revents |= POLLIN;
185 }
186 if (FD_ISSET (f->fd, &wset))
187 f->revents |= POLLOUT;
188 if (FD_ISSET (f->fd, &xset))
189 f->revents |= POLLPRI;
190 }
191 if (f->revents)
192 ready++;
193 }
194 }
195
196 return ready;
197 }
198
199 #endif /* HAVE_SYS_POLL_H */