src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
branchunixdomainchannels
changeset 58911 2c777f25cfff
parent 58847 692de65ab293
parent 58900 434329f6f456
equal deleted inserted replaced
58857:12b08b510fd0 58911:2c777f25cfff
   407             readLock.unlock();
   407             readLock.unlock();
   408         }
   408         }
   409     }
   409     }
   410 
   410 
   411     /**
   411     /**
   412      * Adjust the blocking mode while holding the readLock or writeLock.
   412      * Adjusts the blocking mode. readLock or writeLock must already be held.
   413      */
   413      */
   414     private void lockedConfigureBlocking(boolean block) throws IOException {
   414     private void lockedConfigureBlocking(boolean block) throws IOException {
   415         assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
   415         assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
   416         synchronized (stateLock) {
   416         synchronized (stateLock) {
   417             ensureOpen();
   417             ensureOpen();
   418             IOUtil.configureBlocking(fd, block);
   418             IOUtil.configureBlocking(fd, block);
       
   419         }
       
   420     }
       
   421 
       
   422     /**
       
   423      * Adjusts the blocking mode if the channel is open. readLock or writeLock
       
   424      * must already be held.
       
   425      *
       
   426      * @return {@code true} if the blocking mode was adjusted, {@code false} if
       
   427      *         the blocking mode was not adjusted because the channel is closed
       
   428      */
       
   429     private boolean tryLockedConfigureBlocking(boolean block) throws IOException {
       
   430         assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
       
   431         synchronized (stateLock) {
       
   432             if (isOpen()) {
       
   433                 IOUtil.configureBlocking(fd, block);
       
   434                 return true;
       
   435             } else {
       
   436                 return false;
       
   437             }
   419         }
   438         }
   420     }
   439     }
   421 
   440 
   422     /**
   441     /**
   423      * Returns the local address, or null if not bound
   442      * Returns the local address, or null if not bound
   850                         lockedConfigureBlocking(false);
   869                         lockedConfigureBlocking(false);
   851                         try {
   870                         try {
   852                             int n = connectImpl(fd, sa);
   871                             int n = connectImpl(fd, sa);
   853                             connected = (n > 0) ? true : finishTimedConnect(nanos);
   872                             connected = (n > 0) ? true : finishTimedConnect(nanos);
   854                         } finally {
   873                         } finally {
   855                             // restore socket to blocking mode
   874                             // restore socket to blocking mode (if channel is open)
   856                             lockedConfigureBlocking(true);
   875                             tryLockedConfigureBlocking(true);
   857                         }
   876                         }
   858                     } finally {
   877                     } finally {
   859                         endConnect(true, connected);
   878                         endConnect(true, connected);
   860                     }
   879                     }
   861                 } finally {
   880                 } finally {
   943                     // change socket to non-blocking
   962                     // change socket to non-blocking
   944                     lockedConfigureBlocking(false);
   963                     lockedConfigureBlocking(false);
   945                     try {
   964                     try {
   946                         n = timedRead(b, off, len, nanos);
   965                         n = timedRead(b, off, len, nanos);
   947                     } finally {
   966                     } finally {
   948                         // restore socket to blocking mode
   967                         // restore socket to blocking mode (if channel is open)
   949                         lockedConfigureBlocking(true);
   968                         tryLockedConfigureBlocking(true);
   950                     }
   969                     }
   951                 } else {
   970                 } else {
   952                     // read, no timeout
   971                     // read, no timeout
   953                     n = tryRead(b, off, len);
   972                     n = tryRead(b, off, len);
   954                     while (IOStatus.okayToRetry(n) && isOpen()) {
   973                     while (IOStatus.okayToRetry(n) && isOpen()) {