author | dl |
Fri, 03 Mar 2017 10:45:38 -0800 | |
changeset 44039 | 058585425bb7 |
parent 43545 | fdecd1d14d53 |
child 44589 | 64d9270bd24c |
permissions | -rw-r--r-- |
4110 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
4110 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8765
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
22070 | 38 |
import java.security.AccessControlContext; |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
39 |
import java.security.AccessController; |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
40 |
import java.security.PrivilegedAction; |
22070 | 41 |
import java.security.ProtectionDomain; |
42 |
||
4110 | 43 |
/** |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
44 |
* A thread managed by a {@link ForkJoinPool}, which executes |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
45 |
* {@link ForkJoinTask}s. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
46 |
* This class is subclassable solely for the sake of adding |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
47 |
* functionality -- there are no overridable methods dealing with |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
48 |
* scheduling or execution. However, you can override initialization |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
49 |
* and termination methods surrounding the main task processing loop. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
50 |
* If you do create such a subclass, you will also need to supply a |
18790 | 51 |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
52 |
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
|
4110 | 53 |
* |
54 |
* @since 1.7 |
|
55 |
* @author Doug Lea |
|
56 |
*/ |
|
57 |
public class ForkJoinWorkerThread extends Thread { |
|
58 |
/* |
|
6543 | 59 |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
60 |
* ForkJoinTasks. For explanation, see the internal documentation |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
61 |
* of class ForkJoinPool. |
4110 | 62 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
63 |
* This class just maintains links to its pool and WorkQueue. The |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
64 |
* pool field is set immediately upon construction, but the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
65 |
* workQueue field is not set until a call to registerWorker |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
66 |
* completes. This leads to a visibility race, that is tolerated |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
67 |
* by requiring that the workQueue field is only accessed by the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
68 |
* owning thread. |
22070 | 69 |
* |
70 |
* Support for (non-public) subclass InnocuousForkJoinWorkerThread |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
71 |
* requires that we break quite a lot of encapsulation (via helper |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
72 |
* methods in ThreadLocalRandom) both here and in the subclass to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
73 |
* access and set Thread fields. |
4110 | 74 |
*/ |
75 |
||
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
76 |
final ForkJoinPool pool; // the pool this thread works in |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
77 |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
4110 | 78 |
|
79 |
/** |
|
80 |
* Creates a ForkJoinWorkerThread operating in the given pool. |
|
81 |
* |
|
82 |
* @param pool the pool this thread works in |
|
83 |
* @throws NullPointerException if pool is null |
|
84 |
*/ |
|
85 |
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
86 |
// Use a placeholder until a useful name can be set in registerWorker |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
87 |
super("aForkJoinWorkerThread"); |
4110 | 88 |
this.pool = pool; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
89 |
this.workQueue = pool.registerWorker(this); |
4110 | 90 |
} |
91 |
||
92 |
/** |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
93 |
* Version for use by the default pool. Supports setting the |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
94 |
* context class loader. This is a separate constructor to avoid |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
95 |
* affecting the protected constructor. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
96 |
*/ |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
97 |
ForkJoinWorkerThread(ForkJoinPool pool, ClassLoader ccl) { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
98 |
super("aForkJoinWorkerThread"); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
99 |
super.setContextClassLoader(ccl); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
100 |
this.pool = pool; |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
101 |
this.workQueue = pool.registerWorker(this); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
102 |
} |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
103 |
|
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
104 |
/** |
32988 | 105 |
* Version for InnocuousForkJoinWorkerThread. |
22070 | 106 |
*/ |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
107 |
ForkJoinWorkerThread(ForkJoinPool pool, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
108 |
ClassLoader ccl, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
109 |
ThreadGroup threadGroup, |
22070 | 110 |
AccessControlContext acc) { |
111 |
super(threadGroup, null, "aForkJoinWorkerThread"); |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
112 |
super.setContextClassLoader(ccl); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
113 |
ThreadLocalRandom.setInheritedAccessControlContext(this, acc); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
114 |
ThreadLocalRandom.eraseThreadLocals(this); // clear before registering |
22070 | 115 |
this.pool = pool; |
116 |
this.workQueue = pool.registerWorker(this); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
4110 | 120 |
* Returns the pool hosting this thread. |
121 |
* |
|
122 |
* @return the pool |
|
123 |
*/ |
|
124 |
public ForkJoinPool getPool() { |
|
125 |
return pool; |
|
126 |
} |
|
127 |
||
128 |
/** |
|
18790 | 129 |
* Returns the unique index number of this thread in its pool. |
130 |
* The returned value ranges from zero to the maximum number of |
|
131 |
* threads (minus one) that may exist in the pool, and does not |
|
132 |
* change during the lifetime of the thread. This method may be |
|
133 |
* useful for applications that track status or collect results |
|
134 |
* per-worker-thread rather than per-task. |
|
4110 | 135 |
* |
136 |
* @return the index number |
|
137 |
*/ |
|
138 |
public int getPoolIndex() { |
|
26448 | 139 |
return workQueue.getPoolIndex(); |
4110 | 140 |
} |
141 |
||
142 |
/** |
|
143 |
* Initializes internal state after construction but before |
|
144 |
* processing any tasks. If you override this method, you must |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
145 |
* invoke {@code super.onStart()} at the beginning of the method. |
4110 | 146 |
* Initialization requires care: Most fields must have legal |
147 |
* default values, to ensure that attempted accesses from other |
|
148 |
* threads work correctly even before this thread starts |
|
149 |
* processing tasks. |
|
150 |
*/ |
|
151 |
protected void onStart() { |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Performs cleanup associated with termination of this worker |
|
156 |
* thread. If you override this method, you must invoke |
|
157 |
* {@code super.onTermination} at the end of the overridden method. |
|
158 |
* |
|
159 |
* @param exception the exception causing this thread to abort due |
|
160 |
* to an unrecoverable error, or {@code null} if completed normally |
|
161 |
*/ |
|
162 |
protected void onTermination(Throwable exception) { |
|
6543 | 163 |
} |
164 |
||
165 |
/** |
|
166 |
* This method is required to be public, but should never be |
|
167 |
* called explicitly. It performs the main run loop to execute |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
168 |
* {@link ForkJoinTask}s. |
6543 | 169 |
*/ |
170 |
public void run() { |
|
22070 | 171 |
if (workQueue.array == null) { // only run once |
172 |
Throwable exception = null; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
173 |
try { |
22070 | 174 |
onStart(); |
175 |
pool.runWorker(workQueue); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
176 |
} catch (Throwable ex) { |
22070 | 177 |
exception = ex; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
178 |
} finally { |
22070 | 179 |
try { |
180 |
onTermination(exception); |
|
181 |
} catch (Throwable ex) { |
|
182 |
if (exception == null) |
|
183 |
exception = ex; |
|
184 |
} finally { |
|
185 |
pool.deregisterWorker(this, exception); |
|
186 |
} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
187 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
188 |
} |
4110 | 189 |
} |
22070 | 190 |
|
191 |
/** |
|
32988 | 192 |
* Non-public hook method for InnocuousForkJoinWorkerThread. |
22070 | 193 |
*/ |
194 |
void afterTopLevelExec() { |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* A worker thread that has no permissions, is not a member of any |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
199 |
* user-defined ThreadGroup, uses the system class loader as |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
200 |
* thread context class loader, and erases all ThreadLocals after |
22070 | 201 |
* running each top-level task. |
202 |
*/ |
|
203 |
static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread { |
|
204 |
/** The ThreadGroup for all InnocuousForkJoinWorkerThreads */ |
|
205 |
private static final ThreadGroup innocuousThreadGroup = |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
206 |
java.security.AccessController.doPrivileged( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
207 |
new java.security.PrivilegedAction<>() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
208 |
public ThreadGroup run() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
209 |
ThreadGroup group = Thread.currentThread().getThreadGroup(); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
210 |
for (ThreadGroup p; (p = group.getParent()) != null; ) |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
211 |
group = p; |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
212 |
return new ThreadGroup(group, "InnocuousForkJoinWorkerThreadGroup"); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
213 |
}}); |
22070 | 214 |
|
215 |
/** An AccessControlContext supporting no privileges */ |
|
216 |
private static final AccessControlContext INNOCUOUS_ACC = |
|
217 |
new AccessControlContext( |
|
218 |
new ProtectionDomain[] { |
|
219 |
new ProtectionDomain(null, null) |
|
220 |
}); |
|
221 |
||
222 |
InnocuousForkJoinWorkerThread(ForkJoinPool pool) { |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
223 |
super(pool, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
224 |
ClassLoader.getSystemClassLoader(), |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
225 |
innocuousThreadGroup, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
43219
diff
changeset
|
226 |
INNOCUOUS_ACC); |
22070 | 227 |
} |
228 |
||
229 |
@Override // to erase ThreadLocals |
|
230 |
void afterTopLevelExec() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
36936
diff
changeset
|
231 |
ThreadLocalRandom.eraseThreadLocals(this); |
22070 | 232 |
} |
233 |
||
234 |
@Override // to silently fail |
|
235 |
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { } |
|
236 |
||
237 |
@Override // paranoically |
|
238 |
public void setContextClassLoader(ClassLoader cl) { |
|
239 |
throw new SecurityException("setContextClassLoader"); |
|
240 |
} |
|
241 |
} |
|
4110 | 242 |
} |