Author: mturk
Date: Mon Jan 18 10:27:59 2010
New Revision: 900330

URL: http://svn.apache.org/viewvc?rev=900330&view=rev
Log:
Use common wait code

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c

Modified: 
commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h?rev=900330&r1=900329&r2=900330&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h 
Mon Jan 18 10:27:59 2010
@@ -81,8 +81,11 @@
 /**
  * Common unix utils
  */
+#define ACR_RDWAITIO    POLLIN
+#define ACR_WRWAITIO    POLLOUT
 int     acr_nonblock(int, int);
 int     acr_cloexec(int);
+int     acr_waitio(int, int, int);
 
 /** Common large file replacement macros
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c?rev=900330&r1=900329&r2=900330&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c Mon Jan 18 
10:27:59 2010
@@ -26,66 +26,6 @@
 #include "acr_file.h"
 #include "acr_fileio.h"
 
-/* Platform includes
- */
-#include <sys/time.h>
-
-
-static int wait_for_io_or_timeout(acr_file_t *f, int for_read)
-{
-    int rc;
-    acr_time_t     prev, nexttime;
-    struct timeval t;
-    int    timeout  = f->timeout;
-
-    f->ppoll.fd     = f->fd;
-    f->ppoll.events = for_read ? POLLIN : POLLOUT;
-    /* Store the the current time
-     */
-    gettimeofday(&t, NULL);
-    prev = ((acr_time_t)t.tv_sec * 1000) + t.tv_usec / 1000;
-    for (;;) {
-        rc = poll(&f->ppoll, 1, timeout);
-        if (rc == -1 && errno == EINTR) {
-            if (timeout >= 0) {
-                gettimeofday(&t, NULL);
-                nexttime = ((acr_time_t)t.tv_sec * 1000) + t.tv_usec / 1000;
-                timeout -= (int)(nexttime - prev);
-                if (timeout <= 0) {
-                    rc = 0;
-                    break;
-                }
-                prev = nexttime;
-            }
-        }
-        else
-            break;
-    }
-    if (rc == 0)
-        return ACR_TIMEUP;
-    else if (rc > 0)
-        return ACR_SUCCESS;
-    else
-        return errno;
-}
-
-static int wait_for_io(acr_file_t *f, int for_read)
-{
-    int rc;
-    f->ppoll.fd     = f->fd;
-    f->ppoll.events = for_read ? POLLIN : POLLOUT;
-
-    do {
-        rc = poll(&f->ppoll, 1, -1);
-    } while (rc == -1 && errno == EINTR);
-    if (rc == 0)
-        return ACR_TIMEUP;
-    else if (rc > 0)
-        return ACR_SUCCESS;
-    else
-        return errno;
-}
-
 ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, read0)(ACR_JNISTDARGS,
                                                  jint file)
 {
@@ -108,7 +48,7 @@
     rd = r_read(f->fd, &c, 1);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_read(f->fd, &c, 1);
         }
     }
@@ -185,7 +125,7 @@
     rd = r_read(f->fd, bb, cs);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_read(f->fd, bb, cs);
         }
     }
@@ -268,7 +208,7 @@
     rd = r_read(f->fd, pb + po, cs);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_read(f->fd, pb + po, cs);
         }
     }
@@ -340,7 +280,7 @@
     rd = r_read(f->fd, pb + po, cs);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_read(f->fd, pb + po, cs);
         }
     }
@@ -393,7 +333,7 @@
     rd = r_pread(f->fd, &c, 1, (off_t)pos);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_pread(f->fd, &c, 1, (off_t)pos);
         }
     }
@@ -471,7 +411,7 @@
     rd = r_pread(f->fd, bb, cs, (off_t)pos);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_pread(f->fd, bb, cs, (off_t)pos);
         }
     }
@@ -555,7 +495,7 @@
     rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
         }
     }
@@ -628,7 +568,7 @@
     rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
     if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_RDWAITIO)) == 0) {
             rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
         }
     }
@@ -678,7 +618,7 @@
     wr = r_write(f->fd, &c, 1);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_write(f->fd, &c, 1);
         }
     }
@@ -735,7 +675,7 @@
     wr = r_write(f->fd, bb + po, cs);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_write(f->fd, bb + po, cs);
         }
     }
@@ -798,7 +738,7 @@
     wr = r_write(f->fd, pb + po, cs);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_write(f->fd, pb + po, cs);
         }
     }
@@ -865,7 +805,7 @@
     wr = r_write(f->fd, pb + po, cs);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_write(f->fd, pb + po, cs);
         }
     }
@@ -951,7 +891,7 @@
     } while (wr == -1 && errno == EAGAIN);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             do {
                 wr = writev(f->fd, iov, pl);
             } while (wr == -1 && errno == EAGAIN);
@@ -1036,7 +976,7 @@
     } while (wr == -1 && errno == EAGAIN);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             do {
                 wr = writev(f->fd, iov, pl);
             } while (wr == -1 && errno == EAGAIN);
@@ -1089,7 +1029,7 @@
     wr = r_pwrite(f->fd, &c, 1, (off_t)pos);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_pwrite(f->fd, &c, 1, (off_t)pos);
         }
     }
