Poller.java revision 2362
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* This class is provided for access to the underlying poll(2)
* multiplexing IO when an application cannot afford to have
* a thread block on each outstanding IO request.
*
* It currently supports the same basic functionality as the
* C poll(2) API, although for efficiency we needed to avoid
* passing the entire pollfd array for every call. See man
* pages for poll(2) for info on C API and event types.
*
*
* @author Bruce Chapman
* @see java.io.FileDescriptor
* @see java.net.Socket
* @see attached README.txt
* @since JDK1.2
*/
public class Poller {
/**
* Solaris POLL event types.
*/
public final static short POLLERR = 0x08;
public final static short POLLHUP = 0x10;
public final static short POLLNVAL = 0x20;
public final static short POLLIN = 1;
public final static short POLLPRI = 2;
public final static short POLLOUT = 4;
public final static short POLLRDNORM = 0x40;
public final static short POLLWRNORM = POLLOUT ;
public final static short POLLRDBAND = 0x80;
public final static short POLLWRBAND = 0x100;
public final static short POLLNORM = POLLRDNORM;
/*
* This global synchronization object must be used for all
* creation or destruction of Poller objects.
*/
/*
* The handle for a Poller Object...is used in the JNI C code
* where all the associated data is kept.
*/
private int handle;
/**
* Constructs an instance of a <code>Poller</code> object.
* Native code uses sysconf(_SC_OPEN_MAX) to determine how
*/
synchronized(globalSync) {
}
}
/**
* Constructs an instance of a <code>Poller</code> object.
* @param maxFd the maximum number of FileDescriptors/Sockets
* this Poller object can contain.
*/
synchronized(globalSync) {
}
}
/**
* Needed to clean up at the JNI C level when object is GCd.
*/
synchronized(globalSync) {
super.finalize();
}
}
/**
* Since we can't guarantee WHEN finalize is called, we may
* recycle on our own.
* @param maxFd the maximum number of FileDescriptors/Sockets
* this Poller object can contain.
*/
synchronized(globalSync) {
}
}
/**
* Since we can't guarantee WHEN finalize is called, we may
* recycle on our own.
*/
synchronized(globalSync) {
}
}
/**
* Add FileDescriptor to the set handled by this Poller object.
*
* @param fdObj the FileDescriptor, Socket, or ServerSocket to add.
* @param event the bitmask of events we are interested in.
* @return the OS level fd associated with this IO Object
* (which is what waitMultiple() stores in fds[])
*/
}
/**
* Remove FileDescriptor from the set handled by this Poller object.
*
* @param fdObj the FileDescriptor, Socket, or ServerSocket to remove.
* @return true if removal succeeded.
*/
}
/**
* Check if fd or socket is already in the set handled by this Poller object
*
* @param fdObj the FileDescriptor or [Server]Socket to check.
*/
}
/**
* Wait on Multiple IO Objects.
*
* @param maxRet the maximum number of fds[] and revents[] to return.
* @param fds[] (return) an array of ints in which to store fds with
* available data upon a successful non-timeout return.
* fds.length must be >= maxRet
* @param revents[] (return) the actual events available on the
* same-indexed fds[] (i.e. fds[0] has events revents[0])
* revents.length must be >= maxRet
*
* Note : both above arrays are "dense," i.e. only fds[] with events
* available are returned.
*
* @param timeout the maximum number of milliseconds to wait for
* events before timing out.
* @return the number of fds with triggered events.
*
* Note : convenience methods exist for skipping the timeout parameter
* or the maxRet parameter (in the case of no maxRet, fds.length
* must equal revents.length)
*
* obj.waitMultiple(null,null,timeout) can be used for pausing the LWP
* (much more reliable and scalable than Thread.sleep() or Object.wait())
*/
{
if (maxRet > 0) {
throw new NullPointerException("fds or revents is null");
}
} else if ( (maxRet < 0) ||
throw new IllegalArgumentException("maxRet out of range");
}
if (ret < 0) {
throw new InterruptedIOException();
}
return ret;
}
/**
* Wait on Multiple IO Objects (no timeout).
* A convenience method for waiting indefinitely on IO events
*
* @see Poller#waitMultiple
*
*/
throws Exception
{
}
/**
* Wait on Multiple IO Objects (no maxRet).
* A convenience method for waiting on IO events when the fds
* and revents arrays are the same length and that specifies the
* maximum number of return events.
*
* @see Poller#waitMultiple
*
*/
{
throw new NullPointerException("revents or fds is null");
}
throw new IllegalArgumentException("fds.length != revents.length");
}
/**
* A convenience method for waiting on IO events when the fds
* and revents arrays are the same length and that specifies the
* maximum number of return events, and when waiting indefinitely
* for IO events to occur.
*
* @see Poller#waitMultiple
*
*/
throws Exception
{
throw new NullPointerException("fds or revents is null");
}
throw new IllegalArgumentException("fds.length != revents.length");
}
// Utility - get (int) fd from FileDescriptor or [Server]Socket objects.
Field f;
f.setAccessible(true);
f.setAccessible(true);
f.setAccessible(true);
f.setAccessible(true);
}
else {
throw new IllegalArgumentException("Illegal Object type.");
}
}
// Actual NATIVE calls
private static native int nativeInit();
throws Exception;
throws Exception;
throws Exception;
/**
* Get number of active CPUs in this machine
* to determine proper level of concurrency.
*/
public static native int getNumCPUs();
static {
nativeInit();
}
}