< prev index next >

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

Print this page
*** 106,12 ***
  
      // set to true when the socket is in non-blocking mode
      private volatile boolean nonBlocking;
  
      // used by connect/read/write/accept, protected by stateLock
!     private long readerThread;
!     private long writerThread;
  
      // used when SO_REUSEADDR is emulated, protected by stateLock
      private boolean isReuseAddress;
  
      // read or accept timeout in millis
--- 106,12 ---
  
      // set to true when the socket is in non-blocking mode
      private volatile boolean nonBlocking;
  
      // used by connect/read/write/accept, protected by stateLock
!     private Thread readerThread;
!     private Thread writerThread;
  
      // used when SO_REUSEADDR is emulated, protected by stateLock
      private boolean isReuseAddress;
  
      // read or accept timeout in millis

*** 221,22 ***
       * @throws SocketException if the socket is closed or not connected
       */
      private FileDescriptor beginRead() throws SocketException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
!             readerThread = NativeThread.current();
              return fd;
          }
      }
  
      /**
       * Marks the end of a read operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endRead(boolean completed) throws SocketException {
          synchronized (stateLock) {
!             readerThread = 0;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");
--- 221,22 ---
       * @throws SocketException if the socket is closed or not connected
       */
      private FileDescriptor beginRead() throws SocketException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
!             readerThread = NativeThread.threadToSignal();
              return fd;
          }
      }
  
      /**
       * Marks the end of a read operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endRead(boolean completed) throws SocketException {
          synchronized (stateLock) {
!             readerThread = null;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");

*** 294,10 ***
--- 294,17 ---
          try {
              if (connectionReset)
                  throw new SocketException("Connection reset");
              if (isInputClosed)
                  return -1;
+ 
+             // experimental
+             if (Poller.supportReadOps() && Thread.currentThread().isVirtual()) {
+                 n = Poller.read(fdVal(fd), b, off, len, remainingNanos, this::isOpen);
+                 if (n != IOStatus.UNAVAILABLE) return n;
+             }
+ 
              configureNonBlockingIfNeeded(fd, remainingNanos > 0);
              if (remainingNanos > 0) {
                  // read with timeout
                  n = timedRead(fd, b, off, len, remainingNanos);
              } else {

*** 366,22 ***
       * @throws SocketException if the socket is closed or not connected
       */
      private FileDescriptor beginWrite() throws SocketException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
!             writerThread = NativeThread.current();
              return fd;
          }
      }
  
      /**
       * Marks the end of a write operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endWrite(boolean completed) throws SocketException {
          synchronized (stateLock) {
!             writerThread = 0;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");
--- 373,22 ---
       * @throws SocketException if the socket is closed or not connected
       */
      private FileDescriptor beginWrite() throws SocketException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
