Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/nio/ch/Invoker.java
41159 views
1
/*
2
* Copyright (c) 2008, 2021, 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
import jdk.internal.misc.InnocuousThread;
33
34
/**
35
* Defines static methods to invoke a completion handler or arbitrary task.
36
*/
37
38
class Invoker {
39
private Invoker() { }
40
41
// maximum number of completion handlers that may be invoked on the current
42
// thread before it re-directs invocations to the thread pool. This helps
43
// avoid stack overflow and lessens the risk of starvation.
44
@SuppressWarnings("removal")
45
private static final int maxHandlerInvokeCount = AccessController.doPrivileged(
46
new GetIntegerAction("sun.nio.ch.maxCompletionHandlersOnStack", 16));
47
48
// Per-thread object with reference to channel group and a counter for
49
// the number of completion handlers invoked. This should be reset to 0
50
// when all completion handlers have completed.
51
static class GroupAndInvokeCount {
52
private final AsynchronousChannelGroupImpl group;
53
private int handlerInvokeCount;
54
GroupAndInvokeCount(AsynchronousChannelGroupImpl group) {
55
this.group = group;
56
}
57
AsynchronousChannelGroupImpl group() {
58
return group;
59
}
60
int invokeCount() {
61
return handlerInvokeCount;
62
}
63
void setInvokeCount(int value) {
64
handlerInvokeCount = value;
65
}
66
void resetInvokeCount() {
67
handlerInvokeCount = 0;
68
}
69
void incrementInvokeCount() {
70
handlerInvokeCount++;
71
}
72
}
73
private static final ThreadLocal<GroupAndInvokeCount> myGroupAndInvokeCount =
74
new ThreadLocal<GroupAndInvokeCount>() {
75
@Override protected GroupAndInvokeCount initialValue() {
76
return null;
77
}
78
};
79
80
/**
81
* Binds this thread to the given group
82
*/
83
static void bindToGroup(AsynchronousChannelGroupImpl group) {
84
myGroupAndInvokeCount.set(new GroupAndInvokeCount(group));
85
}
86
87
/**
88
* Returns the GroupAndInvokeCount object for this thread.
89
*/
90
static GroupAndInvokeCount getGroupAndInvokeCount() {
91
return myGroupAndInvokeCount.get();
92
}
93
94
/**
95
* Returns true if the current thread is in a channel group's thread pool
96
*/
97
static boolean isBoundToAnyGroup() {
98
return myGroupAndInvokeCount.get() != null;
99
}
100
101
/**
102
* Returns true if the current thread is in the given channel's thread
103
* pool and we haven't exceeded the maximum number of handler frames on
104
* the stack.
105
*/
106
static boolean mayInvokeDirect(GroupAndInvokeCount myGroupAndInvokeCount,
107
AsynchronousChannelGroupImpl group)
108
{
109
if ((myGroupAndInvokeCount != null) &&
110
(myGroupAndInvokeCount.group() == group) &&
111
(myGroupAndInvokeCount.invokeCount() < maxHandlerInvokeCount))
112
{
113
return true;
114
}
115
return false;
116
}
117
118
/**
119
* Invoke handler without checking the thread identity or number of handlers
120
* on the thread stack.
121
*/
122
@SuppressWarnings("removal")
123
static <V,A> void invokeUnchecked(CompletionHandler<V,? super A> handler,
124
A attachment,
125
V value,
126
Throwable exc)
127
{
128
if (exc == null) {
129
handler.completed(value, attachment);
130
} else {
131
handler.failed(exc, attachment);
132
}
133
134
// clear interrupt
135
Thread.interrupted();
136
137
// clear thread locals when in default thread pool
138
if (System.getSecurityManager() != null) {
139
Thread me = Thread.currentThread();
140
if (me instanceof InnocuousThread) {
141
GroupAndInvokeCount thisGroupAndInvokeCount = myGroupAndInvokeCount.get();
142
((InnocuousThread)me).eraseThreadLocals();
143
if (thisGroupAndInvokeCount != null) {
144
myGroupAndInvokeCount.set(thisGroupAndInvokeCount);
145
}
146
}
147
}
148
}
149
150
/**
151
* Invoke handler assuming thread identity already checked
152
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
325