diff options
4 files changed, 27 insertions, 22 deletions
diff --git a/luni/src/main/java/java/nio/DatagramChannelImpl.java b/luni/src/main/java/java/nio/DatagramChannelImpl.java index 2dc86c7..39f2128 100644 --- a/luni/src/main/java/java/nio/DatagramChannelImpl.java +++ b/luni/src/main/java/java/nio/DatagramChannelImpl.java @@ -414,9 +414,7 @@ class DatagramChannelImpl extends DatagramChannel implements FileDescriptorChann } @Override protected void implConfigureBlocking(boolean blocking) throws IOException { - synchronized (blockingLock()) { - IoUtils.setBlocking(fd, blocking); - } + IoUtils.setBlocking(fd, blocking); } /* diff --git a/luni/src/main/java/java/nio/ServerSocketChannelImpl.java b/luni/src/main/java/java/nio/ServerSocketChannelImpl.java index 5331158..3fb61a3 100644 --- a/luni/src/main/java/java/nio/ServerSocketChannelImpl.java +++ b/luni/src/main/java/java/nio/ServerSocketChannelImpl.java @@ -31,7 +31,9 @@ import java.nio.channels.NotYetBoundException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.channels.spi.SelectorProvider; +import libcore.io.ErrnoException; import libcore.io.IoUtils; +import static libcore.io.OsConstants.*; /** * The default ServerSocketChannel. @@ -65,21 +67,17 @@ final class ServerSocketChannelImpl extends ServerSocketChannel implements FileD // Create an empty socket channel. This will be populated by ServerSocketAdapter.accept. SocketChannelImpl result = new SocketChannelImpl(provider(), false); - boolean connected = false; try { begin(); synchronized (acceptLock) { - synchronized (blockingLock()) { - do { - try { - socket.implAccept(result); - // select successfully, break out immediately. - break; - } catch (SocketTimeoutException e) { - // continue to accept if the channel is in blocking mode. - // TODO: does this make sense? why does blocking imply no timeouts? - } - } while (isBlocking()); + try { + socket.implAccept(result); + } catch (SocketTimeoutException e) { + if (shouldThrowSocketTimeoutExceptionFromAccept(e)) { + throw e; + } + // Otherwise, this is a non-blocking socket and there's nothing ready, so we'll + // fall through and return null. } } } finally { @@ -88,10 +86,21 @@ final class ServerSocketChannelImpl extends ServerSocketChannel implements FileD return result.socket().isConnected() ? result : null; } - @Override protected void implConfigureBlocking(boolean blocking) throws IOException { - synchronized (blockingLock()) { - IoUtils.setBlocking(impl.getFD$(), blocking); + private boolean shouldThrowSocketTimeoutExceptionFromAccept(SocketTimeoutException e) { + if (isBlocking()) { + return true; + } + Throwable cause = e.getCause(); + if (cause instanceof ErrnoException) { + if (((ErrnoException) cause).errno == EAGAIN) { + return false; + } } + return true; + } + + @Override protected void implConfigureBlocking(boolean blocking) throws IOException { + IoUtils.setBlocking(impl.getFD$(), blocking); } synchronized protected void implCloseSelectableChannel() throws IOException { diff --git a/luni/src/main/java/java/nio/SocketChannelImpl.java b/luni/src/main/java/java/nio/SocketChannelImpl.java index afb32a4..4093be2 100644 --- a/luni/src/main/java/java/nio/SocketChannelImpl.java +++ b/luni/src/main/java/java/nio/SocketChannelImpl.java @@ -469,9 +469,7 @@ class SocketChannelImpl extends SocketChannel implements FileDescriptorChannel { } @Override protected void implConfigureBlocking(boolean blocking) throws IOException { - synchronized (blockingLock()) { - IoUtils.setBlocking(fd, blocking); - } + IoUtils.setBlocking(fd, blocking); } /* diff --git a/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java b/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java index 2a5682c..6ab91ab 100644 --- a/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java +++ b/luni/src/test/java/libcore/java/nio/channels/SocketChannelTest.java @@ -73,7 +73,7 @@ public class SocketChannelTest extends junit.framework.TestCase { try { sc.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }), 0)); fail(); - } catch (java.net.ConnectException ex) { + } catch (ConnectException ex) { } try { |