6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
authormartin
Wed, 01 Sep 2010 09:45:08 -0700
changeset 6520 0e7cf575332e
parent 6519 1a7d0030a638
child 6521 3b995fa42e90
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups Summary: Avoid *Critical; fix compile warnings; improve readability Reviewed-by: alanb
jdk/make/java/nio/mapfile-linux
jdk/make/java/nio/mapfile-solaris
jdk/src/share/classes/sun/nio/ch/IOUtil.java
jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
jdk/src/solaris/classes/sun/nio/ch/EPollSelectorImpl.java
jdk/src/solaris/classes/sun/nio/ch/PipeImpl.java
jdk/src/solaris/classes/sun/nio/ch/PollSelectorImpl.java
jdk/src/solaris/native/sun/nio/ch/IOUtil.c
--- a/jdk/make/java/nio/mapfile-linux	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/make/java/nio/mapfile-linux	Wed Sep 01 09:45:08 2010 -0700
@@ -89,7 +89,7 @@
                 Java_sun_nio_ch_IOUtil_drain;
                 Java_sun_nio_ch_IOUtil_fdVal;
                 Java_sun_nio_ch_IOUtil_initIDs;
-                Java_sun_nio_ch_IOUtil_initPipe;
+                Java_sun_nio_ch_IOUtil_makePipe;
                 Java_sun_nio_ch_IOUtil_randomBytes;
                 Java_sun_nio_ch_IOUtil_setfdVal;
 		Java_sun_nio_ch_NativeThread_current;
--- a/jdk/make/java/nio/mapfile-solaris	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/make/java/nio/mapfile-solaris	Wed Sep 01 09:45:08 2010 -0700
@@ -76,7 +76,7 @@
                 Java_sun_nio_ch_IOUtil_drain;
                 Java_sun_nio_ch_IOUtil_fdVal;
                 Java_sun_nio_ch_IOUtil_initIDs;
-                Java_sun_nio_ch_IOUtil_initPipe;
+                Java_sun_nio_ch_IOUtil_makePipe;
                 Java_sun_nio_ch_IOUtil_randomBytes;
                 Java_sun_nio_ch_IOUtil_setfdVal;
 		Java_sun_nio_ch_NativeThread_current;
--- a/jdk/src/share/classes/sun/nio/ch/IOUtil.java	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/share/classes/sun/nio/ch/IOUtil.java	Wed Sep 01 09:45:08 2010 -0700
@@ -319,7 +319,12 @@
 
     static native boolean randomBytes(byte[] someBytes);
 
-    static native void initPipe(int[] fda, boolean blocking);
+    /**
+     * Returns two file descriptors for a pipe encoded in a long.
+     * The read end of the pipe is returned in the high 32 bits,
+     * while the write end is returned in the low 32 bits.
+     */
+    static native long makePipe(boolean blocking);
 
     static native boolean drain(int fd) throws IOException;
 
--- a/jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java	Wed Sep 01 09:45:08 2010 -0700
@@ -65,10 +65,9 @@
      */
     DevPollSelectorImpl(SelectorProvider sp) {
         super(sp);
-        int[] fdes = new int[2];
-        IOUtil.initPipe(fdes, false);
-        fd0 = fdes[0];
-        fd1 = fdes[1];
+        long pipeFds = IOUtil.makePipe(false);
+        fd0 = (int) (pipeFds >>> 32);
+        fd1 = (int) pipeFds;
         pollWrapper = new DevPollArrayWrapper();
         pollWrapper.initInterrupt(fd0, fd1);
         fdToKey = new HashMap<Integer,SelectionKeyImpl>();
@@ -147,7 +146,7 @@
         selectedKeys = null;
 
         // Deregister channels
-        Iterator i = keys.iterator();
+        Iterator<SelectionKey> i = keys.iterator();
         while (i.hasNext()) {
             SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
             deregister(ski);
--- a/jdk/src/solaris/classes/sun/nio/ch/EPollSelectorImpl.java	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/solaris/classes/sun/nio/ch/EPollSelectorImpl.java	Wed Sep 01 09:45:08 2010 -0700
@@ -62,10 +62,9 @@
      */
     EPollSelectorImpl(SelectorProvider sp) {
         super(sp);
-        int[] fdes = new int[2];
-        IOUtil.initPipe(fdes, false);
-        fd0 = fdes[0];
-        fd1 = fdes[1];
+        long pipeFds = IOUtil.makePipe(false);
+        fd0 = (int) (pipeFds >>> 32);
+        fd1 = (int) pipeFds;
         pollWrapper = new EPollArrayWrapper();
         pollWrapper.initInterrupt(fd0, fd1);
         fdToKey = new HashMap<Integer,SelectionKeyImpl>();
@@ -144,7 +143,7 @@
         selectedKeys = null;
 
         // Deregister channels
-        Iterator i = keys.iterator();
+        Iterator<SelectionKey> i = keys.iterator();
         while (i.hasNext()) {
             SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
             deregister(ski);
--- a/jdk/src/solaris/classes/sun/nio/ch/PipeImpl.java	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/solaris/classes/sun/nio/ch/PipeImpl.java	Wed Sep 01 09:45:08 2010 -0700
@@ -39,13 +39,14 @@
     private final SinkChannel sink;
 
     PipeImpl(SelectorProvider sp) {
-        int[] fdes = new int[2];
-        IOUtil.initPipe(fdes, true);
+        long pipeFds = IOUtil.makePipe(true);
+        int readFd = (int) (pipeFds >>> 32);
+        int writeFd = (int) pipeFds;
         FileDescriptor sourcefd = new FileDescriptor();
-        IOUtil.setfdVal(sourcefd, fdes[0]);
+        IOUtil.setfdVal(sourcefd, readFd);
         source = new SourceChannelImpl(sp, sourcefd);
         FileDescriptor sinkfd = new FileDescriptor();
-        IOUtil.setfdVal(sinkfd, fdes[1]);
+        IOUtil.setfdVal(sinkfd, writeFd);
         sink = new SinkChannelImpl(sp, sinkfd);
     }
 
--- a/jdk/src/solaris/classes/sun/nio/ch/PollSelectorImpl.java	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/solaris/classes/sun/nio/ch/PollSelectorImpl.java	Wed Sep 01 09:45:08 2010 -0700
@@ -54,10 +54,9 @@
      */
     PollSelectorImpl(SelectorProvider sp) {
         super(sp, 1, 1);
-        int[] fdes = new int[2];
-        IOUtil.initPipe(fdes, false);
-        fd0 = fdes[0];
-        fd1 = fdes[1];
+        long pipeFds = IOUtil.makePipe(false);
+        fd0 = (int) (pipeFds >>> 32);
+        fd1 = (int) pipeFds;
         pollWrapper = new PollArrayWrapper(INIT_CAP);
         pollWrapper.initInterrupt(fd0, fd1);
         channelArray = new SelectionKeyImpl[INIT_CAP];
--- a/jdk/src/solaris/native/sun/nio/ch/IOUtil.c	Tue Aug 31 09:17:46 2010 -0700
+++ b/jdk/src/solaris/native/sun/nio/ch/IOUtil.c	Wed Sep 01 09:45:08 2010 -0700
@@ -67,12 +67,9 @@
 configureBlocking(int fd, jboolean blocking)
 {
     int flags = fcntl(fd, F_GETFL);
+    int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
 
-    if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))
-        return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
-    else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))
-        return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
-    return 0;
+    return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);
 }
 
 JNIEXPORT void JNICALL
