bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] poll: wait until timeout on pipes (win32)


From: Edward Thomson
Subject: Re: [PATCH] poll: wait until timeout on pipes (win32)
Date: Tue, 16 Sep 2014 14:14:15 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

Updated patch to take INFTIM into account.

I have not yet received copyright assignment paperwork from FSF.  I will
ping them again and forward that along once I receive it.  I'm reposting
this ahead of that to continue any technical discussions that might arise
so that we will hopefully have a final patch in place when the legal
issues are sorted.

Thanks-

-ed

---
 lib/poll.c        | 16 ++++++++++++++--
 tests/test-poll.c | 24 ++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/lib/poll.c b/lib/poll.c
index a3e0ab7..889fa97 100644
--- a/lib/poll.c
+++ b/lib/poll.c
@@ -445,7 +445,7 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
   static HANDLE hEvent;
   WSANETWORKEVENTS ev;
   HANDLE h, handle_array[FD_SETSIZE + 2];
-  DWORD ret, wait_timeout, nhandles;
+  DWORD ret, wait_timeout, nhandles, start, elapsed, orig_timeout;
   fd_set rfds, wfds, xfds;
   BOOL poll_again;
   MSG msg;
@@ -458,6 +458,12 @@ poll (struct pollfd *pfd, nfds_t nfd, int timeout)
       return -1;
     }
 
+  if (timeout != INFTIM)
+    {
+      orig_timeout = timeout;
+      start = GetTickCount();
+    }
+
   if (!hEvent)
     hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
 
@@ -602,7 +608,13 @@ restart:
         rc++;
     }
 
-  if (!rc && timeout == INFTIM)
+  if (!rc && timeout && timeout != INFTIM)
+    {
+      elapsed = GetTickCount() - start;
+      timeout = elapsed < orig_timeout ? orig_timeout - elapsed : 0;
+    }
+
+  if (!rc && timeout)
     {
       SleepEx (1, TRUE);
       goto restart;
diff --git a/tests/test-poll.c b/tests/test-poll.c
index 0cdb1f9..2003b3e 100644
--- a/tests/test-poll.c
+++ b/tests/test-poll.c
@@ -362,6 +362,29 @@ test_pipe (void)
   close (fd[1]);
 }
 
+/* Test that poll(2) will timeout.  */
+
+static void
+test_timeout (void)
+{
+  int fds[2];
+  time_t start, elapsed;
+
+  if (pipe(fds) < 0)
+    failed ("expected pipe to succeed");
+
+  start = time(NULL);
+
+  int ret = poll1(fds[1], POLLIN, 2000);
+  close(fds[0]);
+  close(fds[1]);
+
+  if (ret)
+    failed ("expected poll to not return a fd");
+
+  if (time(NULL) - start < 2)
+    failed ("expected poll to timeout");
+}
 
 /* Do them all.  */
 
@@ -379,6 +402,7 @@ main ()
   result += test (test_socket_pair, "Connected sockets test");
   result += test (test_accept_first, "General socket test with fork");
   result += test (test_pipe, "Pipe test");
+  result += test (test_timeout, "Poll should timeout");
 
   exit (result);
 }
-- 
1.9.2.msysgit.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]