/*
* 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.
*/
/*
* Implementation of Selector using FreeBSD / Mac OS X kqueues
* Derived from Sun's DevPollSelectorImpl
*/
class KQueueSelectorImpl
extends SelectorImpl
{
// File descriptors used for interrupt
protected int fd0;
protected int fd1;
// The kqueue manipulator
// Count of registered descriptors (including interrupt)
private int totalChannels;
// Map from a file descriptor to an entry containing the selection key
// True if this Selector has been closed
private boolean closed = false;
// Lock for interrupt triggering and clearing
private boolean interruptTriggered = false;
// used by updateSelectedKeys to handle cases where the same file
// descriptor is polled by more than one filter
private long updateCount;
// Used to map file descriptors to a selection key and "update count"
// (see updateSelectedKeys for usage).
private static class MapEntry {
long updateCount;
}
}
/**
* Package private constructor called by factory method in
* the abstract superclass Selector.
*/
super(sp);
kqueueWrapper = new KQueueArrayWrapper();
totalChannels = 1;
}
throws IOException
{
int entries = 0;
if (closed)
throw new ClosedSelectorException();
try {
begin();
} finally {
end();
}
return updateSelectedKeys(entries);
}
/**
* Update the keys whose fd's have been selected by kqueue.
* Add the ready keys to the selected key set.
* If the interrupt fd has been selected, drain it and clear the interrupt.
*/
throws IOException
{
int numKeysUpdated = 0;
boolean interrupted = false;
// A file descriptor may be registered with kqueue with more than one
// filter and so there may be more than one event for a fd. The update
// count in the MapEntry tracks when the fd was last updated and this
// ensures that the ready ops are updated rather than replaced by a
// second or subsequent event.
updateCount++;
for (int i = 0; i < entries; i++) {
interrupted = true;
} else {
// entry is null in the case of an interrupt
// first time this file descriptor has been encountered on this
// update?
}
} else {
// ready ops have already been set on this update
}
} else {
}
}
}
}
}
if (interrupted) {
// Clear the wakeup pipe
synchronized (interruptLock) {
interruptTriggered = false;
}
}
return numKeysUpdated;
}
if (!closed) {
closed = true;
// prevent further wakeup
synchronized (interruptLock) {
interruptTriggered = true;
}
if (kqueueWrapper != null) {
selectedKeys = null;
// Deregister channels
while (i.hasNext()) {
i.remove();
}
totalChannels = 0;
}
fd0 = -1;
fd1 = -1;
}
}
if (closed)
throw new ClosedSelectorException();
}
}
if (closed)
throw new ClosedSelectorException();
}
synchronized (interruptLock) {
if (!interruptTriggered) {
interruptTriggered = true;
}
}
return this;
}
static {
}
}