From e09f87f6ad73a4f9bb2d2ebc458edb93984489d0 Mon Sep 17 00:00:00 2001
From: Amr Shahin <amrnablus@gmail.com>
Date: Sun, 18 Sep 2011 03:21:11 +0300
Subject: [PATCH] regrouping some #ifdef dependet functions into groups

---
 lib/select.c |  115 ++++++++++++++++++++++++++++++++++++++--------------------
 lib/select.h |    8 ++++
 2 files changed, 83 insertions(+), 40 deletions(-)

diff --git a/lib/select.c b/lib/select.c
index 56dbc44..0a7034b 100644
--- a/lib/select.c
+++ b/lib/select.c
@@ -113,31 +113,40 @@ int Curl_wait_ms(int timeout_ms)
 #elif defined(USE_WINSOCK)
   Sleep(timeout_ms);
 #else
-  pending_ms = timeout_ms;
+  error=Curl_custom_wait(timeout_ms, initial_tv, &pending_ms, &r);
+#endif /* USE_WINSOCK */
+  if(r)
+    r = -1;
+  return r;
+}
+
+static int Curl_custom_wait(int timeout_ms, struct timeval initial_tv,
+    int *pending_ms, int *r) {
+  int error=0;
+  *pending_ms = timeout_ms;
   initial_tv = curlx_tvnow();
   do {
 #if defined(HAVE_POLL_FINE)
-    r = poll(NULL, 0, pending_ms);
+  *r = poll(NULL, 0, pending_ms);
 #else
-    pending_tv.tv_sec = pending_ms / 1000;
-    pending_tv.tv_usec = (pending_ms % 1000) * 1000;
-    r = select(0, NULL, NULL, NULL, &pending_tv);
+  pending_tv.tv_sec = *pending_ms / 1000;
+  pending_tv.tv_usec = (*pending_ms % 1000) * 1000;
+  *r = select(0, NULL, NULL, NULL, &pending_tv);
 #endif /* HAVE_POLL_FINE */
-    if(r != -1)
-      break;
-    error = SOCKERRNO;
-    if(error && error_not_EINTR)
-      break;
-    pending_ms = timeout_ms - elapsed_ms;
-    if(pending_ms <= 0)
-      break;
-  } while(r == -1);
-#endif /* USE_WINSOCK */
-  if(r)
-    r = -1;
-  return r;
+  if(*r != -1)
+    break;
+  error = SOCKERRNO;
+  if(error && error_not_EINTR)
+    break;
+  *pending_ms = timeout_ms - elapsed_ms;
+  if(*pending_ms <= 0)
+    break;
+  } while(*r == -1);
+
+  return error;
 }
 
+
 /*
  * This is an internal function used for waiting for read or write
  * events on a pair of file descriptors.  It uses poll() when a fine
@@ -159,28 +168,34 @@ int Curl_wait_ms(int timeout_ms)
 int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
                       long timeout_ms)
 {
-#ifdef HAVE_POLL_FINE
-  struct pollfd pfd[2];
-  int num;
-#else
-  struct timeval pending_tv;
-  struct timeval *ptimeout;
-  fd_set fds_read;
-  fd_set fds_write;
-  fd_set fds_err;
-  curl_socket_t maxfd;
-#endif
-  struct timeval initial_tv = {0,0};
-  int pending_ms = 0;
-  int error;
+
   int r;
-  int ret;
 
   if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
     r = Curl_wait_ms((int)timeout_ms);
     return r;
   }
 
+#ifdef HAVE_POLL_FINE
+  return Curl_socket_ready_wpoll(readfd, writefd, timeout_ms);
+#else  /* HAVE_POLL_FINE */
+  return Curl_socket_ready_npoll(readfd, writefd, timeout_ms);
+#endif  /* HAVE_POLL_FINE */
+
+}
+
+
+static int Curl_socket_ready_wpoll(curl_socket_t readfd, curl_socket_t writefd,
+                      long timeout_ms)
+{
+  struct pollfd pfd[2];
+  int num = 0;
+  int pending_ms = 0;
+  int r;
+  int ret;
+  struct timeval initial_tv = {0,0};
+  int error;
+
   /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
      time in this function does not need to be measured. This happens
      when function is called with a zero timeout or a negative timeout
@@ -191,9 +206,6 @@ int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
     initial_tv = curlx_tvnow();
   }
 
-#ifdef HAVE_POLL_FINE
-
-  num = 0;
   if(readfd != CURL_SOCKET_BAD) {
     pfd[num].fd = readfd;
     pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
@@ -247,8 +259,33 @@ int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
   }
 
   return ret;
+}
 
-#else  /* HAVE_POLL_FINE */
+static int Curl_socket_ready_npoll(curl_socket_t readfd, curl_socket_t writefd,
+                      long timeout_ms)
+{
+  struct timeval pending_tv;
+  struct timeval *ptimeout;
+  fd_set fds_read;
+  fd_set fds_write;
+  fd_set fds_err;
+  curl_socket_t maxfd;
+  struct timeval initial_tv = {0,0};
+  int pending_ms = 0;
+  int error;
+  int r;
+  int ret;
+
+
+  /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
+     time in this function does not need to be measured. This happens
+     when function is called with a zero timeout or a negative timeout
+     value indicating a blocking call should be performed. */
+
+  if(timeout_ms > 0) {
+    pending_ms = (int)timeout_ms;
+    initial_tv = curlx_tvnow();
+  }
 
   FD_ZERO(&fds_err);
   maxfd = (curl_socket_t)-1;
@@ -314,11 +351,9 @@ int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
   }
 
   return ret;
-
-#endif  /* HAVE_POLL_FINE */
-
 }
 
+
 /*
  * This is a wrapper around poll().  If poll() does not exist, then
  * select() is used instead.  An error is returned if select() is
diff --git a/lib/select.h b/lib/select.h
index d785fb3..7f8fff8 100644
--- a/lib/select.h
+++ b/lib/select.h
@@ -91,6 +91,14 @@ int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms);
 
 int Curl_wait_ms(int timeout_ms);
 
+static int Curl_custom_wait(int timeout_ms, struct timeval initial_tv,
+                      int *pending_ms, int *r);
+static int Curl_socket_ready_wpoll(curl_socket_t readfd, curl_socket_t writefd,
+                      long timeout_ms);
+
+static int Curl_socket_ready_npoll(curl_socket_t readfd, curl_socket_t writefd,
+                      long timeout_ms);
+
 #ifdef TPF
 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
                        fd_set* excepts, struct timeval* tv);
-- 
1.7.6

