0N/A/*
579N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
113N/Aimport java.nio.channels.*;
113N/Aimport java.util.concurrent.*;
113N/Aimport java.security.AccessController;
0N/Aimport sun.security.action.GetIntegerAction;
0N/A
0N/A/**
0N/A * Defines static methods to invoke a completion handler or arbitrary task.
0N/A */
0N/A
0N/Aclass Invoker {
0N/A private Invoker() { }
0N/A
0N/A // maximum number of completion handlers that may be invoked on the current
0N/A // thread before it re-directs invocations to the thread pool. This helps
0N/A // avoid stack overflow and lessens the risk of starvation.
0N/A private static final int maxHandlerInvokeCount = AccessController.doPrivileged(
0N/A new GetIntegerAction("sun.nio.ch.maxCompletionHandlersOnStack", 16));
0N/A
0N/A // Per-thread object with reference to channel group and a counter for
0N/A // the number of completion handlers invoked. This should be reset to 0
0N/A // when all completion handlers have completed.
0N/A static class GroupAndInvokeCount {
0N/A private final AsynchronousChannelGroupImpl group;
0N/A private int handlerInvokeCount;
0N/A GroupAndInvokeCount(AsynchronousChannelGroupImpl group) {
0N/A this.group = group;
0N/A }
0N/A AsynchronousChannelGroupImpl group() {
0N/A return group;
0N/A }
0N/A int invokeCount() {
0N/A return handlerInvokeCount;
0N/A }
0N/A void setInvokeCount(int value) {
0N/A handlerInvokeCount = value;
0N/A }
0N/A void resetInvokeCount() {
0N/A handlerInvokeCount = 0;
0N/A }
0N/A void incrementInvokeCount() {
0N/A handlerInvokeCount++;
0N/A }
0N/A }
0N/A private static final ThreadLocal<GroupAndInvokeCount> myGroupAndInvokeCount =
0N/A new ThreadLocal<GroupAndInvokeCount>() {
0N/A @Override protected GroupAndInvokeCount initialValue() {
0N/A return null;
0N/A }
0N/A };
0N/A
0N/A /**
0N/A * Binds this thread to the given group
0N/A */
0N/A static void bindToGroup(AsynchronousChannelGroupImpl group) {
0N/A myGroupAndInvokeCount.set(new GroupAndInvokeCount(group));
0N/A }
0N/A
0N/A /**
0N/A * Returns the GroupAndInvokeCount object for this thread.
0N/A */
0N/A static GroupAndInvokeCount getGroupAndInvokeCount() {
0N/A return myGroupAndInvokeCount.get();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current thread is in a channel group's thread pool
0N/A */
0N/A static boolean isBoundToAnyGroup() {
0N/A return myGroupAndInvokeCount.get() != null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current thread is in the given channel's thread
0N/A * pool and we haven't exceeded the maximum number of handler frames on
0N/A * the stack.
0N/A */
0N/A static boolean mayInvokeDirect(GroupAndInvokeCount myGroupAndInvokeCount,
0N/A AsynchronousChannelGroupImpl group)
0N/A {
0N/A if ((myGroupAndInvokeCount != null) &&
342N/A (myGroupAndInvokeCount.group() == group) &&
0N/A (myGroupAndInvokeCount.invokeCount() < maxHandlerInvokeCount))
0N/A {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Invoke handler without checking the thread identity or number of handlers
0N/A * on the thread stack.
0N/A */
0N/A static <V,A> void invokeUnchecked(CompletionHandler<V,? super A> handler,
0N/A A attachment,
0N/A V value,
0N/A Throwable exc)
0N/A {
0N/A if (exc == null) {
0N/A handler.completed(value, attachment);
0N/A } else {
0N/A handler.failed(exc, attachment);
0N/A }
0N/A
0N/A // clear interrupt
0N/A Thread.interrupted();
0N/A }
0N/A
0N/A /**
0N/A * Invoke handler assuming thread identity already checked
0N/A */
0N/A static <V,A> void invokeDirect(GroupAndInvokeCount myGroupAndInvokeCount,
0N/A CompletionHandler<V,? super A> handler,
0N/A A attachment,
0N/A V result,
0N/A Throwable exc)
0N/A {
0N/A myGroupAndInvokeCount.incrementInvokeCount();
0N/A Invoker.invokeUnchecked(handler, attachment, result, exc);
0N/A }
342N/A
342N/A /**
342N/A * Invokes the handler. If the current thread is in the channel group's
342N/A * thread pool then the handler is invoked directly, otherwise it is
342N/A * invoked indirectly.
342N/A */
342N/A static <V,A> void invoke(AsynchronousChannel channel,
342N/A CompletionHandler<V,? super A> handler,
0N/A A attachment,
0N/A V result,
0N/A Throwable exc)
0N/A {
0N/A boolean invokeDirect = false;
0N/A boolean identityOkay = false;
0N/A GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
0N/A if (thisGroupAndInvokeCount != null) {
0N/A if ((thisGroupAndInvokeCount.group() == ((Groupable)channel).group()))
0N/A identityOkay = true;
0N/A if (identityOkay &&
0N/A (thisGroupAndInvokeCount.invokeCount() < maxHandlerInvokeCount))
0N/A {
0N/A // group match
0N/A invokeDirect = true;
0N/A }
0N/A }
0N/A if (invokeDirect) {
0N/A invokeDirect(thisGroupAndInvokeCount, handler, attachment, result, exc);
0N/A } else {
0N/A try {
0N/A invokeIndirectly(channel, handler, attachment, result, exc);
0N/A } catch (RejectedExecutionException ree) {
0N/A // channel group shutdown; fallback to invoking directly
0N/A // if the current thread has the right identity.
0N/A if (identityOkay) {
0N/A invokeDirect(thisGroupAndInvokeCount,
0N/A handler, attachment, result, exc);
0N/A } else {
0N/A throw new ShutdownChannelGroupException();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes the handler indirectly via the channel group's thread pool.
0N/A */
0N/A static <V,A> void invokeIndirectly(AsynchronousChannel channel,
0N/A final CompletionHandler<V,? super A> handler,
0N/A final A attachment,
0N/A final V result,
0N/A final Throwable exc)
0N/A {
0N/A try {
0N/A ((Groupable)channel).group().executeOnPooledThread(new Runnable() {
0N/A public void run() {
0N/A GroupAndInvokeCount thisGroupAndInvokeCount =
0N/A myGroupAndInvokeCount.get();
0N/A if (thisGroupAndInvokeCount != null)
0N/A thisGroupAndInvokeCount.setInvokeCount(1);
0N/A invokeUnchecked(handler, attachment, result, exc);
0N/A }
0N/A });
0N/A } catch (RejectedExecutionException ree) {
0N/A throw new ShutdownChannelGroupException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes the handler "indirectly" in the given Executor
0N/A */
0N/A static <V,A> void invokeIndirectly(final CompletionHandler<V,? super A> handler,
0N/A final A attachment,
0N/A final V value,
0N/A final Throwable exc,
0N/A Executor executor)
0N/A {
0N/A try {
0N/A executor.execute(new Runnable() {
0N/A public void run() {
0N/A invokeUnchecked(handler, attachment, value, exc);
0N/A }
0N/A });
0N/A } catch (RejectedExecutionException ree) {
0N/A throw new ShutdownChannelGroupException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes the given task on the thread pool associated with the given
0N/A * channel. If the current thread is in the thread pool then the task is
0N/A * invoked directly.
0N/A */
0N/A static void invokeOnThreadInThreadPool(Groupable channel,
0N/A Runnable task)
0N/A {
263N/A boolean invokeDirect;
263N/A GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
263N/A AsynchronousChannelGroupImpl targetGroup = channel.group();
0N/A if (thisGroupAndInvokeCount == null) {
0N/A invokeDirect = false;
0N/A } else {
0N/A invokeDirect = (thisGroupAndInvokeCount.group == targetGroup);
0N/A }
0N/A try {
263N/A if (invokeDirect) {
263N/A task.run();
263N/A } else {
263N/A targetGroup.executeOnPooledThread(task);
263N/A }
263N/A } catch (RejectedExecutionException ree) {
263N/A throw new ShutdownChannelGroupException();
0N/A }
0N/A }
356N/A
356N/A /**
263N/A * Invoke handler with completed result. This method does not check the
0N/A * thread identity or the number of handlers on the thread stack.
0N/A */
263N/A static <V,A> void invokeUnchecked(PendingFuture<V,A> future) {
263N/A assert future.isDone();
263N/A CompletionHandler<V,? super A> handler = future.handler();
263N/A if (handler != null) {
263N/A invokeUnchecked(handler,
263N/A future.attachment(),
263N/A future.value(),
0N/A future.exception());
263N/A }
347N/A }
0N/A
0N/A /**
263N/A * Invoke handler with completed result. If the current thread is in the
0N/A * channel group's thread pool then the handler is invoked directly,
0N/A * otherwise it is invoked indirectly.
356N/A */
0N/A static <V,A> void invoke(PendingFuture<V,A> future) {
0N/A assert future.isDone();
0N/A CompletionHandler<V,? super A> handler = future.handler();
342N/A if (handler != null) {
0N/A invoke(future.channel(),
0N/A handler,
0N/A future.attachment(),
0N/A future.value(),
0N/A future.exception());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invoke handler with completed result. The handler is invoked indirectly,
0N/A * via the channel group's thread pool.
263N/A */
263N/A static <V,A> void invokeIndirectly(PendingFuture<V,A> future) {
0N/A assert future.isDone();
0N/A CompletionHandler<V,? super A> handler = future.handler();
0N/A if (handler != null) {
0N/A invokeIndirectly(future.channel(),
0N/A handler,
0N/A future.attachment(),
0N/A future.value(),
0N/A future.exception());
0N/A }
0N/A }
0N/A}
0N/A