提交 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 {
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;
......
......@@ -76,7 +76,7 @@ SUNWprivate_1.1 {
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;
......
......@@ -319,7 +319,12 @@ class IOUtil {
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;
......
......@@ -65,10 +65,9 @@ class DevPollSelectorImpl
*/
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 @@ class DevPollSelectorImpl
selectedKeys = null;
// Deregister channels
Iterator i = keys.iterator();
Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) {
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
deregister(ski);
......
......@@ -62,10 +62,9 @@ class EPollSelectorImpl
*/
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 @@ class EPollSelectorImpl
selectedKeys = null;
// Deregister channels
Iterator i = keys.iterator();
Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) {
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
deregister(ski);
......
......@@ -39,13 +39,14 @@ class PipeImpl
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);
}
......
......@@ -54,10 +54,9 @@ class PollSelectorImpl
*/
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];
......
......@@ -67,12 +67,9 @@ static int
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 @@ Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
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 @@ convertReturnVal(JNIEnv *env, jint n, jboolean reading)
{
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 @@ convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
{
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册