@@ -83,27 +80,25 @@
         JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
 }
 
-JNIEXPORT void JNICALL
-Java_sun_nio_ch_IOUtil_initPipe(JNIEnv *env, jobject this,
-                                    jintArray intArray, jboolean block)
+JNIEXPORT jlong JNICALL
+Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
 {
     int fd[2];
-    jint *ptr = 0;
 
     if (pipe(fd) < 0) {
         JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
-        return;
+        return 0;
     }
-    if (block == JNI_FALSE) {
+    if (blocking == JNI_FALSE) {
         if ((configureBlocking(fd[0], JNI_FALSE) < 0)
             || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
             JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
+            close(fd[0]);
+            close(fd[1]);
+            return 0;
         }
     }
-    ptr = (*env)->GetPrimitiveArrayCritical(env, intArray, 0);
-    ptr[0] = fd[0];
-    ptr[1] = fd[1];
-    (*env)->ReleasePrimitiveArrayCritical(env, intArray, ptr, 0);
+    return ((jlong) fd[0] << 32) | (jlong) fd[1];
 }
 
 JNIEXPORT jboolean JNICALL
@@ -131,21 +126,22 @@
 {
     if (n > 0) /* Number of bytes written */
         return n;
-    if (n < 0) {
-        if (errno == EAGAIN)
-            return IOS_UNAVAILABLE;
-        if (errno == EINTR)
-            return IOS_INTERRUPTED;
-    }
-    if (n == 0) {
+    else if (n == 0) {
         if (reading) {
             return IOS_EOF; /* EOF is -1 in javaland */
         } else {
             return 0;
         }
     }
-    JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
-    return IOS_THROWN;
+    else if (errno == EAGAIN)
+        return IOS_UNAVAILABLE;
+    else if (errno == EINTR)
+        return IOS_INTERRUPTED;
+    else {
+        const char *msg = reading ? "Read failed" : "Write failed";
+        JNU_ThrowIOExceptionWithLastError(env, msg);
+        return IOS_THROWN;
+    }
 }
 
 /* Declared in nio_util.h for use elsewhere in NIO */
@@ -155,21 +151,22 @@
 {
     if (n > 0) /* Number of bytes written */
         return n;
-    if (n < 0) {
-        if (errno == EAGAIN)
-            return IOS_UNAVAILABLE;
-        if (errno == EINTR)
-            return IOS_INTERRUPTED;
-    }
-    if (n == 0) {
+    else if (n == 0) {
         if (reading) {
             return IOS_EOF; /* EOF is -1 in javaland */
         } else {
             return 0;
         }
     }
-    JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
-    return IOS_THROWN;
+    else if (errno == EAGAIN)
+        return IOS_UNAVAILABLE;
+    else if (errno == EINTR)
+        return IOS_INTERRUPTED;
+    else {
+        const char *msg = reading ? "Read failed" : "Write failed";
+        JNU_ThrowIOExceptionWithLastError(env, msg);
+        return IOS_THROWN;
+    }
 }
 
 jint