@@ -1147,7 +1087,7 @@
     wr = r_pwrite(f->fd, bb + po, cs, (off_t)pos);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_pwrite(f->fd, bb + po, cs, (off_t)pos);
         }
     }
@@ -1211,7 +1151,7 @@
     wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
         }
     }
@@ -1279,7 +1219,7 @@
     wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
     if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
         f->timeout != 0) {
-        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+        if ((rc = acr_waitio(f->fd, f->timeout, ACR_WRWAITIO)) == 0) {
             wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
         }
     }
@@ -1338,7 +1278,7 @@
     do {
         wr = r_write(f->fd, wb, cs);
         if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
-            if ((rc = wait_for_io(f, 0)) == 0) {
+            if ((rc = acr_waitio(f->fd, -1, ACR_WRWAITIO)) == 0) {
                 wr = r_write(f->fd, wb, cs);
             }
             else if (rc != ACR_TIMEUP)
@@ -1411,7 +1351,7 @@
     do {
         wr = r_write(f->fd, pb, cs);
         if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
-            if ((rc = wait_for_io(f, 0)) == 0) {
+            if ((rc = acr_waitio(f->fd, -1, ACR_WRWAITIO)) == 0) {
                 wr = r_write(f->fd, pb, cs);
             }
             else if (rc != ACR_TIMEUP)
@@ -1488,7 +1428,7 @@
     do {
         wr = r_write(f->fd, pb, cs);
         if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
-            if ((rc = wait_for_io(f, 0)) == 0) {
+            if ((rc = acr_waitio(f->fd, -1, ACR_WRWAITIO)) == 0) {
                 wr = r_write(f->fd, pb, cs);
             }
             else if (rc != ACR_TIMEUP)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c?rev=900330&r1=900329&r2=900330&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/uutils.c Mon Jan 18 
10:27:59 2010
@@ -24,6 +24,10 @@
 #include "acr_file.h"
 #include "acr_time.h"
 
+/* Platform includes
+ */
+#include <sys/time.h>
+
 #if defined(DEBUG) || defined(_DEBUG)
 void acr_dbprintf(const char *file, int line, const char *format, ...)
 {
@@ -296,64 +300,47 @@
 #define POLLRDBAND POLLPRI
 #endif
 
-int arc_pollrd(int fd, acr_time_t timeout)
+int acr_waitio(int fd, int timeout, int events)
 {
-    int rc;
-    int msecs = 0;
-    struct pollfd pd;
-
-    pd.fd      = fd;
-    pd.events  = POLLIN;
-    pd.revents = 0;
-
-    if (timeout > 0)
-        msecs = (int)ACR_TIME_AS_MSEC(timeout);
-    else if (timeout < 0)
-        msecs = -1;
-
-    do {
-        rc = poll(&pd, 1, msecs);
-    } while (rc == -1 && errno == EINTR);
+    int            rc;
+    time_t         nexttime, prevtime = 0;
+    struct timeval t;
+    struct pollfd  pd = { fd, events, 0 };
 
+    /* Store the the current time
+     */
+    if (timeout > 0) {
+        gettimeofday(&t, NULL);
+        prevtime = t.tv_sec * 1000 + t.tv_usec / 1000;
+    }
+    for (;;) {
+        rc = poll(&pd, 1, timeout);
+        if (rc == -1 && errno == EINTR) {
+            if (timeout > 0) {
+                gettimeofday(&t, NULL);
+                nexttime = t.tv_sec * 1000 + t.tv_usec / 1000;
+                timeout -= (int)(nexttime - prevtime);
+                if (timeout < 0) {
+                    rc = 0;
+                    break;
+                }
+                prevtime = nexttime;
+            }
+        }
+        else
+            break;
+    }
     if (rc < 0)
-        return ACR_GET_OS_ERROR();
+        return errno;
     else if (rc == 0)
         return ACR_TIMEUP;
-    else if (pd.revents & POLLNVAL)
-        return ACR_EBADF;
-    else if (pd.revents & (POLLHUP | POLLERR))
-        return ACR_EIO;
-    else
+    if (pd.revents & events)
         return 0;
-}
-
-int arc_pollwr(int fd, acr_time_t timeout)
-{
-    int rc;
-    int msecs = 0;
-    struct pollfd pd;
-
-    pd.fd      = fd;
-    pd.events  = POLLOUT;
-    pd.revents = 0;
-
-    if (timeout > 0)
-        msecs = (int)ACR_TIME_AS_MSEC(timeout);
-    else if (timeout < 0)
-        msecs = -1;
-    do {
-        rc = poll(&pd, 1, msecs);
-    } while (rc == -1 && errno == EINTR);
-
-    if (rc < 0)
-        return ACR_GET_OS_ERROR();
-    else if (rc == 0)
-        return ACR_TIMEUP;
     else if (pd.revents & POLLNVAL)
         return ACR_EBADF;
-    else if (pd.revents & (POLLHUP | POLLERR))
+    else if (pd.revents & POLLERR)
         return ACR_EIO;
     else
-        return 0;
+        return ACR_EPIPE;
 }
 


Reply via email to