7 |
|
package jsr166y; |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
10 |
> |
|
11 |
> |
import java.util.concurrent.atomic.AtomicReference; |
12 |
|
import java.util.concurrent.locks.LockSupport; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
13 |
|
|
14 |
|
/** |
15 |
|
* A reusable synchronization barrier, similar in functionality to a |
32 |
|
* zero, and advancing when all parties reach the barrier (wrapping |
33 |
|
* around to zero after reaching {@code Integer.MAX_VALUE}). |
34 |
|
* |
35 |
< |
* <li> Like a CyclicBarrier, a Phaser may be repeatedly awaited. |
36 |
< |
* Method {@code arriveAndAwaitAdvance} has effect analogous to |
37 |
< |
* {@code CyclicBarrier.await}. However, Phasers separate two |
38 |
< |
* aspects of coordination, that may also be invoked independently: |
35 |
> |
* <li> Like a {@code CyclicBarrier}, a Phaser may be repeatedly |
36 |
> |
* awaited. Method {@link #arriveAndAwaitAdvance} has effect |
37 |
> |
* analogous to {@link java.util.concurrent.CyclicBarrier#await |
38 |
> |
* CyclicBarrier.await}. However, phasers separate two aspects of |
39 |
> |
* coordination, that may also be invoked independently: |
40 |
|
* |
41 |
|
* <ul> |
42 |
|
* |
43 |
< |
* <li> Arriving at a barrier. Methods {@code arrive} and |
44 |
< |
* {@code arriveAndDeregister} do not block, but return |
43 |
> |
* <li> Arriving at a barrier. Methods {@link #arrive} and |
44 |
> |
* {@link #arriveAndDeregister} do not block, but return |
45 |
|
* the phase value current upon entry to the method. |
46 |
|
* |
47 |
< |
* <li> Awaiting others. Method {@code awaitAdvance} requires an |
47 |
> |
* <li> Awaiting others. Method {@link #awaitAdvance} requires an |
48 |
|
* argument indicating the entry phase, and returns when the |
49 |
|
* barrier advances to a new phase. |
50 |
|
* </ul> |
52 |
|
* |
53 |
|
* <li> Barrier actions, performed by the task triggering a phase |
54 |
|
* advance while others may be waiting, are arranged by overriding |
55 |
< |
* method {@code onAdvance}, that also controls termination. |
55 |
> |
* method {@link #onAdvance}, that also controls termination. |
56 |
|
* Overriding this method may be used to similar but more flexible |
57 |
< |
* effect as providing a barrier action to a CyclicBarrier. |
57 |
> |
* effect as providing a barrier action to a {@code CyclicBarrier}. |
58 |
|
* |
59 |
|
* <li> Phasers may enter a <em>termination</em> state in which all |
60 |
|
* actions immediately return without updating phaser state or waiting |
61 |
|
* for advance, and indicating (via a negative phase value) that |
62 |
|
* execution is complete. Termination is triggered by executing the |
63 |
|
* overridable {@code onAdvance} method that is invoked each time the |
64 |
< |
* barrier is about to be tripped. When a Phaser is controlling an |
64 |
> |
* barrier is about to be tripped. When a phaser is controlling an |
65 |
|
* action with a fixed number of iterations, it is often convenient to |
66 |
|
* override this method to cause termination when the current phase |
67 |
< |
* number reaches a threshold. Method {@code forceTermination} is also |
67 |
> |
* number reaches a threshold. Method {@link #forceTermination} is also |
68 |
|
* available to abruptly release waiting threads and allow them to |
69 |
|
* terminate. |
70 |
|
* |
76 |
|
* |
77 |
|
* <li> By default, {@code awaitAdvance} continues to wait even if |
78 |
|
* the waiting thread is interrupted. And unlike the case in |
79 |
< |
* CyclicBarriers, exceptions encountered while tasks wait |
79 |
> |
* {@code CyclicBarrier}, exceptions encountered while tasks wait |
80 |
|
* interruptibly or with timeout do not change the state of the |
81 |
|
* barrier. If necessary, you can perform any associated recovery |
82 |
|
* within handlers of those exceptions, often after invoking |
88 |
|
* |
89 |
|
* <p><b>Sample usages:</b> |
90 |
|
* |
91 |
< |
* <p>A Phaser may be used instead of a {@code CountDownLatch} to control |
92 |
< |
* a one-shot action serving a variable number of parties. The typical |
93 |
< |
* idiom is for the method setting this up to first register, then |
94 |
< |
* start the actions, then deregister, as in: |
95 |
< |
* |
96 |
< |
* <pre> |
97 |
< |
* void runTasks(List<Runnable> list) { |
98 |
< |
* final Phaser phaser = new Phaser(1); // "1" to register self |
99 |
< |
* for (Runnable r : list) { |
100 |
< |
* phaser.register(); |
101 |
< |
* new Thread() { |
102 |
< |
* public void run() { |
103 |
< |
* phaser.arriveAndAwaitAdvance(); // await all creation |
104 |
< |
* r.run(); |
105 |
< |
* phaser.arriveAndDeregister(); // signal completion |
106 |
< |
* } |
107 |
< |
* }.start(); |
91 |
> |
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
92 |
> |
* to control a one-shot action serving a variable number of |
93 |
> |
* parties. The typical idiom is for the method setting this up to |
94 |
> |
* first register, then start the actions, then deregister, as in: |
95 |
> |
* |
96 |
> |
* <pre> {@code |
97 |
> |
* void runTasks(List<Runnable> list) { |
98 |
> |
* final Phaser phaser = new Phaser(1); // "1" to register self |
99 |
> |
* for (Runnable r : list) { |
100 |
> |
* phaser.register(); |
101 |
> |
* new Thread() { |
102 |
> |
* public void run() { |
103 |
> |
* phaser.arriveAndAwaitAdvance(); // await all creation |
104 |
> |
* r.run(); |
105 |
> |
* phaser.arriveAndDeregister(); // signal completion |
106 |
> |
* } |
107 |
> |
* }.start(); |
108 |
|
* } |
109 |
|
* |
110 |
|
* doSomethingOnBehalfOfWorkers(); |
113 |
|
* p = phaser.awaitAdvance(p); // ... and await arrival |
114 |
|
* otherActions(); // do other things while tasks execute |
115 |
|
* phaser.awaitAdvance(p); // await final completion |
116 |
< |
* } |
117 |
< |
* </pre> |
116 |
> |
* }}</pre> |
117 |
|
* |
118 |
|
* <p>One way to cause a set of threads to repeatedly perform actions |
119 |
|
* for a given number of iterations is to override {@code onAdvance}: |
120 |
|
* |
121 |
< |
* <pre> |
122 |
< |
* void startTasks(List<Runnable> list, final int iterations) { |
123 |
< |
* final Phaser phaser = new Phaser() { |
124 |
< |
* public boolean onAdvance(int phase, int registeredParties) { |
125 |
< |
* return phase >= iterations || registeredParties == 0; |
121 |
> |
* <pre> {@code |
122 |
> |
* void startTasks(List<Runnable> list, final int iterations) { |
123 |
> |
* final Phaser phaser = new Phaser() { |
124 |
> |
* public boolean onAdvance(int phase, int registeredParties) { |
125 |
> |
* return phase >= iterations || registeredParties == 0; |
126 |
> |
* } |
127 |
> |
* }; |
128 |
> |
* phaser.register(); |
129 |
> |
* for (Runnable r : list) { |
130 |
> |
* phaser.register(); |
131 |
> |
* new Thread() { |
132 |
> |
* public void run() { |
133 |
> |
* do { |
134 |
> |
* r.run(); |
135 |
> |
* phaser.arriveAndAwaitAdvance(); |
136 |
> |
* } while(!phaser.isTerminated(); |
137 |
|
* } |
138 |
< |
* }; |
129 |
< |
* phaser.register(); |
130 |
< |
* for (Runnable r : list) { |
131 |
< |
* phaser.register(); |
132 |
< |
* new Thread() { |
133 |
< |
* public void run() { |
134 |
< |
* do { |
135 |
< |
* r.run(); |
136 |
< |
* phaser.arriveAndAwaitAdvance(); |
137 |
< |
* } while(!phaser.isTerminated(); |
138 |
< |
* } |
139 |
< |
* }.start(); |
138 |
> |
* }.start(); |
139 |
|
* } |
140 |
|
* phaser.arriveAndDeregister(); // deregister self, don't wait |
141 |
< |
* } |
143 |
< |
* </pre> |
141 |
> |
* }}</pre> |
142 |
|
* |
143 |
< |
* <p> To create a set of tasks using a tree of Phasers, |
143 |
> |
* <p>To create a set of tasks using a tree of phasers, |
144 |
|
* you could use code of the following form, assuming a |
145 |
< |
* Task class with a constructor accepting a Phaser that |
145 |
> |
* Task class with a constructor accepting a phaser that |
146 |
|
* it registers for upon construction: |
147 |
< |
* <pre> |
148 |
< |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
149 |
< |
* int step = (hi - lo) / TASKS_PER_PHASER; |
150 |
< |
* if (step > 1) { |
151 |
< |
* int i = lo; |
152 |
< |
* while (i < hi) { |
153 |
< |
* int r = Math.min(i + step, hi); |
154 |
< |
* build(actions, i, r, new Phaser(b)); |
155 |
< |
* i = r; |
156 |
< |
* } |
157 |
< |
* } |
158 |
< |
* else { |
159 |
< |
* for (int i = lo; i < hi; ++i) |
160 |
< |
* actions[i] = new Task(b); |
161 |
< |
* // assumes new Task(b) performs b.register() |
162 |
< |
* } |
163 |
< |
* } |
164 |
< |
* // .. initially called, for n tasks via |
167 |
< |
* build(new Task[n], 0, n, new Phaser()); |
168 |
< |
* </pre> |
147 |
> |
* <pre> {@code |
148 |
> |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
149 |
> |
* int step = (hi - lo) / TASKS_PER_PHASER; |
150 |
> |
* if (step > 1) { |
151 |
> |
* int i = lo; |
152 |
> |
* while (i < hi) { |
153 |
> |
* int r = Math.min(i + step, hi); |
154 |
> |
* build(actions, i, r, new Phaser(b)); |
155 |
> |
* i = r; |
156 |
> |
* } |
157 |
> |
* } else { |
158 |
> |
* for (int i = lo; i < hi; ++i) |
159 |
> |
* actions[i] = new Task(b); |
160 |
> |
* // assumes new Task(b) performs b.register() |
161 |
> |
* } |
162 |
> |
* } |
163 |
> |
* // .. initially called, for n tasks via |
164 |
> |
* build(new Task[n], 0, n, new Phaser());}</pre> |
165 |
|
* |
166 |
|
* The best value of {@code TASKS_PER_PHASER} depends mainly on |
167 |
|
* expected barrier synchronization rates. A value as low as four may |
175 |
|
* parties result in IllegalStateExceptions. However, you can and |
176 |
|
* should create tiered phasers to accommodate arbitrarily large sets |
177 |
|
* of participants. |
178 |
+ |
* |
179 |
+ |
* @since 1.7 |
180 |
+ |
* @author Doug Lea |
181 |
|
*/ |
182 |
|
public class Phaser { |
183 |
|
/* |
211 |
|
private static final int phaseMask = 0x7fffffff; |
212 |
|
|
213 |
|
private static int unarrivedOf(long s) { |
214 |
< |
return (int)(s & ushortMask); |
214 |
> |
return (int) (s & ushortMask); |
215 |
|
} |
216 |
|
|
217 |
|
private static int partiesOf(long s) { |
218 |
< |
return ((int)s) >>> 16; |
218 |
> |
return ((int) s) >>> 16; |
219 |
|
} |
220 |
|
|
221 |
|
private static int phaseOf(long s) { |
222 |
< |
return (int)(s >>> 32); |
222 |
> |
return (int) (s >>> 32); |
223 |
|
} |
224 |
|
|
225 |
|
private static int arrivedOf(long s) { |
227 |
|
} |
228 |
|
|
229 |
|
private static long stateFor(int phase, int parties, int unarrived) { |
230 |
< |
return ((((long)phase) << 32) | (((long)parties) << 16) | |
231 |
< |
(long)unarrived); |
230 |
> |
return ((((long) phase) << 32) | (((long) parties) << 16) | |
231 |
> |
(long) unarrived); |
232 |
|
} |
233 |
|
|
234 |
|
private static long trippedStateFor(int phase, int parties) { |
235 |
< |
long lp = (long)parties; |
236 |
< |
return (((long)phase) << 32) | (lp << 16) | lp; |
235 |
> |
long lp = (long) parties; |
236 |
> |
return (((long) phase) << 32) | (lp << 16) | lp; |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* Returns message string for bad bounds exceptions |
240 |
> |
* Returns message string for bad bounds exceptions. |
241 |
|
*/ |
242 |
|
private static String badBounds(int parties, int unarrived) { |
243 |
|
return ("Attempt to set " + unarrived + |
250 |
|
private final Phaser parent; |
251 |
|
|
252 |
|
/** |
253 |
< |
* The root of Phaser tree. Equals this if not in a tree. Used to |
253 |
> |
* The root of phaser tree. Equals this if not in a tree. Used to |
254 |
|
* support faster state push-down. |
255 |
|
*/ |
256 |
|
private final Phaser root; |
266 |
|
private final AtomicReference<QNode> oddQ = new AtomicReference<QNode>(); |
267 |
|
|
268 |
|
private AtomicReference<QNode> queueFor(int phase) { |
269 |
< |
return (phase & 1) == 0? evenQ : oddQ; |
269 |
> |
return ((phase & 1) == 0) ? evenQ : oddQ; |
270 |
|
} |
271 |
|
|
272 |
|
/** |
274 |
|
* root if necessary. |
275 |
|
*/ |
276 |
|
private long getReconciledState() { |
277 |
< |
return parent == null? state : reconcileState(); |
277 |
> |
return (parent == null) ? state : reconcileState(); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
301 |
|
} |
302 |
|
|
303 |
|
/** |
304 |
< |
* Creates a new Phaser without any initially registered parties, |
304 |
> |
* Creates a new phaser without any initially registered parties, |
305 |
|
* initial phase number 0, and no parent. Any thread using this |
306 |
< |
* Phaser will need to first register for it. |
306 |
> |
* phaser will need to first register for it. |
307 |
|
*/ |
308 |
|
public Phaser() { |
309 |
|
this(null); |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* Creates a new Phaser with the given numbers of registered |
313 |
> |
* Creates a new phaser with the given numbers of registered |
314 |
|
* unarrived parties, initial phase number 0, and no parent. |
315 |
< |
* @param parties the number of parties required to trip barrier. |
315 |
> |
* |
316 |
> |
* @param parties the number of parties required to trip barrier |
317 |
|
* @throws IllegalArgumentException if parties less than zero |
318 |
< |
* or greater than the maximum number of parties supported. |
318 |
> |
* or greater than the maximum number of parties supported |
319 |
|
*/ |
320 |
|
public Phaser(int parties) { |
321 |
|
this(null, parties); |
322 |
|
} |
323 |
|
|
324 |
|
/** |
325 |
< |
* Creates a new Phaser with the given parent, without any |
325 |
> |
* Creates a new phaser with the given parent, without any |
326 |
|
* initially registered parties. If parent is non-null this phaser |
327 |
|
* is registered with the parent and its initial phase number is |
328 |
|
* the same as that of parent phaser. |
329 |
< |
* @param parent the parent phaser. |
329 |
> |
* |
330 |
> |
* @param parent the parent phaser |
331 |
|
*/ |
332 |
|
public Phaser(Phaser parent) { |
333 |
|
int phase = 0; |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
< |
* Creates a new Phaser with the given parent and numbers of |
346 |
< |
* registered unarrived parties. If parent is non-null this phaser |
345 |
> |
* Creates a new phaser with the given parent and numbers of |
346 |
> |
* registered unarrived parties. If parent is non-null, this phaser |
347 |
|
* is registered with the parent and its initial phase number is |
348 |
|
* the same as that of parent phaser. |
349 |
< |
* @param parent the parent phaser. |
350 |
< |
* @param parties the number of parties required to trip barrier. |
349 |
> |
* |
350 |
> |
* @param parent the parent phaser |
351 |
> |
* @param parties the number of parties required to trip barrier |
352 |
|
* @throws IllegalArgumentException if parties less than zero |
353 |
< |
* or greater than the maximum number of parties supported. |
353 |
> |
* or greater than the maximum number of parties supported |
354 |
|
*/ |
355 |
|
public Phaser(Phaser parent, int parties) { |
356 |
|
if (parties < 0 || parties > ushortMask) |
368 |
|
|
369 |
|
/** |
370 |
|
* Adds a new unarrived party to this phaser. |
371 |
+ |
* |
372 |
|
* @return the current barrier phase number upon registration |
373 |
|
* @throws IllegalStateException if attempting to register more |
374 |
< |
* than the maximum supported number of parties. |
374 |
> |
* than the maximum supported number of parties |
375 |
|
*/ |
376 |
|
public int register() { |
377 |
|
return doRegister(1); |
379 |
|
|
380 |
|
/** |
381 |
|
* Adds the given number of new unarrived parties to this phaser. |
382 |
< |
* @param parties the number of parties required to trip barrier. |
382 |
> |
* |
383 |
> |
* @param parties the number of parties required to trip barrier |
384 |
|
* @return the current barrier phase number upon registration |
385 |
|
* @throws IllegalStateException if attempting to register more |
386 |
< |
* than the maximum supported number of parties. |
386 |
> |
* than the maximum supported number of parties |
387 |
|
*/ |
388 |
|
public int bulkRegister(int parties) { |
389 |
|
if (parties < 0) |
403 |
|
phase = phaseOf(s); |
404 |
|
int unarrived = unarrivedOf(s) + registrations; |
405 |
|
int parties = partiesOf(s) + registrations; |
406 |
< |
if (phase < 0) |
406 |
> |
if (phase < 0) |
407 |
|
break; |
408 |
|
if (parties > ushortMask || unarrived > ushortMask) |
409 |
|
throw new IllegalStateException(badBounds(parties, unarrived)); |
419 |
|
* in turn wait for others via {@link #awaitAdvance}). |
420 |
|
* |
421 |
|
* @return the barrier phase number upon entry to this method, or a |
422 |
< |
* negative value if terminated; |
422 |
> |
* negative value if terminated |
423 |
|
* @throws IllegalStateException if not terminated and the number |
424 |
< |
* of unarrived parties would become negative. |
424 |
> |
* of unarrived parties would become negative |
425 |
|
*/ |
426 |
|
public int arrive() { |
427 |
|
int phase; |
441 |
|
if (par == null) { // directly trip |
442 |
|
if (casState |
443 |
|
(s, |
444 |
< |
trippedStateFor(onAdvance(phase, parties)? -1 : |
444 |
> |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
445 |
|
((phase + 1) & phaseMask), parties))) { |
446 |
|
releaseWaiters(phase); |
447 |
|
break; |
471 |
|
* zero parties, this phaser is also deregistered from its parent. |
472 |
|
* |
473 |
|
* @return the current barrier phase number upon entry to |
474 |
< |
* this method, or a negative value if terminated; |
474 |
> |
* this method, or a negative value if terminated |
475 |
|
* @throws IllegalStateException if not terminated and the number |
476 |
< |
* of registered or unarrived parties would become negative. |
476 |
> |
* of registered or unarrived parties would become negative |
477 |
|
*/ |
478 |
|
public int arriveAndDeregister() { |
479 |
|
// similar code to arrive, but too different to merge |
502 |
|
if (unarrived == 0) { |
503 |
|
if (casState |
504 |
|
(s, |
505 |
< |
trippedStateFor(onAdvance(phase, parties)? -1 : |
505 |
> |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
506 |
|
((phase + 1) & phaseMask), parties))) { |
507 |
|
releaseWaiters(phase); |
508 |
|
break; |
524 |
|
* to {@code awaitAdvance(arrive())}. If you instead need to |
525 |
|
* await with interruption of timeout, and/or deregister upon |
526 |
|
* arrival, you can arrange them using analogous constructions. |
527 |
+ |
* |
528 |
|
* @return the phase on entry to this method |
529 |
|
* @throws IllegalStateException if not terminated and the number |
530 |
< |
* of unarrived parties would become negative. |
530 |
> |
* of unarrived parties would become negative |
531 |
|
*/ |
532 |
|
public int arriveAndAwaitAdvance() { |
533 |
|
return awaitAdvance(arrive()); |
537 |
|
* Awaits the phase of the barrier to advance from the given |
538 |
|
* value, or returns immediately if argument is negative or this |
539 |
|
* barrier is terminated. |
540 |
+ |
* |
541 |
|
* @param phase the phase on entry to this method |
542 |
|
* @return the phase on exit from this method |
543 |
|
*/ |
559 |
|
* value, or returns immediately if argument is negative or this |
560 |
|
* barrier is terminated, or throws InterruptedException if |
561 |
|
* interrupted while waiting. |
562 |
+ |
* |
563 |
|
* @param phase the phase on entry to this method |
564 |
|
* @return the phase on exit from this method |
565 |
|
* @throws InterruptedException if thread interrupted while waiting |
566 |
|
*/ |
567 |
< |
public int awaitAdvanceInterruptibly(int phase) |
567 |
> |
public int awaitAdvanceInterruptibly(int phase) |
568 |
|
throws InterruptedException { |
569 |
|
if (phase < 0) |
570 |
|
return phase; |
581 |
|
* Awaits the phase of the barrier to advance from the given value |
582 |
|
* or the given timeout elapses, or returns immediately if |
583 |
|
* argument is negative or this barrier is terminated. |
584 |
+ |
* |
585 |
|
* @param phase the phase on entry to this method |
586 |
|
* @return the phase on exit from this method |
587 |
|
* @throws InterruptedException if thread interrupted while waiting |
588 |
|
* @throws TimeoutException if timed out while waiting |
589 |
|
*/ |
590 |
< |
public int awaitAdvanceInterruptibly(int phase, long timeout, TimeUnit unit) |
590 |
> |
public int awaitAdvanceInterruptibly(int phase, |
591 |
> |
long timeout, TimeUnit unit) |
592 |
|
throws InterruptedException, TimeoutException { |
593 |
|
if (phase < 0) |
594 |
|
return phase; |
629 |
|
* Returns the current phase number. The maximum phase number is |
630 |
|
* {@code Integer.MAX_VALUE}, after which it restarts at |
631 |
|
* zero. Upon termination, the phase number is negative. |
632 |
+ |
* |
633 |
|
* @return the phase number, or a negative value if terminated |
634 |
|
*/ |
635 |
|
public final int getPhase() { |
638 |
|
|
639 |
|
/** |
640 |
|
* Returns {@code true} if the current phase number equals the given phase. |
641 |
+ |
* |
642 |
|
* @param phase the phase |
643 |
|
* @return {@code true} if the current phase number equals the given phase |
644 |
|
*/ |
648 |
|
|
649 |
|
/** |
650 |
|
* Returns the number of parties registered at this barrier. |
651 |
+ |
* |
652 |
|
* @return the number of parties |
653 |
|
*/ |
654 |
|
public int getRegisteredParties() { |
658 |
|
/** |
659 |
|
* Returns the number of parties that have arrived at the current |
660 |
|
* phase of this barrier. |
661 |
+ |
* |
662 |
|
* @return the number of arrived parties |
663 |
|
*/ |
664 |
|
public int getArrivedParties() { |
668 |
|
/** |
669 |
|
* Returns the number of registered parties that have not yet |
670 |
|
* arrived at the current phase of this barrier. |
671 |
+ |
* |
672 |
|
* @return the number of unarrived parties |
673 |
|
*/ |
674 |
|
public int getUnarrivedParties() { |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
< |
* Returns the parent of this phaser, or null if none. |
680 |
< |
* @return the parent of this phaser, or null if none |
679 |
> |
* Returns the parent of this phaser, or {@code null} if none. |
680 |
> |
* |
681 |
> |
* @return the parent of this phaser, or {@code null} if none |
682 |
|
*/ |
683 |
|
public Phaser getParent() { |
684 |
|
return parent; |
687 |
|
/** |
688 |
|
* Returns the root ancestor of this phaser, which is the same as |
689 |
|
* this phaser if it has no parent. |
690 |
+ |
* |
691 |
|
* @return the root ancestor of this phaser |
692 |
|
*/ |
693 |
|
public Phaser getRoot() { |
696 |
|
|
697 |
|
/** |
698 |
|
* Returns {@code true} if this barrier has been terminated. |
699 |
+ |
* |
700 |
|
* @return {@code true} if this barrier has been terminated |
701 |
|
*/ |
702 |
|
public boolean isTerminated() { |
707 |
|
* Overridable method to perform an action upon phase advance, and |
708 |
|
* to control termination. This method is invoked whenever the |
709 |
|
* barrier is tripped (and thus all other waiting parties are |
710 |
< |
* dormant). If it returns true, then, rather than advance the |
711 |
< |
* phase number, this barrier will be set to a final termination |
712 |
< |
* state, and subsequent calls to {@code isTerminated} will |
713 |
< |
* return true. |
710 |
> |
* dormant). If it returns {@code true}, then, rather than advance |
711 |
> |
* the phase number, this barrier will be set to a final |
712 |
> |
* termination state, and subsequent calls to {@link #isTerminated} |
713 |
> |
* will return true. |
714 |
|
* |
715 |
< |
* <p> The default version returns true when the number of |
715 |
> |
* <p>The default version returns {@code true} when the number of |
716 |
|
* registered parties is zero. Normally, overrides that arrange |
717 |
|
* termination for other reasons should also preserve this |
718 |
|
* property. |
719 |
|
* |
720 |
< |
* <p> You may override this method to perform an action with side |
720 |
> |
* <p>You may override this method to perform an action with side |
721 |
|
* effects visible to participating tasks, but it is in general |
722 |
|
* only sensible to do so in designs where all parties register |
723 |
< |
* before any arrive, and all {@code awaitAdvance} at each phase. |
723 |
> |
* before any arrive, and all {@link #awaitAdvance} at each phase. |
724 |
|
* Otherwise, you cannot ensure lack of interference. In |
725 |
|
* particular, this method may be invoked more than once per |
726 |
|
* transition if other parties successfully register while the |
812 |
|
try { |
813 |
|
ForkJoinPool.managedBlock(this, false); |
814 |
|
} catch (InterruptedException ie) { |
815 |
< |
} |
815 |
> |
} |
816 |
|
} |
817 |
|
return wasInterrupted; |
818 |
|
} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
823 |
< |
* Removes and signals waiting threads from wait queue |
823 |
> |
* Removes and signals waiting threads from wait queue. |
824 |
|
*/ |
825 |
|
private void releaseWaiters(int phase) { |
826 |
|
AtomicReference<QNode> head = queueFor(phase); |
832 |
|
} |
833 |
|
|
834 |
|
/** |
835 |
< |
* Tries to enqueue given node in the appropriate wait queue |
835 |
> |
* Tries to enqueue given node in the appropriate wait queue. |
836 |
> |
* |
837 |
|
* @return true if successful |
838 |
|
*/ |
839 |
|
private boolean tryEnqueue(QNode node) { |
843 |
|
|
844 |
|
/** |
845 |
|
* Enqueues node and waits unless aborted or signalled. |
846 |
+ |
* |
847 |
|
* @return current phase |
848 |
|
*/ |
849 |
|
private int untimedWait(int phase) { |
931 |
|
return p; |
932 |
|
} |
933 |
|
|
934 |
< |
// Temporary Unsafe mechanics for preliminary release |
916 |
< |
private static Unsafe getUnsafe() throws Throwable { |
917 |
< |
try { |
918 |
< |
return Unsafe.getUnsafe(); |
919 |
< |
} catch (SecurityException se) { |
920 |
< |
try { |
921 |
< |
return java.security.AccessController.doPrivileged |
922 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
923 |
< |
public Unsafe run() throws Exception { |
924 |
< |
return getUnsafePrivileged(); |
925 |
< |
}}); |
926 |
< |
} catch (java.security.PrivilegedActionException e) { |
927 |
< |
throw e.getCause(); |
928 |
< |
} |
929 |
< |
} |
930 |
< |
} |
934 |
> |
// Unsafe mechanics |
935 |
|
|
936 |
< |
private static Unsafe getUnsafePrivileged() |
937 |
< |
throws NoSuchFieldException, IllegalAccessException { |
938 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
935 |
< |
f.setAccessible(true); |
936 |
< |
return (Unsafe)f.get(null); |
937 |
< |
} |
936 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
937 |
> |
private static final long stateOffset = |
938 |
> |
objectFieldOffset("state", Phaser.class); |
939 |
|
|
940 |
< |
private static long fieldOffset(String fieldName) |
941 |
< |
throws NoSuchFieldException { |
941 |
< |
return _unsafe.objectFieldOffset |
942 |
< |
(Phaser.class.getDeclaredField(fieldName)); |
940 |
> |
private final boolean casState(long cmp, long val) { |
941 |
> |
return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); |
942 |
|
} |
943 |
|
|
944 |
< |
static final Unsafe _unsafe; |
946 |
< |
static final long stateOffset; |
947 |
< |
|
948 |
< |
static { |
944 |
> |
private static long objectFieldOffset(String field, Class<?> klazz) { |
945 |
|
try { |
946 |
< |
_unsafe = getUnsafe(); |
947 |
< |
stateOffset = fieldOffset("state"); |
948 |
< |
} catch (Exception e) { |
949 |
< |
throw new RuntimeException("Could not initialize intrinsics", e); |
946 |
> |
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |
947 |
> |
} catch (NoSuchFieldException e) { |
948 |
> |
// Convert Exception to corresponding Error |
949 |
> |
NoSuchFieldError error = new NoSuchFieldError(field); |
950 |
> |
error.initCause(e); |
951 |
> |
throw error; |
952 |
|
} |
953 |
|
} |
954 |
|
|
955 |
< |
final boolean casState(long cmp, long val) { |
956 |
< |
return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val); |
955 |
> |
/** |
956 |
> |
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
957 |
> |
* Replace with a simple call to Unsafe.getUnsafe when integrating |
958 |
> |
* into a jdk. |
959 |
> |
* |
960 |
> |
* @return a sun.misc.Unsafe |
961 |
> |
*/ |
962 |
> |
private static sun.misc.Unsafe getUnsafe() { |
963 |
> |
try { |
964 |
> |
return sun.misc.Unsafe.getUnsafe(); |
965 |
> |
} catch (SecurityException se) { |
966 |
> |
try { |
967 |
> |
return java.security.AccessController.doPrivileged |
968 |
> |
(new java.security |
969 |
> |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
970 |
> |
public sun.misc.Unsafe run() throws Exception { |
971 |
> |
java.lang.reflect.Field f = sun.misc |
972 |
> |
.Unsafe.class.getDeclaredField("theUnsafe"); |
973 |
> |
f.setAccessible(true); |
974 |
> |
return (sun.misc.Unsafe) f.get(null); |
975 |
> |
}}); |
976 |
> |
} catch (java.security.PrivilegedActionException e) { |
977 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
978 |
> |
e.getCause()); |
979 |
> |
} |
980 |
> |
} |
981 |
|
} |
982 |
|
} |