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 |
|
/** |
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 |
|
/** |
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; |
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; |
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; |