4963N/A * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * An implementation of SocketChannels 0N/A // Used to make native read and write calls 0N/A // Our file descriptor object 0N/A // fd value needed for dev/poll. This value will remain valid 0N/A // even after the value in the file descriptor object has been set to -1 0N/A // IDs of native threads doing reads and writes, for signalling 0N/A // Lock held by current reading or connecting thread 0N/A // Lock held by current writing or connecting thread 0N/A // Lock held by any thread that modifies the state fields declared below 0N/A // DO NOT invoke a blocking I/O operation while holding this lock! 0N/A // -- The following fields are protected by stateLock 6272N/A // set true when exclusive binding is on and SO_REUSEADDR is emulated 0N/A // State, increases monotonically 0N/A // Socket adaptor, created on demand 0N/A // -- End of fields protected by stateLock 0N/A // Constructor for normal connecting sockets 0N/A // Constructor for sockets obtained from server sockets 524N/A // special handling for IP_TOS: no-op when IPv6 6272N/A // SO_REUSEADDR emulated when using exclusive bind 524N/A // no options that require special handling 6272N/A // SO_REUSEADDR emulated when using exclusive bind 524N/A // special handling for IP_TOS: always return 0 when IPv6 524N/A // no options that require special handling 524N/A // additional options required by socket adaptor 0N/A // Set up the interruption machinery; see 0N/A // AbstractInterruptibleChannel for details 0N/A // Either the current thread is already interrupted, so 0N/A // begin() closed the channel, or another thread closed the 0N/A // channel since we checked it a few bytecodes ago. In 0N/A // either case the value returned here is irrelevant since 0N/A // the invocation of end() in the finally block will throw 0N/A // an appropriate exception. 0N/A // Save this thread so that it can be signalled on those 0N/A // platforms that require it 0N/A // Between the previous test of isOpen() and the return of the 0N/A // IOUtil.read invocation below, this channel might be closed 0N/A // or this thread might be interrupted. We rely upon the 0N/A // implicit synchronization point in the kernel read() call to 0N/A // make sure that the right thing happens. In either case the 0N/A // implCloseSelectableChannel method is ultimately invoked in 0N/A // some other thread, so there are three possibilities: 0N/A // - implCloseSelectableChannel() invokes nd.preClose() 0N/A // before this thread invokes read(), in which case the 0N/A // read returns immediately with either EOF or an error, 0N/A // the latter of which will cause an IOException to be 0N/A // - implCloseSelectableChannel() invokes nd.preClose() after 0N/A // this thread is blocked in read(). On some operating 0N/A // systems (e.g., Solaris and Windows) this causes the read 0N/A // to return immediately with either EOF or an error 0N/A // - implCloseSelectableChannel() invokes nd.preClose() after 0N/A // this thread is blocked in read() but the operating 0N/A // system (e.g., Linux) doesn't support preemptive close, 0N/A // so implCloseSelectableChannel() proceeds to signal this 0N/A // thread, thereby causing the read to return immediately 0N/A // with IOStatus.INTERRUPTED. 0N/A // In all three cases the invocation of end() in the finally 0N/A // clause will notice that the channel has been closed and 0N/A // throw an appropriate exception (AsynchronousCloseException 0N/A // or ClosedByInterruptException) if necessary. 0N/A // *There is A fourth possibility. implCloseSelectableChannel() 0N/A // moves on to nd.close() in kill(), which does a real close. 0N/A // Then a third thread accepts a new connection, opens file or 0N/A // whatever that causes the released "fd" to be recycled. All 0N/A // above happens just between our last isOpen() check and the 0N/A // next kernel read reached, with the recycled "fd". The solution 0N/A // is to postpone the real kill() if there is a reader or/and 0N/A // to the reader or writer thread. (the preClose() still happens 0N/A // so the connection gets cut off as usual). 0N/A // For socket channels there is the additional wrinkle that 0N/A // asynchronous shutdown works much like asynchronous close, 0N/A // except that the channel is shutdown rather than completely 0N/A // closed. This is analogous to the first two cases above, 0N/A // except that the shutdown operation plays the role of 0N/A // The system call was interrupted but the channel 0N/A // is still open, so retry 0N/A // The end method, which is defined in our superclass 0N/A // AbstractInterruptibleChannel, resets the interruption 0N/A // machinery. If its argument is true then it returns 0N/A // normally; otherwise it checks the interrupt and open state 0N/A // of this channel and throws an appropriate exception if 0N/A // So, if we actually managed to do any I/O in the above try 0N/A // block then we pass true to the end method. We also pass 0N/A // true if the channel was in non-blocking mode when the I/O 0N/A // operation was initiated but no data could be transferred; 0N/A // this prevents spurious exceptions from being thrown in the 0N/A // rare event that a channel is closed or a thread is 0N/A // interrupted at the exact moment that a non-blocking I/O 0N/A // Extra case for socket channels: Asynchronous shutdown 1040N/A // notify hook only if unbound 0N/A // If an exception was thrown, close the channel after 0N/A // invoking end() so as to avoid bogus 0N/A // AsynchronousCloseExceptions 0N/A // Connection succeeded; disallow further 0N/A // If nonblocking and no exception then connection 0N/A // pending; disallow another invocation 0N/A // spurious notifications 0N/A // poll()/getsockopt() does not report 0N/A // error (throws exception, with n = 0) 0N/A // on Linux platform after dup2 and 0N/A // signal-wakeup. Force n to 0 so the 0N/A // end() can throw appropriate exception 0N/A // If an exception was thrown, close the channel after 0N/A // invoking end() so as to avoid bogus 0N/A // AsynchronousCloseExceptions 0N/A // AbstractInterruptibleChannel synchronizes invocations of this method 0N/A // using AbstractInterruptibleChannel.closeLock, and also ensures that this 0N/A // method is only ever invoked once. Before we get to this method, isOpen 0N/A // (which is volatile) will have been set to false. 0N/A // Close the underlying file descriptor and dup it to a known fd 0N/A // that's already closed. This prevents other operations on this 0N/A // channel from using the old fd, which might be recycled in the 0N/A // meantime and allocated to an entirely different channel. 0N/A // Signal native threads, if needed. If a target thread is not 0N/A // currently blocked in an I/O operation then no harm is done since 0N/A // the signal handler doesn't actually do anything. 0N/A // If this channel is not registered then it's safe to close the fd 0N/A // immediately since we know at this point that no thread is 0N/A // blocked in an I/O operation upon the channel and, since the 0N/A // channel is marked closed, no thread will start another such 0N/A // operation. If this channel is registered then we don't close 0N/A // the fd since it might be in use by a selector. In that case 0N/A // closing this channel caused its keys to be cancelled, so the 0N/A // last selector to deregister a key for this channel will invoke 0N/A // kill() to close the fd. 0N/A // Postpone the kill if there is a waiting reader 0N/A // or writer thread. See the comments in read() for 0N/A // more detailed explanation. 0N/A * Translates native poll revent ops into a ready operation ops 0N/A // This should only happen if this channel is pre-closed while a 0N/A // selection operation is in progress 0N/A // ## Throw an error if this channel has not been pre-closed 0N/A // No need to poll again in checkConnect, 0N/A // the error will be detected there 0N/A * Translates an interest operation set into a native poll event set 0N/A // -- Native methods --