From b0a362a72734deb286830e26a3eb20e20234f3e9 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 25 Aug 2009 13:33:19 +0200 Subject: [PATCH] Fix coupling problems on OSX --- deps/coupling/coupling.c | 50 ++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/deps/coupling/coupling.c b/deps/coupling/coupling.c index e4817dabc1..57eca1c8ce 100644 --- a/deps/coupling/coupling.c +++ b/deps/coupling/coupling.c @@ -143,9 +143,8 @@ ring_buffer_push (ring_buffer *ring, int fd) return r; } - static void -pump (int pullfd, int pushfd) +pump (int is_pull, int pullfd, int pushfd) { int r; ring_buffer ring; @@ -160,26 +159,33 @@ pump (int pullfd, int pushfd) FD_ZERO(&readfds); FD_ZERO(&writefds); - maxfd = pushfd; - FD_SET(pushfd, &exceptfds); - - if (pullfd >= 0) { - FD_SET(pullfd, &exceptfds); - maxfd = MAX(pushfd, pullfd); - if (!ring_buffer_filled_p(&ring)) FD_SET(pullfd, &readfds); - } - - if (!ring_buffer_empty_p(&ring)) { - FD_SET(pushfd, &writefds); + maxfd = -1; + + if (is_pull) { + if (!ring_buffer_empty_p(&ring)) { + maxfd = pushfd; + FD_SET(pushfd, &exceptfds); + FD_SET(pushfd, &writefds); + } + } else { + if (pullfd >= 0) { + if (!ring_buffer_filled_p(&ring)) { + maxfd = pullfd; + FD_SET(pullfd, &exceptfds); + FD_SET(pullfd, &readfds); + } + } } - r = select(maxfd+1, &readfds, &writefds, &exceptfds, NULL); + if (maxfd >= 0) { + r = select(maxfd+1, &readfds, &writefds, &exceptfds, NULL); - if (r < 0 || FD_ISSET(pushfd, &exceptfds)) { - close(pushfd); - close(pullfd); - pushfd = pullfd = -1; - return; + if (r < 0 || (pullfd >= 0 && FD_ISSET(pushfd, &exceptfds))) { + close(pushfd); + close(pullfd); + pushfd = pullfd = -1; + return; + } } if (pullfd >= 0 && FD_ISSET(pullfd, &exceptfds)) { @@ -187,7 +193,7 @@ pump (int pullfd, int pushfd) pullfd = -1; } - if (pullfd >= 0 && FD_ISSET(pullfd, &readfds)) { + if (pullfd >= 0 && (is_pull || FD_ISSET(pullfd, &readfds))) { r = ring_buffer_pull(&ring, pullfd); if (r == 0) { /* eof */ @@ -199,7 +205,7 @@ pump (int pullfd, int pushfd) } } - if (FD_ISSET(pushfd, &writefds)) { + if (!is_pull || FD_ISSET(pushfd, &writefds)) { r = ring_buffer_push(&ring, pushfd); if (r < 0) { switch (errno) { @@ -256,7 +262,7 @@ pump_thread (void *data) { struct coupling *c = (struct coupling*)data; - pump(c->pullfd, c->pushfd); + pump(c->is_pull, c->pullfd, c->pushfd); return NULL; }