5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import java.util.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.util.concurrent.locks.*; |
12 |
< |
import sun.misc.Unsafe; |
13 |
< |
import java.lang.reflect.*; |
10 |
> |
|
11 |
> |
import java.util.Collection; |
12 |
|
|
13 |
|
/** |
14 |
|
* A thread managed by a {@link ForkJoinPool}. This class is |
15 |
|
* subclassable solely for the sake of adding functionality -- there |
16 |
< |
* are no overridable methods dealing with scheduling or |
17 |
< |
* execution. However, you can override initialization and termination |
18 |
< |
* cleanup methods surrounding the main task processing loop. If you |
19 |
< |
* do create such a subclass, you will also need to supply a custom |
20 |
< |
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
16 |
> |
* are no overridable methods dealing with scheduling or execution. |
17 |
> |
* However, you can override initialization and termination methods |
18 |
> |
* surrounding the main task processing loop. If you do create such a |
19 |
> |
* subclass, you will also need to supply a custom {@link |
20 |
> |
* ForkJoinPool.ForkJoinWorkerThreadFactory} to use it in a {@code |
21 |
> |
* ForkJoinPool}. |
22 |
|
* |
23 |
+ |
* @since 1.7 |
24 |
+ |
* @author Doug Lea |
25 |
|
*/ |
26 |
|
public class ForkJoinWorkerThread extends Thread { |
27 |
|
/* |
45 |
|
* of tasks. To accomplish this, we shift the CAS arbitrating pop |
46 |
|
* vs deq (steal) from being on the indices ("base" and "sp") to |
47 |
|
* the slots themselves (mainly via method "casSlotNull()"). So, |
48 |
< |
* both a successful pop and deq mainly entail CAS'ing a nonnull |
48 |
> |
* both a successful pop and deq mainly entail CAS'ing a non-null |
49 |
|
* slot to null. Because we rely on CASes of references, we do |
50 |
|
* not need tag bits on base or sp. They are simple ints as used |
51 |
|
* in any circular array-based queue (see for example ArrayDeque). |
57 |
|
* considered individually, is not wait-free. One thief cannot |
58 |
|
* successfully continue until another in-progress one (or, if |
59 |
|
* previously empty, a push) completes. However, in the |
60 |
< |
* aggregate, we ensure at least probablistic non-blockingness. If |
61 |
< |
* an attempted steal fails, a thief always chooses a different |
62 |
< |
* random victim target to try next. So, in order for one thief to |
63 |
< |
* progress, it suffices for any in-progress deq or new push on |
64 |
< |
* any empty queue to complete. One reason this works well here is |
65 |
< |
* that apparently-nonempty often means soon-to-be-stealable, |
66 |
< |
* which gives threads a chance to activate if necessary before |
67 |
< |
* stealing (see below). |
60 |
> |
* aggregate, we ensure at least probabilistic |
61 |
> |
* non-blockingness. If an attempted steal fails, a thief always |
62 |
> |
* chooses a different random victim target to try next. So, in |
63 |
> |
* order for one thief to progress, it suffices for any |
64 |
> |
* in-progress deq or new push on any empty queue to complete. One |
65 |
> |
* reason this works well here is that apparently-nonempty often |
66 |
> |
* means soon-to-be-stealable, which gives threads a chance to |
67 |
> |
* activate if necessary before stealing (see below). |
68 |
> |
* |
69 |
> |
* This approach also enables support for "async mode" where local |
70 |
> |
* task processing is in FIFO, not LIFO order; simply by using a |
71 |
> |
* version of deq rather than pop when locallyFifo is true (as set |
72 |
> |
* by the ForkJoinPool). This allows use in message-passing |
73 |
> |
* frameworks in which tasks are never joined. |
74 |
|
* |
75 |
|
* Efficient implementation of this approach currently relies on |
76 |
|
* an uncomfortable amount of "Unsafe" mechanics. To maintain |
80 |
|
* protected by volatile base reads, reads of the queue array and |
81 |
|
* its slots do not need volatile load semantics, but writes (in |
82 |
|
* push) require store order and CASes (in pop and deq) require |
83 |
< |
* (volatile) CAS semantics. Since these combinations aren't |
84 |
< |
* supported using ordinary volatiles, the only way to accomplish |
85 |
< |
* these effciently is to use direct Unsafe calls. (Using external |
83 |
> |
* (volatile) CAS semantics. (See "Idempotent work stealing" by |
84 |
> |
* Michael, Saraswat, and Vechev, PPoPP 2009 |
85 |
> |
* http://portal.acm.org/citation.cfm?id=1504186 for an algorithm |
86 |
> |
* with similar properties, but without support for nulling |
87 |
> |
* slots.) Since these combinations aren't supported using |
88 |
> |
* ordinary volatiles, the only way to accomplish these |
89 |
> |
* efficiently is to use direct Unsafe calls. (Using external |
90 |
|
* AtomicIntegers and AtomicReferenceArrays for the indices and |
91 |
|
* array is significantly slower because of memory locality and |
92 |
< |
* indirection effects.) Further, performance on most platforms is |
93 |
< |
* very sensitive to placement and sizing of the (resizable) queue |
94 |
< |
* array. Even though these queues don't usually become all that |
95 |
< |
* big, the initial size must be large enough to counteract cache |
92 |
> |
* indirection effects.) |
93 |
> |
* |
94 |
> |
* Further, performance on most platforms is very sensitive to |
95 |
> |
* placement and sizing of the (resizable) queue array. Even |
96 |
> |
* though these queues don't usually become all that big, the |
97 |
> |
* initial size must be large enough to counteract cache |
98 |
|
* contention effects across multiple queues (especially in the |
99 |
|
* presence of GC cardmarking). Also, to improve thread-locality, |
100 |
|
* queues are currently initialized immediately after the thread |
111 |
|
* counter (activeCount) held by the pool. It uses an algorithm |
112 |
|
* similar to that in Herlihy and Shavit section 17.6 to cause |
113 |
|
* threads to eventually block when all threads declare they are |
114 |
< |
* inactive. (See variable "scans".) For this to work, threads |
115 |
< |
* must be declared active when executing tasks, and before |
116 |
< |
* stealing a task. They must be inactive before blocking on the |
117 |
< |
* Pool Barrier (awaiting a new submission or other Pool |
118 |
< |
* event). In between, there is some free play which we take |
119 |
< |
* advantage of to avoid contention and rapid flickering of the |
120 |
< |
* global activeCount: If inactive, we activate only if a victim |
121 |
< |
* queue appears to be nonempty (see above). Similarly, a thread |
122 |
< |
* tries to inactivate only after a full scan of other threads. |
123 |
< |
* The net effect is that contention on activeCount is rarely a |
124 |
< |
* measurable performance issue. (There are also a few other cases |
125 |
< |
* where we scan for work rather than retry/block upon |
113 |
< |
* contention.) |
114 |
> |
* inactive. For this to work, threads must be declared active |
115 |
> |
* when executing tasks, and before stealing a task. They must be |
116 |
> |
* inactive before blocking on the Pool Barrier (awaiting a new |
117 |
> |
* submission or other Pool event). In between, there is some free |
118 |
> |
* play which we take advantage of to avoid contention and rapid |
119 |
> |
* flickering of the global activeCount: If inactive, we activate |
120 |
> |
* only if a victim queue appears to be nonempty (see above). |
121 |
> |
* Similarly, a thread tries to inactivate only after a full scan |
122 |
> |
* of other threads. The net effect is that contention on |
123 |
> |
* activeCount is rarely a measurable performance issue. (There |
124 |
> |
* are also a few other cases where we scan for work rather than |
125 |
> |
* retry/block upon contention.) |
126 |
|
* |
127 |
|
* 3. Selection control. We maintain policy of always choosing to |
128 |
|
* run local tasks rather than stealing, and always trying to |
149 |
|
private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28; |
150 |
|
|
151 |
|
/** |
152 |
< |
* The pool this thread works in. Accessed directly by ForkJoinTask |
152 |
> |
* The pool this thread works in. Accessed directly by ForkJoinTask. |
153 |
|
*/ |
154 |
|
final ForkJoinPool pool; |
155 |
|
|
177 |
|
* Activity status. When true, this worker is considered active. |
178 |
|
* Must be false upon construction. It must be true when executing |
179 |
|
* tasks, and BEFORE stealing a task. It must be false before |
180 |
< |
* calling pool.sync |
180 |
> |
* calling pool.sync. |
181 |
|
*/ |
182 |
|
private boolean active; |
183 |
|
|
200 |
|
|
201 |
|
/** |
202 |
|
* Index of this worker in pool array. Set once by pool before |
203 |
< |
* running, and accessed directly by pool during cleanup etc |
203 |
> |
* running, and accessed directly by pool during cleanup etc. |
204 |
|
*/ |
205 |
|
int poolIndex; |
206 |
|
|
211 |
|
long lastEventCount; |
212 |
|
|
213 |
|
/** |
214 |
+ |
* True if use local fifo, not default lifo, for local polling |
215 |
+ |
*/ |
216 |
+ |
private boolean locallyFifo; |
217 |
+ |
|
218 |
+ |
/** |
219 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
220 |
+ |
* |
221 |
|
* @param pool the pool this thread works in |
222 |
|
* @throws NullPointerException if pool is null |
223 |
|
*/ |
231 |
|
// Public access methods |
232 |
|
|
233 |
|
/** |
234 |
< |
* Returns the pool hosting this thread |
234 |
> |
* Returns the pool hosting this thread. |
235 |
> |
* |
236 |
|
* @return the pool |
237 |
|
*/ |
238 |
|
public ForkJoinPool getPool() { |
245 |
|
* threads (minus one) that have ever been created in the pool. |
246 |
|
* This method may be useful for applications that track status or |
247 |
|
* collect results per-worker rather than per-task. |
248 |
< |
* @return the index number. |
248 |
> |
* |
249 |
> |
* @return the index number |
250 |
|
*/ |
251 |
|
public int getPoolIndex() { |
252 |
|
return poolIndex; |
253 |
|
} |
254 |
|
|
255 |
+ |
/** |
256 |
+ |
* Establishes local first-in-first-out scheduling mode for forked |
257 |
+ |
* tasks that are never joined. |
258 |
+ |
* |
259 |
+ |
* @param async if true, use locally FIFO scheduling |
260 |
+ |
*/ |
261 |
+ |
void setAsyncMode(boolean async) { |
262 |
+ |
locallyFifo = async; |
263 |
+ |
} |
264 |
|
|
265 |
|
// Runstate management |
266 |
|
|
277 |
|
final boolean shutdownNow() { return transitionRunStateTo(TERMINATING); } |
278 |
|
|
279 |
|
/** |
280 |
< |
* Transition to at least the given state. Return true if not |
281 |
< |
* already at least given state. |
280 |
> |
* Transitions to at least the given state. |
281 |
> |
* |
282 |
> |
* @return {@code true} if not already at least at given state |
283 |
|
*/ |
284 |
|
private boolean transitionRunStateTo(int state) { |
285 |
|
for (;;) { |
286 |
|
int s = runState; |
287 |
|
if (s >= state) |
288 |
|
return false; |
289 |
< |
if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state)) |
289 |
> |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state)) |
290 |
|
return true; |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
< |
* Try to set status to active; fail on contention |
295 |
> |
* Tries to set status to active; fails on contention. |
296 |
|
*/ |
297 |
|
private boolean tryActivate() { |
298 |
|
if (!active) { |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* Try to set status to active; fail on contention |
307 |
> |
* Tries to set status to inactive; fails on contention. |
308 |
|
*/ |
309 |
|
private boolean tryInactivate() { |
310 |
|
if (active) { |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
< |
* Computes next value for random victim probe. Scans don't |
319 |
> |
* Computes next value for random victim probe. Scans don't |
320 |
|
* require a very high quality generator, but also not a crummy |
321 |
< |
* one. Marsaglia xor-shift is cheap and works well. |
321 |
> |
* one. Marsaglia xor-shift is cheap and works well. |
322 |
|
*/ |
323 |
|
private static int xorShift(int r) { |
324 |
< |
r ^= r << 1; |
325 |
< |
r ^= r >>> 3; |
326 |
< |
r ^= r << 10; |
297 |
< |
return r; |
324 |
> |
r ^= (r << 13); |
325 |
> |
r ^= (r >>> 17); |
326 |
> |
return r ^ (r << 5); |
327 |
|
} |
328 |
|
|
329 |
|
// Lifecycle methods |
347 |
|
} |
348 |
|
|
349 |
|
/** |
350 |
< |
* Execute tasks until shut down. |
350 |
> |
* Executes tasks until shut down. |
351 |
|
*/ |
352 |
|
private void mainLoop() { |
353 |
|
while (!isShutdown()) { |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* Perform cleanup associated with termination of this worker |
382 |
> |
* Performs cleanup associated with termination of this worker |
383 |
|
* thread. If you override this method, you must invoke |
384 |
< |
* super.onTermination at the end of the overridden method. |
384 |
> |
* {@code super.onTermination} at the end of the overridden method. |
385 |
|
* |
386 |
|
* @param exception the exception causing this thread to abort due |
387 |
< |
* to an unrecoverable error, or null if completed normally. |
387 |
> |
* to an unrecoverable error, or {@code null} if completed normally |
388 |
|
*/ |
389 |
|
protected void onTermination(Throwable exception) { |
390 |
|
// Execute remaining local tasks unless aborting or terminating |
391 |
< |
while (exception == null && !pool.isTerminating() && base != sp) { |
391 |
> |
while (exception == null && pool.isProcessingTasks() && base != sp) { |
392 |
|
try { |
393 |
|
ForkJoinTask<?> t = popTask(); |
394 |
|
if (t != null) |
395 |
|
t.quietlyExec(); |
396 |
< |
} catch(Throwable ex) { |
396 |
> |
} catch (Throwable ex) { |
397 |
|
exception = ex; |
398 |
|
} |
399 |
|
} |
400 |
|
// Cancel other tasks, transition status, notify pool, and |
401 |
|
// propagate exception to uncaught exception handler |
402 |
|
try { |
403 |
< |
do;while (!tryInactivate()); // ensure inactive |
403 |
> |
do {} while (!tryInactivate()); // ensure inactive |
404 |
|
cancelTasks(); |
405 |
|
runState = TERMINATED; |
406 |
|
pool.workerTerminated(this); |
415 |
|
|
416 |
|
// Intrinsics-based support for queue operations. |
417 |
|
|
418 |
+ |
private static long slotOffset(int i) { |
419 |
+ |
return ((long) i << qShift) + qBase; |
420 |
+ |
} |
421 |
+ |
|
422 |
|
/** |
423 |
< |
* Add in store-order the given task at given slot of q to |
424 |
< |
* null. Caller must ensure q is nonnull and index is in range. |
423 |
> |
* Adds in store-order the given task at given slot of q to null. |
424 |
> |
* Caller must ensure q is non-null and index is in range. |
425 |
|
*/ |
426 |
|
private static void setSlot(ForkJoinTask<?>[] q, int i, |
427 |
< |
ForkJoinTask<?> t){ |
428 |
< |
_unsafe.putOrderedObject(q, (i << qShift) + qBase, t); |
427 |
> |
ForkJoinTask<?> t) { |
428 |
> |
UNSAFE.putOrderedObject(q, slotOffset(i), t); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* CAS given slot of q to null. Caller must ensure q is nonnull |
432 |
> |
* CAS given slot of q to null. Caller must ensure q is non-null |
433 |
|
* and index is in range. |
434 |
|
*/ |
435 |
|
private static boolean casSlotNull(ForkJoinTask<?>[] q, int i, |
436 |
|
ForkJoinTask<?> t) { |
437 |
< |
return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
437 |
> |
return UNSAFE.compareAndSwapObject(q, slotOffset(i), t, null); |
438 |
|
} |
439 |
|
|
440 |
|
/** |
441 |
|
* Sets sp in store-order. |
442 |
|
*/ |
443 |
|
private void storeSp(int s) { |
444 |
< |
_unsafe.putOrderedInt(this, spOffset, s); |
444 |
> |
UNSAFE.putOrderedInt(this, spOffset, s); |
445 |
|
} |
446 |
|
|
447 |
|
// Main queue methods |
448 |
|
|
449 |
|
/** |
450 |
|
* Pushes a task. Called only by current thread. |
451 |
< |
* @param t the task. Caller must ensure nonnull |
451 |
> |
* |
452 |
> |
* @param t the task. Caller must ensure non-null. |
453 |
|
*/ |
454 |
|
final void pushTask(ForkJoinTask<?> t) { |
455 |
|
ForkJoinTask<?>[] q = queue; |
466 |
|
/** |
467 |
|
* Tries to take a task from the base of the queue, failing if |
468 |
|
* either empty or contended. |
469 |
< |
* @return a task, or null if none or contended. |
469 |
> |
* |
470 |
> |
* @return a task, or null if none or contended |
471 |
|
*/ |
472 |
< |
private ForkJoinTask<?> deqTask() { |
472 |
> |
final ForkJoinTask<?> deqTask() { |
473 |
|
ForkJoinTask<?> t; |
474 |
|
ForkJoinTask<?>[] q; |
475 |
|
int i; |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
+ |
* Tries to take a task from the base of own queue, activating if |
489 |
+ |
* necessary, failing only if empty. Called only by current thread. |
490 |
+ |
* |
491 |
+ |
* @return a task, or null if none |
492 |
+ |
*/ |
493 |
+ |
final ForkJoinTask<?> locallyDeqTask() { |
494 |
+ |
int b; |
495 |
+ |
while (sp != (b = base)) { |
496 |
+ |
if (tryActivate()) { |
497 |
+ |
ForkJoinTask<?>[] q = queue; |
498 |
+ |
int i = (q.length - 1) & b; |
499 |
+ |
ForkJoinTask<?> t = q[i]; |
500 |
+ |
if (t != null && casSlotNull(q, i, t)) { |
501 |
+ |
base = b + 1; |
502 |
+ |
return t; |
503 |
+ |
} |
504 |
+ |
} |
505 |
+ |
} |
506 |
+ |
return null; |
507 |
+ |
} |
508 |
+ |
|
509 |
+ |
/** |
510 |
|
* Returns a popped task, or null if empty. Ensures active status |
511 |
< |
* if nonnull. Called only by current thread. |
511 |
> |
* if non-null. Called only by current thread. |
512 |
|
*/ |
513 |
|
final ForkJoinTask<?> popTask() { |
514 |
|
int s = sp; |
531 |
|
* Specialized version of popTask to pop only if |
532 |
|
* topmost element is the given task. Called only |
533 |
|
* by current thread while active. |
534 |
< |
* @param t the task. Caller must ensure nonnull |
534 |
> |
* |
535 |
> |
* @param t the task. Caller must ensure non-null. |
536 |
|
*/ |
537 |
|
final boolean unpushTask(ForkJoinTask<?> t) { |
538 |
|
ForkJoinTask<?>[] q = queue; |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* Returns next task to pop. |
549 |
> |
* Returns next task or null if empty or contended |
550 |
|
*/ |
551 |
|
final ForkJoinTask<?> peekTask() { |
552 |
|
ForkJoinTask<?>[] q = queue; |
553 |
< |
return q == null? null : q[(sp - 1) & (q.length - 1)]; |
553 |
> |
if (q == null) |
554 |
> |
return null; |
555 |
> |
int mask = q.length - 1; |
556 |
> |
int i = locallyFifo ? base : (sp - 1); |
557 |
> |
return q[i & mask]; |
558 |
|
} |
559 |
|
|
560 |
|
/** |
616 |
|
ForkJoinWorkerThread v = ws[mask & idx]; |
617 |
|
if (v == null || v.sp == v.base) { |
618 |
|
if (probes <= mask) |
619 |
< |
idx = (probes++ < 0)? r : (idx + 1); |
619 |
> |
idx = (probes++ < 0) ? r : (idx + 1); |
620 |
|
else |
621 |
|
break; |
622 |
|
} |
632 |
|
} |
633 |
|
|
634 |
|
/** |
635 |
< |
* Pops or steals a task |
635 |
> |
* Gets and removes a local or stolen task. |
636 |
> |
* |
637 |
|
* @return a task, if available |
638 |
|
*/ |
639 |
|
final ForkJoinTask<?> pollTask() { |
640 |
< |
ForkJoinTask<?> t = popTask(); |
640 |
> |
ForkJoinTask<?> t = locallyFifo ? locallyDeqTask() : popTask(); |
641 |
|
if (t == null && (t = scan()) != null) |
642 |
|
++stealCount; |
643 |
|
return t; |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
+ |
* Gets a local task. |
648 |
+ |
* |
649 |
+ |
* @return a task, if available |
650 |
+ |
*/ |
651 |
+ |
final ForkJoinTask<?> pollLocalTask() { |
652 |
+ |
return locallyFifo ? locallyDeqTask() : popTask(); |
653 |
+ |
} |
654 |
+ |
|
655 |
+ |
/** |
656 |
|
* Returns a pool submission, if one exists, activating first. |
657 |
+ |
* |
658 |
|
* @return a submission, if available |
659 |
|
*/ |
660 |
|
private ForkJoinTask<?> pollSubmission() { |
680 |
|
} |
681 |
|
|
682 |
|
/** |
683 |
< |
* Get and clear steal count for accumulation by pool. Called |
683 |
> |
* Drains tasks to given collection c. |
684 |
> |
* |
685 |
> |
* @return the number of tasks drained |
686 |
> |
*/ |
687 |
> |
final int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
688 |
> |
int n = 0; |
689 |
> |
ForkJoinTask<?> t; |
690 |
> |
while (base != sp && (t = deqTask()) != null) { |
691 |
> |
c.add(t); |
692 |
> |
++n; |
693 |
> |
} |
694 |
> |
return n; |
695 |
> |
} |
696 |
> |
|
697 |
> |
/** |
698 |
> |
* Gets and clears steal count for accumulation by pool. Called |
699 |
|
* only when known to be idle (in pool.sync and termination). |
700 |
|
*/ |
701 |
|
final int getAndClearStealCount() { |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* Returns true if at least one worker in the given array appears |
709 |
< |
* to have at least one queued task. |
708 |
> |
* Returns {@code true} if at least one worker in the given array |
709 |
> |
* appears to have at least one queued task. |
710 |
> |
* |
711 |
|
* @param ws array of workers |
712 |
|
*/ |
713 |
|
static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) { |
730 |
|
* Returns an estimate of the number of tasks in the queue. |
731 |
|
*/ |
732 |
|
final int getQueueSize() { |
733 |
< |
int n = sp - base; |
734 |
< |
return n < 0? 0 : n; // suppress momentarily negative values |
733 |
> |
// suppress momentarily negative values |
734 |
> |
return Math.max(0, sp - base); |
735 |
|
} |
736 |
|
|
737 |
|
/** |
744 |
|
} |
745 |
|
|
746 |
|
/** |
747 |
< |
* Scan, returning early if joinMe done |
747 |
> |
* Scans, returning early if joinMe done. |
748 |
|
*/ |
749 |
|
final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) { |
750 |
|
ForkJoinTask<?> t = pollTask(); |
756 |
|
} |
757 |
|
|
758 |
|
/** |
759 |
< |
* Runs tasks until pool isQuiescent |
759 |
> |
* Runs tasks until {@code pool.isQuiescent()}. |
760 |
|
*/ |
761 |
|
final void helpQuiescePool() { |
762 |
|
for (;;) { |
766 |
|
else if (tryInactivate() && pool.isQuiescent()) |
767 |
|
break; |
768 |
|
} |
769 |
< |
do;while (!tryActivate()); // re-activate on exit |
769 |
> |
do {} while (!tryActivate()); // re-activate on exit |
770 |
|
} |
771 |
|
|
772 |
< |
// Temporary Unsafe mechanics for preliminary release |
773 |
< |
private static Unsafe getUnsafe() throws Throwable { |
772 |
> |
// Unsafe mechanics |
773 |
> |
|
774 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
775 |
> |
private static final long spOffset = |
776 |
> |
objectFieldOffset("sp", ForkJoinWorkerThread.class); |
777 |
> |
private static final long runStateOffset = |
778 |
> |
objectFieldOffset("runState", ForkJoinWorkerThread.class); |
779 |
> |
private static final long qBase; |
780 |
> |
private static final int qShift; |
781 |
> |
|
782 |
> |
static { |
783 |
> |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
784 |
> |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
785 |
> |
if ((s & (s-1)) != 0) |
786 |
> |
throw new Error("data type scale not a power of two"); |
787 |
> |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
788 |
> |
} |
789 |
> |
|
790 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
791 |
> |
try { |
792 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
793 |
> |
} catch (NoSuchFieldException e) { |
794 |
> |
// Convert Exception to corresponding Error |
795 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
796 |
> |
error.initCause(e); |
797 |
> |
throw error; |
798 |
> |
} |
799 |
> |
} |
800 |
> |
|
801 |
> |
/** |
802 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
803 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
804 |
> |
* into a jdk. |
805 |
> |
* |
806 |
> |
* @return a sun.misc.Unsafe |
807 |
> |
*/ |
808 |
> |
private static sun.misc.Unsafe getUnsafe() { |
809 |
|
try { |
810 |
< |
return Unsafe.getUnsafe(); |
810 |
> |
return sun.misc.Unsafe.getUnsafe(); |
811 |
|
} catch (SecurityException se) { |
812 |
|
try { |
813 |
|
return java.security.AccessController.doPrivileged |
814 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
815 |
< |
public Unsafe run() throws Exception { |
816 |
< |
return getUnsafePrivileged(); |
814 |
> |
(new java.security |
815 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
816 |
> |
public sun.misc.Unsafe run() throws Exception { |
817 |
> |
java.lang.reflect.Field f = sun.misc |
818 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
819 |
> |
f.setAccessible(true); |
820 |
> |
return (sun.misc.Unsafe) f.get(null); |
821 |
|
}}); |
822 |
|
} catch (java.security.PrivilegedActionException e) { |
823 |
< |
throw e.getCause(); |
823 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
824 |
> |
e.getCause()); |
825 |
|
} |
826 |
|
} |
827 |
|
} |
699 |
– |
|
700 |
– |
private static Unsafe getUnsafePrivileged() |
701 |
– |
throws NoSuchFieldException, IllegalAccessException { |
702 |
– |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
703 |
– |
f.setAccessible(true); |
704 |
– |
return (Unsafe) f.get(null); |
705 |
– |
} |
706 |
– |
|
707 |
– |
private static long fieldOffset(String fieldName) |
708 |
– |
throws NoSuchFieldException { |
709 |
– |
return _unsafe.objectFieldOffset |
710 |
– |
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
711 |
– |
} |
712 |
– |
|
713 |
– |
static final Unsafe _unsafe; |
714 |
– |
static final long baseOffset; |
715 |
– |
static final long spOffset; |
716 |
– |
static final long runStateOffset; |
717 |
– |
static final long qBase; |
718 |
– |
static final int qShift; |
719 |
– |
static { |
720 |
– |
try { |
721 |
– |
_unsafe = getUnsafe(); |
722 |
– |
baseOffset = fieldOffset("base"); |
723 |
– |
spOffset = fieldOffset("sp"); |
724 |
– |
runStateOffset = fieldOffset("runState"); |
725 |
– |
qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class); |
726 |
– |
int s = _unsafe.arrayIndexScale(ForkJoinTask[].class); |
727 |
– |
if ((s & (s-1)) != 0) |
728 |
– |
throw new Error("data type scale not a power of two"); |
729 |
– |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
730 |
– |
} catch (Throwable e) { |
731 |
– |
throw new RuntimeException("Could not initialize intrinsics", e); |
732 |
– |
} |
733 |
– |
} |
828 |
|
} |