6 |
|
|
7 |
|
package jsr166y; |
8 |
|
|
9 |
< |
import java.util.concurrent.*; |
10 |
< |
|
9 |
> |
import java.util.concurrent.TimeUnit; |
10 |
> |
import java.util.concurrent.TimeoutException; |
11 |
|
import java.util.concurrent.atomic.AtomicReference; |
12 |
|
import java.util.concurrent.locks.LockSupport; |
13 |
|
|
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 |
102 |
|
* #getRegisteredParties} parties in total, of which {@link |
103 |
|
* #getArrivedParties} have arrived at the current phase ({@link |
104 |
|
* #getPhase}). When the remaining ({@link #getUnarrivedParties}) |
105 |
< |
* parties arrive, the phase advances; thus, this value is always |
106 |
< |
* greater than zero if there are any registered parties. The values |
107 |
< |
* returned by these methods may reflect transient states and so are |
108 |
< |
* not in general useful for synchronization control. Method {@link |
109 |
< |
* #toString} returns snapshots of these state queries in a form |
108 |
< |
* convenient for informal monitoring. |
105 |
> |
* parties arrive, the phase advances. The values returned by these |
106 |
> |
* methods may reflect transient states and so are not in general |
107 |
> |
* useful for synchronization control. Method {@link #toString} |
108 |
> |
* returns snapshots of these state queries in a form convenient for |
109 |
> |
* informal monitoring. |
110 |
|
* |
111 |
|
* <p><b>Sample usages:</b> |
112 |
|
* |
113 |
|
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
114 |
< |
* to control a one-shot action serving a variable number of |
115 |
< |
* parties. The typical idiom is for the method setting this up to |
116 |
< |
* first register, then start the actions, then deregister, as in: |
114 |
> |
* to control a one-shot action serving a variable number of parties. |
115 |
> |
* The typical idiom is for the method setting this up to first |
116 |
> |
* register, then start the actions, then deregister, as in: |
117 |
|
* |
118 |
|
* <pre> {@code |
119 |
|
* void runTasks(List<Runnable> tasks) { |
144 |
|
* } |
145 |
|
* }; |
146 |
|
* phaser.register(); |
147 |
< |
* for (Runnable task : tasks) { |
147 |
> |
* for (final Runnable task : tasks) { |
148 |
|
* phaser.register(); |
149 |
|
* new Thread() { |
150 |
|
* public void run() { |
151 |
|
* do { |
152 |
|
* task.run(); |
153 |
|
* phaser.arriveAndAwaitAdvance(); |
154 |
< |
* } while(!phaser.isTerminated(); |
154 |
> |
* } while (!phaser.isTerminated()); |
155 |
|
* } |
156 |
|
* }.start(); |
157 |
|
* } |
160 |
|
* |
161 |
|
* If the main task must later await termination, it |
162 |
|
* may re-register and then execute a similar loop: |
163 |
< |
* <pre> {@code |
163 |
> |
* <pre> {@code |
164 |
|
* // ... |
165 |
|
* phaser.register(); |
166 |
|
* while (!phaser.isTerminated()) |
167 |
< |
* phaser.arriveAndAwaitAdvance(); |
167 |
< |
* }</pre> |
167 |
> |
* phaser.arriveAndAwaitAdvance();}</pre> |
168 |
|
* |
169 |
< |
* Related constructions may be used to await particular phase numbers |
169 |
> |
* <p>Related constructions may be used to await particular phase numbers |
170 |
|
* in contexts where you are sure that the phase will never wrap around |
171 |
|
* {@code Integer.MAX_VALUE}. For example: |
172 |
|
* |
173 |
< |
* <pre> {@code |
174 |
< |
* void awaitPhase(Phaser phaser, int phase) { |
175 |
< |
* int p = phaser.register(); // assumes caller not already registered |
176 |
< |
* while (p < phase) { |
177 |
< |
* if (phaser.isTerminated()) |
178 |
< |
* // ... deal with unexpected termination |
179 |
< |
* else |
180 |
< |
* p = phaser.arriveAndAwaitAdvance(); |
181 |
< |
* } |
182 |
< |
* phaser.arriveAndDeregister(); |
173 |
> |
* <pre> {@code |
174 |
> |
* void awaitPhase(Phaser phaser, int phase) { |
175 |
> |
* int p = phaser.register(); // assumes caller not already registered |
176 |
> |
* while (p < phase) { |
177 |
> |
* if (phaser.isTerminated()) |
178 |
> |
* // ... deal with unexpected termination |
179 |
> |
* else |
180 |
> |
* p = phaser.arriveAndAwaitAdvance(); |
181 |
|
* } |
182 |
< |
* }</pre> |
182 |
> |
* phaser.arriveAndDeregister(); |
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 for upon construction: |
188 |
> |
* Task class with a constructor accepting a {@code Phaser} that |
189 |
> |
* it registers with upon construction: |
190 |
> |
* |
191 |
|
* <pre> {@code |
192 |
< |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
193 |
< |
* int step = (hi - lo) / TASKS_PER_PHASER; |
194 |
< |
* if (step > 1) { |
195 |
< |
* int i = lo; |
196 |
< |
* while (i < hi) { |
197 |
< |
* int r = Math.min(i + step, hi); |
198 |
< |
* build(actions, i, r, new Phaser(b)); |
199 |
< |
* i = r; |
192 |
> |
* void build(Task[] actions, int lo, int hi, Phaser ph) { |
193 |
> |
* if (hi - lo > TASKS_PER_PHASER) { |
194 |
> |
* for (int i = lo; i < hi; i += TASKS_PER_PHASER) { |
195 |
> |
* int j = Math.min(i + TASKS_PER_PHASER, hi); |
196 |
> |
* build(actions, i, j, new Phaser(ph)); |
197 |
|
* } |
198 |
|
* } else { |
199 |
|
* for (int i = lo; i < hi; ++i) |
200 |
< |
* actions[i] = new Task(b); |
201 |
< |
* // assumes new Task(b) performs b.register() |
200 |
> |
* actions[i] = new Task(ph); |
201 |
> |
* // assumes new Task(ph) performs ph.register() |
202 |
|
* } |
203 |
|
* } |
204 |
|
* // .. initially called, for n tasks via |
205 |
|
* build(new Task[n], 0, n, new Phaser());}</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 |
|
* |
215 |
– |
* </pre> |
216 |
– |
* |
212 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
213 |
|
* maximum number of parties to 65535. Attempts to register additional |
214 |
|
* parties result in {@code IllegalStateException}. However, you can and |
226 |
|
*/ |
227 |
|
|
228 |
|
/** |
229 |
< |
* Barrier state representation. Conceptually, a barrier contains |
235 |
< |
* four values: |
229 |
> |
* Primary state representation, holding four fields: |
230 |
|
* |
231 |
< |
* * parties -- the number of parties to wait (16 bits) |
232 |
< |
* * unarrived -- the number of parties yet to hit barrier (16 bits) |
233 |
< |
* * phase -- the generation of the barrier (31 bits) |
234 |
< |
* * terminated -- set if barrier is terminated (1 bit) |
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. |
247 |
– |
* |
248 |
– |
* Note: there are some cheats in arrive() that rely on unarrived |
249 |
– |
* count being lowest 16 bits. |
241 |
|
*/ |
242 |
|
private volatile long state; |
243 |
|
|
244 |
< |
private static final int ushortBits = 16; |
245 |
< |
private static final int ushortMask = 0xffff; |
246 |
< |
private static final int phaseMask = 0x7fffffff; |
244 |
> |
private static final int MAX_PARTIES = 0xffff; |
245 |
> |
private static final int MAX_PHASE = 0x7fffffff; |
246 |
> |
private static final int PARTIES_SHIFT = 16; |
247 |
> |
private static final int PHASE_SHIFT = 32; |
248 |
> |
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
249 |
> |
private static final long PARTIES_MASK = 0xffff0000L; // to mask longs |
250 |
> |
private static final long ONE_ARRIVAL = 1L; |
251 |
> |
private static final long ONE_PARTY = 1L << PARTIES_SHIFT; |
252 |
> |
private static final long TERMINATION_BIT = 1L << 63; |
253 |
> |
|
254 |
> |
// The following unpacking methods are usually manually inlined |
255 |
|
|
256 |
|
private static int unarrivedOf(long s) { |
257 |
< |
return (int) (s & ushortMask); |
257 |
> |
return (int)s & UNARRIVED_MASK; |
258 |
|
} |
259 |
|
|
260 |
|
private static int partiesOf(long s) { |
261 |
< |
return ((int) s) >>> 16; |
261 |
> |
return (int)s >>> PARTIES_SHIFT; |
262 |
|
} |
263 |
|
|
264 |
|
private static int phaseOf(long s) { |
265 |
< |
return (int) (s >>> 32); |
265 |
> |
return (int) (s >>> PHASE_SHIFT); |
266 |
|
} |
267 |
|
|
268 |
|
private static int arrivedOf(long s) { |
269 |
|
return partiesOf(s) - unarrivedOf(s); |
270 |
|
} |
271 |
|
|
273 |
– |
private static long stateFor(int phase, int parties, int unarrived) { |
274 |
– |
return ((((long) phase) << 32) | (((long) parties) << 16) | |
275 |
– |
(long) unarrived); |
276 |
– |
} |
277 |
– |
|
278 |
– |
private static long trippedStateFor(int phase, int parties) { |
279 |
– |
long lp = (long) parties; |
280 |
– |
return (((long) phase) << 32) | (lp << 16) | lp; |
281 |
– |
} |
282 |
– |
|
283 |
– |
/** |
284 |
– |
* Returns message string for bad bounds exceptions. |
285 |
– |
*/ |
286 |
– |
private static String badBounds(int parties, int unarrived) { |
287 |
– |
return ("Attempt to set " + unarrived + |
288 |
– |
" unarrived of " + parties + " parties"); |
289 |
– |
} |
290 |
– |
|
272 |
|
/** |
273 |
|
* The parent of this phaser, or null if none |
274 |
|
*/ |
280 |
|
*/ |
281 |
|
private final Phaser root; |
282 |
|
|
302 |
– |
// Wait queues |
303 |
– |
|
283 |
|
/** |
284 |
|
* Heads of Treiber stacks for waiting threads. To eliminate |
285 |
< |
* contention while releasing some threads while adding others, we |
285 |
> |
* contention when releasing some threads while adding others, we |
286 |
|
* use two of them, alternating across even and odd phases. |
287 |
+ |
* Subphasers share queues with root to speed up releases. |
288 |
|
*/ |
289 |
< |
private final AtomicReference<QNode> evenQ = new AtomicReference<QNode>(); |
290 |
< |
private final AtomicReference<QNode> oddQ = new AtomicReference<QNode>(); |
289 |
> |
private final AtomicReference<QNode> evenQ; |
290 |
> |
private final AtomicReference<QNode> oddQ; |
291 |
|
|
292 |
|
private AtomicReference<QNode> queueFor(int phase) { |
293 |
|
return ((phase & 1) == 0) ? evenQ : oddQ; |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
< |
* Returns current state, first resolving lagged propagation from |
318 |
< |
* root if necessary. |
297 |
> |
* Returns message string for bounds exceptions on arrival. |
298 |
|
*/ |
299 |
< |
private long getReconciledState() { |
300 |
< |
return (parent == null) ? state : reconcileState(); |
299 |
> |
private String badArrive(long s) { |
300 |
> |
return "Attempted arrival of unregistered party for " + |
301 |
> |
stateToString(s); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
< |
* Recursively resolves state. |
305 |
> |
* Returns message string for bounds exceptions on registration. |
306 |
|
*/ |
307 |
< |
private long reconcileState() { |
308 |
< |
Phaser p = parent; |
309 |
< |
long s = state; |
310 |
< |
if (p != null) { |
311 |
< |
while (unarrivedOf(s) == 0 && phaseOf(s) != phaseOf(root.state)) { |
312 |
< |
long parentState = p.getReconciledState(); |
313 |
< |
int parentPhase = phaseOf(parentState); |
314 |
< |
int phase = phaseOf(s = state); |
315 |
< |
if (phase != parentPhase) { |
316 |
< |
long next = trippedStateFor(parentPhase, partiesOf(s)); |
317 |
< |
if (casState(s, next)) { |
307 |
> |
private String badRegister(long s) { |
308 |
> |
return "Attempt to register more than " + |
309 |
> |
MAX_PARTIES + " parties for " + stateToString(s); |
310 |
> |
} |
311 |
> |
|
312 |
> |
/** |
313 |
> |
* Main implementation for methods arrive and arriveAndDeregister. |
314 |
> |
* Manually tuned to speed up and minimize race windows for the |
315 |
> |
* common case of just decrementing unarrived field. |
316 |
> |
* |
317 |
> |
* @param adj - adjustment to apply to state -- either |
318 |
> |
* ONE_ARRIVAL (for arrive) or |
319 |
> |
* ONE_ARRIVAL|ONE_PARTY (for arriveAndDeregister) |
320 |
> |
*/ |
321 |
> |
private int doArrive(long adj) { |
322 |
> |
for (;;) { |
323 |
> |
long s = state; |
324 |
> |
int unarrived = (int)s & UNARRIVED_MASK; |
325 |
> |
int phase = (int)(s >>> PHASE_SHIFT); |
326 |
> |
if (phase < 0) |
327 |
> |
return phase; |
328 |
> |
else if (unarrived == 0) { |
329 |
> |
if (reconcileState() == s) // recheck |
330 |
> |
throw new IllegalStateException(badArrive(s)); |
331 |
> |
} |
332 |
> |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adj)) { |
333 |
> |
if (unarrived == 1) { |
334 |
> |
long p = s & PARTIES_MASK; // unshifted parties field |
335 |
> |
long lu = p >>> PARTIES_SHIFT; |
336 |
> |
int u = (int)lu; |
337 |
> |
int nextPhase = (phase + 1) & MAX_PHASE; |
338 |
> |
long next = ((long)nextPhase << PHASE_SHIFT) | p | lu; |
339 |
> |
final Phaser parent = this.parent; |
340 |
> |
if (parent == null) { |
341 |
> |
if (onAdvance(phase, u)) |
342 |
> |
next |= TERMINATION_BIT; |
343 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, next); |
344 |
|
releaseWaiters(phase); |
345 |
< |
s = next; |
345 |
> |
} |
346 |
> |
else { |
347 |
> |
parent.doArrive((u == 0) ? |
348 |
> |
ONE_ARRIVAL|ONE_PARTY : ONE_ARRIVAL); |
349 |
> |
if ((int)(parent.state >>> PHASE_SHIFT) != nextPhase) |
350 |
> |
reconcileState(); |
351 |
> |
else if (state == s) |
352 |
> |
UNSAFE.compareAndSwapLong(this, stateOffset, s, |
353 |
> |
next); |
354 |
|
} |
355 |
|
} |
356 |
+ |
return phase; |
357 |
+ |
} |
358 |
+ |
} |
359 |
+ |
} |
360 |
+ |
|
361 |
+ |
/** |
362 |
+ |
* Implementation of register, bulkRegister |
363 |
+ |
* |
364 |
+ |
* @param registrations number to add to both parties and |
365 |
+ |
* unarrived fields. Must be greater than zero. |
366 |
+ |
*/ |
367 |
+ |
private int doRegister(int registrations) { |
368 |
+ |
// adjustment to state |
369 |
+ |
long adj = ((long)registrations << PARTIES_SHIFT) | registrations; |
370 |
+ |
final Phaser parent = this.parent; |
371 |
+ |
for (;;) { |
372 |
+ |
long s = (parent == null) ? state : reconcileState(); |
373 |
+ |
int parties = (int)s >>> PARTIES_SHIFT; |
374 |
+ |
int phase = (int)(s >>> PHASE_SHIFT); |
375 |
+ |
if (phase < 0) |
376 |
+ |
return phase; |
377 |
+ |
else if (registrations > MAX_PARTIES - parties) |
378 |
+ |
throw new IllegalStateException(badRegister(s)); |
379 |
+ |
else if ((parties == 0 && parent == null) || // first reg of root |
380 |
+ |
((int)s & UNARRIVED_MASK) != 0) { // not advancing |
381 |
+ |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s + adj)) |
382 |
+ |
return phase; |
383 |
+ |
} |
384 |
+ |
else if (parties != 0) // wait for onAdvance |
385 |
+ |
root.internalAwaitAdvance(phase, null); |
386 |
+ |
else { // 1st registration of child |
387 |
+ |
synchronized (this) { // register parent first |
388 |
+ |
if (reconcileState() == s) { // recheck under lock |
389 |
+ |
parent.doRegister(1); // OK if throws IllegalState |
390 |
+ |
for (;;) { // simpler form of outer loop |
391 |
+ |
s = reconcileState(); |
392 |
+ |
phase = (int)(s >>> PHASE_SHIFT); |
393 |
+ |
if (phase < 0 || |
394 |
+ |
UNSAFE.compareAndSwapLong(this, stateOffset, |
395 |
+ |
s, s + adj)) |
396 |
+ |
return phase; |
397 |
+ |
} |
398 |
+ |
} |
399 |
+ |
} |
400 |
+ |
} |
401 |
+ |
} |
402 |
+ |
} |
403 |
+ |
|
404 |
+ |
/** |
405 |
+ |
* Recursively resolves lagged phase propagation from root if necessary. |
406 |
+ |
*/ |
407 |
+ |
private long reconcileState() { |
408 |
+ |
Phaser par = parent; |
409 |
+ |
long s = state; |
410 |
+ |
if (par != null) { |
411 |
+ |
Phaser rt = root; |
412 |
+ |
int phase, rPhase; |
413 |
+ |
while ((phase = (int)(s >>> PHASE_SHIFT)) >= 0 && |
414 |
+ |
(rPhase = (int)(rt.state >>> PHASE_SHIFT)) != phase) { |
415 |
+ |
if (par != rt && (int)(par.state >>> PHASE_SHIFT) != rPhase) |
416 |
+ |
par.reconcileState(); |
417 |
+ |
else if (rPhase < 0 || ((int)s & UNARRIVED_MASK) == 0) { |
418 |
+ |
long u = s & PARTIES_MASK; // reset unarrived to parties |
419 |
+ |
long next = ((((long) rPhase) << PHASE_SHIFT) | u | |
420 |
+ |
(u >>> PARTIES_SHIFT)); |
421 |
+ |
UNSAFE.compareAndSwapLong(this, stateOffset, s, next); |
422 |
+ |
} |
423 |
+ |
s = state; |
424 |
|
} |
425 |
|
} |
426 |
|
return s; |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
< |
* Creates a new phaser without any initially registered parties, |
431 |
< |
* initial phase number 0, and no parent. Any thread using this |
430 |
> |
* Creates a new phaser with no initially registered parties, no |
431 |
> |
* parent, and initial phase number 0. Any thread using this |
432 |
|
* phaser will need to first register for it. |
433 |
|
*/ |
434 |
|
public Phaser() { |
435 |
< |
this(null); |
435 |
> |
this(null, 0); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
< |
* Creates a new phaser with the given numbers of registered |
440 |
< |
* unarrived parties, initial phase number 0, and no parent. |
439 |
> |
* Creates a new phaser with the given number of registered |
440 |
> |
* unarrived parties, no parent, and initial phase number 0. |
441 |
|
* |
442 |
< |
* @param parties the number of parties required to trip barrier |
442 |
> |
* @param parties the number of parties required to advance to the |
443 |
> |
* next phase |
444 |
|
* @throws IllegalArgumentException if parties less than zero |
445 |
|
* or greater than the maximum number of parties supported |
446 |
|
*/ |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
< |
* Creates a new phaser with the given parent, without any |
370 |
< |
* initially registered parties. If parent is non-null this phaser |
371 |
< |
* is registered with the parent and its initial phase number is |
372 |
< |
* the same as that of parent phaser. |
452 |
> |
* Equivalent to {@link #Phaser(Phaser, int) Phaser(parent, 0)}. |
453 |
|
* |
454 |
|
* @param parent the parent phaser |
455 |
|
*/ |
456 |
|
public Phaser(Phaser parent) { |
457 |
< |
int phase = 0; |
378 |
< |
this.parent = parent; |
379 |
< |
if (parent != null) { |
380 |
< |
this.root = parent.root; |
381 |
< |
phase = parent.register(); |
382 |
< |
} |
383 |
< |
else |
384 |
< |
this.root = this; |
385 |
< |
this.state = trippedStateFor(phase, 0); |
457 |
> |
this(parent, 0); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* Creates a new phaser with the given parent and numbers of |
462 |
< |
* registered unarrived parties. If parent is non-null, this phaser |
463 |
< |
* is registered with the parent and its initial phase number is |
464 |
< |
* the same as that of parent phaser. |
461 |
> |
* Creates a new phaser with the given parent and number of |
462 |
> |
* registered unarrived parties. Registration and deregistration |
463 |
> |
* of this child phaser with its parent are managed automatically. |
464 |
> |
* If the given parent is non-null, whenever this child phaser has |
465 |
> |
* any registered parties (as established in this constructor, |
466 |
> |
* {@link #register}, or {@link #bulkRegister}), this child phaser |
467 |
> |
* is registered with its parent. Whenever the number of |
468 |
> |
* registered parties becomes zero as the result of an invocation |
469 |
> |
* of {@link #arriveAndDeregister}, this child phaser is |
470 |
> |
* deregistered from its parent. |
471 |
|
* |
472 |
|
* @param parent the parent phaser |
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 |
|
*/ |
478 |
|
public Phaser(Phaser parent, int parties) { |
479 |
< |
if (parties < 0 || parties > ushortMask) |
479 |
> |
if (parties >>> PARTIES_SHIFT != 0) |
480 |
|
throw new IllegalArgumentException("Illegal number of parties"); |
481 |
< |
int phase = 0; |
481 |
> |
long s = ((long) parties) | (((long) parties) << PARTIES_SHIFT); |
482 |
|
this.parent = parent; |
483 |
|
if (parent != null) { |
484 |
< |
this.root = parent.root; |
485 |
< |
phase = parent.register(); |
484 |
> |
Phaser r = parent.root; |
485 |
> |
this.root = r; |
486 |
> |
this.evenQ = r.evenQ; |
487 |
> |
this.oddQ = r.oddQ; |
488 |
> |
if (parties != 0) |
489 |
> |
s |= ((long)(parent.doRegister(1))) << PHASE_SHIFT; |
490 |
|
} |
491 |
< |
else |
491 |
> |
else { |
492 |
|
this.root = this; |
493 |
< |
this.state = trippedStateFor(phase, parties); |
493 |
> |
this.evenQ = new AtomicReference<QNode>(); |
494 |
> |
this.oddQ = new AtomicReference<QNode>(); |
495 |
> |
} |
496 |
> |
this.state = s; |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
< |
* Adds a new unarrived party to this phaser. |
500 |
> |
* Adds a new unarrived party to this phaser. If an ongoing |
501 |
> |
* invocation of {@link #onAdvance} is in progress, this method |
502 |
> |
* may await its completion before returning. If this phaser has |
503 |
> |
* a parent, and this phaser previously had no registered parties, |
504 |
> |
* this phaser is also registered with its parent. |
505 |
|
* |
506 |
|
* @return the arrival phase number to which this registration applied |
507 |
|
* @throws IllegalStateException if attempting to register more |
513 |
|
|
514 |
|
/** |
515 |
|
* Adds the given number of new unarrived parties to this phaser. |
516 |
+ |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
517 |
+ |
* this method may await its completion before returning. If this |
518 |
+ |
* phaser has a parent, and the given number of parities is |
519 |
+ |
* greater than zero, and this phaser previously had no registered |
520 |
+ |
* parties, this phaser is also registered with its parent. |
521 |
|
* |
522 |
< |
* @param parties the number of parties required to trip barrier |
522 |
> |
* @param parties the number of additional parties required to |
523 |
> |
* advance to the next phase |
524 |
|
* @return the arrival phase number to which this registration applied |
525 |
|
* @throws IllegalStateException if attempting to register more |
526 |
|
* than the maximum supported number of parties |
527 |
+ |
* @throws IllegalArgumentException if {@code parties < 0} |
528 |
|
*/ |
529 |
|
public int bulkRegister(int parties) { |
530 |
|
if (parties < 0) |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
< |
* Shared code for register, bulkRegister |
539 |
< |
*/ |
540 |
< |
private int doRegister(int registrations) { |
541 |
< |
int phase; |
542 |
< |
for (;;) { |
543 |
< |
long s = getReconciledState(); |
447 |
< |
phase = phaseOf(s); |
448 |
< |
int unarrived = unarrivedOf(s) + registrations; |
449 |
< |
int parties = partiesOf(s) + registrations; |
450 |
< |
if (phase < 0) |
451 |
< |
break; |
452 |
< |
if (parties > ushortMask || unarrived > ushortMask) |
453 |
< |
throw new IllegalStateException(badBounds(parties, unarrived)); |
454 |
< |
if (phase == phaseOf(root.state) && |
455 |
< |
casState(s, stateFor(phase, parties, unarrived))) |
456 |
< |
break; |
457 |
< |
} |
458 |
< |
return phase; |
459 |
< |
} |
460 |
< |
|
461 |
< |
/** |
462 |
< |
* Arrives at the barrier, but does not wait for others. (You can |
463 |
< |
* in turn wait for others via {@link #awaitAdvance}). It is an |
464 |
< |
* unenforced usage error for an unregistered party to invoke this |
465 |
< |
* method. |
538 |
> |
* Arrives at this phaser, without waiting for others to arrive. |
539 |
> |
* |
540 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
541 |
> |
* method. However, this error may result in an {@code |
542 |
> |
* IllegalStateException} only upon some subsequent operation on |
543 |
> |
* this phaser, if ever. |
544 |
|
* |
545 |
|
* @return the arrival phase number, or a negative value if terminated |
546 |
|
* @throws IllegalStateException if not terminated and the number |
547 |
|
* of unarrived parties would become negative |
548 |
|
*/ |
549 |
|
public int arrive() { |
550 |
< |
int phase; |
473 |
< |
for (;;) { |
474 |
< |
long s = state; |
475 |
< |
phase = phaseOf(s); |
476 |
< |
if (phase < 0) |
477 |
< |
break; |
478 |
< |
int parties = partiesOf(s); |
479 |
< |
int unarrived = unarrivedOf(s) - 1; |
480 |
< |
if (unarrived > 0) { // Not the last arrival |
481 |
< |
if (casState(s, s - 1)) // s-1 adds one arrival |
482 |
< |
break; |
483 |
< |
} |
484 |
< |
else if (unarrived == 0) { // the last arrival |
485 |
< |
Phaser par = parent; |
486 |
< |
if (par == null) { // directly trip |
487 |
< |
if (casState |
488 |
< |
(s, |
489 |
< |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
490 |
< |
((phase + 1) & phaseMask), parties))) { |
491 |
< |
releaseWaiters(phase); |
492 |
< |
break; |
493 |
< |
} |
494 |
< |
} |
495 |
< |
else { // cascade to parent |
496 |
< |
if (casState(s, s - 1)) { // zeroes unarrived |
497 |
< |
par.arrive(); |
498 |
< |
reconcileState(); |
499 |
< |
break; |
500 |
< |
} |
501 |
< |
} |
502 |
< |
} |
503 |
< |
else if (phase != phaseOf(root.state)) // or if unreconciled |
504 |
< |
reconcileState(); |
505 |
< |
else |
506 |
< |
throw new IllegalStateException(badBounds(parties, unarrived)); |
507 |
< |
} |
508 |
< |
return phase; |
550 |
> |
return doArrive(ONE_ARRIVAL); |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* Arrives at the barrier and deregisters from it without waiting |
555 |
< |
* for others. Deregistration reduces the number of parties |
556 |
< |
* required to trip the barrier in future phases. If this phaser |
554 |
> |
* Arrives at this phaser and deregisters from it without waiting |
555 |
> |
* for others to arrive. Deregistration reduces the number of |
556 |
> |
* parties required to advance in future phases. If this phaser |
557 |
|
* has a parent, and deregistration causes this phaser to have |
558 |
< |
* zero parties, this phaser also arrives at and is deregistered |
559 |
< |
* from its parent. It is an unenforced usage error for an |
560 |
< |
* unregistered party to invoke this method. |
558 |
> |
* zero parties, this phaser is also deregistered from its parent. |
559 |
> |
* |
560 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
561 |
> |
* method. However, this error may result in an {@code |
562 |
> |
* IllegalStateException} only upon some subsequent operation on |
563 |
> |
* this phaser, if ever. |
564 |
|
* |
565 |
|
* @return the arrival phase number, or a negative value if terminated |
566 |
|
* @throws IllegalStateException if not terminated and the number |
567 |
|
* of registered or unarrived parties would become negative |
568 |
|
*/ |
569 |
|
public int arriveAndDeregister() { |
570 |
< |
// similar code to arrive, but too different to merge |
526 |
< |
Phaser par = parent; |
527 |
< |
int phase; |
528 |
< |
for (;;) { |
529 |
< |
long s = state; |
530 |
< |
phase = phaseOf(s); |
531 |
< |
if (phase < 0) |
532 |
< |
break; |
533 |
< |
int parties = partiesOf(s) - 1; |
534 |
< |
int unarrived = unarrivedOf(s) - 1; |
535 |
< |
if (parties >= 0) { |
536 |
< |
if (unarrived > 0 || (unarrived == 0 && par != null)) { |
537 |
< |
if (casState |
538 |
< |
(s, |
539 |
< |
stateFor(phase, parties, unarrived))) { |
540 |
< |
if (unarrived == 0) { |
541 |
< |
par.arriveAndDeregister(); |
542 |
< |
reconcileState(); |
543 |
< |
} |
544 |
< |
break; |
545 |
< |
} |
546 |
< |
continue; |
547 |
< |
} |
548 |
< |
if (unarrived == 0) { |
549 |
< |
if (casState |
550 |
< |
(s, |
551 |
< |
trippedStateFor(onAdvance(phase, parties) ? -1 : |
552 |
< |
((phase + 1) & phaseMask), parties))) { |
553 |
< |
releaseWaiters(phase); |
554 |
< |
break; |
555 |
< |
} |
556 |
< |
continue; |
557 |
< |
} |
558 |
< |
if (par != null && phase != phaseOf(root.state)) { |
559 |
< |
reconcileState(); |
560 |
< |
continue; |
561 |
< |
} |
562 |
< |
} |
563 |
< |
throw new IllegalStateException(badBounds(parties, unarrived)); |
564 |
< |
} |
565 |
< |
return phase; |
570 |
> |
return doArrive(ONE_ARRIVAL|ONE_PARTY); |
571 |
|
} |
572 |
|
|
573 |
|
/** |
574 |
< |
* Arrives at the barrier and awaits others. Equivalent in effect |
574 |
> |
* Arrives at this phaser and awaits others. Equivalent in effect |
575 |
|
* to {@code awaitAdvance(arrive())}. If you need to await with |
576 |
|
* interruption or timeout, you can arrange this with an analogous |
577 |
< |
* construction using one of the other forms of the awaitAdvance |
578 |
< |
* method. If instead you need to deregister upon arrival use |
579 |
< |
* {@code arriveAndDeregister}. It is an unenforced usage error |
580 |
< |
* for an unregistered party to invoke this method. |
577 |
> |
* construction using one of the other forms of the {@code |
578 |
> |
* awaitAdvance} method. If instead you need to deregister upon |
579 |
> |
* arrival, use {@code awaitAdvance(arriveAndDeregister())}. |
580 |
> |
* |
581 |
> |
* <p>It is a usage error for an unregistered party to invoke this |
582 |
> |
* method. However, this error may result in an {@code |
583 |
> |
* IllegalStateException} only upon some subsequent operation on |
584 |
> |
* this phaser, if ever. |
585 |
|
* |
586 |
|
* @return the arrival phase number, or a negative number if terminated |
587 |
|
* @throws IllegalStateException if not terminated and the number |
588 |
|
* of unarrived parties would become negative |
589 |
|
*/ |
590 |
|
public int arriveAndAwaitAdvance() { |
591 |
< |
return awaitAdvance(arrive()); |
591 |
> |
return awaitAdvance(doArrive(ONE_ARRIVAL)); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
< |
* Awaits the phase of the barrier to advance from the given phase |
596 |
< |
* value, returning immediately if the current phase of the |
597 |
< |
* barrier is not equal to the given phase value or this barrier |
589 |
< |
* is terminated. It is an unenforced usage error for an |
590 |
< |
* unregistered party to invoke this method. |
595 |
> |
* Awaits the phase of this phaser to advance from the given phase |
596 |
> |
* value, returning immediately if the current phase is not equal |
597 |
> |
* to the given phase value or this phaser is terminated. |
598 |
|
* |
599 |
|
* @param phase an arrival phase number, or negative value if |
600 |
|
* terminated; this argument is normally the value returned by a |
601 |
< |
* previous call to {@code arrive} or its variants |
601 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
602 |
|
* @return the next arrival phase number, or a negative value |
603 |
|
* if terminated or argument is negative |
604 |
|
*/ |
605 |
|
public int awaitAdvance(int phase) { |
606 |
+ |
Phaser rt; |
607 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
608 |
|
if (phase < 0) |
609 |
|
return phase; |
610 |
< |
long s = getReconciledState(); |
611 |
< |
int p = phaseOf(s); |
612 |
< |
if (p != phase) |
613 |
< |
return p; |
605 |
< |
if (unarrivedOf(s) == 0 && parent != null) |
606 |
< |
parent.awaitAdvance(phase); |
607 |
< |
// Fall here even if parent waited, to reconcile and help release |
608 |
< |
return untimedWait(phase); |
610 |
> |
if (p == phase && |
611 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) |
612 |
> |
return rt.internalAwaitAdvance(phase, null); |
613 |
> |
return p; |
614 |
|
} |
615 |
|
|
616 |
|
/** |
617 |
< |
* Awaits the phase of the barrier to advance from the given phase |
617 |
> |
* Awaits the phase of this phaser to advance from the given phase |
618 |
|
* value, throwing {@code InterruptedException} if interrupted |
619 |
< |
* while waiting, or returning immediately if the current phase of |
620 |
< |
* the barrier is not equal to the given phase value or this |
621 |
< |
* barrier is terminated. It is an unenforced usage error for an |
617 |
< |
* unregistered party to invoke this method. |
619 |
> |
* while waiting, or returning immediately if the current phase is |
620 |
> |
* not equal to the given phase value or this phaser is |
621 |
> |
* terminated. |
622 |
|
* |
623 |
|
* @param phase an arrival phase number, or negative value if |
624 |
|
* terminated; this argument is normally the value returned by a |
625 |
< |
* previous call to {@code arrive} or its variants |
625 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
626 |
|
* @return the next arrival phase number, or a negative value |
627 |
|
* if terminated or argument is negative |
628 |
|
* @throws InterruptedException if thread interrupted while waiting |
629 |
|
*/ |
630 |
|
public int awaitAdvanceInterruptibly(int phase) |
631 |
|
throws InterruptedException { |
632 |
+ |
Phaser rt; |
633 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
634 |
|
if (phase < 0) |
635 |
|
return phase; |
636 |
< |
long s = getReconciledState(); |
637 |
< |
int p = phaseOf(s); |
638 |
< |
if (p != phase) |
639 |
< |
return p; |
640 |
< |
if (unarrivedOf(s) == 0 && parent != null) |
641 |
< |
parent.awaitAdvanceInterruptibly(phase); |
642 |
< |
return interruptibleWait(phase); |
636 |
> |
if (p == phase && |
637 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
638 |
> |
QNode node = new QNode(this, phase, true, false, 0L); |
639 |
> |
p = rt.internalAwaitAdvance(phase, node); |
640 |
> |
if (node.wasInterrupted) |
641 |
> |
throw new InterruptedException(); |
642 |
> |
} |
643 |
> |
return p; |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* Awaits the phase of the barrier to advance from the given phase |
647 |
> |
* Awaits the phase of this phaser to advance from the given phase |
648 |
|
* value or the given timeout to elapse, throwing {@code |
649 |
|
* InterruptedException} if interrupted while waiting, or |
650 |
< |
* returning immediately if the current phase of the barrier is |
651 |
< |
* not equal to the given phase value or this barrier is |
645 |
< |
* terminated. It is an unenforced usage error for an |
646 |
< |
* unregistered party to invoke this method. |
650 |
> |
* returning immediately if the current phase is not equal to the |
651 |
> |
* given phase value or this phaser is terminated. |
652 |
|
* |
653 |
|
* @param phase an arrival phase number, or negative value if |
654 |
|
* terminated; this argument is normally the value returned by a |
655 |
< |
* previous call to {@code arrive} or its variants |
655 |
> |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
656 |
|
* @param timeout how long to wait before giving up, in units of |
657 |
|
* {@code unit} |
658 |
|
* @param unit a {@code TimeUnit} determining how to interpret the |
665 |
|
public int awaitAdvanceInterruptibly(int phase, |
666 |
|
long timeout, TimeUnit unit) |
667 |
|
throws InterruptedException, TimeoutException { |
668 |
+ |
long nanos = unit.toNanos(timeout); |
669 |
+ |
Phaser rt; |
670 |
+ |
int p = (int)(state >>> PHASE_SHIFT); |
671 |
|
if (phase < 0) |
672 |
|
return phase; |
673 |
< |
long s = getReconciledState(); |
674 |
< |
int p = phaseOf(s); |
675 |
< |
if (p != phase) |
676 |
< |
return p; |
677 |
< |
if (unarrivedOf(s) == 0 && parent != null) |
678 |
< |
parent.awaitAdvanceInterruptibly(phase, timeout, unit); |
679 |
< |
return timedWait(phase, unit.toNanos(timeout)); |
673 |
> |
if (p == phase && |
674 |
> |
(p = (int)((rt = root).state >>> PHASE_SHIFT)) == phase) { |
675 |
> |
QNode node = new QNode(this, phase, true, true, nanos); |
676 |
> |
p = rt.internalAwaitAdvance(phase, node); |
677 |
> |
if (node.wasInterrupted) |
678 |
> |
throw new InterruptedException(); |
679 |
> |
else if (p == phase) |
680 |
> |
throw new TimeoutException(); |
681 |
> |
} |
682 |
> |
return p; |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* Forces this barrier to enter termination state. Counts of |
687 |
< |
* arrived and registered parties are unaffected. If this phaser |
688 |
< |
* has a parent, it too is terminated. This method may be useful |
689 |
< |
* for coordinating recovery after one or more tasks encounter |
690 |
< |
* unexpected exceptions. |
686 |
> |
* Forces this phaser to enter termination state. Counts of |
687 |
> |
* arrived and registered parties are unaffected. If this phaser |
688 |
> |
* is a member of a tiered set of phasers, then all of the phasers |
689 |
> |
* in the set are terminated. If this phaser is already |
690 |
> |
* terminated, this method has no effect. This method may be |
691 |
> |
* useful for coordinating recovery after one or more tasks |
692 |
> |
* encounter unexpected exceptions. |
693 |
|
*/ |
694 |
|
public void forceTermination() { |
695 |
< |
for (;;) { |
696 |
< |
long s = getReconciledState(); |
697 |
< |
int phase = phaseOf(s); |
698 |
< |
int parties = partiesOf(s); |
699 |
< |
int unarrived = unarrivedOf(s); |
700 |
< |
if (phase < 0 || |
701 |
< |
casState(s, stateFor(-1, parties, unarrived))) { |
689 |
< |
releaseWaiters(0); |
695 |
> |
// Only need to change root state |
696 |
> |
final Phaser root = this.root; |
697 |
> |
long s; |
698 |
> |
while ((s = root.state) >= 0) { |
699 |
> |
if (UNSAFE.compareAndSwapLong(root, stateOffset, |
700 |
> |
s, s | TERMINATION_BIT)) { |
701 |
> |
releaseWaiters(0); // signal all threads |
702 |
|
releaseWaiters(1); |
691 |
– |
if (parent != null) |
692 |
– |
parent.forceTermination(); |
703 |
|
return; |
704 |
|
} |
705 |
|
} |
708 |
|
/** |
709 |
|
* Returns the current phase number. The maximum phase number is |
710 |
|
* {@code Integer.MAX_VALUE}, after which it restarts at |
711 |
< |
* zero. Upon termination, the phase number is negative. |
711 |
> |
* zero. Upon termination, the phase number is negative, |
712 |
> |
* in which case the prevailing phase prior to termination |
713 |
> |
* may be obtained via {@code getPhase() + Integer.MIN_VALUE}. |
714 |
|
* |
715 |
|
* @return the phase number, or a negative value if terminated |
716 |
|
*/ |
717 |
|
public final int getPhase() { |
718 |
< |
return phaseOf(getReconciledState()); |
718 |
> |
return (int)(root.state >>> PHASE_SHIFT); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
< |
* Returns the number of parties registered at this barrier. |
722 |
> |
* Returns the number of parties registered at this phaser. |
723 |
|
* |
724 |
|
* @return the number of parties |
725 |
|
*/ |
729 |
|
|
730 |
|
/** |
731 |
|
* Returns the number of registered parties that have arrived at |
732 |
< |
* the current phase of this barrier. |
732 |
> |
* the current phase of this phaser. |
733 |
|
* |
734 |
|
* @return the number of arrived parties |
735 |
|
*/ |
736 |
|
public int getArrivedParties() { |
737 |
< |
return arrivedOf(state); |
737 |
> |
long s = state; |
738 |
> |
int u = unarrivedOf(s); // only reconcile if possibly needed |
739 |
> |
return (u != 0 || parent == null) ? |
740 |
> |
partiesOf(s) - u : |
741 |
> |
arrivedOf(reconcileState()); |
742 |
|
} |
743 |
|
|
744 |
|
/** |
745 |
|
* Returns the number of registered parties that have not yet |
746 |
< |
* arrived at the current phase of this barrier. |
746 |
> |
* arrived at the current phase of this phaser. |
747 |
|
* |
748 |
|
* @return the number of unarrived parties |
749 |
|
*/ |
750 |
|
public int getUnarrivedParties() { |
751 |
< |
return unarrivedOf(state); |
751 |
> |
int u = unarrivedOf(state); |
752 |
> |
return (u != 0 || parent == null) ? u : unarrivedOf(reconcileState()); |
753 |
|
} |
754 |
|
|
755 |
|
/** |
772 |
|
} |
773 |
|
|
774 |
|
/** |
775 |
< |
* Returns {@code true} if this barrier has been terminated. |
775 |
> |
* Returns {@code true} if this phaser has been terminated. |
776 |
|
* |
777 |
< |
* @return {@code true} if this barrier has been terminated |
777 |
> |
* @return {@code true} if this phaser has been terminated |
778 |
|
*/ |
779 |
|
public boolean isTerminated() { |
780 |
< |
return getPhase() < 0; |
780 |
> |
return root.state < 0L; |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
< |
* Overridable method to perform an action upon phase advance, and |
785 |
< |
* to control termination. This method is invoked whenever the |
786 |
< |
* barrier is tripped (and thus all other waiting parties are |
787 |
< |
* dormant). If it returns {@code true}, then, rather than advance |
788 |
< |
* the phase number, this barrier will be set to a final |
789 |
< |
* termination state, and subsequent calls to {@link #isTerminated} |
790 |
< |
* will return true. |
791 |
< |
* |
792 |
< |
* <p>The default version returns {@code true} when the number of |
793 |
< |
* registered parties is zero. Normally, overrides that arrange |
794 |
< |
* termination for other reasons should also preserve this |
795 |
< |
* property. |
796 |
< |
* |
797 |
< |
* <p>You may override this method to perform an action with side |
798 |
< |
* effects visible to participating tasks, but it is in general |
799 |
< |
* only sensible to do so in designs where all parties register |
800 |
< |
* before any arrive, and all {@link #awaitAdvance} at each phase. |
801 |
< |
* Otherwise, you cannot ensure lack of interference from other |
802 |
< |
* parties during the invocation of this method. |
784 |
> |
* Overridable method to perform an action upon impending phase |
785 |
> |
* advance, and to control termination. This method is invoked |
786 |
> |
* upon arrival of the party advancing this phaser (when all other |
787 |
> |
* waiting parties are dormant). If this method returns {@code |
788 |
> |
* true}, then, rather than advance the phase number, this phaser |
789 |
> |
* will be set to a final termination state, and subsequent calls |
790 |
> |
* to {@link #isTerminated} will return true. Any (unchecked) |
791 |
> |
* Exception or Error thrown by an invocation of this method is |
792 |
> |
* propagated to the party attempting to advance this phaser, in |
793 |
> |
* which case no advance occurs. |
794 |
> |
* |
795 |
> |
* <p>The arguments to this method provide the state of the phaser |
796 |
> |
* prevailing for the current transition. The effects of invoking |
797 |
> |
* arrival, registration, and waiting methods on this phaser from |
798 |
> |
* within {@code onAdvance} are unspecified and should not be |
799 |
> |
* relied on. |
800 |
> |
* |
801 |
> |
* <p>If this phaser is a member of a tiered set of phasers, then |
802 |
> |
* {@code onAdvance} is invoked only for its root phaser on each |
803 |
> |
* advance. |
804 |
> |
* |
805 |
> |
* <p>To support the most common use cases, the default |
806 |
> |
* implementation of this method returns {@code true} when the |
807 |
> |
* number of registered parties has become zero as the result of a |
808 |
> |
* party invoking {@code arriveAndDeregister}. You can disable |
809 |
> |
* this behavior, thus enabling continuation upon future |
810 |
> |
* registrations, by overriding this method to always return |
811 |
> |
* {@code false}: |
812 |
> |
* |
813 |
> |
* <pre> {@code |
814 |
> |
* Phaser phaser = new Phaser() { |
815 |
> |
* protected boolean onAdvance(int phase, int parties) { return false; } |
816 |
> |
* }}</pre> |
817 |
|
* |
818 |
< |
* @param phase the phase number on entering the barrier |
818 |
> |
* @param phase the current phase number on entry to this method, |
819 |
> |
* before this phaser is advanced |
820 |
|
* @param registeredParties the current number of registered parties |
821 |
< |
* @return {@code true} if this barrier should terminate |
821 |
> |
* @return {@code true} if this phaser should terminate |
822 |
|
*/ |
823 |
|
protected boolean onAdvance(int phase, int registeredParties) { |
824 |
< |
return registeredParties <= 0; |
824 |
> |
return registeredParties == 0; |
825 |
|
} |
826 |
|
|
827 |
|
/** |
831 |
|
* followed by the number of registered parties, and {@code |
832 |
|
* "arrived = "} followed by the number of arrived parties. |
833 |
|
* |
834 |
< |
* @return a string identifying this barrier, as well as its state |
834 |
> |
* @return a string identifying this phaser, as well as its state |
835 |
|
*/ |
836 |
|
public String toString() { |
837 |
< |
long s = getReconciledState(); |
837 |
> |
return stateToString(reconcileState()); |
838 |
> |
} |
839 |
> |
|
840 |
> |
/** |
841 |
> |
* Implementation of toString and string-based error messages |
842 |
> |
*/ |
843 |
> |
private String stateToString(long s) { |
844 |
|
return super.toString() + |
845 |
|
"[phase = " + phaseOf(s) + |
846 |
|
" parties = " + partiesOf(s) + |
847 |
|
" arrived = " + arrivedOf(s) + "]"; |
848 |
|
} |
849 |
|
|
850 |
< |
// methods for waiting |
850 |
> |
// Waiting mechanics |
851 |
|
|
852 |
|
/** |
853 |
< |
* Wait nodes for Treiber stack representing wait queue |
853 |
> |
* Removes and signals threads from queue for phase. |
854 |
|
*/ |
855 |
< |
static final class QNode implements ForkJoinPool.ManagedBlocker { |
856 |
< |
final Phaser phaser; |
857 |
< |
final int phase; |
858 |
< |
final long startTime; |
859 |
< |
final long nanos; |
860 |
< |
final boolean timed; |
861 |
< |
final boolean interruptible; |
862 |
< |
volatile boolean wasInterrupted = false; |
863 |
< |
volatile Thread thread; // nulled to cancel wait |
864 |
< |
QNode next; |
865 |
< |
QNode(Phaser phaser, int phase, boolean interruptible, |
828 |
< |
boolean timed, long startTime, long nanos) { |
829 |
< |
this.phaser = phaser; |
830 |
< |
this.phase = phase; |
831 |
< |
this.timed = timed; |
832 |
< |
this.interruptible = interruptible; |
833 |
< |
this.startTime = startTime; |
834 |
< |
this.nanos = nanos; |
835 |
< |
thread = Thread.currentThread(); |
836 |
< |
} |
837 |
< |
public boolean isReleasable() { |
838 |
< |
return (thread == null || |
839 |
< |
phaser.getPhase() != phase || |
840 |
< |
(interruptible && wasInterrupted) || |
841 |
< |
(timed && (nanos - (System.nanoTime() - startTime)) <= 0)); |
842 |
< |
} |
843 |
< |
public boolean block() { |
844 |
< |
if (Thread.interrupted()) { |
845 |
< |
wasInterrupted = true; |
846 |
< |
if (interruptible) |
847 |
< |
return true; |
848 |
< |
} |
849 |
< |
if (!timed) |
850 |
< |
LockSupport.park(this); |
851 |
< |
else { |
852 |
< |
long waitTime = nanos - (System.nanoTime() - startTime); |
853 |
< |
if (waitTime <= 0) |
854 |
< |
return true; |
855 |
< |
LockSupport.parkNanos(this, waitTime); |
856 |
< |
} |
857 |
< |
return isReleasable(); |
858 |
< |
} |
859 |
< |
void signal() { |
860 |
< |
Thread t = thread; |
861 |
< |
if (t != null) { |
862 |
< |
thread = null; |
855 |
> |
private void releaseWaiters(int phase) { |
856 |
> |
QNode q; // first element of queue |
857 |
> |
int p; // its phase |
858 |
> |
Thread t; // its thread |
859 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
860 |
> |
while ((q = head.get()) != null && |
861 |
> |
((p = q.phase) == phase || |
862 |
> |
(int)(root.state >>> PHASE_SHIFT) != p)) { |
863 |
> |
if (head.compareAndSet(q, q.next) && |
864 |
> |
(t = q.thread) != null) { |
865 |
> |
q.thread = null; |
866 |
|
LockSupport.unpark(t); |
867 |
|
} |
868 |
|
} |
866 |
– |
boolean doWait() { |
867 |
– |
if (thread != null) { |
868 |
– |
try { |
869 |
– |
ForkJoinPool.managedBlock(this, false); |
870 |
– |
} catch (InterruptedException ie) { |
871 |
– |
} |
872 |
– |
} |
873 |
– |
return wasInterrupted; |
874 |
– |
} |
875 |
– |
|
869 |
|
} |
870 |
|
|
871 |
< |
/** |
872 |
< |
* Removes and signals waiting threads from wait queue. |
880 |
< |
*/ |
881 |
< |
private void releaseWaiters(int phase) { |
882 |
< |
AtomicReference<QNode> head = queueFor(phase); |
883 |
< |
QNode q; |
884 |
< |
while ((q = head.get()) != null) { |
885 |
< |
if (head.compareAndSet(q, q.next)) |
886 |
< |
q.signal(); |
887 |
< |
} |
888 |
< |
} |
871 |
> |
/** The number of CPUs, for spin control */ |
872 |
> |
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
873 |
|
|
874 |
|
/** |
875 |
< |
* Tries to enqueue given node in the appropriate wait queue. |
876 |
< |
* |
877 |
< |
* @return true if successful |
875 |
> |
* The number of times to spin before blocking while waiting for |
876 |
> |
* advance, per arrival while waiting. On multiprocessors, fully |
877 |
> |
* blocking and waking up a large number of threads all at once is |
878 |
> |
* usually a very slow process, so we use rechargeable spins to |
879 |
> |
* avoid it when threads regularly arrive: When a thread in |
880 |
> |
* internalAwaitAdvance notices another arrival before blocking, |
881 |
> |
* and there appear to be enough CPUs available, it spins |
882 |
> |
* SPINS_PER_ARRIVAL more times before blocking. The value trades |
883 |
> |
* off good-citizenship vs big unnecessary slowdowns. |
884 |
|
*/ |
885 |
< |
private boolean tryEnqueue(QNode node) { |
896 |
< |
AtomicReference<QNode> head = queueFor(node.phase); |
897 |
< |
return head.compareAndSet(node.next = head.get(), node); |
898 |
< |
} |
885 |
> |
static final int SPINS_PER_ARRIVAL = (NCPU < 2) ? 1 : 1 << 8; |
886 |
|
|
887 |
|
/** |
888 |
< |
* Enqueues node and waits unless aborted or signalled. |
888 |
> |
* Possibly blocks and waits for phase to advance unless aborted. |
889 |
> |
* Call only from root node. |
890 |
|
* |
891 |
+ |
* @param phase current phase |
892 |
+ |
* @param node if non-null, the wait node to track interrupt and timeout; |
893 |
+ |
* if null, denotes noninterruptible wait |
894 |
|
* @return current phase |
895 |
|
*/ |
896 |
< |
private int untimedWait(int phase) { |
897 |
< |
QNode node = null; |
898 |
< |
boolean queued = false; |
899 |
< |
boolean interrupted = false; |
896 |
> |
private int internalAwaitAdvance(int phase, QNode node) { |
897 |
> |
releaseWaiters(phase-1); // ensure old queue clean |
898 |
> |
boolean queued = false; // true when node is enqueued |
899 |
> |
int lastUnarrived = 0; // to increase spins upon change |
900 |
> |
int spins = SPINS_PER_ARRIVAL; |
901 |
> |
long s; |
902 |
|
int p; |
903 |
< |
while ((p = getPhase()) == phase) { |
904 |
< |
if (Thread.interrupted()) |
905 |
< |
interrupted = true; |
906 |
< |
else if (node == null) |
907 |
< |
node = new QNode(this, phase, false, false, 0, 0); |
908 |
< |
else if (!queued) |
909 |
< |
queued = tryEnqueue(node); |
910 |
< |
else |
911 |
< |
interrupted = node.doWait(); |
903 |
> |
while ((p = (int)((s = state) >>> PHASE_SHIFT)) == phase) { |
904 |
> |
if (node == null) { // spinning in noninterruptible mode |
905 |
> |
int unarrived = (int)s & UNARRIVED_MASK; |
906 |
> |
if (unarrived != lastUnarrived && |
907 |
> |
(lastUnarrived = unarrived) < NCPU) |
908 |
> |
spins += SPINS_PER_ARRIVAL; |
909 |
> |
boolean interrupted = Thread.interrupted(); |
910 |
> |
if (interrupted || --spins < 0) { // need node to record intr |
911 |
> |
node = new QNode(this, phase, false, false, 0L); |
912 |
> |
node.wasInterrupted = interrupted; |
913 |
> |
} |
914 |
> |
} |
915 |
> |
else if (node.isReleasable()) // done or aborted |
916 |
> |
break; |
917 |
> |
else if (!queued) { // push onto queue |
918 |
> |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
919 |
> |
QNode q = node.next = head.get(); |
920 |
> |
if ((q == null || q.phase == phase) && |
921 |
> |
(int)(state >>> PHASE_SHIFT) == phase) // avoid stale enq |
922 |
> |
queued = head.compareAndSet(q, node); |
923 |
> |
} |
924 |
> |
else { |
925 |
> |
try { |
926 |
> |
ForkJoinPool.managedBlock(node); |
927 |
> |
} catch (InterruptedException ie) { |
928 |
> |
node.wasInterrupted = true; |
929 |
> |
} |
930 |
> |
} |
931 |
> |
} |
932 |
> |
|
933 |
> |
if (node != null) { |
934 |
> |
if (node.thread != null) |
935 |
> |
node.thread = null; // avoid need for unpark() |
936 |
> |
if (node.wasInterrupted && !node.interruptible) |
937 |
> |
Thread.currentThread().interrupt(); |
938 |
> |
if ((p = (int)(state >>> PHASE_SHIFT)) == phase) |
939 |
> |
return p; // recheck abort |
940 |
|
} |
920 |
– |
if (node != null) |
921 |
– |
node.thread = null; |
941 |
|
releaseWaiters(phase); |
923 |
– |
if (interrupted) |
924 |
– |
Thread.currentThread().interrupt(); |
942 |
|
return p; |
943 |
|
} |
944 |
|
|
945 |
|
/** |
946 |
< |
* Interruptible version |
930 |
< |
* @return current phase |
946 |
> |
* Wait nodes for Treiber stack representing wait queue |
947 |
|
*/ |
948 |
< |
private int interruptibleWait(int phase) throws InterruptedException { |
949 |
< |
QNode node = null; |
950 |
< |
boolean queued = false; |
951 |
< |
boolean interrupted = false; |
952 |
< |
int p; |
953 |
< |
while ((p = getPhase()) == phase && !interrupted) { |
954 |
< |
if (Thread.interrupted()) |
955 |
< |
interrupted = true; |
956 |
< |
else if (node == null) |
957 |
< |
node = new QNode(this, phase, true, false, 0, 0); |
942 |
< |
else if (!queued) |
943 |
< |
queued = tryEnqueue(node); |
944 |
< |
else |
945 |
< |
interrupted = node.doWait(); |
946 |
< |
} |
947 |
< |
if (node != null) |
948 |
< |
node.thread = null; |
949 |
< |
if (p != phase || (p = getPhase()) != phase) |
950 |
< |
releaseWaiters(phase); |
951 |
< |
if (interrupted) |
952 |
< |
throw new InterruptedException(); |
953 |
< |
return p; |
954 |
< |
} |
948 |
> |
static final class QNode implements ForkJoinPool.ManagedBlocker { |
949 |
> |
final Phaser phaser; |
950 |
> |
final int phase; |
951 |
> |
final boolean interruptible; |
952 |
> |
final boolean timed; |
953 |
> |
boolean wasInterrupted; |
954 |
> |
long nanos; |
955 |
> |
long lastTime; |
956 |
> |
volatile Thread thread; // nulled to cancel wait |
957 |
> |
QNode next; |
958 |
|
|
959 |
< |
/** |
960 |
< |
* Timeout version. |
961 |
< |
* @return current phase |
962 |
< |
*/ |
963 |
< |
private int timedWait(int phase, long nanos) |
964 |
< |
throws InterruptedException, TimeoutException { |
965 |
< |
long startTime = System.nanoTime(); |
966 |
< |
QNode node = null; |
967 |
< |
boolean queued = false; |
968 |
< |
boolean interrupted = false; |
969 |
< |
int p; |
970 |
< |
while ((p = getPhase()) == phase && !interrupted) { |
959 |
> |
QNode(Phaser phaser, int phase, boolean interruptible, |
960 |
> |
boolean timed, long nanos) { |
961 |
> |
this.phaser = phaser; |
962 |
> |
this.phase = phase; |
963 |
> |
this.interruptible = interruptible; |
964 |
> |
this.nanos = nanos; |
965 |
> |
this.timed = timed; |
966 |
> |
this.lastTime = timed ? System.nanoTime() : 0L; |
967 |
> |
thread = Thread.currentThread(); |
968 |
> |
} |
969 |
> |
|
970 |
> |
public boolean isReleasable() { |
971 |
> |
if (thread == null) |
972 |
> |
return true; |
973 |
> |
if (phaser.getPhase() != phase) { |
974 |
> |
thread = null; |
975 |
> |
return true; |
976 |
> |
} |
977 |
|
if (Thread.interrupted()) |
978 |
< |
interrupted = true; |
979 |
< |
else if (nanos - (System.nanoTime() - startTime) <= 0) |
980 |
< |
break; |
981 |
< |
else if (node == null) |
982 |
< |
node = new QNode(this, phase, true, true, startTime, nanos); |
983 |
< |
else if (!queued) |
984 |
< |
queued = tryEnqueue(node); |
985 |
< |
else |
986 |
< |
interrupted = node.doWait(); |
987 |
< |
} |
988 |
< |
if (node != null) |
989 |
< |
node.thread = null; |
990 |
< |
if (p != phase || (p = getPhase()) != phase) |
991 |
< |
releaseWaiters(phase); |
992 |
< |
if (interrupted) |
993 |
< |
throw new InterruptedException(); |
994 |
< |
if (p == phase) |
995 |
< |
throw new TimeoutException(); |
996 |
< |
return p; |
978 |
> |
wasInterrupted = true; |
979 |
> |
if (wasInterrupted && interruptible) { |
980 |
> |
thread = null; |
981 |
> |
return true; |
982 |
> |
} |
983 |
> |
if (timed) { |
984 |
> |
if (nanos > 0L) { |
985 |
> |
long now = System.nanoTime(); |
986 |
> |
nanos -= now - lastTime; |
987 |
> |
lastTime = now; |
988 |
> |
} |
989 |
> |
if (nanos <= 0L) { |
990 |
> |
thread = null; |
991 |
> |
return true; |
992 |
> |
} |
993 |
> |
} |
994 |
> |
return false; |
995 |
> |
} |
996 |
> |
|
997 |
> |
public boolean block() { |
998 |
> |
if (isReleasable()) |
999 |
> |
return true; |
1000 |
> |
else if (!timed) |
1001 |
> |
LockSupport.park(this); |
1002 |
> |
else if (nanos > 0) |
1003 |
> |
LockSupport.parkNanos(this, nanos); |
1004 |
> |
return isReleasable(); |
1005 |
> |
} |
1006 |
|
} |
1007 |
|
|
1008 |
|
// Unsafe mechanics |
1011 |
|
private static final long stateOffset = |
1012 |
|
objectFieldOffset("state", Phaser.class); |
1013 |
|
|
996 |
– |
private final boolean casState(long cmp, long val) { |
997 |
– |
return UNSAFE.compareAndSwapLong(this, stateOffset, cmp, val); |
998 |
– |
} |
999 |
– |
|
1014 |
|
private static long objectFieldOffset(String field, Class<?> klazz) { |
1015 |
|
try { |
1016 |
|
return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field)); |