Updated Branches: refs/heads/trunk e2e0ac8ff -> 2ff729a63
support for pushing runnable in the SelectorLoop Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/919154a2 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/919154a2 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/919154a2 Branch: refs/heads/trunk Commit: 919154a2d9db9d75336bb52b83380f3a3728d24d Parents: e2e0ac8 Author: jvermillard <[email protected]> Authored: Mon Oct 28 14:14:09 2013 +0100 Committer: jvermillard <[email protected]> Committed: Mon Oct 28 14:14:09 2013 +0100 ---------------------------------------------------------------------- .../mina/transport/nio/NioSelectorLoop.java | 24 ++++++++++++++++++-- .../apache/mina/transport/nio/SelectorLoop.java | 7 ++++++ 2 files changed, 29 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/919154a2/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java b/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java index 382489e..d778ca6 100644 --- a/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java +++ b/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java @@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory; /** * This class holds a Selector and handle all the incoming events for the sessions registered on this selector.ALl the - * events will be processed by some dedicated thread, taken from a pool. It will loop forever, untill the instance is + * events will be processed by some dedicated thread, taken from a pool. It will loop forever, until the instance is * stopped. * * @author <a href="http://mina.apache.org">Apache MINA Project</a> @@ -52,7 +52,13 @@ public class NioSelectorLoop implements SelectorLoop { private final ByteBuffer readBuffer = ByteBuffer.allocateDirect(64 * 1024); /** The queue containing the channels to register on the selector */ - private final Queue<Registration> registrationQueue = new ConcurrentLinkedQueue<Registration>(); + private final Queue<Registration> registrationQueue = new ConcurrentLinkedQueue<>(); + + /** + * Queue of runnable events to be run by the selector loop, used for running user code in the I/O loop and avoiding + * concurrency issues + */ + private final Queue<Runnable> runnableQueue = new ConcurrentLinkedQueue<>(); /** * Creates an instance of the SelectorLoop. @@ -139,6 +145,15 @@ public class NioSelectorLoop implements SelectorLoop { * {@inheritDoc} */ @Override + public void runInLoop(Runnable task) { + runnableQueue.add(task); + wakeup(); + } + + /** + * {@inheritDoc} + */ + @Override public void modifyRegistration(boolean accept, boolean read, boolean write, final SelectorListener listener, SelectableChannel channel, boolean wakeup) { if (IS_DEBUG) { @@ -262,6 +277,11 @@ public class NioSelectorLoop implements SelectorLoop { LOG.error("socket is already dead", ex); } } + + // tasks + while (!runnableQueue.isEmpty()) { + runnableQueue.poll().run(); + } } catch (final Exception e) { LOG.error("Unexpected exception : ", e); } http://git-wip-us.apache.org/repos/asf/mina/blob/919154a2/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java b/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java index 45e4b0e..9bb737e 100644 --- a/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java +++ b/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java @@ -47,4 +47,11 @@ public interface SelectorLoop { * Wake up the selector */ void wakeup(); + + /** + * Run a given runnable in the loop. + * + * @param task the task to be run in the main working loop. + */ + void runInLoop(Runnable task); } \ No newline at end of file
