1 /*
2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.nio.ch;
27 
28 import java.nio.channels.*;
29 import java.util.concurrent.*;
30 import java.security.AccessController;
31 import sun.security.action.GetIntegerAction;
32 
33 /**
34  * Defines static methods to invoke a completion handler or arbitrary task.
35  */
36 
37 class Invoker {
Invoker()38     private Invoker() { }
39 
40     // maximum number of completion handlers that may be invoked on the current
41     // thread before it re-directs invocations to the thread pool. This helps
42     // avoid stack overflow and lessens the risk of starvation.
43     private static final int maxHandlerInvokeCount = AccessController.doPrivileged(
44         new GetIntegerAction("sun.nio.ch.maxCompletionHandlersOnStack", 16));
45 
46     // Per-thread object with reference to channel group and a counter for
47     // the number of completion handlers invoked. This should be reset to 0
48     // when all completion handlers have completed.
49     static class GroupAndInvokeCount {
50         private final AsynchronousChannelGroupImpl group;
51         private int handlerInvokeCount;
GroupAndInvokeCount(AsynchronousChannelGroupImpl group)52         GroupAndInvokeCount(AsynchronousChannelGroupImpl group) {
53             this.group = group;
54         }
group()55         AsynchronousChannelGroupImpl group() {
56             return group;
57         }
invokeCount()58         int invokeCount() {
59             return handlerInvokeCount;
60         }
setInvokeCount(int value)61         void setInvokeCount(int value) {
62             handlerInvokeCount = value;
63         }
resetInvokeCount()64         void resetInvokeCount() {
65             handlerInvokeCount = 0;
66         }
incrementInvokeCount()67         void incrementInvokeCount() {
68             handlerInvokeCount++;
69         }
70     }
71     private static final ThreadLocal<GroupAndInvokeCount> myGroupAndInvokeCount =
72         new ThreadLocal<GroupAndInvokeCount>() {
73             @Override protected GroupAndInvokeCount initialValue() {
74                 return null;
75             }
76         };
77 
78     /**
79      * Binds this thread to the given group
80      */
bindToGroup(AsynchronousChannelGroupImpl group)81     static void bindToGroup(AsynchronousChannelGroupImpl group) {
82         myGroupAndInvokeCount.set(new GroupAndInvokeCount(group));
83     }
84 
85     /**
86      * Returns the GroupAndInvokeCount object for this thread.
87      */
getGroupAndInvokeCount()88     static GroupAndInvokeCount getGroupAndInvokeCount() {
89         return myGroupAndInvokeCount.get();
90     }
91 
92     /**
93      * Returns true if the current thread is in a channel group's thread pool
94      */
isBoundToAnyGroup()95     static boolean isBoundToAnyGroup() {
96         return myGroupAndInvokeCount.get() != null;
97     }
98 
99     /**
100      * Returns true if the current thread is in the given channel's thread
101      * pool and we haven't exceeded the maximum number of handler frames on
102      * the stack.
103      */
mayInvokeDirect(GroupAndInvokeCount myGroupAndInvokeCount, AsynchronousChannelGroupImpl group)104     static boolean mayInvokeDirect(GroupAndInvokeCount myGroupAndInvokeCount,
105                                    AsynchronousChannelGroupImpl group)
106     {
107         if ((myGroupAndInvokeCount != null) &&
108             (myGroupAndInvokeCount.group() == group) &&
109             (myGroupAndInvokeCount.invokeCount() < maxHandlerInvokeCount))
110         {
111             return true;
112         }
113         return false;
114     }
115 
116     /**
117      * Invoke handler without checking the thread identity or number of handlers
118      * on the thread stack.
119      */
invokeUnchecked(CompletionHandler<V,? super A> handler, A attachment, V value, Throwable exc)120     static <V,A> void invokeUnchecked(CompletionHandler<V,? super A> handler,
121                                       A attachment,
122                                       V value,
123                                       Throwable exc)
124     {
125         if (exc == null) {
126             handler.completed(value, attachment);
127         } else {
128             handler.failed(exc, attachment);
129         }
130 
131         // clear interrupt
132         Thread.interrupted();
133 
134         // clear thread locals when in default thread pool
135         // Android-changed: System.getSecurityManager always returns null.
136         /*
137         if (System.getSecurityManager() != null) {
138             Thread me = Thread.currentThread();
139             if (me instanceof sun.misc.InnocuousThread) {
140                 GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
141                 ((sun.misc.InnocuousThread)me).eraseThreadLocals();
142                 if (thisGroupAndInvokeCount != null) {
143                     myGroupAndInvokeCount.set(thisGroupAndInvokeCount);
144                 }
145             }
146         }
147         */
148     }
149 
150     /**
151      * Invoke handler assuming thread identity already checked
152      */
invokeDirect(GroupAndInvokeCount myGroupAndInvokeCount, CompletionHandler<V,? super A> handler, A attachment, V result, Throwable exc)153     static <V,A> void invokeDirect(GroupAndInvokeCount myGroupAndInvokeCount,
154                                    CompletionHandler<V,? super A> handler,
155                                    A attachment,
156                                    V result,
157                                    Throwable exc)
158     {
159         myGroupAndInvokeCount.incrementInvokeCount();
160         Invoker.invokeUnchecked(handler, attachment, result, exc);
161     }
162 
163     /**
164      * Invokes the handler. If the current thread is in the channel group's
165      * thread pool then the handler is invoked directly, otherwise it is
166      * invoked indirectly.
167      */
invoke(AsynchronousChannel channel, CompletionHandler<V,? super A> handler, A attachment, V result, Throwable exc)168     static <V,A> void invoke(AsynchronousChannel channel,
169                              CompletionHandler<V,? super A> handler,
170                              A attachment,
171                              V result,
172                              Throwable exc)
173     {
174         boolean invokeDirect = false;
175         boolean identityOkay = false;
176         GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
177         if (thisGroupAndInvokeCount != null) {
178             if ((thisGroupAndInvokeCount.group() == ((Groupable)channel).group()))
179                 identityOkay = true;
180             if (identityOkay &&
181                 (thisGroupAndInvokeCount.invokeCount() < maxHandlerInvokeCount))
182             {
183                 // group match
184                 invokeDirect = true;
185             }
186         }
187         if (invokeDirect) {
188             invokeDirect(thisGroupAndInvokeCount, handler, attachment, result, exc);
189         } else {
190             try {
191                 invokeIndirectly(channel, handler, attachment, result, exc);
192             } catch (RejectedExecutionException ree) {
193                 // channel group shutdown; fallback to invoking directly
194                 // if the current thread has the right identity.
195                 if (identityOkay) {
196                     invokeDirect(thisGroupAndInvokeCount,
197                                  handler, attachment, result, exc);
198                 } else {
199                     throw new ShutdownChannelGroupException();
200                 }
201             }
202         }
203     }
204 
205     /**
206      * Invokes the handler indirectly via the channel group's thread pool.
207      */
invokeIndirectly(AsynchronousChannel channel, final CompletionHandler<V,? super A> handler, final A attachment, final V result, final Throwable exc)208     static <V,A> void invokeIndirectly(AsynchronousChannel channel,
209                                        final CompletionHandler<V,? super A> handler,
210                                        final A attachment,
211                                        final V result,
212                                        final Throwable exc)
213     {
214         try {
215             ((Groupable)channel).group().executeOnPooledThread(new Runnable() {
216                 public void run() {
217                     GroupAndInvokeCount thisGroupAndInvokeCount =
218                         myGroupAndInvokeCount.get();
219                     if (thisGroupAndInvokeCount != null)
220                         thisGroupAndInvokeCount.setInvokeCount(1);
221                     invokeUnchecked(handler, attachment, result, exc);
222                 }
223             });
224         } catch (RejectedExecutionException ree) {
225             throw new ShutdownChannelGroupException();
226         }
227     }
228 
229     /**
230      * Invokes the handler "indirectly" in the given Executor
231      */
invokeIndirectly(final CompletionHandler<V,? super A> handler, final A attachment, final V value, final Throwable exc, Executor executor)232     static <V,A> void invokeIndirectly(final CompletionHandler<V,? super A> handler,
233                                        final A attachment,
234                                        final V value,
235                                        final Throwable exc,
236                                        Executor executor)
237     {
238          try {
239             executor.execute(new Runnable() {
240                 public void run() {
241                     invokeUnchecked(handler, attachment, value, exc);
242                 }
243             });
244         } catch (RejectedExecutionException ree) {
245             throw new ShutdownChannelGroupException();
246         }
247     }
248 
249     /**
250      * Invokes the given task on the thread pool associated with the given
251      * channel. If the current thread is in the thread pool then the task is
252      * invoked directly.
253      */
invokeOnThreadInThreadPool(Groupable channel, Runnable task)254     static void invokeOnThreadInThreadPool(Groupable channel,
255                                            Runnable task)
256     {
257         boolean invokeDirect;
258         GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
259         AsynchronousChannelGroupImpl targetGroup = channel.group();
260         if (thisGroupAndInvokeCount == null) {
261             invokeDirect = false;
262         } else {
263             invokeDirect = (thisGroupAndInvokeCount.group == targetGroup);
264         }
265         try {
266             if (invokeDirect) {
267                 task.run();
268             } else {
269                 targetGroup.executeOnPooledThread(task);
270             }
271         } catch (RejectedExecutionException ree) {
272             throw new ShutdownChannelGroupException();
273         }
274     }
275 
276     /**
277      * Invoke handler with completed result. This method does not check the
278      * thread identity or the number of handlers on the thread stack.
279      */
invokeUnchecked(PendingFuture<V,A> future)280     static <V,A> void invokeUnchecked(PendingFuture<V,A> future) {
281         assert future.isDone();
282         CompletionHandler<V,? super A> handler = future.handler();
283         if (handler != null) {
284             invokeUnchecked(handler,
285                             future.attachment(),
286                             future.value(),
287                             future.exception());
288         }
289     }
290 
291     /**
292      * Invoke handler with completed result. If the current thread is in the
293      * channel group's thread pool then the handler is invoked directly,
294      * otherwise it is invoked indirectly.
295      */
invoke(PendingFuture<V,A> future)296     static <V,A> void invoke(PendingFuture<V,A> future) {
297         assert future.isDone();
298         CompletionHandler<V,? super A> handler = future.handler();
299         if (handler != null) {
300             invoke(future.channel(),
301                    handler,
302                    future.attachment(),
303                    future.value(),
304                    future.exception());
305         }
306     }
307 
308     /**
309      * Invoke handler with completed result. The handler is invoked indirectly,
310      * via the channel group's thread pool.
311      */
invokeIndirectly(PendingFuture<V,A> future)312     static <V,A> void invokeIndirectly(PendingFuture<V,A> future) {
313         assert future.isDone();
314         CompletionHandler<V,? super A> handler = future.handler();
315         if (handler != null) {
316             invokeIndirectly(future.channel(),
317                              handler,
318                              future.attachment(),
319                              future.value(),
320                              future.exception());
321         }
322     }
323 }
324