1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.46 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
|
|
|
9 |
dl |
1.60 |
import java.security.AccessControlContext; |
10 |
jsr166 |
1.74 |
import java.security.AccessController; |
11 |
|
|
import java.security.PrivilegedAction; |
12 |
dl |
1.60 |
import java.security.ProtectionDomain; |
13 |
|
|
|
14 |
jsr166 |
1.1 |
/** |
15 |
jsr166 |
1.40 |
* A thread managed by a {@link ForkJoinPool}, which executes |
16 |
|
|
* {@link ForkJoinTask}s. |
17 |
|
|
* This class is subclassable solely for the sake of adding |
18 |
|
|
* functionality -- there are no overridable methods dealing with |
19 |
|
|
* scheduling or execution. However, you can override initialization |
20 |
|
|
* and termination methods surrounding the main task processing loop. |
21 |
|
|
* If you do create such a subclass, you will also need to supply a |
22 |
jsr166 |
1.57 |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to |
23 |
|
|
* {@linkplain ForkJoinPool#ForkJoinPool use it} in a {@code ForkJoinPool}. |
24 |
jsr166 |
1.1 |
* |
25 |
|
|
* @since 1.7 |
26 |
|
|
* @author Doug Lea |
27 |
|
|
*/ |
28 |
|
|
public class ForkJoinWorkerThread extends Thread { |
29 |
|
|
/* |
30 |
dl |
1.14 |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
31 |
dl |
1.52 |
* ForkJoinTasks. For explanation, see the internal documentation |
32 |
|
|
* of class ForkJoinPool. |
33 |
dl |
1.55 |
* |
34 |
|
|
* This class just maintains links to its pool and WorkQueue. The |
35 |
dl |
1.56 |
* pool field is set immediately upon construction, but the |
36 |
|
|
* workQueue field is not set until a call to registerWorker |
37 |
|
|
* completes. This leads to a visibility race, that is tolerated |
38 |
|
|
* by requiring that the workQueue field is only accessed by the |
39 |
|
|
* owning thread. |
40 |
dl |
1.60 |
* |
41 |
|
|
* Support for (non-public) subclass InnocuousForkJoinWorkerThread |
42 |
dl |
1.70 |
* requires that we break quite a lot of encapsulation (via helper |
43 |
jsr166 |
1.72 |
* methods in ThreadLocalRandom) both here and in the subclass to |
44 |
dl |
1.70 |
* access and set Thread fields. |
45 |
jsr166 |
1.1 |
*/ |
46 |
|
|
|
47 |
dl |
1.52 |
final ForkJoinPool pool; // the pool this thread works in |
48 |
dl |
1.56 |
final ForkJoinPool.WorkQueue workQueue; // work-stealing mechanics |
49 |
dl |
1.54 |
|
50 |
|
|
/** |
51 |
jsr166 |
1.1 |
* Creates a ForkJoinWorkerThread operating in the given pool. |
52 |
|
|
* |
53 |
|
|
* @param pool the pool this thread works in |
54 |
|
|
* @throws NullPointerException if pool is null |
55 |
|
|
*/ |
56 |
|
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
57 |
dl |
1.56 |
// Use a placeholder until a useful name can be set in registerWorker |
58 |
|
|
super("aForkJoinWorkerThread"); |
59 |
dl |
1.55 |
this.pool = pool; |
60 |
dl |
1.56 |
this.workQueue = pool.registerWorker(this); |
61 |
dl |
1.14 |
} |
62 |
|
|
|
63 |
jsr166 |
1.1 |
/** |
64 |
jsr166 |
1.74 |
* Version for use by the default pool. Supports setting the |
65 |
|
|
* context class loader. This is a separate constructor to avoid |
66 |
|
|
* affecting the protected constructor. |
67 |
|
|
*/ |
68 |
|
|
ForkJoinWorkerThread(ForkJoinPool pool, ClassLoader ccl) { |
69 |
|
|
super("aForkJoinWorkerThread"); |
70 |
|
|
super.setContextClassLoader(ccl); |
71 |
|
|
this.pool = pool; |
72 |
|
|
this.workQueue = pool.registerWorker(this); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
jsr166 |
1.68 |
* Version for InnocuousForkJoinWorkerThread. |
77 |
dl |
1.60 |
*/ |
78 |
jsr166 |
1.74 |
ForkJoinWorkerThread(ForkJoinPool pool, |
79 |
|
|
ClassLoader ccl, |
80 |
|
|
ThreadGroup threadGroup, |
81 |
dl |
1.60 |
AccessControlContext acc) { |
82 |
|
|
super(threadGroup, null, "aForkJoinWorkerThread"); |
83 |
jsr166 |
1.74 |
super.setContextClassLoader(ccl); |
84 |
jsr166 |
1.71 |
ThreadLocalRandom.setInheritedAccessControlContext(this, acc); |
85 |
dl |
1.70 |
ThreadLocalRandom.eraseThreadLocals(this); // clear before registering |
86 |
dl |
1.60 |
this.pool = pool; |
87 |
|
|
this.workQueue = pool.registerWorker(this); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
jsr166 |
1.1 |
* Returns the pool hosting this thread. |
92 |
|
|
* |
93 |
|
|
* @return the pool |
94 |
|
|
*/ |
95 |
|
|
public ForkJoinPool getPool() { |
96 |
|
|
return pool; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
/** |
100 |
jsr166 |
1.59 |
* Returns the unique index number of this thread in its pool. |
101 |
|
|
* The returned value ranges from zero to the maximum number of |
102 |
|
|
* threads (minus one) that may exist in the pool, and does not |
103 |
|
|
* change during the lifetime of the thread. This method may be |
104 |
|
|
* useful for applications that track status or collect results |
105 |
|
|
* per-worker-thread rather than per-task. |
106 |
jsr166 |
1.1 |
* |
107 |
|
|
* @return the index number |
108 |
|
|
*/ |
109 |
|
|
public int getPoolIndex() { |
110 |
dl |
1.65 |
return workQueue.getPoolIndex(); |
111 |
dl |
1.45 |
} |
112 |
|
|
|
113 |
jsr166 |
1.1 |
/** |
114 |
dl |
1.14 |
* Initializes internal state after construction but before |
115 |
|
|
* processing any tasks. If you override this method, you must |
116 |
jsr166 |
1.39 |
* invoke {@code super.onStart()} at the beginning of the method. |
117 |
dl |
1.14 |
* Initialization requires care: Most fields must have legal |
118 |
|
|
* default values, to ensure that attempted accesses from other |
119 |
|
|
* threads work correctly even before this thread starts |
120 |
|
|
* processing tasks. |
121 |
jsr166 |
1.1 |
*/ |
122 |
dl |
1.14 |
protected void onStart() { |
123 |
|
|
} |
124 |
jsr166 |
1.1 |
|
125 |
|
|
/** |
126 |
dl |
1.14 |
* Performs cleanup associated with termination of this worker |
127 |
|
|
* thread. If you override this method, you must invoke |
128 |
|
|
* {@code super.onTermination} at the end of the overridden method. |
129 |
jsr166 |
1.4 |
* |
130 |
dl |
1.14 |
* @param exception the exception causing this thread to abort due |
131 |
|
|
* to an unrecoverable error, or {@code null} if completed normally |
132 |
jsr166 |
1.1 |
*/ |
133 |
dl |
1.14 |
protected void onTermination(Throwable exception) { |
134 |
jsr166 |
1.1 |
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* This method is required to be public, but should never be |
138 |
|
|
* called explicitly. It performs the main run loop to execute |
139 |
jsr166 |
1.40 |
* {@link ForkJoinTask}s. |
140 |
jsr166 |
1.1 |
*/ |
141 |
|
|
public void run() { |
142 |
dl |
1.60 |
if (workQueue.array == null) { // only run once |
143 |
|
|
Throwable exception = null; |
144 |
dl |
1.52 |
try { |
145 |
dl |
1.60 |
onStart(); |
146 |
|
|
pool.runWorker(workQueue); |
147 |
dl |
1.52 |
} catch (Throwable ex) { |
148 |
dl |
1.60 |
exception = ex; |
149 |
dl |
1.52 |
} finally { |
150 |
dl |
1.60 |
try { |
151 |
|
|
onTermination(exception); |
152 |
|
|
} catch (Throwable ex) { |
153 |
|
|
if (exception == null) |
154 |
|
|
exception = ex; |
155 |
|
|
} finally { |
156 |
|
|
pool.deregisterWorker(this, exception); |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/** |
163 |
jsr166 |
1.68 |
* Non-public hook method for InnocuousForkJoinWorkerThread. |
164 |
dl |
1.60 |
*/ |
165 |
|
|
void afterTopLevelExec() { |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
/** |
169 |
|
|
* A worker thread that has no permissions, is not a member of any |
170 |
jsr166 |
1.74 |
* user-defined ThreadGroup, uses the system class loader as |
171 |
|
|
* thread context class loader, and erases all ThreadLocals after |
172 |
dl |
1.60 |
* running each top-level task. |
173 |
|
|
*/ |
174 |
|
|
static final class InnocuousForkJoinWorkerThread extends ForkJoinWorkerThread { |
175 |
|
|
/** The ThreadGroup for all InnocuousForkJoinWorkerThreads */ |
176 |
|
|
private static final ThreadGroup innocuousThreadGroup = |
177 |
jsr166 |
1.74 |
java.security.AccessController.doPrivileged( |
178 |
|
|
new java.security.PrivilegedAction<>() { |
179 |
|
|
public ThreadGroup run() { |
180 |
|
|
ThreadGroup group = Thread.currentThread().getThreadGroup(); |
181 |
|
|
for (ThreadGroup p; (p = group.getParent()) != null; ) |
182 |
|
|
group = p; |
183 |
|
|
return new ThreadGroup(group, "InnocuousForkJoinWorkerThreadGroup"); |
184 |
|
|
}}); |
185 |
dl |
1.60 |
|
186 |
|
|
/** An AccessControlContext supporting no privileges */ |
187 |
|
|
private static final AccessControlContext INNOCUOUS_ACC = |
188 |
|
|
new AccessControlContext( |
189 |
|
|
new ProtectionDomain[] { |
190 |
|
|
new ProtectionDomain(null, null) |
191 |
|
|
}); |
192 |
|
|
|
193 |
|
|
InnocuousForkJoinWorkerThread(ForkJoinPool pool) { |
194 |
jsr166 |
1.74 |
super(pool, |
195 |
|
|
ClassLoader.getSystemClassLoader(), |
196 |
|
|
innocuousThreadGroup, |
197 |
|
|
INNOCUOUS_ACC); |
198 |
dl |
1.60 |
} |
199 |
|
|
|
200 |
|
|
@Override // to erase ThreadLocals |
201 |
|
|
void afterTopLevelExec() { |
202 |
dl |
1.70 |
ThreadLocalRandom.eraseThreadLocals(this); |
203 |
dl |
1.60 |
} |
204 |
|
|
|
205 |
|
|
@Override // to silently fail |
206 |
|
|
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { } |
207 |
|
|
|
208 |
|
|
@Override // paranoically |
209 |
|
|
public void setContextClassLoader(ClassLoader cl) { |
210 |
|
|
throw new SecurityException("setContextClassLoader"); |
211 |
|
|
} |
212 |
jsr166 |
1.1 |
} |
213 |
|
|
} |