34 |
|
* Phaser} may be repeatedly awaited. Method {@link |
35 |
|
* #arriveAndAwaitAdvance} has effect analogous to {@link |
36 |
|
* java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each |
37 |
< |
* generation of a {@code Phaser} has an associated phase number. The |
38 |
< |
* phase number starts at zero, and advances when all parties arrive |
39 |
< |
* at the barrier, wrapping around to zero after reaching {@code |
37 |
> |
* generation of a phaser has an associated phase number. The phase |
38 |
> |
* number starts at zero, and advances when all parties arrive at the |
39 |
> |
* phaser, wrapping around to zero after reaching {@code |
40 |
|
* Integer.MAX_VALUE}. The use of phase numbers enables independent |
41 |
< |
* control of actions upon arrival at a barrier and upon awaiting |
41 |
> |
* control of actions upon arrival at a phaser and upon awaiting |
42 |
|
* others, via two kinds of methods that may be invoked by any |
43 |
|
* registered party: |
44 |
|
* |
45 |
|
* <ul> |
46 |
|
* |
47 |
|
* <li> <b>Arrival.</b> Methods {@link #arrive} and |
48 |
< |
* {@link #arriveAndDeregister} record arrival at a |
49 |
< |
* barrier. These methods do not block, but return an associated |
50 |
< |
* <em>arrival phase number</em>; that is, the phase number of |
51 |
< |
* the barrier to which the arrival applied. When the final |
52 |
< |
* party for a given phase arrives, an optional barrier action |
53 |
< |
* is performed and the phase advances. Barrier actions, |
54 |
< |
* performed by the party triggering a phase advance, are |
55 |
< |
* arranged by overriding method {@link #onAdvance(int, int)}, |
56 |
< |
* which also controls termination. Overriding this method is |
57 |
< |
* similar to, but more flexible than, providing a barrier |
58 |
< |
* action to a {@code CyclicBarrier}. |
48 |
> |
* {@link #arriveAndDeregister} record arrival. These methods |
49 |
> |
* do not block, but return an associated <em>arrival phase |
50 |
> |
* number</em>; that is, the phase number of the phaser to which |
51 |
> |
* the arrival applied. When the final party for a given phase |
52 |
> |
* arrives, an optional action is performed and the phase |
53 |
> |
* advances. These actions are performed by the party |
54 |
> |
* triggering a phase advance, and are arranged by overriding |
55 |
> |
* method {@link #onAdvance(int, int)}, which also controls |
56 |
> |
* termination. Overriding this method is similar to, but more |
57 |
> |
* flexible than, providing a barrier action to a {@code |
58 |
> |
* CyclicBarrier}. |
59 |
|
* |
60 |
|
* <li> <b>Waiting.</b> Method {@link #awaitAdvance} requires an |
61 |
|
* argument indicating an arrival phase number, and returns when |
62 |
< |
* the barrier advances to (or is already at) a different phase. |
62 |
> |
* the phaser advances to (or is already at) a different phase. |
63 |
|
* Unlike similar constructions using {@code CyclicBarrier}, |
64 |
|
* method {@code awaitAdvance} continues to wait even if the |
65 |
|
* waiting thread is interrupted. Interruptible and timeout |
66 |
|
* versions are also available, but exceptions encountered while |
67 |
|
* tasks wait interruptibly or with timeout do not change the |
68 |
< |
* state of the barrier. If necessary, you can perform any |
68 |
> |
* state of the phaser. If necessary, you can perform any |
69 |
|
* associated recovery within handlers of those exceptions, |
70 |
|
* often after invoking {@code forceTermination}. Phasers may |
71 |
|
* also be used by tasks executing in a {@link ForkJoinPool}, |
74 |
|
* |
75 |
|
* </ul> |
76 |
|
* |
77 |
< |
* <p> <b>Termination.</b> A {@code Phaser} may enter a |
78 |
< |
* <em>termination</em> state in which all synchronization methods |
79 |
< |
* immediately return without updating phaser state or waiting for |
80 |
< |
* advance, and indicating (via a negative phase value) that execution |
81 |
< |
* is complete. Termination is triggered when an invocation of {@code |
82 |
< |
* onAdvance} returns {@code true}. As illustrated below, when |
83 |
< |
* phasers control actions with a fixed number of iterations, it is |
84 |
< |
* often convenient to override this method to cause termination when |
85 |
< |
* the current phase number reaches a threshold. Method {@link |
86 |
< |
* #forceTermination} is also available to abruptly release waiting |
87 |
< |
* threads and allow them to terminate. |
88 |
< |
* |
89 |
< |
* <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., arranged |
90 |
< |
* in tree structures) to reduce contention. Phasers with large |
91 |
< |
* numbers of parties that would otherwise experience heavy |
77 |
> |
* <p> <b>Termination.</b> A phaser may enter a <em>termination</em> |
78 |
> |
* state in which all synchronization methods immediately return |
79 |
> |
* without updating phaser state or waiting for advance, and |
80 |
> |
* indicating (via a negative phase value) that execution is complete. |
81 |
> |
* Termination is triggered when an invocation of {@code onAdvance} |
82 |
> |
* returns {@code true}. The default implementation returns {@code |
83 |
> |
* true} if a deregistration has caused the number of registered |
84 |
> |
* parties to become zero. As illustrated below, when phasers control |
85 |
> |
* actions with a fixed number of iterations, it is often convenient |
86 |
> |
* to override this method to cause termination when the current phase |
87 |
> |
* number reaches a threshold. Method {@link #forceTermination} is |
88 |
> |
* also available to abruptly release waiting threads and allow them |
89 |
> |
* to terminate. |
90 |
> |
* |
91 |
> |
* <p> <b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., |
92 |
> |
* constructed in tree structures) to reduce contention. Phasers with |
93 |
> |
* large numbers of parties that would otherwise experience heavy |
94 |
|
* synchronization contention costs may instead be set up so that |
95 |
|
* groups of sub-phasers share a common parent. This may greatly |
96 |
|
* increase throughput even though it incurs greater per-operation |
183 |
|
* }}</pre> |
184 |
|
* |
185 |
|
* |
186 |
< |
* <p>To create a set of tasks using a tree of phasers, |
187 |
< |
* you could use code of the following form, assuming a |
188 |
< |
* Task class with a constructor accepting a phaser that |
189 |
< |
* it registers with upon construction: |
186 |
> |
* <p>To create a set of {@code n} tasks using a tree of phasers, you |
187 |
> |
* could use code of the following form, assuming a Task class with a |
188 |
> |
* constructor accepting a {@code Phaser} that it registers with upon |
189 |
> |
* construction. After invocation of {@code build(new Task[n], 0, n, |
190 |
> |
* new Phaser())}, these tasks could then be started, for example by |
191 |
> |
* submitting to a pool: |
192 |
|
* |
193 |
|
* <pre> {@code |
194 |
< |
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
194 |
> |
* void build(Task[] tasks, int lo, int hi, Phaser ph) { |
195 |
|
* if (hi - lo > TASKS_PER_PHASER) { |
196 |
|
* for (int i = lo; i < hi; i += TASKS_PER_PHASER) { |
197 |
|
* int j = Math.min(i + TASKS_PER_PHASER, hi); |
198 |
< |
* build(actions, i, j, new Phaser(ph)); |
198 |
> |
* build(tasks, i, j, new Phaser(ph)); |
199 |
|
* } |
200 |
|
* } else { |
201 |
|
* for (int i = lo; i < hi; ++i) |
202 |
< |
* actions[i] = new Task(ph); |
202 |
> |
* tasks[i] = new Task(ph); |
203 |
|
* // assumes new Task(ph) performs ph.register() |
204 |
|
* } |
205 |
< |
* } |
202 |
< |
* // .. initially called, for n tasks via |
203 |
< |
* build(new Task[n], 0, n, new Phaser());}</pre> |
205 |
> |
* }}</pre> |
206 |
|
* |
207 |
|
* The best value of {@code TASKS_PER_PHASER} depends mainly on |
208 |
< |
* expected barrier synchronization rates. A value as low as four may |
209 |
< |
* be appropriate for extremely small per-barrier task bodies (thus |
208 |
> |
* expected synchronization rates. A value as low as four may |
209 |
> |
* be appropriate for extremely small per-phase task bodies (thus |
210 |
|
* high rates), or up to hundreds for extremely large ones. |
211 |
|
* |
212 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
226 |
|
*/ |
227 |
|
|
228 |
|
/** |
229 |
< |
* Barrier state representation. Conceptually, a barrier contains |
228 |
< |
* four values: |
229 |
> |
* Primary state representation, holding four fields: |
230 |
|
* |
231 |
|
* * unarrived -- the number of parties yet to hit barrier (bits 0-15) |
232 |
|
* * parties -- the number of parties to wait (bits 16-31) |
233 |
|
* * phase -- the generation of the barrier (bits 32-62) |
234 |
|
* * terminated -- set if barrier is terminated (bit 63 / sign) |
235 |
|
* |
236 |
< |
* However, to efficiently maintain atomicity, these values are |
237 |
< |
* packed into a single (atomic) long. Termination uses the sign |
238 |
< |
* bit of 32 bit representation of phase, so phase is set to -1 on |
239 |
< |
* termination. Good performance relies on keeping state decoding |
240 |
< |
* and encoding simple, and keeping race windows short. |
236 |
> |
* Except that a phaser with no registered parties is |
237 |
> |
* distinguished with the otherwise illegal state of having zero |
238 |
> |
* parties and one unarrived parties (encoded as EMPTY below). |
239 |
> |
* |
240 |
> |
* To efficiently maintain atomicity, these values are packed into |
241 |
> |
* a single (atomic) long. Good performance relies on keeping |
242 |
> |
* state decoding and encoding simple, and keeping race windows |
243 |
> |
* short. |
244 |
> |
* |
245 |
> |
* All state updates are performed via CAS except initial |
246 |
> |
* registration of a sub-phaser (i.e., one with a non-null |
247 |
> |
* parent). In this (relatively rare) case, we use built-in |
248 |
> |
* synchronization to lock while first registering with its |
249 |
> |
* parent. |
250 |
> |
* |
251 |
> |
* The phase of a subphaser is allowed to lag that of its |
252 |
> |
* ancestors until it is actually accessed. Method reconcileState |
253 |
> |
* is usually attempted only only when the number of unarrived |
254 |
> |
* parties appears to be zero, which indicates a potential lag in |
255 |
> |
* updating phase after the root advanced. |
256 |
|
*/ |
257 |
|
private volatile long state; |
258 |
|
|
259 |
< |
private static final int MAX_COUNT = 0xffff; |
260 |
< |
private static final int MAX_PHASE = 0x7fffffff; |
261 |
< |
private static final int PARTIES_SHIFT = 16; |
262 |
< |
private static final int PHASE_SHIFT = 32; |
263 |
< |
private static final long UNARRIVED_MASK = 0xffffL; |
264 |
< |
private static final long PARTIES_MASK = 0xffff0000L; |
265 |
< |
private static final long ONE_ARRIVAL = 1L; |
266 |
< |
private static final long ONE_PARTY = 1L << PARTIES_SHIFT; |
267 |
< |
private static final long TERMINATION_PHASE = -1L << PHASE_SHIFT; |
259 |
> |
private static final int MAX_PARTIES = 0xffff; |
260 |
> |
private static final int MAX_PHASE = 0x7fffffff; |
261 |
> |
private static final int PARTIES_SHIFT = 16; |
262 |
> |
private static final int PHASE_SHIFT = 32; |
263 |
> |
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
264 |
> |
private static final long PARTIES_MASK = 0xffff0000L; // to mask longs |
265 |
> |
private static final long TERMINATION_BIT = 1L << 63; |
266 |
> |
|
267 |
> |
// some special values |
268 |
> |
private static final int ONE_ARRIVAL = 1; |
269 |
> |
private static final int ONE_PARTY = 1 << PARTIES_SHIFT; |
270 |
> |
private static final int EMPTY = 1; |
271 |
|
|
272 |
|
// The following unpacking methods are usually manually inlined |
273 |
|
|
274 |
|
private static int unarrivedOf(long s) { |
275 |
< |
return (int) (s & UNARRIVED_MASK); |
275 |
> |
int counts = (int)s; |
276 |
> |
return (counts == EMPTY) ? 0 : counts & UNARRIVED_MASK; |
277 |
|
} |
278 |
|
|
279 |
|
private static int partiesOf(long s) { |
280 |
< |
return ((int) (s & PARTIES_MASK)) >>> PARTIES_SHIFT; |
280 |
> |
int counts = (int)s; |
281 |
> |
return (counts == EMPTY) ? 0 : counts >>> PARTIES_SHIFT; |
282 |
|
} |
283 |
|
|
284 |
|
private static int phaseOf(long s) { |
286 |
|
} |
287 |
|
|
288 |
|
private static int arrivedOf(long s) { |
289 |
< |
return partiesOf(s) - unarrivedOf(s); |
289 |
> |
int counts = (int)s; |
290 |
> |
return (counts == EMPTY) ? 0 : |
291 |
> |
(counts >>> PARTIES_SHIFT) - (counts & UNARRIVED_MASK); |
292 |
|
} |
293 |
|
|
294 |
|
/** |
297 |
|
private final Phaser parent; |
298 |
|
|
299 |
|
/** |
300 |
< |
* The root of phaser tree. Equals this if not in a tree. Used to |
278 |
< |
* support faster state push-down. |
300 |
> |
* The root of phaser tree. Equals this if not in a tree. |
301 |
|
*/ |
302 |
|
private final Phaser root; |
303 |
|
|
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
+ |
* Returns message string for bounds exceptions on arrival. |
319 |
+ |
*/ |
320 |
+ |
private String badArrive(long s) { |
321 |
+ |
return "Attempted arrival of unregistered party for " + |
322 |
+ |
stateToString(s); |
323 |
+ |
} |
324 |
+ |
|
325 |
+ |
/** |
326 |
+ |
* Returns message string for bounds exceptions on registration. |
327 |
+ |
*/ |
328 |
+ |
private String badRegister(long s) { |
329 |
+ |
return "Attempt to register more than " + |
330 |
+ |
MAX_PARTIES + " parties for " + stateToString(s); |
331 |
+ |
} |
332 |
+ |
|
333 |
+ |
/** |
334 |
|
* Main implementation for methods arrive and arriveAndDeregister. |
335 |
|
* Manually tuned to speed up and minimize race windows for the |
336 |
|
* common case of just decrementing unarrived field. |
337 |
|
* |
338 |
< |
* @param adj - adjustment to apply to state -- either |
301 |
< |
* ONE_ARRIVAL (for arrive) or |
302 |
< |
* ONE_ARRIVAL|ONE_PARTY (for arriveAndDeregister) |
338 |
> |
* @param deregister false for arrive, true for arriveAndDeregister |
339 |
|
*/ |
340 |
< |
private int doArrive(long adj) { |
340 |
> |
private int doArrive(boolean deregister) { |
341 |
> |
int adj = deregister ? ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL; |
342 |
|
long s; |
343 |
< |
int phase, unarrived; |
343 |
> |
int phase; |
344 |
|
while ((phase = (int)((s = state) >>> PHASE_SHIFT)) >= 0) { |
345 |
< |
if ((unarrived = (int)(s & UNARRIVED_MASK)) != 0) { |
346 |
< |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s -= adj)) { |
347 |
< |
if (unarrived == 1) { |
348 |
< |
Phaser par; |
349 |
< |
long p = s & PARTIES_MASK; // unshifted parties field |
350 |
< |
long lu = p >>> PARTIES_SHIFT; |
351 |
< |
int u = (int)lu; |
352 |
< |
int nextPhase = (phase + 1) & MAX_PHASE; |
353 |
< |
long next = ((long)nextPhase << PHASE_SHIFT) | p | lu; |
354 |
< |
if ((par = parent) == null) { |
355 |
< |
UNSAFE.compareAndSwapLong |
356 |
< |
(this, stateOffset, s, onAdvance(phase, u)? |
357 |
< |
next | TERMINATION_PHASE : next); |
358 |
< |
releaseWaiters(phase); |
359 |
< |
} |
360 |
< |
else { |
361 |
< |
par.doArrive(u == 0? |
362 |
< |
ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL); |
363 |
< |
if ((int)(par.state >>> PHASE_SHIFT) != nextPhase || |
364 |
< |
((int)(state >>> PHASE_SHIFT) != nextPhase && |
365 |
< |
!UNSAFE.compareAndSwapLong(this, stateOffset, |
366 |
< |
s, next))) |
367 |
< |
reconcileState(); |
368 |
< |
} |
345 |
> |
int counts = (int)s; |
346 |
> |
int unarrived = counts & UNARRIVED_MASK; |
347 |
> |
if (counts == EMPTY || unarrived == 0) { |
348 |
> |
if (reconcileState() == s) |
349 |
> |
throw new IllegalStateException(badArrive(s)); |
350 |
> |
} |
351 |
> |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) { |
352 |
> |
if (unarrived == 1) { |
353 |
> |
long n = s & PARTIES_MASK; // unshifted parties field |
354 |
> |
int u = ((int)n) >>> PARTIES_SHIFT; |
355 |
> |
Phaser par = parent; |
356 |
> |
if (par != null) { |
357 |
> |
par.doArrive(u == 0); |
358 |
> |
reconcileState(); |
359 |
> |
} |
360 |
> |
else { |
361 |
> |
n |= (((long)((phase+1) & MAX_PHASE)) << PHASE_SHIFT); |
362 |
> |
if (onAdvance(phase, u)) |
363 |
> |
n |= TERMINATION_BIT; |
364 |
> |
else if (u == 0) |
365 |
> |
n |= EMPTY; // reset to unregistered |
366 |
> |
else |
367 |
> |
n |= (long)u; // reset unarr to parties |
368 |
> |
// assert state == s || isTerminated(); |
369 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, n); |
370 |
> |
releaseWaiters(phase); |
371 |
|
} |
333 |
– |
break; |
372 |
|
} |
373 |
+ |
break; |
374 |
|
} |
336 |
– |
else if (state == s && reconcileState() == s) // recheck |
337 |
– |
throw new IllegalStateException(badArrive()); |
375 |
|
} |
376 |
|
return phase; |
377 |
|
} |
378 |
|
|
379 |
|
/** |
343 |
– |
* Returns message string for bounds exceptions on arrival. |
344 |
– |
* Declared out of-line from doArrive to reduce string op bulk. |
345 |
– |
*/ |
346 |
– |
private String badArrive() { |
347 |
– |
return ("Attempted arrival of unregistered party for " + |
348 |
– |
this.toString()); |
349 |
– |
} |
350 |
– |
|
351 |
– |
/** |
380 |
|
* Implementation of register, bulkRegister |
381 |
|
* |
382 |
< |
* @param registrations number to add to both parties and unarrived fields |
382 |
> |
* @param registrations number to add to both parties and |
383 |
> |
* unarrived fields. Must be greater than zero. |
384 |
|
*/ |
385 |
|
private int doRegister(int registrations) { |
386 |
< |
long adj = (long)registrations; // adjustment to state |
387 |
< |
adj |= adj << PARTIES_SHIFT; |
386 |
> |
// adjustment to state |
387 |
> |
long adj = ((long)registrations << PARTIES_SHIFT) | registrations; |
388 |
|
Phaser par = parent; |
360 |
– |
long s; |
389 |
|
int phase; |
390 |
< |
while ((phase = (int)((s = (par == null? state : reconcileState())) |
391 |
< |
>>> PHASE_SHIFT)) >= 0) { |
392 |
< |
int parties = ((int)(s & PARTIES_MASK)) >>> PARTIES_SHIFT; |
393 |
< |
if (parties != 0 && (s & UNARRIVED_MASK) == 0) |
394 |
< |
internalAwaitAdvance(phase, null); // wait for onAdvance |
395 |
< |
else if (parties + registrations > MAX_COUNT) |
396 |
< |
throw new IllegalStateException(badRegister()); |
397 |
< |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj)) |
390 |
> |
for (;;) { |
391 |
> |
long s = state; |
392 |
> |
int counts = (int)s; |
393 |
> |
int parties = counts >>> PARTIES_SHIFT; |
394 |
> |
int unarrived = counts & UNARRIVED_MASK; |
395 |
> |
if (registrations > MAX_PARTIES - parties) |
396 |
> |
throw new IllegalStateException(badRegister(s)); |
397 |
> |
else if ((phase = (int)(s >>> PHASE_SHIFT)) < 0) |
398 |
|
break; |
399 |
+ |
else if (counts != EMPTY) { // not 1st registration |
400 |
+ |
if (par == null || reconcileState() == s) { |
401 |
+ |
if (unarrived == 0) // wait out advance |
402 |
+ |
root.internalAwaitAdvance(phase, null); |
403 |
+ |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, |
404 |
+ |
s, s + adj)) |
405 |
+ |
break; |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
else if (par == null) { // 1st root registration |
409 |
+ |
long next = (((long) phase) << PHASE_SHIFT) | adj; |
410 |
+ |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
411 |
+ |
break; |
412 |
+ |
} |
413 |
+ |
else { |
414 |
+ |
synchronized (this) { // 1st sub registration |
415 |
+ |
if (state == s) { // recheck under lock |
416 |
+ |
par.doRegister(1); |
417 |
+ |
do { // force current phase |
418 |
+ |
phase = (int)(root.state >>> PHASE_SHIFT); |
419 |
+ |
// assert phase < 0 || (int)state == EMPTY; |
420 |
+ |
} while (!UNSAFE.compareAndSwapLong |
421 |
+ |
(this, stateOffset, state, |
422 |
+ |
(((long) phase) << PHASE_SHIFT) | adj)); |
423 |
+ |
break; |
424 |
+ |
} |
425 |
+ |
} |
426 |
+ |
} |
427 |
|
} |
428 |
|
return phase; |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* Returns message string for bounds exceptions on registration |
377 |
< |
*/ |
378 |
< |
private String badRegister() { |
379 |
< |
return ("Attempt to register more than " + MAX_COUNT + " parties for "+ |
380 |
< |
this.toString()); |
381 |
< |
} |
382 |
< |
|
383 |
< |
/** |
384 |
< |
* Recursively resolves lagged phase propagation from root if |
385 |
< |
* necessary. |
432 |
> |
* Resolves lagged phase propagation from root if necessary. |
433 |
|
*/ |
434 |
|
private long reconcileState() { |
388 |
– |
Phaser par = parent; |
389 |
– |
if (par == null) |
390 |
– |
return state; |
435 |
|
Phaser rt = root; |
436 |
< |
long s; |
437 |
< |
int phase, rPhase; |
438 |
< |
while ((phase = (int)((s = state) >>> PHASE_SHIFT)) >= 0 && |
439 |
< |
(rPhase = (int)(rt.state >>> PHASE_SHIFT)) != phase) { |
440 |
< |
if (rPhase < 0 || (s & UNARRIVED_MASK) == 0) { |
441 |
< |
long ps = par.parent == null? par.state : par.reconcileState(); |
442 |
< |
int pPhase = (int)(ps >>> PHASE_SHIFT); |
443 |
< |
if (pPhase < 0 || pPhase == ((phase + 1) & MAX_PHASE)) { |
444 |
< |
if (state != s) |
445 |
< |
continue; |
446 |
< |
long p = s & PARTIES_MASK; |
447 |
< |
long next = ((((long) pPhase) << PHASE_SHIFT) | |
448 |
< |
(p >>> PARTIES_SHIFT) | p); |
449 |
< |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
406 |
< |
return next; |
436 |
> |
long s = state; |
437 |
> |
if (rt != this) { |
438 |
> |
int phase; |
439 |
> |
while ((phase = (int)(rt.state >>> PHASE_SHIFT)) != |
440 |
> |
(int)(s >>> PHASE_SHIFT)) { |
441 |
> |
// assert phase < 0 || unarrivedOf(s) == 0 |
442 |
> |
long t; // to reread s |
443 |
> |
long p = s & PARTIES_MASK; // unshifted parties field |
444 |
> |
long n = (((long) phase) << PHASE_SHIFT) | p; |
445 |
> |
if (phase >= 0) { |
446 |
> |
if (p == 0L) |
447 |
> |
n |= EMPTY; // reset to empty |
448 |
> |
else |
449 |
> |
n |= p >>> PARTIES_SHIFT; // set unarr to parties |
450 |
|
} |
451 |
+ |
if ((t = state) == s && |
452 |
+ |
UNSAFE.compareAndSwapLong(this, stateOffset, s, s = n)) |
453 |
+ |
break; |
454 |
+ |
s = t; |
455 |
|
} |
409 |
– |
if (state == s) |
410 |
– |
releaseWaiters(phase); // help release others |
456 |
|
} |
457 |
|
return s; |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* Creates a new phaser without any initially registered parties, |
462 |
< |
* initial phase number 0, and no parent. Any thread using this |
461 |
> |
* Creates a new phaser with no initially registered parties, no |
462 |
> |
* parent, and initial phase number 0. Any thread using this |
463 |
|
* phaser will need to first register for it. |
464 |
|
*/ |
465 |
|
public Phaser() { |
468 |
|
|
469 |
|
/** |
470 |
|
* Creates a new phaser with the given number of registered |
471 |
< |
* unarrived parties, initial phase number 0, and no parent. |
471 |
> |
* unarrived parties, no parent, and initial phase number 0. |
472 |
|
* |
473 |
< |
* @param parties the number of parties required to trip barrier |
473 |
> |
* @param parties the number of parties required to advance to the |
474 |
> |
* next phase |
475 |
|
* @throws IllegalArgumentException if parties less than zero |
476 |
|
* or greater than the maximum number of parties supported |
477 |
|
*/ |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
< |
* Creates a new phaser with the given parent, without any |
438 |
< |
* initially registered parties. If parent is non-null this phaser |
439 |
< |
* is registered with the parent and its initial phase number is |
440 |
< |
* the same as that of parent phaser. |
483 |
> |
* Equivalent to {@link #Phaser(Phaser, int) Phaser(parent, 0)}. |
484 |
|
* |
485 |
|
* @param parent the parent phaser |
486 |
|
*/ |
490 |
|
|
491 |
|
/** |
492 |
|
* Creates a new phaser with the given parent and number of |
493 |
< |
* registered unarrived parties. If parent is non-null, this phaser |
494 |
< |
* is registered with the parent and its initial phase number is |
495 |
< |
* the same as that of parent phaser. |
493 |
> |
* registered unarrived parties. Registration and deregistration |
494 |
> |
* of this child phaser with its parent are managed automatically. |
495 |
> |
* If the given parent is non-null, whenever this child phaser has |
496 |
> |
* any registered parties (as established in this constructor, |
497 |
> |
* {@link #register}, or {@link #bulkRegister}), this child phaser |
498 |
> |
* is registered with its parent. Whenever the number of |
499 |
> |
* registered parties becomes zero as the result of an invocation |
500 |
> |
* of {@link #arriveAndDeregister}, this child phaser is |
501 |
> |
* deregistered from its parent. |
502 |
|
* |
503 |
|
* @param parent the parent phaser |
504 |
< |
* @param parties the number of parties required to trip barrier |
504 |
> |
* @param parties the number of parties required to advance to the |
505 |
> |
* next phase |
506 |
|
* @throws IllegalArgumentException if parties less than zero |
507 |
|
* or greater than the maximum number of parties supported |
508 |
|
*/ |
509 |
|
public Phaser(Phaser parent, int parties) { |
510 |
< |
if (parties < 0 || parties > MAX_COUNT) |
510 |
> |
if (parties >>> PARTIES_SHIFT != 0) |
511 |
|
throw new IllegalArgumentException("Illegal number of parties"); |
512 |
< |
int phase; |
512 |
> |
int phase = 0; |
513 |
|
this.parent = parent; |
514 |
|
if (parent != null) { |
515 |
|
Phaser r = parent.root; |
516 |
|
this.root = r; |
517 |
|
this.evenQ = r.evenQ; |
518 |
|
this.oddQ = r.oddQ; |
519 |
< |
phase = parent.register(); |
519 |
> |
if (parties != 0) |
520 |
> |
phase = parent.doRegister(1); |
521 |
|
} |
522 |
|
else { |
523 |
|
this.root = this; |
524 |
|
this.evenQ = new AtomicReference<QNode>(); |
525 |
|
this.oddQ = new AtomicReference<QNode>(); |
475 |
– |
phase = 0; |
526 |
|
} |
527 |
< |
long p = (long)parties; |
528 |
< |
this.state = (((long) phase) << PHASE_SHIFT) | p | (p << PARTIES_SHIFT); |
527 |
> |
this.state = (parties == 0) ? ((long) EMPTY) : |
528 |
> |
((((long) phase) << PHASE_SHIFT) | |
529 |
> |
(((long) parties) << PARTIES_SHIFT) | |
530 |
> |
((long) parties)); |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* Adds a new unarrived party to this phaser. |
535 |
< |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
536 |
< |
* this method may wait until its completion before registering. |
534 |
> |
* Adds a new unarrived party to this phaser. If an ongoing |
535 |
> |
* invocation of {@link #onAdvance} is in progress, this method |
536 |
> |
* may await its completion before returning. If this phaser has |
537 |
> |
* a parent, and this phaser previously had no registered parties, |
538 |
> |
* this phaser is also registered with its parent. |
539 |
|
* |
540 |
|
* @return the arrival phase number to which this registration applied |
541 |
|
* @throws IllegalStateException if attempting to register more |
548 |
|
/** |
549 |
|
* Adds the given number of new unarrived parties to this phaser. |
550 |
|
* If an ongoing invocation of {@link #onAdvance} is in progress, |
551 |
< |
* this method may wait until its completion before registering. |
551 |
> |
* this method may await its completion before returning. If this |
552 |
> |
* phaser has a parent, and the given number of parties is |
553 |
> |
* greater than zero, and this phaser previously had no registered |
554 |
> |
* parties, this phaser is also registered with its parent. |
555 |
|
* |
556 |
< |
* @param parties the number of additional parties required to trip barrier |
556 |
> |
* @param parties the number of additional parties required to |
557 |
> |
* advance to the next phase |
558 |
|
* @return the arrival phase number to which this registration applied |
559 |
|
* @throws IllegalStateException if attempting to register more |
560 |
|
* than the maximum supported number of parties |
563 |
|
public int bulkRegister(int parties) { |
564 |
|
if (parties < 0) |
565 |
|
throw new IllegalArgumentException(); |
508 |
– |
if (parties > MAX_COUNT) |
509 |
– |
throw new IllegalStateException(badRegister()); |
566 |
|
if (parties == 0) |
567 |
|
return getPhase(); |
568 |
|
return doRegister(parties); |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
< |
* Arrives at the barrier, but does not wait for others. (You can |
573 |
< |
* in turn wait for others via {@link #awaitAdvance}). It is an |
574 |
< |
* unenforced usage error for an unregistered party to invoke this |
575 |
< |
* method. |
572 |
> |
* Arrives at this phaser, without waiting for others to arrive. |
573 |
> |
* |
574 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
575 |
> |
* method. However, this error may result in an {@code |
576 |
> |
* IllegalStateException} only upon some subsequent operation on |
577 |
> |
* this phaser, if ever. |
578 |
|
* |
579 |
|
* @return the arrival phase number, or a negative value if terminated |
580 |
|
* @throws IllegalStateException if not terminated and the number |
581 |
|
* of unarrived parties would become negative |
582 |
|
*/ |
583 |
|
public int arrive() { |
584 |
< |
return doArrive(ONE_ARRIVAL); |
584 |
> |
return doArrive(false); |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
< |
* Arrives at the barrier and deregisters from it without waiting |
589 |
< |
* for others. Deregistration reduces the number of parties |
590 |
< |
* required to trip the barrier in future phases. If this phaser |
588 |
> |
* Arrives at this phaser and deregisters from it without waiting |
589 |
> |
* for others to arrive. Deregistration reduces the number of |
590 |
> |
* parties required to advance in future phases. If this phaser |
591 |
|
* has a parent, and deregistration causes this phaser to have |
592 |
< |
* zero parties, this phaser also arrives at and is deregistered |
593 |
< |
* from its parent. It is an unenforced usage error for an |
594 |
< |
* unregistered party to invoke this method. |
592 |
> |
* zero parties, this phaser is also deregistered from its parent. |
593 |
> |
* |
594 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
595 |
> |
* method. However, this error may result in an {@code |
596 |
> |
* IllegalStateException} only upon some subsequent operation on |
597 |
> |
* this phaser, if ever. |
598 |
|
* |
599 |
|
* @return the arrival phase number, or a negative value if terminated |
600 |
|
* @throws IllegalStateException if not terminated and the number |
601 |
|
* of registered or unarrived parties would become negative |
602 |
|
*/ |
603 |
|
public int arriveAndDeregister() { |
604 |
< |
return doArrive(ONE_ARRIVAL|ONE_PARTY); |
604 |
> |
return doArrive(true); |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* Arrives at the barrier and awaits others. Equivalent in effect |
608 |
> |
* Arrives at this phaser and awaits others. Equivalent in effect |
609 |
|
* to {@code awaitAdvance(arrive())}. If you need to await with |
610 |
|
* interruption or timeout, you can arrange this with an analogous |
611 |
|
* construction using one of the other forms of the {@code |
612 |
|
* awaitAdvance} method. If instead you need to deregister upon |
613 |
< |
* arrival, use {@link #arriveAndDeregister}. It is an unenforced |
614 |
< |
* usage error for an unregistered party to invoke this method. |
613 |
> |
* arrival, use {@code awaitAdvance(arriveAndDeregister())}. |
614 |
> |
* |
615 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
616 |
> |
* method. However, this error may result in an {@code |
617 |
> |
* IllegalStateException} only upon some subsequent operation on |
618 |
> |
* this phaser, if ever. |
619 |
|
* |
620 |
|
* @return the arrival phase number, or a negative number if terminated |
621 |
|
* @throws IllegalStateException if not terminated and the number |
622 |
|
* of unarrived parties would become negative |
623 |
|
*/ |
624 |
|
public int arriveAndAwaitAdvance() { |
625 |
< |
return awaitAdvance(arrive()); |
625 |
> |
return awaitAdvance(doArrive(false)); |
626 |
|
} |
627 |
|
|
628 |
|
/** |
629 |
< |
* Awaits the phase of the barrier to advance from the given phase |
630 |
< |
* value, returning immediately if the current phase of the |
631 |
< |
* barrier is not equal to the given phase value or this barrier |
567 |
< |
* is terminated. |
629 |
> |
* Awaits the phase of this phaser to advance from the given phase |
630 |
> |
* value, returning immediately if the current phase is not equal |
631 |
> |
* to the given phase value or this phaser is terminated. |
632 |
|
* |
633 |
|
* @param phase an arrival phase number, or negative value if |
634 |
|
* terminated; this argument is normally the value returned by a |
635 |
< |
* previous call to {@code arrive} or its variants |
635 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
636 |
|
* @return the next arrival phase number, or a negative value |
637 |
|
* if terminated or argument is negative |
638 |
|
*/ |
639 |
|
public int awaitAdvance(int phase) { |
640 |
+ |
Phaser rt; |
641 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
642 |
|
if (phase < 0) |
643 |
|
return phase; |
644 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
645 |
< |
if (p != phase) |
646 |
< |
return p; |
647 |
< |
return internalAwaitAdvance(phase, null); |
644 |
> |
if (p == phase) { |
645 |
> |
if ((p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) |
646 |
> |
return rt.internalAwaitAdvance(phase, null); |
647 |
> |
reconcileState(); |
648 |
> |
} |
649 |
> |
return p; |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* Awaits the phase of the barrier to advance from the given phase |
653 |
> |
* Awaits the phase of this phaser to advance from the given phase |
654 |
|
* value, throwing {@code InterruptedException} if interrupted |
655 |
< |
* while waiting, or returning immediately if the current phase of |
656 |
< |
* the barrier is not equal to the given phase value or this |
657 |
< |
* barrier is terminated. |
655 |
> |
* while waiting, or returning immediately if the current phase is |
656 |
> |
* not equal to the given phase value or this phaser is |
657 |
> |
* terminated. |
658 |
|
* |
659 |
|
* @param phase an arrival phase number, or negative value if |
660 |
|
* terminated; this argument is normally the value returned by a |
661 |
< |
* previous call to {@code arrive} or its variants |
661 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
662 |
|
* @return the next arrival phase number, or a negative value |
663 |
|
* if terminated or argument is negative |
664 |
|
* @throws InterruptedException if thread interrupted while waiting |
665 |
|
*/ |
666 |
|
public int awaitAdvanceInterruptibly(int phase) |
667 |
|
throws InterruptedException { |
668 |
+ |
Phaser rt; |
669 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
670 |
|
if (phase < 0) |
671 |
|
return phase; |
672 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
673 |
< |
if (p != phase) |
674 |
< |
return p; |
675 |
< |
QNode node = new QNode(this, phase, true, false, 0L); |
676 |
< |
p = internalAwaitAdvance(phase, node); |
677 |
< |
if (node.wasInterrupted) |
678 |
< |
throw new InterruptedException(); |
679 |
< |
else |
680 |
< |
return p; |
672 |
> |
if (p == phase) { |
673 |
> |
if ((p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
674 |
> |
QNode node = new QNode(this, phase, true, false, 0L); |
675 |
> |
p = rt.internalAwaitAdvance(phase, node); |
676 |
> |
if (node.wasInterrupted) |
677 |
> |
throw new InterruptedException(); |
678 |
> |
} |
679 |
> |
else |
680 |
> |
reconcileState(); |
681 |
> |
} |
682 |
> |
return p; |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* Awaits the phase of the barrier to advance from the given phase |
686 |
> |
* Awaits the phase of this phaser to advance from the given phase |
687 |
|
* value or the given timeout to elapse, throwing {@code |
688 |
|
* InterruptedException} if interrupted while waiting, or |
689 |
< |
* returning immediately if the current phase of the barrier is |
690 |
< |
* not equal to the given phase value or this barrier is |
619 |
< |
* terminated. |
689 |
> |
* returning immediately if the current phase is not equal to the |
690 |
> |
* given phase value or this phaser is terminated. |
691 |
|
* |
692 |
|
* @param phase an arrival phase number, or negative value if |
693 |
|
* terminated; this argument is normally the value returned by a |
694 |
< |
* previous call to {@code arrive} or its variants |
694 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
695 |
|
* @param timeout how long to wait before giving up, in units of |
696 |
|
* {@code unit} |
697 |
|
* @param unit a {@code TimeUnit} determining how to interpret the |
705 |
|
long timeout, TimeUnit unit) |
706 |
|
throws InterruptedException, TimeoutException { |
707 |
|
long nanos = unit.toNanos(timeout); |
708 |
+ |
Phaser rt; |
709 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
710 |
|
if (phase < 0) |
711 |
|
return phase; |
712 |
< |
int p = (int)((parent==null? state : reconcileState()) >>> PHASE_SHIFT); |
713 |
< |
if (p != phase) |
714 |
< |
return p; |
715 |
< |
QNode node = new QNode(this, phase, true, true, nanos); |
716 |
< |
p = internalAwaitAdvance(phase, node); |
717 |
< |
if (node.wasInterrupted) |
718 |
< |
throw new InterruptedException(); |
719 |
< |
else if (p == phase) |
720 |
< |
throw new TimeoutException(); |
721 |
< |
else |
722 |
< |
return p; |
712 |
> |
if (p == phase) { |
713 |
> |
if ((p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
714 |
> |
QNode node = new QNode(this, phase, true, true, nanos); |
715 |
> |
p = rt.internalAwaitAdvance(phase, node); |
716 |
> |
if (node.wasInterrupted) |
717 |
> |
throw new InterruptedException(); |
718 |
> |
else if (p == phase) |
719 |
> |
throw new TimeoutException(); |
720 |
> |
} |
721 |
> |
else |
722 |
> |
reconcileState(); |
723 |
> |
} |
724 |
> |
return p; |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* Forces this barrier to enter termination state. Counts of |
729 |
< |
* arrived and registered parties are unaffected. If this phaser |
730 |
< |
* has a parent, it too is terminated. This method may be useful |
731 |
< |
* for coordinating recovery after one or more tasks encounter |
728 |
> |
* Forces this phaser to enter termination state. Counts of |
729 |
> |
* registered parties are unaffected. If this phaser is a member |
730 |
> |
* of a tiered set of phasers, then all of the phasers in the set |
731 |
> |
* are terminated. If this phaser is already terminated, this |
732 |
> |
* method has no effect. This method may be useful for |
733 |
> |
* coordinating recovery after one or more tasks encounter |
734 |
|
* unexpected exceptions. |
735 |
|
*/ |
736 |
|
public void forceTermination() { |
737 |
< |
Phaser r = root; // force at root then reconcile |
737 |
> |
// Only need to change root state |
738 |
> |
final Phaser root = this.root; |
739 |
|
long s; |
740 |
< |
while ((s = r.state) >= 0) |
741 |
< |
UNSAFE.compareAndSwapLong(r, stateOffset, s, s | TERMINATION_PHASE); |
742 |
< |
reconcileState(); |
743 |
< |
releaseWaiters(0); // signal all threads |
744 |
< |
releaseWaiters(1); |
740 |
> |
while ((s = root.state) >= 0) { |
741 |
> |
long next = (s & ~(long)(MAX_PARTIES)) | TERMINATION_BIT; |
742 |
> |
if (UNSAFE.compareAndSwapLong(root, stateOffset, s, next)) { |
743 |
> |
releaseWaiters(0); // signal all threads |
744 |
> |
releaseWaiters(1); |
745 |
> |
return; |
746 |
> |
} |
747 |
> |
} |
748 |
|
} |
749 |
|
|
750 |
|
/** |
751 |
|
* Returns the current phase number. The maximum phase number is |
752 |
|
* {@code Integer.MAX_VALUE}, after which it restarts at |
753 |
< |
* zero. Upon termination, the phase number is negative. |
753 |
> |
* zero. Upon termination, the phase number is negative, |
754 |
> |
* in which case the prevailing phase prior to termination |
755 |
> |
* may be obtained via {@code getPhase() + Integer.MIN_VALUE}. |
756 |
|
* |
757 |
|
* @return the phase number, or a negative value if terminated |
758 |
|
*/ |
759 |
|
public final int getPhase() { |
760 |
< |
return (int)((parent == null? state : reconcileState()) >>> PHASE_SHIFT); |
760 |
> |
return (int)(root.state >>> PHASE_SHIFT); |
761 |
|
} |
762 |
|
|
763 |
|
/** |
764 |
< |
* Returns the number of parties registered at this barrier. |
764 |
> |
* Returns the number of parties registered at this phaser. |
765 |
|
* |
766 |
|
* @return the number of parties |
767 |
|
*/ |
768 |
|
public int getRegisteredParties() { |
769 |
< |
return partiesOf(parent == null? state : reconcileState()); |
769 |
> |
return partiesOf(state); |
770 |
|
} |
771 |
|
|
772 |
|
/** |
773 |
|
* Returns the number of registered parties that have arrived at |
774 |
< |
* the current phase of this barrier. |
774 |
> |
* the current phase of this phaser. |
775 |
|
* |
776 |
|
* @return the number of arrived parties |
777 |
|
*/ |
778 |
|
public int getArrivedParties() { |
779 |
< |
return arrivedOf(parent == null? state : reconcileState()); |
779 |
> |
return arrivedOf(reconcileState()); |
780 |
|
} |
781 |
|
|
782 |
|
/** |
783 |
|
* Returns the number of registered parties that have not yet |
784 |
< |
* arrived at the current phase of this barrier. |
784 |
> |
* arrived at the current phase of this phaser. |
785 |
|
* |
786 |
|
* @return the number of unarrived parties |
787 |
|
*/ |
788 |
|
public int getUnarrivedParties() { |
789 |
< |
return unarrivedOf(parent == null? state : reconcileState()); |
789 |
> |
return unarrivedOf(reconcileState()); |
790 |
|
} |
791 |
|
|
792 |
|
/** |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
< |
* Returns {@code true} if this barrier has been terminated. |
812 |
> |
* Returns {@code true} if this phaser has been terminated. |
813 |
|
* |
814 |
< |
* @return {@code true} if this barrier has been terminated |
814 |
> |
* @return {@code true} if this phaser has been terminated |
815 |
|
*/ |
816 |
|
public boolean isTerminated() { |
817 |
< |
return (parent == null? state : reconcileState()) < 0; |
817 |
> |
return root.state < 0L; |
818 |
|
} |
819 |
|
|
820 |
|
/** |
821 |
|
* Overridable method to perform an action upon impending phase |
822 |
|
* advance, and to control termination. This method is invoked |
823 |
< |
* upon arrival of the party tripping the barrier (when all other |
823 |
> |
* upon arrival of the party advancing this phaser (when all other |
824 |
|
* waiting parties are dormant). If this method returns {@code |
825 |
< |
* true}, then, rather than advance the phase number, this barrier |
826 |
< |
* will be set to a final termination state, and subsequent calls |
827 |
< |
* to {@link #isTerminated} will return true. Any (unchecked) |
828 |
< |
* Exception or Error thrown by an invocation of this method is |
829 |
< |
* propagated to the party attempting to trip the barrier, in |
830 |
< |
* which case no advance occurs. |
825 |
> |
* true}, this phaser will be set to a final termination state |
826 |
> |
* upon advance, and subsequent calls to {@link #isTerminated} |
827 |
> |
* will return true. Any (unchecked) Exception or Error thrown by |
828 |
> |
* an invocation of this method is propagated to the party |
829 |
> |
* attempting to advance this phaser, in which case no advance |
830 |
> |
* occurs. |
831 |
|
* |
832 |
|
* <p>The arguments to this method provide the state of the phaser |
833 |
|
* prevailing for the current transition. The effects of invoking |
834 |
< |
* arrival, registration, and waiting methods on this Phaser from |
834 |
> |
* arrival, registration, and waiting methods on this phaser from |
835 |
|
* within {@code onAdvance} are unspecified and should not be |
836 |
|
* relied on. |
837 |
|
* |
838 |
< |
* <p>If this Phaser is a member of a tiered set of Phasers, then |
839 |
< |
* {@code onAdvance} is invoked only for its root Phaser on each |
838 |
> |
* <p>If this phaser is a member of a tiered set of phasers, then |
839 |
> |
* {@code onAdvance} is invoked only for its root phaser on each |
840 |
|
* advance. |
841 |
|
* |
842 |
< |
* <p>The default version returns {@code true} when the number of |
843 |
< |
* registered parties is zero. Normally, overrides that arrange |
844 |
< |
* termination for other reasons should also preserve this |
845 |
< |
* property. |
842 |
> |
* <p>To support the most common use cases, the default |
843 |
> |
* implementation of this method returns {@code true} when the |
844 |
> |
* number of registered parties has become zero as the result of a |
845 |
> |
* party invoking {@code arriveAndDeregister}. You can disable |
846 |
> |
* this behavior, thus enabling continuation upon future |
847 |
> |
* registrations, by overriding this method to always return |
848 |
> |
* {@code false}: |
849 |
> |
* |
850 |
> |
* <pre> {@code |
851 |
> |
* Phaser phaser = new Phaser() { |
852 |
> |
* protected boolean onAdvance(int phase, int parties) { return false; } |
853 |
> |
* }}</pre> |
854 |
|
* |
855 |
< |
* @param phase the phase number on entering the barrier |
855 |
> |
* @param phase the current phase number on entry to this method, |
856 |
> |
* before this phaser is advanced |
857 |
|
* @param registeredParties the current number of registered parties |
858 |
< |
* @return {@code true} if this barrier should terminate |
858 |
> |
* @return {@code true} if this phaser should terminate |
859 |
|
*/ |
860 |
|
protected boolean onAdvance(int phase, int registeredParties) { |
861 |
< |
return registeredParties <= 0; |
861 |
> |
return registeredParties == 0; |
862 |
|
} |
863 |
|
|
864 |
|
/** |
868 |
|
* followed by the number of registered parties, and {@code |
869 |
|
* "arrived = "} followed by the number of arrived parties. |
870 |
|
* |
871 |
< |
* @return a string identifying this barrier, as well as its state |
871 |
> |
* @return a string identifying this phaser, as well as its state |
872 |
|
*/ |
873 |
|
public String toString() { |
874 |
< |
long s = reconcileState(); |
874 |
> |
return stateToString(reconcileState()); |
875 |
> |
} |
876 |
> |
|
877 |
> |
/** |
878 |
> |
* Implementation of toString and string-based error messages |
879 |
> |
*/ |
880 |
> |
private String stateToString(long s) { |
881 |
|
return super.toString() + |
882 |
|
"[phase = " + phaseOf(s) + |
883 |
|
" parties = " + partiesOf(s) + |
884 |
|
" arrived = " + arrivedOf(s) + "]"; |
885 |
|
} |
886 |
|
|
887 |
+ |
// Waiting mechanics |
888 |
+ |
|
889 |
|
/** |
890 |
< |
* Removes and signals threads from queue for phase |
890 |
> |
* Removes and signals threads from queue for phase. |
891 |
|
*/ |
892 |
|
private void releaseWaiters(int phase) { |
893 |
< |
AtomicReference<QNode> head = queueFor(phase); |
894 |
< |
QNode q; |
895 |
< |
int p; |
893 |
> |
QNode q; // first element of queue |
894 |
> |
int p; // its phase |
895 |
> |
Thread t; // its thread |
896 |
> |
// assert phase != phaseOf(root.state); |
897 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
898 |
|
while ((q = head.get()) != null && |
899 |
< |
((p = q.phase) == phase || |
900 |
< |
(int)(root.state >>> PHASE_SHIFT) != p)) { |
901 |
< |
if (head.compareAndSet(q, q.next)) |
902 |
< |
q.signal(); |
899 |
> |
q.phase != (int)(root.state >>> PHASE_SHIFT)) { |
900 |
> |
if (head.compareAndSet(q, q.next) && |
901 |
> |
(t = q.thread) != null) { |
902 |
> |
q.thread = null; |
903 |
> |
LockSupport.unpark(t); |
904 |
> |
} |
905 |
|
} |
906 |
|
} |
907 |
|
|
804 |
– |
/** |
805 |
– |
* Tries to enqueue given node in the appropriate wait queue. |
806 |
– |
* |
807 |
– |
* @return true if successful |
808 |
– |
*/ |
809 |
– |
private boolean tryEnqueue(int phase, QNode node) { |
810 |
– |
releaseWaiters(phase-1); // ensure old queue clean |
811 |
– |
AtomicReference<QNode> head = queueFor(phase); |
812 |
– |
QNode q = head.get(); |
813 |
– |
return ((q == null || q.phase == phase) && |
814 |
– |
(int)(root.state >>> PHASE_SHIFT) == phase && |
815 |
– |
head.compareAndSet(node.next = q, node)); |
816 |
– |
} |
817 |
– |
|
908 |
|
/** The number of CPUs, for spin control */ |
909 |
|
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
910 |
|
|
916 |
|
* avoid it when threads regularly arrive: When a thread in |
917 |
|
* internalAwaitAdvance notices another arrival before blocking, |
918 |
|
* and there appear to be enough CPUs available, it spins |
919 |
< |
* SPINS_PER_ARRIVAL more times before continuing to try to |
920 |
< |
* block. The value trades off good-citizenship vs big unnecessary |
831 |
< |
* slowdowns. |
919 |
> |
* SPINS_PER_ARRIVAL more times before blocking. The value trades |
920 |
> |
* off good-citizenship vs big unnecessary slowdowns. |
921 |
|
*/ |
922 |
< |
static final int SPINS_PER_ARRIVAL = NCPU < 2? 1 : 1 << 8; |
922 |
> |
static final int SPINS_PER_ARRIVAL = (NCPU < 2) ? 1 : 1 << 8; |
923 |
|
|
924 |
|
/** |
925 |
|
* Possibly blocks and waits for phase to advance unless aborted. |
926 |
+ |
* Call only from root node. |
927 |
|
* |
928 |
|
* @param phase current phase |
929 |
< |
* @param node if nonnull, the wait node to track interrupt and timeout; |
929 |
> |
* @param node if non-null, the wait node to track interrupt and timeout; |
930 |
|
* if null, denotes noninterruptible wait |
931 |
|
* @return current phase |
932 |
|
*/ |
933 |
|
private int internalAwaitAdvance(int phase, QNode node) { |
934 |
< |
Phaser current = this; // to eventually wait at root if tiered |
935 |
< |
Phaser par = parent; |
936 |
< |
boolean queued = false; |
934 |
> |
releaseWaiters(phase-1); // ensure old queue clean |
935 |
> |
boolean queued = false; // true when node is enqueued |
936 |
> |
int lastUnarrived = 0; // to increase spins upon change |
937 |
|
int spins = SPINS_PER_ARRIVAL; |
848 |
– |
int lastUnarrived = -1; // to increase spins upon change |
938 |
|
long s; |
939 |
|
int p; |
940 |
< |
while ((p = (int)((s = current.state) >>> PHASE_SHIFT)) == phase) { |
941 |
< |
int unarrived = (int)(s & UNARRIVED_MASK); |
942 |
< |
if (unarrived != lastUnarrived) { |
943 |
< |
if ((lastUnarrived = unarrived) < NCPU) |
940 |
> |
while ((p = (int)((s = state) >>> PHASE_SHIFT)) == phase) { |
941 |
> |
if (node == null) { // spinning in noninterruptible mode |
942 |
> |
int unarrived = (int)s & UNARRIVED_MASK; |
943 |
> |
if (unarrived != lastUnarrived && |
944 |
> |
(lastUnarrived = unarrived) < NCPU) |
945 |
|
spins += SPINS_PER_ARRIVAL; |
946 |
+ |
boolean interrupted = Thread.interrupted(); |
947 |
+ |
if (interrupted || --spins < 0) { // need node to record intr |
948 |
+ |
node = new QNode(this, phase, false, false, 0L); |
949 |
+ |
node.wasInterrupted = interrupted; |
950 |
+ |
} |
951 |
|
} |
952 |
< |
else if (unarrived == 0 && par != null) { |
858 |
< |
current = par; // if all arrived, use parent |
859 |
< |
par = par.parent; |
860 |
< |
} |
861 |
< |
else if (spins > 0) |
862 |
< |
--spins; |
863 |
< |
else if (node == null) |
864 |
< |
node = new QNode(this, phase, false, false, 0L); |
865 |
< |
else if (node.isReleasable()) |
952 |
> |
else if (node.isReleasable()) // done or aborted |
953 |
|
break; |
954 |
< |
else if (!queued) |
955 |
< |
queued = tryEnqueue(phase, node); |
954 |
> |
else if (!queued) { // push onto queue |
955 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
956 |
> |
QNode q = node.next = head.get(); |
957 |
> |
if ((q == null || q.phase == phase) && |
958 |
> |
(int)(state >>> PHASE_SHIFT) == phase) // avoid stale enq |
959 |
> |
queued = head.compareAndSet(q, node); |
960 |
> |
} |
961 |
|
else { |
962 |
|
try { |
963 |
|
ForkJoinPool.managedBlock(node); |
966 |
|
} |
967 |
|
} |
968 |
|
} |
969 |
+ |
|
970 |
|
if (node != null) { |
971 |
|
if (node.thread != null) |
972 |
< |
node.thread = null; |
973 |
< |
if (!node.interruptible && node.wasInterrupted) |
972 |
> |
node.thread = null; // avoid need for unpark() |
973 |
> |
if (node.wasInterrupted && !node.interruptible) |
974 |
|
Thread.currentThread().interrupt(); |
975 |
+ |
if (p == phase && (p = (int)(state >>> PHASE_SHIFT)) == phase) |
976 |
+ |
return p; // recheck abort |
977 |
|
} |
978 |
< |
if (p == phase && parent != null) |
884 |
< |
p = (int)(reconcileState() >>> PHASE_SHIFT); |
885 |
< |
if (p != phase) |
886 |
< |
releaseWaiters(phase); |
978 |
> |
releaseWaiters(phase); |
979 |
|
return p; |
980 |
|
} |
981 |
|
|
1000 |
|
this.interruptible = interruptible; |
1001 |
|
this.nanos = nanos; |
1002 |
|
this.timed = timed; |
1003 |
< |
this.lastTime = timed? System.nanoTime() : 0L; |
1003 |
> |
this.lastTime = timed ? System.nanoTime() : 0L; |
1004 |
|
thread = Thread.currentThread(); |
1005 |
|
} |
1006 |
|
|
1007 |
|
public boolean isReleasable() { |
1008 |
< |
Thread t = thread; |
1009 |
< |
if (t != null) { |
1010 |
< |
if (phaser.getPhase() != phase) |
919 |
< |
t = null; |
920 |
< |
else { |
921 |
< |
if (Thread.interrupted()) |
922 |
< |
wasInterrupted = true; |
923 |
< |
if (interruptible && wasInterrupted) |
924 |
< |
t = null; |
925 |
< |
else if (timed) { |
926 |
< |
if (nanos > 0) { |
927 |
< |
long now = System.nanoTime(); |
928 |
< |
nanos -= now - lastTime; |
929 |
< |
lastTime = now; |
930 |
< |
} |
931 |
< |
if (nanos <= 0) |
932 |
< |
t = null; |
933 |
< |
} |
934 |
< |
} |
935 |
< |
if (t != null) |
936 |
< |
return false; |
1008 |
> |
if (thread == null) |
1009 |
> |
return true; |
1010 |
> |
if (phaser.getPhase() != phase) { |
1011 |
|
thread = null; |
1012 |
+ |
return true; |
1013 |
|
} |
1014 |
< |
return true; |
1014 |
> |
if (Thread.interrupted()) |
1015 |
> |
wasInterrupted = true; |
1016 |
> |
if (wasInterrupted && interruptible) { |
1017 |
> |
thread = null; |
1018 |
> |
return true; |
1019 |
> |
} |
1020 |
> |
if (timed) { |
1021 |
> |
if (nanos > 0L) { |
1022 |
> |
long now = System.nanoTime(); |
1023 |
> |
nanos -= now - lastTime; |
1024 |
> |
lastTime = now; |
1025 |
> |
} |
1026 |
> |
if (nanos <= 0L) { |
1027 |
> |
thread = null; |
1028 |
> |
return true; |
1029 |
> |
} |
1030 |
> |
} |
1031 |
> |
return false; |
1032 |
|
} |
1033 |
|
|
1034 |
|
public boolean block() { |
1040 |
|
LockSupport.parkNanos(this, nanos); |
1041 |
|
return isReleasable(); |
1042 |
|
} |
951 |
– |
|
952 |
– |
void signal() { |
953 |
– |
Thread t = thread; |
954 |
– |
if (t != null) { |
955 |
– |
thread = null; |
956 |
– |
LockSupport.unpark(t); |
957 |
– |
} |
958 |
– |
} |
1043 |
|
} |
1044 |
|
|
1045 |
|
// Unsafe mechanics |