< prev index next >

src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.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 write, 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     SinkChannelImpl(SelectorProvider sp, FileDescriptor fd) {
 81         super(sp);

 82         this.fd = fd;
 83         this.fdVal = IOUtil.fdVal(fd);
 84     }
 85 
 86     /**
 87      * Closes the write end of the pipe if there are no write 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 write operation in progress then the write-end of the pipe
116      * is pre-closed and the writer is signalled, in which case the final close
117      * is deferred until the writer 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 write operation to complete before trying to close
145         writeLock.lock();
146         writeLock.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 write, 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     SinkChannelImpl(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 write end of the pipe if there are no write 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 write operation in progress then the write-end of the pipe
117      * is pre-closed and the writer is signalled, in which case the final close
118      * is deferred until the writer 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.isVirtualThread(th)) {
128                         Poller.stopPoll(fdVal);
129                     } else {
130                         nd.preClose(fd);
131                         NativeThread.signal(th);
132                     }
133                 }
134             }
135         }
136     }
137 
138     /**
139      * Closes this channel when configured in non-blocking mode.
140      *
141      * If the channel is registered with a Selector then the close is deferred
142      * until the channel is flushed from all Selectors.
143      */
144     private void implCloseNonBlockingMode() throws IOException {
145         synchronized (stateLock) {
146             assert state < ST_CLOSING;
147             state = ST_CLOSING;
148         }
149         // wait for any write operation to complete before trying to close
150         writeLock.lock();
151         writeLock.unlock();
152         synchronized (stateLock) {
< prev index next >