< prev index next > src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
Print this page
// 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
// 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 NativeThread readerThread;
! private NativeThread writerThread;
// used when SO_REUSEADDR is emulated, protected by stateLock
private boolean isReuseAddress;
// read or accept timeout in millis
* 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");
* 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");
* 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");
* 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");
* 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;
* 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;
* @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");
}
* @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");
}
/**
* 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 {
/**
* 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 {
synchronized (stateLock) {
ensureOpenAndConnected();
if (!isInputClosed) {
Net.shutdown(fd, Net.SHUT_RD);
if (NativeThread.isVirtualThread(readerThread)) {
! Poller.stopPoll(fdVal(fd), Net.POLLIN);
}
isInputClosed = true;
}
}
}
synchronized (stateLock) {
ensureOpenAndConnected();
if (!isInputClosed) {
Net.shutdown(fd, Net.SHUT_RD);
if (NativeThread.isVirtualThread(readerThread)) {
! Poller.stopPoll(readerThread.thread());
}
isInputClosed = true;
}
}
}
synchronized (stateLock) {
ensureOpenAndConnected();
if (!isOutputClosed) {
Net.shutdown(fd, Net.SHUT_WR);
if (NativeThread.isVirtualThread(writerThread)) {
! Poller.stopPoll(fdVal(fd), Net.POLLOUT);
}
isOutputClosed = true;
}
}
}
synchronized (stateLock) {
ensureOpenAndConnected();
if (!isOutputClosed) {
Net.shutdown(fd, Net.SHUT_WR);
if (NativeThread.isVirtualThread(writerThread)) {
! Poller.stopPoll(writerThread.thread());
}
isOutputClosed = true;
}
}
}
< prev index next >