< prev index next >

src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java

Print this page




  60     // Channel state
  61     private static final int ST_INUSE = 0;
  62     private static final int ST_CLOSING = 1;
  63     private static final int ST_CLOSED = 2;
  64     private int state;
  65 
  66     // ID of native thread doing read, for signalling
  67     private long thread;
  68 
  69     // -- End of fields protected by stateLock
  70 
  71 
  72     public FileDescriptor getFD() {
  73         return fd;
  74     }
  75 
  76     public int getFDVal() {
  77         return fdVal;
  78     }
  79 
  80     SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) {
  81         super(sp);

  82         this.fd = fd;
  83         this.fdVal = IOUtil.fdVal(fd);
  84     }
  85 
  86     /**
  87      * Closes the read end of the pipe if there are no read operation in
  88      * progress and the channel is not registered with a Selector.
  89      */
  90     private boolean tryClose() throws IOException {
  91         assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
  92         if (thread == 0 && !isRegistered()) {
  93             state = ST_CLOSED;
  94             nd.close(fd);
  95             return true;
  96         } else {
  97             return false;
  98         }
  99     }
 100 
 101     /**


 106     private void tryFinishClose() {
 107         try {
 108             tryClose();
 109         } catch (IOException ignore) { }
 110     }
 111 
 112     /**
 113      * Closes this channel when configured in blocking mode.
 114      *
 115      * If there is a read operation in progress then the read-end of the pipe
 116      * is pre-closed and the reader is signalled, in which case the final close
 117      * is deferred until the reader aborts.
 118      */
 119     private void implCloseBlockingMode() throws IOException {
 120         synchronized (stateLock) {
 121             assert state < ST_CLOSING;
 122             state = ST_CLOSING;
 123             if (!tryClose()) {
 124                 long th = thread;
 125                 if (th != 0) {


 126                     nd.preClose(fd);
 127                     NativeThread.signal(th);

 128                 }
 129             }
 130         }
 131     }
 132 
 133     /**
 134      * Closes this channel when configured in non-blocking mode.
 135      *
 136      * If the channel is registered with a Selector then the close is deferred
 137      * until the channel is flushed from all Selectors.
 138      */
 139     private void implCloseNonBlockingMode() throws IOException {
 140         synchronized (stateLock) {
 141             assert state < ST_CLOSING;
 142             state = ST_CLOSING;
 143         }
 144         // wait for any read operation to complete before trying to close
 145         readLock.lock();
 146         readLock.unlock();
 147         synchronized (stateLock) {




  60     // Channel state
  61     private static final int ST_INUSE = 0;
  62     private static final int ST_CLOSING = 1;
  63     private static final int ST_CLOSED = 2;
  64     private int state;
  65 
  66     // ID of native thread doing read, for signalling
  67     private long thread;
  68 
  69     // -- End of fields protected by stateLock
  70 
  71 
  72     public FileDescriptor getFD() {
  73         return fd;
  74     }
  75 
  76     public int getFDVal() {
  77         return fdVal;
  78     }
  79 
  80     SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) throws IOException {
  81         super(sp);
  82         IOUtil.configureBlocking(fd, false);
  83         this.fd = fd;
  84         this.fdVal = IOUtil.fdVal(fd);
  85     }
  86 
  87     /**
  88      * Closes the read end of the pipe if there are no read operation in
  89      * progress and the channel is not registered with a Selector.
  90      */
  91     private boolean tryClose() throws IOException {
  92         assert Thread.holdsLock(stateLock) && state == ST_CLOSING;
  93         if (thread == 0 && !isRegistered()) {
  94             state = ST_CLOSED;
  95             nd.close(fd);
  96             return true;
  97         } else {
  98             return false;
  99         }
 100     }
 101 
 102     /**


 107     private void tryFinishClose() {
 108         try {
 109             tryClose();
 110         } catch (IOException ignore) { }
 111     }
 112 
 113     /**
 114      * Closes this channel when configured in blocking mode.
 115      *
 116      * If there is a read operation in progress then the read-end of the pipe
 117      * is pre-closed and the reader is signalled, in which case the final close
 118      * is deferred until the reader aborts.
 119      */
 120     private void implCloseBlockingMode() throws IOException {
 121         synchronized (stateLock) {
 122             assert state < ST_CLOSING;
 123             state = ST_CLOSING;
 124             if (!tryClose()) {
 125                 long th = thread;
 126                 if (th != 0) {
 127                     if (NativeThread.isFiber(th))
 128                         Poller.stopPoll(fdVal);
 129                     nd.preClose(fd);
 130                     if (NativeThread.isKernelThread(th))
 131                         NativeThread.signal(th);
 132                 }
 133             }
 134         }
 135     }
 136 
 137     /**
 138      * Closes this channel when configured in non-blocking mode.
 139      *
 140      * If the channel is registered with a Selector then the close is deferred
 141      * until the channel is flushed from all Selectors.
 142      */
 143     private void implCloseNonBlockingMode() throws IOException {
 144         synchronized (stateLock) {
 145             assert state < ST_CLOSING;
 146             state = ST_CLOSING;
 147         }
 148         // wait for any read operation to complete before trying to close
 149         readLock.lock();
 150         readLock.unlock();
 151         synchronized (stateLock) {


< prev index next >