diff --git a/transport/src/main/java/io/netty/channel/aio/AioChannelFinder.java b/transport/src/main/java/io/netty/channel/aio/AioChannelFinder.java deleted file mode 100644 index 2ba0bfb345..0000000000 --- a/transport/src/main/java/io/netty/channel/aio/AioChannelFinder.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -/** - * Allow to fine the {@link AbstractAioChannel} for a task. - */ -interface AioChannelFinder { - - /** - * Try to find the {@link AbstractAioChannel} for the given {@link Runnable}. - * - * @param command the {@link Runnable} for which the {@link AbstractAioChannel} should be found. - * @return channel the {@link AbstractAioChannel} which belongs to the {@link Runnable} or {@code null} if - * it could not found. - * @throws Exception will get thrown if an error accours. - */ - AbstractAioChannel findChannel(Runnable command) throws Exception; -} diff --git a/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java b/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java index 6dd06e8e54..d965d39db1 100644 --- a/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java +++ b/transport/src/main/java/io/netty/channel/aio/AioCompletionHandler.java @@ -35,59 +35,33 @@ public abstract class AioCompletionHandler implements Comp */ protected abstract void failed0(Throwable exc, A channel); - private static final int MAX_STACK_DEPTH = 4; - private static final ThreadLocal STACK_DEPTH = new ThreadLocal() { - @Override - protected Integer initialValue() { - return 0; - } - }; - @Override public final void completed(final V result, final A channel) { EventLoop loop = channel.eventLoop(); if (loop.inEventLoop()) { - Integer d = STACK_DEPTH.get(); - if (d < MAX_STACK_DEPTH) { - STACK_DEPTH.set(d + 1); - try { + completed0(result, channel); + } else { + loop.execute(new Runnable() { + @Override + public void run() { completed0(result, channel); - } finally { - STACK_DEPTH.set(d); } - return; - } + }); } - - loop.execute(new Runnable() { - @Override - public void run() { - completed0(result, channel); - } - }); } @Override public final void failed(final Throwable exc, final A channel) { EventLoop loop = channel.eventLoop(); if (loop.inEventLoop()) { - Integer d = STACK_DEPTH.get(); - if (d < MAX_STACK_DEPTH) { - STACK_DEPTH.set(d + 1); - try { + failed0(exc, channel); + } else { + loop.execute(new Runnable() { + @Override + public void run() { failed0(exc, channel); - } finally { - STACK_DEPTH.set(d); } - return; - } + }); } - - loop.execute(new Runnable() { - @Override - public void run() { - failed0(exc, channel); - } - }); } } diff --git a/transport/src/main/java/io/netty/channel/aio/AioEventLoopGroup.java b/transport/src/main/java/io/netty/channel/aio/AioEventLoopGroup.java index 340629e201..5073d1d0a7 100644 --- a/transport/src/main/java/io/netty/channel/aio/AioEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/aio/AioEventLoopGroup.java @@ -20,9 +20,6 @@ import io.netty.channel.ChannelTaskScheduler; import io.netty.channel.EventExecutor; import io.netty.channel.EventLoopException; import io.netty.channel.MultithreadEventLoopGroup; -import io.netty.util.internal.InternalLogger; -import io.netty.util.internal.InternalLoggerFactory; -import io.netty.util.internal.PlatformDependent; import java.io.IOException; import java.nio.channels.AsynchronousChannelGroup; @@ -38,26 +35,6 @@ import java.util.concurrent.TimeUnit; * */ public class AioEventLoopGroup extends MultithreadEventLoopGroup { - private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(AioEventLoopGroup.class); - private static final AioChannelFinder CHANNEL_FINDER; - - static { - AioChannelFinder finder; - try { - if (PlatformDependent.hasUnsafe()) { - finder = new UnsafeAioChannelFinder(); - } else { - finder = new ReflectiveAioChannelFinder(); - } - } catch (Throwable t) { - LOGGER.debug(String.format( - "Failed to instantiate the optimal %s implementation - falling back to %s.", - AioChannelFinder.class.getSimpleName(), ReflectiveAioChannelFinder.class.getSimpleName()), t); - finder = new ReflectiveAioChannelFinder(); - } - CHANNEL_FINDER = finder; - } - private final AioExecutorService groupExecutor = new AioExecutorService(); private final AsynchronousChannelGroup group; @@ -135,7 +112,7 @@ public class AioEventLoopGroup extends MultithreadEventLoopGroup { return new AioEventLoop(this, threadFactory, scheduler); } - private final class AioExecutorService extends AbstractExecutorService { + private static final class AioExecutorService extends AbstractExecutorService { // It does not shut down the underlying EventExecutor - it merely pretends to be shut down. // The actual shut down is done by EventLoopGroup and EventLoop implementation. @@ -169,34 +146,7 @@ public class AioEventLoopGroup extends MultithreadEventLoopGroup { @Override public void execute(Runnable command) { - Class commandType = command.getClass(); - if (commandType.getName().startsWith("sun.nio.ch.")) { - executeAioTask(command); - } else { - next().execute(command); - } - } - - private void executeAioTask(Runnable command) { - AbstractAioChannel ch = null; - try { - ch = CHANNEL_FINDER.findChannel(command); - } catch (Throwable t) { - // Ignore - } - - EventExecutor l; - if (ch != null) { - l = ch.eventLoop(); - } else { - l = next(); - } - - if (l.isShutdown()) { - command.run(); - } else { - l.execute(command); - } + command.run(); } } } diff --git a/transport/src/main/java/io/netty/channel/aio/ReflectiveAioChannelFinder.java b/transport/src/main/java/io/netty/channel/aio/ReflectiveAioChannelFinder.java deleted file mode 100644 index 9299202e34..0000000000 --- a/transport/src/main/java/io/netty/channel/aio/ReflectiveAioChannelFinder.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -/** - * {@link AioChannelFinder} implementation which use reflection for find the right {@link AbstractAioChannel}. - */ -final class ReflectiveAioChannelFinder implements AioChannelFinder { - private static volatile Map, Field> fieldCache = new HashMap, Field>(); - - @Override - public AbstractAioChannel findChannel(Runnable command) throws Exception { - Field f; - for (;;) { - f = findField(command); - if (f == null) { - return null; - } - Object next = f.get(command); - if (next instanceof AbstractAioChannel) { - return (AbstractAioChannel) next; - } - command = (Runnable) next; - } - } - - private static Field findField(Object command) throws Exception { - Map, Field> fieldCache = ReflectiveAioChannelFinder.fieldCache; - Class commandType = command.getClass(); - Field res = fieldCache.get(commandType); - if (res != null) { - return res; - } - - for (Field f: commandType.getDeclaredFields()) { - if (f.getType() == Runnable.class) { - f.setAccessible(true); - put(fieldCache, commandType, f); - return f; - } - - if (f.getType() == Object.class) { - f.setAccessible(true); - Object candidate = f.get(command); - if (candidate instanceof AbstractAioChannel) { - put(fieldCache, commandType, f); - return f; - } - } - } - return null; - } - - private static void put(Map, Field> oldCache, Class key, Field value) { - Map, Field> newCache = new HashMap, Field>(oldCache.size()); - newCache.putAll(oldCache); - newCache.put(key, value); - fieldCache = newCache; - } -} diff --git a/transport/src/main/java/io/netty/channel/aio/UnsafeAioChannelFinder.java b/transport/src/main/java/io/netty/channel/aio/UnsafeAioChannelFinder.java deleted file mode 100644 index 94fa97b2aa..0000000000 --- a/transport/src/main/java/io/netty/channel/aio/UnsafeAioChannelFinder.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -import io.netty.util.internal.PlatformDependent; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Map; - -/** - * {@link AioChannelFinder} implementation which uses {@code sun.misc.Unsafe}. - */ -final class UnsafeAioChannelFinder implements AioChannelFinder { - private static volatile Map, Long> offsetCache = new HashMap, Long>(); - - @Override - public AbstractAioChannel findChannel(Runnable command) throws Exception { - Long offset; - for (;;) { - offset = findFieldOffset(command); - if (offset == null) { - return null; - } - Object next = PlatformDependent.getObject(command, offset); - if (next instanceof AbstractAioChannel) { - return (AbstractAioChannel) next; - } - command = (Runnable) next; - } - } - - private static Long findFieldOffset(Object command) throws Exception { - Map, Long> offsetCache = UnsafeAioChannelFinder.offsetCache; - Class commandType = command.getClass(); - Long res = offsetCache.get(commandType); - if (res != null) { - return res; - } - - for (Field f: commandType.getDeclaredFields()) { - if (f.getType() == Runnable.class) { - res = PlatformDependent.objectFieldOffset(f); - put(offsetCache, commandType, res); - return res; - } - - if (f.getType() == Object.class) { - f.setAccessible(true); - Object candidate = f.get(command); - if (candidate instanceof AbstractAioChannel) { - res = PlatformDependent.objectFieldOffset(f); - put(offsetCache, commandType, res); - return res; - } - } - } - return null; - } - - private static void put(Map, Long> oldCache, Class key, Long value) { - Map, Long> newCache = new HashMap, Long>(oldCache.size()); - newCache.putAll(oldCache); - newCache.put(key, value); - offsetCache = newCache; - } -} diff --git a/transport/src/test/java/io/netty/channel/aio/AbstractAioChannelFinderTest.java b/transport/src/test/java/io/netty/channel/aio/AbstractAioChannelFinderTest.java deleted file mode 100644 index bfdeca75f8..0000000000 --- a/transport/src/test/java/io/netty/channel/aio/AbstractAioChannelFinderTest.java +++ /dev/null @@ -1,98 +0,0 @@ -package io.netty.channel.aio; - -import static org.junit.Assert.*; -import static org.easymock.EasyMock.*; - -import org.junit.Test; - -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -public abstract class AbstractAioChannelFinderTest { - - protected abstract AioChannelFinder create(); - - @Test - public void testNull() throws Exception { - AioChannelFinder finder = create(); - AbstractAioChannel channel = finder.findChannel(new Runnable() { - - @Override - public void run() { - // Noop - } - }); - assertNull(channel); - } - - @Test - public void testRunnableWrappsAbstractAioChannel() throws Exception { - final Object mockChannel = createMock("mockChannel", AbstractAioChannel.class); - replay(mockChannel); - - Runnable r = new Runnable() { - - @SuppressWarnings("unused") - @Override - public void run() { - Object channel = mockChannel; - // Noop - } - }; - AioChannelFinder finder = create(); - AbstractAioChannel channel = finder.findChannel(r); - assertNotNull(channel); - - AbstractAioChannel channel2 = finder.findChannel(r); - assertNotNull(channel2); - assertSame(channel2, channel); - verify(mockChannel); - reset(mockChannel); - } - - @Test - public void testRunnableWrappsRunnable() throws Exception { - final Object mockChannel = createMock("mockChannel", AbstractAioChannel.class); - replay(mockChannel); - - final Runnable r = new Runnable() { - - @SuppressWarnings("unused") - @Override - public void run() { - Object channel = mockChannel; - // Noop - } - }; - Runnable r2 = new Runnable() { - - @SuppressWarnings("unused") - @Override - public void run() { - Runnable runnable = r; - // Noop - } - }; - AioChannelFinder finder = create(); - AbstractAioChannel channel = finder.findChannel(r2); - assertNotNull(channel); - - AbstractAioChannel channel2 = finder.findChannel(r2); - assertNotNull(channel2); - assertSame(channel2, channel); - verify(mockChannel); - reset(mockChannel); - } -} diff --git a/transport/src/test/java/io/netty/channel/aio/AioEventLoopTest.java b/transport/src/test/java/io/netty/channel/aio/AioEventLoopTest.java deleted file mode 100644 index 5890a11dd9..0000000000 --- a/transport/src/test/java/io/netty/channel/aio/AioEventLoopTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -import io.netty.channel.AbstractEventLoopTest; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.socket.ServerSocketChannel; -import io.netty.channel.socket.aio.AioServerSocketChannel; - -public class AioEventLoopTest extends AbstractEventLoopTest { - @Override - protected EventLoopGroup newEventLoopGroup() { - return new AioEventLoopGroup(); - } - - @Override - protected Class newChannel() { - return AioServerSocketChannel.class; - } -} diff --git a/transport/src/test/java/io/netty/channel/aio/ReflectiveAioChannelFinderTest.java b/transport/src/test/java/io/netty/channel/aio/ReflectiveAioChannelFinderTest.java deleted file mode 100644 index 818ceae935..0000000000 --- a/transport/src/test/java/io/netty/channel/aio/ReflectiveAioChannelFinderTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -public class ReflectiveAioChannelFinderTest extends AbstractAioChannelFinderTest { - - @Override - protected AioChannelFinder create() { - return new ReflectiveAioChannelFinder(); - } - -} diff --git a/transport/src/test/java/io/netty/channel/aio/UnsafeAioChannelFinderTest.java b/transport/src/test/java/io/netty/channel/aio/UnsafeAioChannelFinderTest.java deleted file mode 100644 index 7405f2ddb4..0000000000 --- a/transport/src/test/java/io/netty/channel/aio/UnsafeAioChannelFinderTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.aio; - -public class UnsafeAioChannelFinderTest extends AbstractAioChannelFinderTest { - - @Override - protected AioChannelFinder create() { - return new UnsafeAioChannelFinder(); - } - -}