From 6cddd10e0a15f0cb1adc758845f2d348b3ead2df Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 25 Mar 2008 16:17:29 -0600 Subject: [PATCH] various code cleanups, bug fixes, and compatibility fixes in NIO code --- classpath/java-nio.cpp | 12 +++++++----- .../java/nio/channels/SelectableChannel.java | 4 +--- classpath/java/nio/channels/SelectionKey.java | 3 ++- classpath/java/nio/channels/Selector.java | 2 +- classpath/java/nio/channels/SocketChannel.java | 2 +- .../java/nio/channels/SocketSelector.java | 18 +++++++----------- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/classpath/java-nio.cpp b/classpath/java-nio.cpp index aee81139e7..08b391bc6b 100644 --- a/classpath/java-nio.cpp +++ b/classpath/java-nio.cpp @@ -575,6 +575,13 @@ Java_java_nio_channels_SocketSelector_natDoSocketSelect(JNIEnv *e, jclass, timeval time = { interval / 1000, (interval % 1000) * 1000 }; int r = ::select(max + 1, &(s->read), &(s->write), &(s->except), &time); + if (r < 0) { + if (errno != EINTR) { + throwIOException(e); + return 0; + } + } + #ifdef WIN32 if (FD_ISSET(s->control.writer(), &(s->write)) or FD_ISSET(s->control.writer(), &(s->except))) @@ -618,11 +625,6 @@ Java_java_nio_channels_SocketSelector_natDoSocketSelect(JNIEnv *e, jclass, } } - if (r < 0) { - if (errno != EINTR) { - throwIOException(e); - } - } return r; } diff --git a/classpath/java/nio/channels/SelectableChannel.java b/classpath/java/nio/channels/SelectableChannel.java index 286337a40d..a49d6a292b 100644 --- a/classpath/java/nio/channels/SelectableChannel.java +++ b/classpath/java/nio/channels/SelectableChannel.java @@ -22,8 +22,7 @@ public abstract class SelectableChannel implements Channel { public SelectionKey register(Selector selector, int interestOps, Object attachment) { - SelectionKey key = new SelectionKey - (this, selector, interestOps, attachment); + key = new SelectionKey(this, selector, interestOps, attachment); selector.add(key); return key; } @@ -34,7 +33,6 @@ public abstract class SelectableChannel implements Channel { public void close() throws IOException { if (key != null) { - key.selector().remove(key); key = null; } } diff --git a/classpath/java/nio/channels/SelectionKey.java b/classpath/java/nio/channels/SelectionKey.java index 6d96ed2422..fbb1159d92 100644 --- a/classpath/java/nio/channels/SelectionKey.java +++ b/classpath/java/nio/channels/SelectionKey.java @@ -36,8 +36,9 @@ public class SelectionKey { return interestOps; } - public void interestOps(int v) { + public SelectionKey interestOps(int v) { this.interestOps = v; + return this; } public int readyOps() { diff --git a/classpath/java/nio/channels/Selector.java b/classpath/java/nio/channels/Selector.java index 2e24fa69b9..021f267f17 100644 --- a/classpath/java/nio/channels/Selector.java +++ b/classpath/java/nio/channels/Selector.java @@ -40,7 +40,7 @@ public abstract class Selector { public abstract boolean isOpen(); - public abstract void wakeup(); + public abstract Selector wakeup(); public abstract int select(long interval) throws IOException; diff --git a/classpath/java/nio/channels/SocketChannel.java b/classpath/java/nio/channels/SocketChannel.java index 79bcccf2ad..cc4b2abb90 100644 --- a/classpath/java/nio/channels/SocketChannel.java +++ b/classpath/java/nio/channels/SocketChannel.java @@ -44,7 +44,7 @@ public class SocketChannel extends SelectableChannel } public void close() throws IOException { - if (! isOpen()) { + if (isOpen()) { super.close(); closeSocket(); } diff --git a/classpath/java/nio/channels/SocketSelector.java b/classpath/java/nio/channels/SocketSelector.java index c32bc9be32..587340a508 100644 --- a/classpath/java/nio/channels/SocketSelector.java +++ b/classpath/java/nio/channels/SocketSelector.java @@ -14,15 +14,10 @@ import java.io.IOException; import java.util.Iterator; class SocketSelector extends Selector { - private static final boolean IsWin32; protected long state; protected final Object lock = new Object(); protected boolean woken = false; - static { - IsWin32 = false; - } - public SocketSelector() { state = natInit(); } @@ -31,7 +26,7 @@ class SocketSelector extends Selector { return state != 0; } - public void wakeup() { + public Selector wakeup() { synchronized (lock) { if (! woken) { woken = true; @@ -39,6 +34,7 @@ class SocketSelector extends Selector { natWakeup(state); } } + return this; } private boolean clearWoken() { @@ -64,14 +60,14 @@ class SocketSelector extends Selector { SelectionKey key = it.next(); SocketChannel c = (SocketChannel)key.channel(); int socket = c.socketFD(); - if (! c.isOpen()) { + if (c.isOpen()) { + key.readyOps(0); + max = natSelectUpdateInterestSet + (socket, key.interestOps(), state, max); + } else { natSelectClearAll(socket, state); it.remove(); - continue; } - - key.readyOps(0); - max = natSelectUpdateInterestSet(socket, key.interestOps(), state, max); } int r = natDoSocketSelect(state, max, interval);