!             writerThread = NativeThread.threadToSignal();
              return fd;
          }
      }
  
      /**
       * Marks the end of a write operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endWrite(boolean completed) throws SocketException {
          synchronized (stateLock) {
!             writerThread = null;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");

*** 412,10 ***
--- 419,17 ---
       */
      private int implWrite(byte[] b, int off, int len) throws IOException {
          int n = 0;
          FileDescriptor fd = beginWrite();
          try {
+ 
+             // experimental
+             if (Poller.supportWriteOps() && Thread.currentThread().isVirtual()) {
+                 n = Poller.write(fdVal(fd), b, off, len, this::isOpen);
+                 if (n != IOStatus.UNAVAILABLE) return n;
+             }
+ 
              configureNonBlockingIfNeeded(fd, false);
              n = tryWrite(fd, b, off, len);
              while (IOStatus.okayToRetry(n) && isOpen()) {
                  park(fd, Net.POLLOUT);
                  n = tryWrite(fd, b, off, len);

*** 508,22 ***
  
              // save the remote address/port
              this.address = address;
              this.port = port;
  
!             readerThread = NativeThread.current();
              return fd;
          }
      }
  
      /**
       * Marks the end of a connect operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endConnect(FileDescriptor fd, boolean completed) throws IOException {
          synchronized (stateLock) {
!             readerThread = 0;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (completed && state == ST_CONNECTING) {
                  this.state = ST_CONNECTED;
--- 522,22 ---
  
              // save the remote address/port
              this.address = address;
              this.port = port;
  
!             readerThread = NativeThread.threadToSignal();
              return fd;
          }
      }
  
      /**
       * Marks the end of a connect operation that may have blocked.
       * @throws SocketException is the socket is closed
       */
      private void endConnect(FileDescriptor fd, boolean completed) throws IOException {
          synchronized (stateLock) {
!             readerThread = null;
              int state = this.state;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (completed && state == ST_CONNECTING) {
                  this.state = ST_CONNECTED;

*** 664,11 ***
      private FileDescriptor beginAccept() throws SocketException {
          synchronized (stateLock) {
              ensureOpen();
              if (localport == 0)
                  throw new SocketException("Not bound");
!             readerThread = NativeThread.current();
              return fd;
          }
      }
  
      /**
--- 678,11 ---
      private FileDescriptor beginAccept() throws SocketException {
          synchronized (stateLock) {
              ensureOpen();
              if (localport == 0)
                  throw new SocketException("Not bound");
!             readerThread = NativeThread.threadToSignal();
              return fd;
          }
      }
  
      /**

*** 676,11 ***
       * @throws SocketException is the socket is closed
       */
      private void endAccept(boolean completed) throws SocketException {
          synchronized (stateLock) {
              int state = this.state;
!             readerThread = 0;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");
          }
--- 690,11 ---
       * @throws SocketException is the socket is closed
       */
      private void endAccept(boolean completed) throws SocketException {
          synchronized (stateLock) {
              int state = this.state;
!             readerThread = null;
              if (state == ST_CLOSING)
                  tryFinishClose();
              if (!completed && state >= ST_CLOSING)
                  throw new SocketException("Socket closed");
          }

*** 842,11 ***
      /**
       * Closes the socket if there are no I/O operations in progress.
       */
      private boolean tryClose() throws IOException {
          assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
!         if (readerThread == 0 && writerThread == 0) {
              try {
                  cleaner.clean();
              } catch (UncheckedIOException ioe) {
                  throw ioe.getCause();
              } finally {
--- 856,11 ---
      /**
       * Closes the socket if there are no I/O operations in progress.
       */
      private boolean tryClose() throws IOException {
          assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
!         if (readerThread == null && writerThread == null) {
              try {
                  cleaner.clean();
              } catch (UncheckedIOException ioe) {
                  throw ioe.getCause();
              } finally {

*** 1141,12 ***
      protected void shutdownInput() throws IOException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
              if (!isInputClosed) {
                  Net.shutdown(fd, Net.SHUT_RD);
!                 if (NativeThread.isVirtualThread(readerThread)) {
!                     Poller.stopPoll(fdVal(fd), Net.POLLIN);
                  }
                  isInputClosed = true;
              }
          }
      }
--- 1155,12 ---
      protected void shutdownInput() throws IOException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
              if (!isInputClosed) {
                  Net.shutdown(fd, Net.SHUT_RD);
!                 if (readerThread != null && readerThread.isVirtual()) {
!                     Poller.stopPoll(readerThread);
                  }
                  isInputClosed = true;
              }
          }
      }

*** 1155,12 ***
      protected void shutdownOutput() throws IOException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
              if (!isOutputClosed) {
                  Net.shutdown(fd, Net.SHUT_WR);
!                 if (NativeThread.isVirtualThread(writerThread)) {
!                     Poller.stopPoll(fdVal(fd), Net.POLLOUT);
                  }
                  isOutputClosed = true;
              }
          }
      }
--- 1169,12 ---
      protected void shutdownOutput() throws IOException {
          synchronized (stateLock) {
              ensureOpenAndConnected();
              if (!isOutputClosed) {
                  Net.shutdown(fd, Net.SHUT_WR);
!                 if (writerThread != null && writerThread.isVirtual()) {
!                     Poller.stopPoll(writerThread);
                  }
                  isOutputClosed = true;
              }
          }
      }
< prev index next >