< 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 Thread readerThread;
! private Thread writerThread;
// used when SO_REUSEADDR is emulated, protected by stateLock
private boolean isReuseAddress;
// read or accept timeout in millis
* @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");
* @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");
* @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");
* @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");
// 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;
// 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;
private FileDescriptor beginAccept() throws SocketException {
synchronized (stateLock) {
ensureOpen();
if (localport == 0)
throw new SocketException("Not bound");
! readerThread = NativeThread.current();
return fd;
}
}
/**
private FileDescriptor beginAccept() throws SocketException {
synchronized (stateLock) {
ensureOpen();
if (localport == 0)
throw new SocketException("Not bound");
! readerThread = NativeThread.threadToSignal();
return fd;
}
}
/**
* @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 {
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;
}
}
}
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;
}
}
}
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;
}
}
}
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 >