[pulseaudio-discuss] [PATCH 4/7] poll() is totally broken on Mac OS X

Daniel Mack daniel at caiaq.de
Tue Dec 8 18:47:12 PST 2009


Even on 10.5.8, poll() does not do the right thing. Haven't checked on
newer versions. Hence, wrap all occurences of poll() to pa_poll and
emulate that call with select() on OSX. This is totally embarassing.
---
 src/modules/module-sine-source.c    |    2 +-
 src/modules/raop/module-raop-sink.c |    2 +-
 src/modules/raop/raop_client.c      |    5 -----
 src/modules/rtp/rtsp_client.c       |    5 -----
 src/pulse/mainloop.c                |    9 ++-------
 src/pulsecore/lock-autospawn.c      |    3 ++-
 src/pulsecore/poll.c                |   17 ++++++++++++++---
 src/pulsecore/poll.h                |    9 +++++++--
 src/pulsecore/rtpoll.c              |    9 ++-------
 src/tests/lock-autospawn-test.c     |    4 ++--
 src/tests/rtpoll-test.c             |    2 +-
 src/utils/pacmd.c                   |    4 ++--
 12 files changed, 34 insertions(+), 37 deletions(-)

diff --git a/src/modules/module-sine-source.c b/src/modules/module-sine-source.c
index 9826e5f..53f5335 100644
--- a/src/modules/module-sine-source.c
+++ b/src/modules/module-sine-source.c
@@ -32,7 +32,6 @@
 #include <unistd.h>
 #include <limits.h>
 #include <sys/ioctl.h>
-#include <sys/poll.h>
 
 #include <pulse/rtclock.h>
 #include <pulse/timeval.h>
@@ -48,6 +47,7 @@
 #include <pulsecore/thread.h>
 #include <pulsecore/thread-mq.h>
 #include <pulsecore/rtpoll.h>
+#include <pulsecore/poll.h>
 
 #include "module-sine-source-symdef.h"
 
diff --git a/src/modules/raop/module-raop-sink.c b/src/modules/raop/module-raop-sink.c
index ac48ab1..ce534ce 100644
--- a/src/modules/raop/module-raop-sink.c
+++ b/src/modules/raop/module-raop-sink.c
@@ -32,7 +32,6 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <limits.h>
-#include <poll.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
@@ -60,6 +59,7 @@
 #include <pulsecore/thread.h>
 #include <pulsecore/time-smoother.h>
 #include <pulsecore/socket-util.h>
+#include <pulsecore/poll.h>
 
 #include "module-raop-sink-symdef.h"
 #include "rtp.h"
diff --git a/src/modules/raop/raop_client.c b/src/modules/raop/raop_client.c
index c4b0237..e3152dd 100644
--- a/src/modules/raop/raop_client.c
+++ b/src/modules/raop/raop_client.c
@@ -51,12 +51,7 @@
 #include <pulsecore/macro.h>
 #include <pulsecore/strbuf.h>
 #include <pulsecore/random.h>
-
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
 #include <pulsecore/poll.h>
-#endif
 
 #include "raop_client.h"
 #include "rtsp_client.h"
diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c
index 5961806..6094eb8 100644
--- a/src/modules/rtp/rtsp_client.c
+++ b/src/modules/rtp/rtsp_client.c
@@ -45,12 +45,7 @@
 #include <pulsecore/macro.h>
 #include <pulsecore/strbuf.h>
 #include <pulsecore/ioline.h>
-
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
 #include <pulsecore/poll.h>
-#endif
 
 #include "rtsp_client.h"
 
diff --git a/src/pulse/mainloop.c b/src/pulse/mainloop.c
index 6cd089e..8f743ec 100644
--- a/src/pulse/mainloop.c
+++ b/src/pulse/mainloop.c
@@ -32,12 +32,6 @@
 #include <fcntl.h>
 #include <errno.h>
 
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
-#include <pulsecore/poll.h>
-#endif
-
 #ifndef HAVE_PIPE
 #include <pulsecore/pipe.h>
 #endif
@@ -47,6 +41,7 @@
 #include <pulse/timeval.h>
 #include <pulse/xmalloc.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/llist.h>
@@ -887,7 +882,7 @@ int pa_mainloop_poll(pa_mainloop *m) {
                     m->prepared_timeout == PA_USEC_INVALID ? NULL : pa_timespec_store(&ts, m->prepared_timeout),
                     NULL);
 #else
