/*
* 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.
*/
/**
* Defines static methods to invoke a completion handler or arbitrary task.
*/
class Invoker {
private Invoker() { }
// maximum number of completion handlers that may be invoked on the current
// thread before it re-directs invocations to the thread pool. This helps
// avoid stack overflow and lessens the risk of starvation.
// Per-thread object with reference to channel group and a counter for
// the number of completion handlers invoked. This should be reset to 0
// when all completion handlers have completed.
static class GroupAndInvokeCount {
private int handlerInvokeCount;
}
return group;
}
int invokeCount() {
return handlerInvokeCount;
}
}
void resetInvokeCount() {
handlerInvokeCount = 0;
}
void incrementInvokeCount() {
}
}
new ThreadLocal<GroupAndInvokeCount>() {
return null;
}
};
/**
* Binds this thread to the given group
*/
}
/**
* Returns the GroupAndInvokeCount object for this thread.
*/
return myGroupAndInvokeCount.get();
}
/**
* Returns true if the current thread is in a channel group's thread pool
*/
static boolean isBoundToAnyGroup() {
}
/**
* Returns true if the current thread is in the given channel's thread
* pool and we haven't exceeded the maximum number of handler frames on
* the stack.
*/
{
if ((myGroupAndInvokeCount != null) &&
{
return true;
}
return false;
}
/**
* Invoke handler without checking the thread identity or number of handlers
* on the thread stack.
*/
A attachment,
V value,
{
} else {
}
// clear interrupt
}
/**
* Invoke handler assuming thread identity already checked
*/
CompletionHandler<V,? super A> handler,
A attachment,
V result,
{
}
/**
* Invokes the handler. If the current thread is in the channel group's
* thread pool then the handler is invoked directly, otherwise it is
* invoked indirectly.
*/
CompletionHandler<V,? super A> handler,
A attachment,
V result,
{
boolean invokeDirect = false;
boolean identityOkay = false;
if (thisGroupAndInvokeCount != null) {
identityOkay = true;
if (identityOkay &&
{
// group match
invokeDirect = true;
}
}
if (invokeDirect) {
} else {
try {
} catch (RejectedExecutionException ree) {
// channel group shutdown; fallback to invoking directly
// if the current thread has the right identity.
if (identityOkay) {
} else {
throw new ShutdownChannelGroupException();
}
}
}
}
/**
* Invokes the handler indirectly via the channel group's thread pool.
*/
final CompletionHandler<V,? super A> handler,
final A attachment,
final V result,
{
try {
public void run() {
if (thisGroupAndInvokeCount != null)
}
});
} catch (RejectedExecutionException ree) {
throw new ShutdownChannelGroupException();
}
}
/**
* Invokes the handler "indirectly" in the given Executor
*/
final A attachment,
final V value,
{
try {
public void run() {
}
});
} catch (RejectedExecutionException ree) {
throw new ShutdownChannelGroupException();
}
}
/**
* Invokes the given task on the thread pool associated with the given
* channel. If the current thread is in the thread pool then the task is
* invoked directly.
*/
{
boolean invokeDirect;
if (thisGroupAndInvokeCount == null) {
invokeDirect = false;
} else {
}
try {
if (invokeDirect) {
} else {
}
} catch (RejectedExecutionException ree) {
throw new ShutdownChannelGroupException();
}
}
/**
* Invoke handler with completed result. This method does not check the
* thread identity or the number of handlers on the thread stack.
*/
future.attachment(),
}
}
/**
* Invoke handler with completed result. If the current thread is in the
* channel group's thread pool then the handler is invoked directly,
* otherwise it is invoked indirectly.
*/
future.attachment(),
}
}
/**
* Invoke handler with completed result. The handler is invoked indirectly,
* via the channel group's thread pool.
*/
future.attachment(),
}
}
}