From: <hag...@us...> - 2006-12-14 09:29:11
|
Revision: 2908 http://jnode.svn.sourceforge.net/jnode/?rev=2908&view=rev Author: hagar-wize Date: 2006-12-14 01:29:10 -0800 (Thu, 14 Dec 2006) Log Message: ----------- Classpath patches Modified Paths: -------------- trunk/core/src/classpath/gnu/gnu/java/nio/PipeImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectionKeyImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectorImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/SelectorProviderImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelImpl.java trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelSelectionKey.java Added Paths: ----------- trunk/core/src/classpath/gnu/gnu/java/nio/VMChannelOwner.java Modified: trunk/core/src/classpath/gnu/gnu/java/nio/PipeImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/PipeImpl.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/PipeImpl.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -37,6 +37,7 @@ package gnu.java.nio; + import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Pipe; @@ -45,38 +46,39 @@ class PipeImpl extends Pipe { public static final class SourceChannelImpl extends Pipe.SourceChannel + implements VMChannelOwner { - private int native_fd; + private VMChannel vmch; public SourceChannelImpl (SelectorProvider selectorProvider, - int native_fd) + VMChannel channel) { super (selectorProvider); - this.native_fd = native_fd; + vmch = channel; } protected final void implCloseSelectableChannel() throws IOException { - throw new Error ("Not implemented"); + vmch.close(); } protected void implConfigureBlocking (boolean blocking) throws IOException { - throw new Error ("Not implemented"); + vmch.setBlocking(blocking); } public final int read (ByteBuffer src) throws IOException { - throw new Error ("Not implemented"); + return vmch.read(src); } public final long read (ByteBuffer[] srcs) throws IOException { - return read (srcs, 0, srcs.length); + return vmch.readScattering(srcs, 0, srcs.length); } public final synchronized long read (ByteBuffer[] srcs, int offset, @@ -89,54 +91,49 @@ || len > srcs.length - offset) throw new IndexOutOfBoundsException(); - long bytesRead = 0; - - for (int index = 0; index < len; index++) - bytesRead += read (srcs [offset + index]); - - return bytesRead; - + return vmch.readScattering(srcs, offset, len); } - public final int getNativeFD() + public VMChannel getVMChannel() { - return native_fd; + return vmch; } } public static final class SinkChannelImpl extends Pipe.SinkChannel + implements VMChannelOwner { - private int native_fd; + private VMChannel vmch; public SinkChannelImpl (SelectorProvider selectorProvider, - int native_fd) + VMChannel channel) { super (selectorProvider); - this.native_fd = native_fd; + vmch = channel; } protected final void implCloseSelectableChannel() throws IOException { - throw new Error ("Not implemented"); + vmch.close(); } protected final void implConfigureBlocking (boolean blocking) throws IOException { - throw new Error ("Not implemented"); + vmch.setBlocking(blocking); } public final int write (ByteBuffer dst) throws IOException { - throw new Error ("Not implemented"); + return vmch.write(dst); } public final long write (ByteBuffer[] srcs) throws IOException { - return write (srcs, 0, srcs.length); + return vmch.writeGathering(srcs, 0, srcs.length); } public final synchronized long write (ByteBuffer[] srcs, int offset, int len) @@ -148,17 +145,12 @@ || len > srcs.length - offset) throw new IndexOutOfBoundsException(); - long bytesWritten = 0; - - for (int index = 0; index < len; index++) - bytesWritten += write (srcs [offset + index]); - - return bytesWritten; + return vmch.writeGathering(srcs, offset, len); } - public final int getNativeFD() + public VMChannel getVMChannel() { - return native_fd; + return vmch; } } @@ -169,7 +161,9 @@ throws IOException { super(); - VMPipe.init (this, provider); + VMChannel[] pipe = VMPipe.pipe(); + sink = new SinkChannelImpl(provider, pipe[0]); + source = new SourceChannelImpl(provider, pipe[1]); } public Pipe.SinkChannel sink() Modified: trunk/core/src/classpath/gnu/gnu/java/nio/SelectionKeyImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/SelectionKeyImpl.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/SelectionKeyImpl.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -1,5 +1,5 @@ /* SelectionKeyImpl.java -- - Copyright (C) 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -47,8 +47,8 @@ { private int readyOps; private int interestOps; - private SelectorImpl impl; - SelectableChannel ch; + private final SelectorImpl impl; + final SelectableChannel ch; public SelectionKeyImpl (SelectableChannel ch, SelectorImpl impl) { @@ -61,7 +61,7 @@ return ch; } - public int readyOps () + public synchronized int readyOps () { if (!isValid()) throw new CancelledKeyException(); @@ -69,7 +69,7 @@ return readyOps; } - public SelectionKey readyOps (int ops) + public synchronized SelectionKey readyOps (int ops) { if (!isValid()) throw new CancelledKeyException(); @@ -83,15 +83,21 @@ if (!isValid()) throw new CancelledKeyException(); - return interestOps; + synchronized (impl.selectedKeys()) + { + return interestOps; } + } public SelectionKey interestOps (int ops) { if (!isValid()) throw new CancelledKeyException(); + synchronized (impl.selectedKeys()) + { interestOps = ops; + } return this; } @@ -100,5 +106,6 @@ return impl; } + /* @deprecated */ public abstract int getNativeFD(); } Modified: trunk/core/src/classpath/gnu/gnu/java/nio/SelectorImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/SelectorImpl.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/SelectorImpl.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -54,8 +54,8 @@ public class SelectorImpl extends AbstractSelector { - private Set keys; - private Set selected; + private Set<SelectionKey> keys; + private Set<SelectionKey> selected; /** * A dummy object whose monitor regulates access to both our @@ -83,8 +83,8 @@ { super (provider); - keys = new HashSet (); - selected = new HashSet (); + keys = new HashSet<SelectionKey> (); + selected = new HashSet<SelectionKey> (); } protected void finalize() throws Throwable @@ -110,7 +110,7 @@ } } - public final Set keys() + public final Set<SelectionKey> keys() { if (!isOpen()) throw new ClosedSelectorException(); @@ -136,7 +136,7 @@ { int[] result; int counter = 0; - Iterator it = keys.iterator (); + Iterator<SelectionKey> it = keys.iterator (); // Count the number of file descriptors needed while (it.hasNext ()) @@ -253,7 +253,7 @@ selectThread = null; } - Iterator it = keys.iterator (); + Iterator<SelectionKey> it = keys.iterator (); while (it.hasNext ()) { @@ -317,7 +317,7 @@ } } - public final Set selectedKeys() + public final Set<SelectionKey> selectedKeys() { if (!isOpen()) throw new ClosedSelectorException(); @@ -350,10 +350,10 @@ private final void deregisterCancelledKeys() { - Set ckeys = cancelledKeys (); + Set<SelectionKey> ckeys = cancelledKeys (); synchronized (ckeys) { - Iterator it = ckeys.iterator(); + Iterator<SelectionKey> it = ckeys.iterator(); while (it.hasNext ()) { Modified: trunk/core/src/classpath/gnu/gnu/java/nio/SelectorProviderImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/SelectorProviderImpl.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/SelectorProviderImpl.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -37,6 +37,9 @@ package gnu.java.nio; + +import gnu.classpath.SystemProperties; + import java.io.IOException; import java.nio.channels.DatagramChannel; import java.nio.channels.Pipe; @@ -47,6 +50,11 @@ public class SelectorProviderImpl extends SelectorProvider { + private static final String SELECTOR_IMPL_KQUEUE = "kqueue"; + private static final String SELECTOR_IMPL_EPOLL = "epoll"; + private static final String SELECTOR_IMPL = "gnu.java.nio.selectorImpl"; + private static boolean epoll_failed = false; + public SelectorProviderImpl () { } @@ -66,6 +74,35 @@ public AbstractSelector openSelector () throws IOException { + String selectorImpl = "default"; + if (KqueueSelectorImpl.kqueue_supported()) + selectorImpl = SELECTOR_IMPL_KQUEUE; + if (EpollSelectorImpl.epoll_supported() && !epoll_failed) + selectorImpl = SELECTOR_IMPL_EPOLL; + selectorImpl = SystemProperties.getProperty(SELECTOR_IMPL, selectorImpl); + + if (selectorImpl.equals(SELECTOR_IMPL_KQUEUE)) + return new KqueueSelectorImpl(this); + + if (selectorImpl.equals(SELECTOR_IMPL_EPOLL)) + { + // We jump through these hoops because even though epoll may look + // like it's available (sys/epoll.h exists, and you can link against + // all the epoll functions) it may not be available in the kernel + // (especially 2.4 kernels), meaning you will get ENOSYS at run time. + // + // Madness! + try + { + return new EpollSelectorImpl(this); + } + catch (InternalError e) + { + // epoll_create throws this on ENOSYS. + epoll_failed = true; + } + } + return new SelectorImpl (this); } Modified: trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelImpl.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelImpl.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelImpl.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -48,7 +48,9 @@ import java.nio.channels.spi.SelectorProvider; public final class ServerSocketChannelImpl extends ServerSocketChannel + implements VMChannelOwner { + private VMChannel channel; private NIOServerSocket serverSocket; private boolean connected; @@ -56,13 +58,15 @@ throws IOException { super (provider); - serverSocket = new NIOServerSocket (this); + serverSocket = new NIOServerSocket(this); + channel = serverSocket.getPlainSocketImpl().getVMChannel(); configureBlocking(true); - } + } + // XXX do we need this? public void finalizer() { - if (connected) + if (channel.getState().isValid()) { try { @@ -70,20 +74,20 @@ } catch (Exception e) { - } - } - } + } + } + } protected void implCloseSelectableChannel () throws IOException { - connected = false; - serverSocket.close(); - } + connected = false; + channel.close(); + } protected void implConfigureBlocking (boolean blocking) throws IOException { - serverSocket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT); - } + channel.setBlocking(blocking); + } public SocketChannel accept () throws IOException { @@ -98,27 +102,28 @@ try { begin(); - serverSocket.getPlainSocketImpl().setInChannelOperation(true); - // indicate that a channel is initiating the accept operation - // so that the socket ignores the fact that we might be in - // non-blocking mode. - NIOSocket socket = (NIOSocket) serverSocket.accept(); - completed = true; - return socket.getChannel(); + VMChannel client = channel.accept(); + if (client == null) + return null; + else + { + completed = true; + return new SocketChannelImpl(provider(), client, false); + } } - catch (SocketTimeoutException e) - { - return null; - } finally { - serverSocket.getPlainSocketImpl().setInChannelOperation(false); end (completed); } - } + } - public ServerSocket socket () + public ServerSocket socket() { return serverSocket; - } + } + + public VMChannel getVMChannel() + { + return channel; + } } Modified: trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelSelectionKey.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelSelectionKey.java 2006-12-14 09:24:37 UTC (rev 2907) +++ trunk/core/src/classpath/gnu/gnu/java/nio/ServerSocketChannelSelectionKey.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -38,6 +38,7 @@ package gnu.java.nio; +import java.io.IOException; import java.nio.channels.spi.AbstractSelectableChannel; public final class ServerSocketChannelSelectionKey @@ -49,10 +50,16 @@ super (channel, selector); } + // FIXME don't use file descriptor integers public int getNativeFD() { - NIOServerSocket socket = - (NIOServerSocket) ((ServerSocketChannelImpl) ch).socket(); - return socket.getPlainSocketImpl().getNativeFD(); + try + { + return ((ServerSocketChannelImpl) ch).getVMChannel().getState().getNativeFD(); + } + catch (IOException ioe) + { + throw new IllegalStateException(ioe); + } } } Added: trunk/core/src/classpath/gnu/gnu/java/nio/VMChannelOwner.java =================================================================== --- trunk/core/src/classpath/gnu/gnu/java/nio/VMChannelOwner.java (rev 0) +++ trunk/core/src/classpath/gnu/gnu/java/nio/VMChannelOwner.java 2006-12-14 09:29:10 UTC (rev 2908) @@ -0,0 +1,57 @@ +/* NativeFD.java -- interface for Channels that have an underlying file descriptor. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath 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 for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.nio; + +/** + * This interface is meant to be implemented by any {@link Channel} + * implementation we support that uses a platform-specific {@link VMChannel} + * at their core. This is primarily used by {@link Selector} implementations, + * for easier access to the native state. + * + * @author Casey Marshall (cs...@gn...) + */ +interface VMChannelOwner +{ + /** + * Return the underlying platform-specific Channel instance. + * + * @return The platform channel object. + */ + VMChannel getVMChannel(); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |