/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Unix implementation of AsynchronousSocketChannel
*/
{
private static final boolean disableSynchronousRead;
static {
}
private final int fdVal;
// used to ensure that the context for I/O operations that complete
// ascynrhonously is visible to the pooled threads handling I/O events.
// pending connect (updateLock)
private boolean connectPending;
// pending remote address (stateLock)
// pending read (updateLock)
private boolean readPending;
private boolean isScatteringRead;
// pending write (updateLock)
private boolean writePending;
private boolean isGatheringWrite;
throws IOException
{
super(port);
// set non-blocking
try {
} catch (IOException x) {
throw x;
}
// add mapping from file descriptor to this channel
}
// Constructor for sockets created by UnixAsynchronousServerSocketChannelImpl
throws IOException
{
try {
} catch (ShutdownChannelGroupException x) {
// ShutdownChannelGroupException thrown if we attempt to register a
// new channel after the group is shutdown
throw new IOException(x);
}
}
return port;
}
// register events for outstanding I/O operations, caller already owns updateLock
private void updateEvents() {
int events = 0;
if (readPending)
if (connectPending || writePending)
if (events != 0)
}
// register events for outstanding I/O operations
private void lockAndUpdateEvents() {
synchronized (updateLock) {
updateEvents();
}
}
boolean readable,
boolean writable)
{
boolean finishRead = false;
boolean finishWrite = false;
boolean finishConnect = false;
// map event to pending result
synchronized (updateLock) {
if (readable && this.readPending) {
this.readPending = false;
finishRead = true;
}
if (writable) {
if (this.writePending) {
this.writePending = false;
finishWrite = true;
} else if (this.connectPending) {
this.connectPending = false;
finishConnect = true;
}
}
}
// complete the I/O operation. Special case for when channel is
// ready for both reading and writing. In that case, submit task to
// complete write if write operation has a completion handler.
if (finishRead) {
if (finishWrite)
finishWrite(false);
return;
}
if (finishWrite) {
}
if (finishConnect) {
}
}
/**
* Invoked by event handler thread when file descriptor is polled
*/
readable = true;
writable = true;
}
}
// remove the mapping
// close file descriptor
// All outstanding I/O operations are required to fail
finish(false, true, true);
}
killConnect();
killReading();
killWriting();
}
// -- connect --
synchronized (stateLock) {
}
}
try {
begin();
setConnected();
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
e = x;
} finally {
end();
}
if (e != null) {
// close channel if connection cannot be established
try {
close();
} catch (Throwable suppressed) {
}
}
// invoke handler and set result
} else {
if (mayInvokeDirect) {
} else {
}
}
}
@SuppressWarnings("unchecked")
A attachment,
{
if (!isOpen()) {
Throwable e = new ClosedChannelException();
return CompletedFuture.withFailure(e);
} else {
return null;
}
}
// permission check
// check and set state
boolean notifyBeforeTcpConnect;
synchronized (stateLock) {
if (state == ST_CONNECTED)
throw new AlreadyConnectedException();
if (state == ST_PENDING)
throw new ConnectionPendingException();
state = ST_PENDING;
}
try {
begin();
// notify hook if unbound
if (n == IOStatus.UNAVAILABLE) {
// connection could not be established immediately
synchronized (updateLock) {
} else {
this.connectAttachment = attachment;
}
this.connectPending = true;
updateEvents();
}
return result;
}
setConnected();
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
e = x;
} finally {
end();
}
// close channel if connect fails
if (e != null) {
try {
close();
} catch (Throwable suppressed) {
}
}
} else {
return null;
}
}
// -- read --
int n = -1;
// copy fields as we can't access them after reading is re-enabled.
boolean scattering = isScatteringRead;
try {
begin();
if (scattering) {
} else {
}
if (n == IOStatus.UNAVAILABLE) {
// spurious wakeup, is this possible?
synchronized (updateLock) {
readPending = true;
}
return;
}
// allow objects to be GC'ed.
this.readBuffer = null;
this.readBuffers = null;
this.readAttachment = null;
// allow another read to be initiated
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
// restart poll in case of concurrent write
if (!(exc instanceof AsynchronousCloseException))
end();
}
// cancel the associated timer
// create result
// invoke handler or set result
} else {
if (mayInvokeDirect) {
} else {
}
}
}
public void run() {
synchronized (updateLock) {
if (!readPending)
return;
readPending = false;
future = readFuture;
}
// kill further reading before releasing waiters
enableReading(true);
// invoke handler or set result
} else {
}
}
};
/**
* Initiates a read or scattering read operation
*/
@SuppressWarnings("unchecked")
ByteBuffer[] dsts,
long timeout,
A attachment,
CompletionHandler<V,? super A> handler)
{
// A synchronous read is not attempted if disallowed by system property
// or, we are using a fixed thread pool and the completion handler may
// not be invoked directly (because the thread is not a pooled thread or
// there are too many handlers on the stack).
boolean invokeDirect = false;
boolean attemptRead = false;
if (!disableSynchronousRead) {
attemptRead = true;
} else {
// okay to attempt read with user thread pool
}
}
int n = IOStatus.UNAVAILABLE;
boolean pending = false;
try {
begin();
if (attemptRead) {
if (isScatteringRead) {
} else {
}
}
if (n == IOStatus.UNAVAILABLE) {
synchronized (updateLock) {
this.isScatteringRead = isScatteringRead;
this.readBuffer = dst;
this.readBuffers = dsts;
this.readHandler = null;
this.readAttachment = null;
} else {
this.readAttachment = attachment;
this.readFuture = null;
}
if (timeout > 0L) {
}
this.readPending = true;
updateEvents();
}
pending = true;
return result;
}
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
if (!pending)
end();
}
// read completed immediately
if (invokeDirect) {
} else {
}
return null;
} else {
}
}
// -- write --
int n = -1;
// copy fields as we can't access them after reading is re-enabled.
boolean gathering = this.isGatheringWrite;
try {
begin();
if (gathering) {
} else {
}
if (n == IOStatus.UNAVAILABLE) {
// spurious wakeup, is this possible?
synchronized (updateLock) {
writePending = true;
}
return;
}
// allow objects to be GC'ed.
this.writeBuffer = null;
this.writeBuffers = null;
this.writeAttachment = null;
// allow another write to be initiated
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
// restart poll in case of concurrent write
if (!(exc instanceof AsynchronousCloseException))
end();
}
// cancel the associated timer
// create result
// invoke handler or set result
} else {
if (mayInvokeDirect) {
} else {
}
}
}
public void run() {
synchronized (updateLock) {
if (!writePending)
return;
writePending = false;
}
// kill further writing before releasing waiters
enableWriting(true);
// invoke handler or set result
} else {
}
}
};
/**
* Initiates a read or scattering read operation
*/
@SuppressWarnings("unchecked")
ByteBuffer[] srcs,
long timeout,
A attachment,
CompletionHandler<V,? super A> handler)
{
int n = IOStatus.UNAVAILABLE;
boolean pending = false;
try {
begin();
if (attemptWrite) {
if (isGatheringWrite) {
} else {
}
}
if (n == IOStatus.UNAVAILABLE) {
synchronized (updateLock) {
this.isGatheringWrite = isGatheringWrite;
this.writeBuffer = src;
this.writeBuffers = srcs;
this.writeHandler = null;
this.writeAttachment = null;
} else {
this.writeAttachment = attachment;
this.writeFuture = null;
}
if (timeout > 0L) {
}
this.writePending = true;
updateEvents();
}
pending = true;
return result;
}
} catch (Throwable x) {
if (x instanceof ClosedChannelException)
x = new AsynchronousCloseException();
exc = x;
} finally {
if (!pending)
end();
}
// write completed immediately
if (invokeDirect) {
} else {
}
return null;
} else {
}
}
// -- Native methods --
static {
}
}