< prev index next >

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Print this page

        

*** 53,62 **** --- 53,63 ---- import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.concurrent.locks.ReentrantLock; + import jdk.internal.misc.Strands; import sun.net.ConnectionResetException; import sun.net.NetHooks; import sun.net.ext.ExtendedSocketOptions; import sun.net.util.SocketExceptions;
*** 114,123 **** --- 115,127 ---- private InetSocketAddress remoteAddress; // Socket adaptor, created on demand private Socket socket; + // lazily set to true when the socket is configured non-blocking + private volatile boolean nonBlocking; + // -- End of fields protected by stateLock // Constructor for normal connecting sockets //
*** 362,371 **** --- 366,376 ---- // check if input is shutdown if (isInputClosed) return IOStatus.EOF; + lockedConfigureNonBlockingIfFiber(); n = IOUtil.read(fd, buf, -1, nd); if (blocking) { while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLIN); n = IOUtil.read(fd, buf, -1, nd);
*** 404,413 **** --- 409,419 ---- // check if input is shutdown if (isInputClosed) return IOStatus.EOF; + lockedConfigureNonBlockingIfFiber(); n = IOUtil.read(fd, dsts, offset, length, nd); if (blocking) { while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLIN); n = IOUtil.read(fd, dsts, offset, length, nd);
*** 479,488 **** --- 485,495 ---- try { boolean blocking = isBlocking(); int n = 0; try { beginWrite(blocking); + lockedConfigureNonBlockingIfFiber(); n = IOUtil.write(fd, buf, -1, nd); if (blocking) { while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLOUT); n = IOUtil.write(fd, buf, -1, nd);
*** 509,518 **** --- 516,526 ---- try { boolean blocking = isBlocking(); long n = 0; try { beginWrite(blocking); + lockedConfigureNonBlockingIfFiber(); n = IOUtil.write(fd, srcs, offset, length, nd); if (blocking) { while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLOUT); n = IOUtil.write(fd, srcs, offset, length, nd);
*** 537,552 **** try { boolean blocking = isBlocking(); int n = 0; try { beginWrite(blocking); ! if (blocking) { ! do { ! n = Net.sendOOB(fd, b); ! } while (n == IOStatus.INTERRUPTED && isOpen()); ! } else { n = Net.sendOOB(fd, b); } } finally { endWrite(blocking, n > 0); if (n <= 0 && isOutputClosed) throw new AsynchronousCloseException(); --- 545,560 ---- try { boolean blocking = isBlocking(); int n = 0; try { beginWrite(blocking); ! lockedConfigureNonBlockingIfFiber(); ! do { n = Net.sendOOB(fd, b); + } while (n == IOStatus.INTERRUPTED && isOpen()); + if (blocking && n == IOStatus.UNAVAILABLE) { + throw new SocketException("No buffer space available"); } } finally { endWrite(blocking, n > 0); if (n <= 0 && isOutputClosed) throw new AsynchronousCloseException();
*** 571,587 **** readLock.unlock(); } } /** ! * Adjust the blocking mode while holding the readLock or writeLock. */ private void lockedConfigureBlocking(boolean block) throws IOException { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); synchronized (stateLock) { ensureOpen(); ! IOUtil.configureBlocking(fd, block); } } /** * Returns the local address, or null if not bound --- 579,613 ---- readLock.unlock(); } } /** ! * Adjust the blocking mode while holding readLock or writeLock. */ private void lockedConfigureBlocking(boolean block) throws IOException { assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); synchronized (stateLock) { ensureOpen(); ! // do nothing if fiber has forced the socket to be non-blocking ! if (!nonBlocking) { ! IOUtil.configureBlocking(fd, block); ! } ! } ! } ! ! /** ! * Ensures that the socket is configured non-blocking when the current ! * strand is a fiber. ! */ ! private void lockedConfigureNonBlockingIfFiber() throws IOException { ! assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread(); ! if (!nonBlocking && (Strands.currentStrand() instanceof Fiber)) { ! synchronized (stateLock) { ! ensureOpen(); ! IOUtil.configureBlocking(fd, false); ! nonBlocking = true; ! } } } /** * Returns the local address, or null if not bound
*** 727,736 **** --- 753,763 ---- try { boolean blocking = isBlocking(); boolean connected = false; try { beginConnect(blocking, isa); + lockedConfigureNonBlockingIfFiber(); int n = Net.connect(fd, isa.getAddress(), isa.getPort()); if (n > 0) { connected = true; } else if (blocking) { assert IOStatus.okayToRetry(n);
*** 886,899 **** state = ST_CLOSING; if (!tryClose()) { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { nd.preClose(fd); ! if (reader != 0) NativeThread.signal(reader); ! if (writer != 0) NativeThread.signal(writer); } } } } --- 913,929 ---- state = ST_CLOSING; if (!tryClose()) { long reader = readerThread; long writer = writerThread; if (reader != 0 || writer != 0) { + if (NativeThread.isFiber(reader) || NativeThread.isFiber(writer)) { + Poller.stopPoll(fdVal); + } nd.preClose(fd); ! if (NativeThread.isKernelThread(reader)) NativeThread.signal(reader); ! if (NativeThread.isKernelThread(writer)) NativeThread.signal(writer); } } } }
*** 970,982 **** ensureOpen(); if (!isConnected()) throw new NotYetConnectedException(); if (!isInputClosed) { Net.shutdown(fd, Net.SHUT_RD); ! long thread = readerThread; ! if (thread != 0) ! NativeThread.signal(thread); isInputClosed = true; } return this; } } --- 1000,1015 ---- ensureOpen(); if (!isConnected()) throw new NotYetConnectedException(); if (!isInputClosed) { Net.shutdown(fd, Net.SHUT_RD); ! long reader = readerThread; ! if (NativeThread.isFiber(reader)) { ! Poller.stopPoll(fdVal, Net.POLLIN); ! } else if (NativeThread.isKernelThread(reader)) { ! NativeThread.signal(reader); ! } isInputClosed = true; } return this; } }
*** 987,999 **** ensureOpen(); if (!isConnected()) throw new NotYetConnectedException(); if (!isOutputClosed) { Net.shutdown(fd, Net.SHUT_WR); ! long thread = writerThread; ! if (thread != 0) ! NativeThread.signal(thread); isOutputClosed = true; } return this; } } --- 1020,1035 ---- ensureOpen(); if (!isConnected()) throw new NotYetConnectedException(); if (!isOutputClosed) { Net.shutdown(fd, Net.SHUT_WR); ! long writer = writerThread; ! if (NativeThread.isFiber(writer)) { ! Poller.stopPoll(fdVal, Net.POLLOUT); ! } else if (NativeThread.isKernelThread(writer)) { ! NativeThread.signal(writer); ! } isOutputClosed = true; } return this; } }
*** 1147,1156 **** --- 1183,1193 ---- // restore socket to blocking mode lockedConfigureBlocking(true); } } else { // read, no timeout + lockedConfigureNonBlockingIfFiber(); n = tryRead(b, off, len); while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLIN); n = tryRead(b, off, len); }
*** 1206,1215 **** --- 1243,1253 ---- // loop until all bytes have been written int pos = off; int end = off + len; beginWrite(true); try { + lockedConfigureNonBlockingIfFiber(); while (pos < end && isOpen()) { int size = end - pos; int n = tryWrite(b, pos, size); while (IOStatus.okayToRetry(n) && isOpen()) { park(Net.POLLOUT);
< prev index next >