Poller.java revision 4378
1472N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions 0N/A * - Redistributions of source code must retain the above copyright 0N/A * notice, this list of conditions and the following disclaimer. 0N/A * - Redistributions in binary form must reproduce the above copyright 0N/A * notice, this list of conditions and the following disclaimer in the 0N/A * documentation and/or other materials provided with the distribution. 0N/A * - Neither the name of Oracle nor the names of its 0N/A * contributors may be used to endorse or promote products derived 0N/A * from this software without specific prior written permission. 1472N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1472N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1472N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0N/A * This source code is provided to illustrate the usage of a given feature 0N/A * or technique and has been deliberately simplified. Additional steps 0N/A * required for a production-quality application, such as security checks, 0N/A * input validation and proper error handling, might not be present in 0N/A * This class is provided for access to the underlying poll(2) 0N/A * or /dev/poll kernel interfaces. This may be needed for 0N/A * multiplexing IO when an application cannot afford to have 0N/A * a thread block on each outstanding IO request. 0N/A * It currently supports the same basic functionality as the 0N/A * C poll(2) API, although for efficiency we needed to avoid 0N/A * passing the entire pollfd array for every call. See man 0N/A * pages for poll(2) for info on C API and event types. 0N/A * @author Bruce Chapman 0N/A * @see java.io.FileDescriptor 0N/A * @see java.net.Socket 0N/A * Solaris POLL event types. 0N/A * This global synchronization object must be used for all 0N/A * creation or destruction of Poller objects. 0N/A * The handle for a Poller Object...is used in the JNI C code 0N/A * where all the associated data is kept. 0N/A * Constructs an instance of a <code>Poller</code> object. 0N/A * Native code uses sysconf(_SC_OPEN_MAX) to determine how 0N/A * many fd/skt objects this Poller object can contain. 0N/A * Constructs an instance of a <code>Poller</code> object. 0N/A * this Poller object can contain. 0N/A * Needed to clean up at the JNI C level when object is GCd. 0N/A * Since we can't guarantee WHEN finalize is called, we may 0N/A * recycle on our own. 0N/A * this Poller object can contain. 0N/A * Since we can't guarantee WHEN finalize is called, we may 0N/A * recycle on our own. 0N/A * Add FileDescriptor to the set handled by this Poller object. 0N/A * @param fdObj the FileDescriptor, Socket, or ServerSocket to add. 0N/A * @param event the bitmask of events we are interested in. 0N/A * @return the OS level fd associated with this IO Object 0N/A * (which is what waitMultiple() stores in fds[]) 0N/A * Remove FileDescriptor from the set handled by this Poller object. 0N/A * Must be called before the fd/skt is closed. 0N/A * @param fdObj the FileDescriptor, Socket, or ServerSocket to remove. 0N/A * @return true if removal succeeded. 0N/A * Check if fd or socket is already in the set handled by this Poller object 0N/A * @param fdObj the FileDescriptor or [Server]Socket to check. 0N/A * @return true if fd/skt is in the set for this Poller object. 0N/A * Wait on Multiple IO Objects. 0N/A * @param maxRet the maximum number of fds[] and revents[] to return. 0N/A * @param fds[] (return) an array of ints in which to store fds with 0N/A * available data upon a successful non-timeout return. 0N/A * fds.length must be >= maxRet 0N/A * @param revents[] (return) the actual events available on the 0N/A * same-indexed fds[] (i.e. fds[0] has events revents[0]) 0N/A * revents.length must be >= maxRet 0N/A * Note : both above arrays are "dense," i.e. only fds[] with events 0N/A * available are returned. 0N/A * @param timeout the maximum number of milliseconds to wait for 0N/A * events before timing out. 0N/A * @return the number of fds with triggered events. 0N/A * Note : convenience methods exist for skipping the timeout parameter 0N/A * or the maxRet parameter (in the case of no maxRet, fds.length 0N/A * must equal revents.length) 0N/A * obj.waitMultiple(null,null,timeout) can be used for pausing the LWP 0N/A * (much more reliable and scalable than Thread.sleep() or Object.wait()) 0N/A * Wait on Multiple IO Objects (no timeout). 0N/A * A convenience method for waiting indefinitely on IO events 0N/A * @see Poller#waitMultiple 0N/A * Wait on Multiple IO Objects (no maxRet). 0N/A * A convenience method for waiting on IO events when the fds 0N/A * and revents arrays are the same length and that specifies the 0N/A * maximum number of return events. 0N/A * @see Poller#waitMultiple 0N/A * A convenience method for waiting on IO events when the fds 0N/A * and revents arrays are the same length and that specifies the 0N/A * maximum number of return events, and when waiting indefinitely 0N/A * for IO events to occur. 0N/A * @see Poller#waitMultiple 1133N/A // Utility - get (int) fd from FileDescriptor or [Server]Socket objects. 1133N/A * Get number of active CPUs in this machine 0N/A * to determine proper level of concurrency.