提交 cfe2eb4d 编写于 作者: M martin

6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups

Summary: Avoid *Critical; fix compile warnings; improve readability
Reviewed-by: alanb
上级 8e54f2aa
...@@ -89,7 +89,7 @@ SUNWprivate_1.1 { ...@@ -89,7 +89,7 @@ SUNWprivate_1.1 {
Java_sun_nio_ch_IOUtil_drain; Java_sun_nio_ch_IOUtil_drain;
Java_sun_nio_ch_IOUtil_fdVal; Java_sun_nio_ch_IOUtil_fdVal;
Java_sun_nio_ch_IOUtil_initIDs; 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_randomBytes;
Java_sun_nio_ch_IOUtil_setfdVal; Java_sun_nio_ch_IOUtil_setfdVal;
Java_sun_nio_ch_NativeThread_current; Java_sun_nio_ch_NativeThread_current;
......
...@@ -76,7 +76,7 @@ SUNWprivate_1.1 { ...@@ -76,7 +76,7 @@ SUNWprivate_1.1 {
Java_sun_nio_ch_IOUtil_drain; Java_sun_nio_ch_IOUtil_drain;
Java_sun_nio_ch_IOUtil_fdVal; Java_sun_nio_ch_IOUtil_fdVal;
Java_sun_nio_ch_IOUtil_initIDs; 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_randomBytes;
Java_sun_nio_ch_IOUtil_setfdVal; Java_sun_nio_ch_IOUtil_setfdVal;
Java_sun_nio_ch_NativeThread_current; Java_sun_nio_ch_NativeThread_current;
......
...@@ -319,7 +319,12 @@ class IOUtil { ...@@ -319,7 +319,12 @@ class IOUtil {
static native boolean randomBytes(byte[] someBytes); 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; static native boolean drain(int fd) throws IOException;
......
...@@ -65,10 +65,9 @@ class DevPollSelectorImpl ...@@ -65,10 +65,9 @@ class DevPollSelectorImpl
*/ */
DevPollSelectorImpl(SelectorProvider sp) { DevPollSelectorImpl(SelectorProvider sp) {
super(sp); super(sp);
int[] fdes = new int[2]; long pipeFds = IOUtil.makePipe(false);
IOUtil.initPipe(fdes, false); fd0 = (int) (pipeFds >>> 32);
fd0 = fdes[0]; fd1 = (int) pipeFds;
fd1 = fdes[1];
pollWrapper = new DevPollArrayWrapper(); pollWrapper = new DevPollArrayWrapper();
pollWrapper.initInterrupt(fd0, fd1); pollWrapper.initInterrupt(fd0, fd1);
fdToKey = new HashMap<Integer,SelectionKeyImpl>(); fdToKey = new HashMap<Integer,SelectionKeyImpl>();
...@@ -147,7 +146,7 @@ class DevPollSelectorImpl ...@@ -147,7 +146,7 @@ class DevPollSelectorImpl
selectedKeys = null; selectedKeys = null;
// Deregister channels // Deregister channels
Iterator i = keys.iterator(); Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) { while (i.hasNext()) {
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
deregister(ski); deregister(ski);
......
...@@ -62,10 +62,9 @@ class EPollSelectorImpl ...@@ -62,10 +62,9 @@ class EPollSelectorImpl
*/ */
EPollSelectorImpl(SelectorProvider sp) { EPollSelectorImpl(SelectorProvider sp) {
super(sp); super(sp);
int[] fdes = new int[2]; long pipeFds = IOUtil.makePipe(false);
IOUtil.initPipe(fdes, false); fd0 = (int) (pipeFds >>> 32);
fd0 = fdes[0]; fd1 = (int) pipeFds;
fd1 = fdes[1];
pollWrapper = new EPollArrayWrapper(); pollWrapper = new EPollArrayWrapper();
pollWrapper.initInterrupt(fd0, fd1); pollWrapper.initInterrupt(fd0, fd1);
fdToKey = new HashMap<Integer,SelectionKeyImpl>(); fdToKey = new HashMap<Integer,SelectionKeyImpl>();
...@@ -144,7 +143,7 @@ class EPollSelectorImpl ...@@ -144,7 +143,7 @@ class EPollSelectorImpl
selectedKeys = null; selectedKeys = null;
// Deregister channels // Deregister channels
Iterator i = keys.iterator(); Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) { while (i.hasNext()) {
SelectionKeyImpl ski = (SelectionKeyImpl)i.next(); SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
deregister(ski); deregister(ski);
......
...@@ -39,13 +39,14 @@ class PipeImpl ...@@ -39,13 +39,14 @@ class PipeImpl
private final SinkChannel sink; private final SinkChannel sink;
PipeImpl(SelectorProvider sp) { PipeImpl(SelectorProvider sp) {
int[] fdes = new int[2]; long pipeFds = IOUtil.makePipe(true);
IOUtil.initPipe(fdes, true); int readFd = (int) (pipeFds >>> 32);
int writeFd = (int) pipeFds;
FileDescriptor sourcefd = new FileDescriptor(); FileDescriptor sourcefd = new FileDescriptor();
IOUtil.setfdVal(sourcefd, fdes[0]); IOUtil.setfdVal(sourcefd, readFd);
source = new SourceChannelImpl(sp, sourcefd); source = new SourceChannelImpl(sp, sourcefd);
FileDescriptor sinkfd = new FileDescriptor(); FileDescriptor sinkfd = new FileDescriptor();
IOUtil.setfdVal(sinkfd, fdes[1]); IOUtil.setfdVal(sinkfd, writeFd);
sink = new SinkChannelImpl(sp, sinkfd); sink = new SinkChannelImpl(sp, sinkfd);
} }
......
...@@ -54,10 +54,9 @@ class PollSelectorImpl ...@@ -54,10 +54,9 @@ class PollSelectorImpl
*/ */
PollSelectorImpl(SelectorProvider sp) { PollSelectorImpl(SelectorProvider sp) {
super(sp, 1, 1); super(sp, 1, 1);
int[] fdes = new int[2]; long pipeFds = IOUtil.makePipe(false);
IOUtil.initPipe(fdes, false); fd0 = (int) (pipeFds >>> 32);
fd0 = fdes[0]; fd1 = (int) pipeFds;
fd1 = fdes[1];
pollWrapper = new PollArrayWrapper(INIT_CAP); pollWrapper = new PollArrayWrapper(INIT_CAP);
pollWrapper.initInterrupt(fd0, fd1); pollWrapper.initInterrupt(fd0, fd1);
channelArray = new SelectionKeyImpl[INIT_CAP]; channelArray = new SelectionKeyImpl[INIT_CAP];
......
...@@ -67,12 +67,9 @@ static int ...@@ -67,12 +67,9 @@ static int
configureBlocking(int fd, jboolean blocking) configureBlocking(int fd, jboolean blocking)
{ {
int flags = fcntl(fd, F_GETFL); int flags = fcntl(fd, F_GETFL);
int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK)) return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags);
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;
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
...@@ -83,27 +80,25 @@ Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, ...@@ -83,27 +80,25 @@ Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
} }
JNIEXPORT void JNICALL JNIEXPORT jlong JNICALL
Java_sun_nio_ch_IOUtil_initPipe(JNIEnv *env, jobject this, Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking)
jintArray intArray, jboolean block)
{ {
int fd[2]; int fd[2];
jint *ptr = 0;
if (pipe(fd) < 0) { if (pipe(fd) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
return; return 0;
} }
if (block == JNI_FALSE) { if (blocking == JNI_FALSE) {
if ((configureBlocking(fd[0], JNI_FALSE) < 0) if ((configureBlocking(fd[0], JNI_FALSE) < 0)
|| (configureBlocking(fd[1], JNI_FALSE) < 0)) { || (configureBlocking(fd[1], JNI_FALSE) < 0)) {
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
close(fd[0]);
close(fd[1]);
return 0;
} }
} }
ptr = (*env)->GetPrimitiveArrayCritical(env, intArray, 0); return ((jlong) fd[0] << 32) | (jlong) fd[1];
ptr[0] = fd[0];
ptr[1] = fd[1];
(*env)->ReleasePrimitiveArrayCritical(env, intArray, ptr, 0);
} }
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
...@@ -131,21 +126,22 @@ convertReturnVal(JNIEnv *env, jint n, jboolean reading) ...@@ -131,21 +126,22 @@ convertReturnVal(JNIEnv *env, jint n, jboolean reading)
{ {
if (n > 0) /* Number of bytes written */ if (n > 0) /* Number of bytes written */
return n; return n;
if (n < 0) { else if (n == 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if (errno == EINTR)
return IOS_INTERRUPTED;
}
if (n == 0) {
if (reading) { if (reading) {
return IOS_EOF; /* EOF is -1 in javaland */ return IOS_EOF; /* EOF is -1 in javaland */
} else { } else {
return 0; return 0;
} }
} }
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed"); 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; return IOS_THROWN;
}
} }
/* Declared in nio_util.h for use elsewhere in NIO */ /* Declared in nio_util.h for use elsewhere in NIO */
...@@ -155,21 +151,22 @@ convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) ...@@ -155,21 +151,22 @@ convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
{ {
if (n > 0) /* Number of bytes written */ if (n > 0) /* Number of bytes written */
return n; return n;
if (n < 0) { else if (n == 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
if (errno == EINTR)
return IOS_INTERRUPTED;
}
if (n == 0) {
if (reading) { if (reading) {
return IOS_EOF; /* EOF is -1 in javaland */ return IOS_EOF; /* EOF is -1 in javaland */
} else { } else {
return 0; return 0;
} }
} }
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed"); 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; return IOS_THROWN;
}
} }
jint jint
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册