-            m->poll_func_ret = poll(
+            m->poll_func_ret = pa_poll(
                     m->pollfds, m->n_pollfds,
                     usec_to_timeout(m->prepared_timeout));
 #endif
diff --git a/src/pulsecore/lock-autospawn.c b/src/pulsecore/lock-autospawn.c
index 65e3563..95ca04a 100644
--- a/src/pulsecore/lock-autospawn.c
+++ b/src/pulsecore/lock-autospawn.c
@@ -33,6 +33,7 @@
 #include <pulse/i18n.h>
 #include <pulse/xmalloc.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/mutex.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/core-util.h>
@@ -182,7 +183,7 @@ static void wait_for_ping(void) {
     pfd.fd = pipe_fd[0];
     pfd.events = POLLIN;
 
-    if ((k = poll(&pfd, 1, -1)) != 1) {
+    if ((k = pa_poll(&pfd, 1, -1)) != 1) {
         pa_assert(k < 0);
         pa_assert(errno == EINTR);
     } else if ((s = read(pipe_fd[0], &x, 1)) != 1) {
diff --git a/src/pulsecore/poll.c b/src/pulsecore/poll.c
index 46a69c5..27f04aa 100644
--- a/src/pulsecore/poll.c
+++ b/src/pulsecore/poll.c
@@ -43,13 +43,24 @@
 
 #include "winsock.h"
 
-#ifndef HAVE_POLL_H
-
 #include <pulsecore/core-util.h>
+#include <pulse/util.h>
 
 #include "poll.h"
 
-int poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
+/* Mac OSX fails to implement poll() in a working way since 10.4. IOW, for
+ * several years. We need to enable a dirty workaround and emulate that call
+ * with select(), just like for Windows. sic! */
+
+#if defined(HAVE_POLL_H) && !defined(OS_IS_DARWIN)
+
+int pa_poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
+	return poll(fds, nfds, timeout);
+}
+
+#else
+
+int pa_poll (struct pollfd *fds, unsigned long int nfds, int timeout) {
     struct timeval tv;
     fd_set rset, wset, xset;
     struct pollfd *f;
diff --git a/src/pulsecore/poll.h b/src/pulsecore/poll.h
index fe0c6af..2d94fab 100644
--- a/src/pulsecore/poll.h
+++ b/src/pulsecore/poll.h
@@ -24,6 +24,10 @@
    Copyright (C) 1994,96,97,98,99,2000,2001,2004 Free Software Foundation, Inc.
 ***/
 
+#ifdef HAVE_POLL_H
+#include <poll.h>
+#else
+
 /* Event types that can be polled for.  These bits may be set in `events'
    to indicate the interesting event types; they will appear in `revents'
    to indicate the status of the file descriptor.  */
@@ -38,7 +42,6 @@
 #define POLLHUP         0x010           /* Hung up.  */
 #define POLLNVAL        0x020           /* Invalid polling request.  */
 
-
 /* Type used for the number of file descriptors.  */
 typedef unsigned long int nfds_t;
 
@@ -50,9 +53,11 @@ struct pollfd
     short int revents;          /* Types of events that actually occurred.  */
   };
 
+#endif /* HAVE_POLL_H */
+
 /* Poll the file descriptors described by the NFDS structures starting at
    FDS.  If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
    an event to occur; if TIMEOUT is -1, block until an event occurs.
    Returns the number of file descriptors with events, zero if timed out,
    or -1 for errors.  */
-extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
+extern int pa_poll (struct pollfd *fds, unsigned long nfds, int timeout);
diff --git a/src/pulsecore/rtpoll.c b/src/pulsecore/rtpoll.c
index 666cbc9..83993f0 100644
--- a/src/pulsecore/rtpoll.c
+++ b/src/pulsecore/rtpoll.c
@@ -30,15 +30,10 @@
 #include <string.h>
 #include <errno.h>
 
-#ifdef HAVE_POLL_H
-#include <poll.h>
-#else
-#include <pulsecore/poll.h>
-#endif
-
 #include <pulse/xmalloc.h>
 #include <pulse/timeval.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/core-rtclock.h>
 #include <pulsecore/macro.h>
@@ -304,7 +299,7 @@ int pa_rtpoll_run(pa_rtpoll *p, pa_bool_t wait_op) {
         r = ppoll(p->pollfd, p->n_pollfd_used, (!wait_op || p->quit || p->timer_enabled) ? &ts : NULL, NULL);
     }
 #else
-    r = poll(p->pollfd, p->n_pollfd_used, (!wait_op || p->quit || p->timer_enabled) ? (int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)) : -1);
+    r = pa_poll(p->pollfd, p->n_pollfd_used, (!wait_op || p->quit || p->timer_enabled) ? (int) ((timeout.tv_sec*1000) + (timeout.tv_usec / 1000)) : -1);
 #endif
 
     p->timer_elapsed = r == 0;
diff --git a/src/tests/lock-autospawn-test.c b/src/tests/lock-autospawn-test.c
index c754e23..6f7156d 100644
--- a/src/tests/lock-autospawn-test.c
+++ b/src/tests/lock-autospawn-test.c
@@ -23,9 +23,9 @@
 #include <config.h>
 #endif
 
-#include <sys/poll.h>
 #include <string.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/lock-autospawn.h>
@@ -69,7 +69,7 @@ static void thread_func2(void *k) {
         pollfd.fd = fd;
         pollfd.events = POLLIN;
 
-        pa_assert_se(poll(&pollfd, 1, -1) == 1);
+        pa_assert_se(pa_poll(&pollfd, 1, -1) == 1);
 
         pa_log("%i, woke up", PA_PTR_TO_INT(k));
     }
diff --git a/src/tests/rtpoll-test.c b/src/tests/rtpoll-test.c
index 1706cdf..6a6b73a 100644
--- a/src/tests/rtpoll-test.c
+++ b/src/tests/rtpoll-test.c
@@ -22,8 +22,8 @@
 #endif
 
 #include <signal.h>
-#include <poll.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/log.h>
 #include <pulsecore/rtpoll.h>
 
diff --git a/src/utils/pacmd.c b/src/utils/pacmd.c
index ef58e9c..6ffe94f 100644
--- a/src/utils/pacmd.c
+++ b/src/utils/pacmd.c
@@ -25,7 +25,6 @@
 
 #include <assert.h>
 #include <signal.h>
-#include <sys/poll.h>
 #include <sys/socket.h>
 #include <unistd.h>
 #include <errno.h>
@@ -38,6 +37,7 @@
 #include <pulse/xmalloc.h>
 #include <pulse/i18n.h>
 
+#include <pulsecore/poll.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/log.h>
@@ -153,7 +153,7 @@ int main(int argc, char*argv[]) {
         else if (!ibuf_eof)
             pollfd[WATCH_STDIN].events |= POLLIN;
 
-        if (poll(pollfd, N_WATCH, -1) < 0) {
+        if (pa_poll(pollfd, N_WATCH, -1) < 0) {
 
             if (errno == EINTR)
                 continue;
-- 
1.6.3.3




More information about the pulseaudio-discuss mailing list