5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import jsr166y.forkjoin.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.util.concurrent.locks.LockSupport; |
12 |
+ |
import sun.misc.Unsafe; |
13 |
+ |
import java.lang.reflect.*; |
14 |
|
|
15 |
|
/** |
16 |
|
* A reusable synchronization barrier, similar in functionality to a |
17 |
< |
* {@link java.util.concurrent.CyclicBarrier}, but supporting more |
18 |
< |
* flexible usage. |
17 |
> |
* {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and |
18 |
> |
* {@link java.util.concurrent.CountDownLatch CountDownLatch} |
19 |
> |
* but supporting more flexible usage. |
20 |
|
* |
21 |
|
* <ul> |
22 |
|
* |
23 |
< |
* <li> The number of parties synchronizing on the barrier may vary |
24 |
< |
* over time. A task may register to be a party in a barrier at any |
25 |
< |
* time, and may deregister upon arriving at the barrier. As is the |
26 |
< |
* case with most basic synchronization constructs, registration |
27 |
< |
* and deregistration affect only internal counts; they do not |
28 |
< |
* establish any further internal bookkeeping, so tasks cannot query |
29 |
< |
* whether they are registered. |
23 |
> |
* <li> The number of parties synchronizing on a phaser may vary over |
24 |
> |
* time. A task may register to be a party at any time, and may |
25 |
> |
* deregister upon arriving at the barrier. As is the case with most |
26 |
> |
* basic synchronization constructs, registration and deregistration |
27 |
> |
* affect only internal counts; they do not establish any further |
28 |
> |
* internal bookkeeping, so tasks cannot query whether they are |
29 |
> |
* registered. (However, you can introduce such bookkeeping by |
30 |
> |
* subclassing this class.) |
31 |
|
* |
32 |
|
* <li> Each generation has an associated phase value, starting at |
33 |
|
* zero, and advancing when all parties reach the barrier (wrapping |
34 |
< |
* around to zero after reaching <tt>Integer.MAX_VALUE</tt>). |
34 |
> |
* around to zero after reaching {@code Integer.MAX_VALUE}). |
35 |
|
* |
36 |
|
* <li> Like a CyclicBarrier, a Phaser may be repeatedly awaited. |
37 |
< |
* Method <tt>arriveAndAwaitAdvance</tt> has effect analogous to |
38 |
< |
* <tt>CyclicBarrier.await</tt>. However, Phasers separate two |
39 |
< |
* aspects of coordination, that may be invoked independently: |
37 |
> |
* Method {@code arriveAndAwaitAdvance} has effect analogous to |
38 |
> |
* {@code CyclicBarrier.await}. However, Phasers separate two |
39 |
> |
* aspects of coordination, that may also be invoked independently: |
40 |
|
* |
41 |
|
* <ul> |
42 |
|
* |
43 |
< |
* <li> Arriving at a barrier. Methods <tt>arrive</tt> and |
44 |
< |
* <tt>arriveAndDeregister</tt> do not block, but return |
45 |
< |
* the phase value on entry to the method. |
43 |
> |
* <li> Arriving at a barrier. Methods {@code arrive} and |
44 |
> |
* {@code arriveAndDeregister} do not block, but return |
45 |
> |
* the phase value current upon entry to the method. |
46 |
|
* |
47 |
< |
* <li> Awaiting others. Method <tt>awaitAdvance</tt> requires an |
47 |
> |
* <li> Awaiting others. Method {@code awaitAdvance} requires an |
48 |
|
* argument indicating the entry phase, and returns when the |
49 |
|
* barrier advances to a new phase. |
50 |
|
* </ul> |
52 |
|
* |
53 |
|
* <li> Barrier actions, performed by the task triggering a phase |
54 |
|
* advance while others may be waiting, are arranged by overriding |
55 |
< |
* method <tt>onAdvance</tt>, that also controls termination. |
55 |
> |
* method {@code onAdvance}, that also controls termination. |
56 |
> |
* Overriding this method may be used to similar but more flexible |
57 |
> |
* effect as providing a barrier action to a CyclicBarrier. |
58 |
|
* |
59 |
|
* <li> Phasers may enter a <em>termination</em> state in which all |
60 |
< |
* await actions immediately return, indicating (via a negative phase |
61 |
< |
* value) that execution is complete. Termination is triggered by |
62 |
< |
* executing the overridable <tt>onAdvance</tt> method that is invoked |
63 |
< |
* each time the barrier is tripped. When a Phaser is controlling an |
60 |
> |
* actions immediately return without updating phaser state or waiting |
61 |
> |
* for advance, and indicating (via a negative phase value) that |
62 |
> |
* execution is complete. Termination is triggered by executing the |
63 |
> |
* overridable {@code onAdvance} method that is invoked each time the |
64 |
> |
* barrier is about to be tripped. When a Phaser is controlling an |
65 |
|
* action with a fixed number of iterations, it is often convenient to |
66 |
|
* override this method to cause termination when the current phase |
67 |
< |
* number reaches a threshold. Method <tt>forceTermination</tt> is |
68 |
< |
* also available to assist recovery actions upon failure. |
69 |
< |
* |
70 |
< |
* <li> Unlike most synchronizers, a Phaser may also be used with |
71 |
< |
* ForkJoinTasks (as well as plain threads). |
67 |
> |
* number reaches a threshold. Method {@code forceTermination} is also |
68 |
> |
* available to abruptly release waiting threads and allow them to |
69 |
> |
* terminate. |
70 |
> |
* |
71 |
> |
* <li> Phasers may be tiered to reduce contention. Phasers with large |
72 |
> |
* numbers of parties that would otherwise experience heavy |
73 |
> |
* synchronization contention costs may instead be arranged in trees. |
74 |
> |
* This will typically greatly increase throughput even though it |
75 |
> |
* incurs somewhat greater per-operation overhead. |
76 |
|
* |
77 |
< |
* <li> By default, <tt>awaitAdvance</tt> continues to wait even if |
78 |
< |
* the current thread is interrupted. And unlike the case in |
77 |
> |
* <li> By default, {@code awaitAdvance} continues to wait even if |
78 |
> |
* the waiting thread is interrupted. And unlike the case in |
79 |
|
* CyclicBarriers, exceptions encountered while tasks wait |
80 |
|
* interruptibly or with timeout do not change the state of the |
81 |
|
* barrier. If necessary, you can perform any associated recovery |
82 |
< |
* within handlers of those exceptions. |
82 |
> |
* within handlers of those exceptions, often after invoking |
83 |
> |
* {@code forceTermination}. |
84 |
|
* |
85 |
< |
* </ul> |
85 |
> |
* <li>Phasers ensure lack of starvation when used by ForkJoinTasks. |
86 |
|
* |
87 |
< |
* <p><b>Sample usage:</b> |
87 |
> |
* </ul> |
88 |
|
* |
89 |
< |
* <p>[todo: non-FJ example] |
89 |
> |
* <p><b>Sample usages:</b> |
90 |
|
* |
91 |
< |
* <p> A Phaser may be used to support a style of programming in |
92 |
< |
* which a task waits for others to complete, without otherwise |
93 |
< |
* needing to keep track of which tasks it is waiting for. This is |
94 |
< |
* similar to the "sync" construct in Cilk and "clocks" in X10. |
83 |
< |
* Special constructions based on such barriers are available using |
84 |
< |
* the <tt>LinkedAsyncAction</tt> and <tt>CyclicAction</tt> classes, |
85 |
< |
* but they can be useful in other contexts as well. For a simple |
86 |
< |
* (but not very useful) example, here is a variant of Fibonacci: |
91 |
> |
* <p>A Phaser may be used instead of a {@code CountDownLatch} to control |
92 |
> |
* a one-shot action serving a variable number of parties. The typical |
93 |
> |
* idiom is for the method setting this up to first register, then |
94 |
> |
* start the actions, then deregister, as in: |
95 |
|
* |
96 |
|
* <pre> |
97 |
< |
* class BarrierFibonacci extends RecursiveAction { |
98 |
< |
* int argument, result; |
99 |
< |
* final Phaser parentBarrier; |
100 |
< |
* BarrierFibonacci(int n, Phaser parentBarrier) { |
101 |
< |
* this.argument = n; |
102 |
< |
* this.parentBarrier = parentBarrier; |
103 |
< |
* parentBarrier.register(); |
97 |
> |
* void runTasks(List<Runnable> list) { |
98 |
> |
* final Phaser phaser = new Phaser(1); // "1" to register self |
99 |
> |
* for (Runnable r : list) { |
100 |
> |
* phaser.register(); |
101 |
> |
* new Thread() { |
102 |
> |
* public void run() { |
103 |
> |
* phaser.arriveAndAwaitAdvance(); // await all creation |
104 |
> |
* r.run(); |
105 |
> |
* phaser.arriveAndDeregister(); // signal completion |
106 |
> |
* } |
107 |
> |
* }.start(); |
108 |
|
* } |
109 |
< |
* protected void compute() { |
110 |
< |
* int n = argument; |
111 |
< |
* if (n <= 1) |
112 |
< |
* result = n; |
113 |
< |
* else { |
114 |
< |
* Phaser childBarrier = new Phaser(1); |
115 |
< |
* BarrierFibonacci f1 = new BarrierFibonacci(n - 1, childBarrier); |
116 |
< |
* BarrierFibonacci f2 = new BarrierFibonacci(n - 2, childBarrier); |
117 |
< |
* f1.fork(); |
118 |
< |
* f2.fork(); |
119 |
< |
* childBarrier.arriveAndAwait(); |
120 |
< |
* result = f1.result + f2.result; |
121 |
< |
* } |
122 |
< |
* parentBarrier.arriveAndDeregister(); |
109 |
> |
* |
110 |
> |
* doSomethingOnBehalfOfWorkers(); |
111 |
> |
* phaser.arrive(); // allow threads to start |
112 |
> |
* int p = phaser.arriveAndDeregister(); // deregister self ... |
113 |
> |
* p = phaser.awaitAdvance(p); // ... and await arrival |
114 |
> |
* otherActions(); // do other things while tasks execute |
115 |
> |
* phaser.awaitAdvance(p); // await final completion |
116 |
> |
* } |
117 |
> |
* </pre> |
118 |
> |
* |
119 |
> |
* <p>One way to cause a set of threads to repeatedly perform actions |
120 |
> |
* for a given number of iterations is to override {@code onAdvance}: |
121 |
> |
* |
122 |
> |
* <pre> |
123 |
> |
* void startTasks(List<Runnable> list, final int iterations) { |
124 |
> |
* final Phaser phaser = new Phaser() { |
125 |
> |
* public boolean onAdvance(int phase, int registeredParties) { |
126 |
> |
* return phase >= iterations || registeredParties == 0; |
127 |
> |
* } |
128 |
> |
* }; |
129 |
> |
* phaser.register(); |
130 |
> |
* for (Runnable r : list) { |
131 |
> |
* phaser.register(); |
132 |
> |
* new Thread() { |
133 |
> |
* public void run() { |
134 |
> |
* do { |
135 |
> |
* r.run(); |
136 |
> |
* phaser.arriveAndAwaitAdvance(); |
137 |
> |
* } while(!phaser.isTerminated(); |
138 |
> |
* } |
139 |
> |
* }.start(); |
140 |
|
* } |
141 |
+ |
* phaser.arriveAndDeregister(); // deregister self, don't wait |
142 |
|
* } |
143 |
|
* </pre> |
144 |
|
* |
145 |
+ |
* <p> To create a set of tasks using a tree of Phasers, |
146 |
+ |
* you could use code of the following form, assuming a |
147 |
+ |
* Task class with a constructor accepting a Phaser that |
148 |
+ |
* it registers for upon construction: |
149 |
+ |
* <pre> |
150 |
+ |
* void build(Task[] actions, int lo, int hi, Phaser b) { |
151 |
+ |
* int step = (hi - lo) / TASKS_PER_PHASER; |
152 |
+ |
* if (step > 1) { |
153 |
+ |
* int i = lo; |
154 |
+ |
* while (i < hi) { |
155 |
+ |
* int r = Math.min(i + step, hi); |
156 |
+ |
* build(actions, i, r, new Phaser(b)); |
157 |
+ |
* i = r; |
158 |
+ |
* } |
159 |
+ |
* } |
160 |
+ |
* else { |
161 |
+ |
* for (int i = lo; i < hi; ++i) |
162 |
+ |
* actions[i] = new Task(b); |
163 |
+ |
* // assumes new Task(b) performs b.register() |
164 |
+ |
* } |
165 |
+ |
* } |
166 |
+ |
* // .. initially called, for n tasks via |
167 |
+ |
* build(new Task[n], 0, n, new Phaser()); |
168 |
+ |
* </pre> |
169 |
+ |
* |
170 |
+ |
* The best value of {@code TASKS_PER_PHASER} depends mainly on |
171 |
+ |
* expected barrier synchronization rates. A value as low as four may |
172 |
+ |
* be appropriate for extremely small per-barrier task bodies (thus |
173 |
+ |
* high rates), or up to hundreds for extremely large ones. |
174 |
+ |
* |
175 |
+ |
* </pre> |
176 |
+ |
* |
177 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
178 |
< |
* maximum number of parties to 65535. Attempts to register |
179 |
< |
* additional parties result in IllegalStateExceptions. |
178 |
> |
* maximum number of parties to 65535. Attempts to register additional |
179 |
> |
* parties result in IllegalStateExceptions. However, you can and |
180 |
> |
* should create tiered phasers to accommodate arbitrarily large sets |
181 |
> |
* of participants. |
182 |
|
*/ |
183 |
|
public class Phaser { |
184 |
|
/* |
185 |
|
* This class implements an extension of X10 "clocks". Thanks to |
186 |
< |
* Vijay Saraswat for the idea of applying it to ForkJoinTasks, |
187 |
< |
* and to Vivek Sarkar for enhancements to extend functionality. |
186 |
> |
* Vijay Saraswat for the idea, and to Vivek Sarkar for |
187 |
> |
* enhancements to extend functionality. |
188 |
|
*/ |
189 |
|
|
190 |
|
/** |
197 |
|
* * terminated -- set if barrier is terminated (1 bit) |
198 |
|
* |
199 |
|
* However, to efficiently maintain atomicity, these values are |
200 |
< |
* packed into a single AtomicLong. Termination uses the sign bit |
201 |
< |
* of 32 bit representation of phase, so phase is set to -1 on |
202 |
< |
* termination. |
203 |
< |
*/ |
204 |
< |
private final AtomicLong state; |
205 |
< |
|
206 |
< |
/** |
143 |
< |
* Head of Treiber stack for waiting nonFJ threads. |
200 |
> |
* packed into a single (atomic) long. Termination uses the sign |
201 |
> |
* bit of 32 bit representation of phase, so phase is set to -1 on |
202 |
> |
* termination. Good performance relies on keeping state decoding |
203 |
> |
* and encoding simple, and keeping race windows short. |
204 |
> |
* |
205 |
> |
* Note: there are some cheats in arrive() that rely on unarrived |
206 |
> |
* count being lowest 16 bits. |
207 |
|
*/ |
208 |
< |
private final AtomicReference<QNode> head = new AtomicReference<QNode>(); |
208 |
> |
private volatile long state; |
209 |
|
|
210 |
|
private static final int ushortBits = 16; |
211 |
< |
private static final int ushortMask = (1 << ushortBits) - 1; |
212 |
< |
private static final int phaseMask = 0x7fffffff; |
211 |
> |
private static final int ushortMask = 0xffff; |
212 |
> |
private static final int phaseMask = 0x7fffffff; |
213 |
|
|
214 |
|
private static int unarrivedOf(long s) { |
215 |
|
return (int)(s & ushortMask); |
216 |
|
} |
217 |
|
|
218 |
|
private static int partiesOf(long s) { |
219 |
< |
return (int)(s & (ushortMask << 16)) >>> 16; |
219 |
> |
return ((int)s) >>> 16; |
220 |
|
} |
221 |
|
|
222 |
|
private static int phaseOf(long s) { |
228 |
|
} |
229 |
|
|
230 |
|
private static long stateFor(int phase, int parties, int unarrived) { |
231 |
< |
return (((long)phase) << 32) | ((parties << 16) | unarrived); |
231 |
> |
return ((((long)phase) << 32) | (((long)parties) << 16) | |
232 |
> |
(long)unarrived); |
233 |
> |
} |
234 |
> |
|
235 |
> |
private static long trippedStateFor(int phase, int parties) { |
236 |
> |
long lp = (long)parties; |
237 |
> |
return (((long)phase) << 32) | (lp << 16) | lp; |
238 |
> |
} |
239 |
> |
|
240 |
> |
/** |
241 |
> |
* Returns message string for bad bounds exceptions |
242 |
> |
*/ |
243 |
> |
private static String badBounds(int parties, int unarrived) { |
244 |
> |
return ("Attempt to set " + unarrived + |
245 |
> |
" unarrived of " + parties + " parties"); |
246 |
|
} |
247 |
|
|
248 |
< |
private static IllegalStateException badBounds(int parties, int unarrived) { |
249 |
< |
return new IllegalStateException("Attempt to set " + unarrived + |
250 |
< |
" unarrived of " + parties + " parties"); |
248 |
> |
/** |
249 |
> |
* The parent of this phaser, or null if none |
250 |
> |
*/ |
251 |
> |
private final Phaser parent; |
252 |
> |
|
253 |
> |
/** |
254 |
> |
* The root of Phaser tree. Equals this if not in a tree. Used to |
255 |
> |
* support faster state push-down. |
256 |
> |
*/ |
257 |
> |
private final Phaser root; |
258 |
> |
|
259 |
> |
// Wait queues |
260 |
> |
|
261 |
> |
/** |
262 |
> |
* Heads of Treiber stacks for waiting threads. To eliminate |
263 |
> |
* contention while releasing some threads while adding others, we |
264 |
> |
* use two of them, alternating across even and odd phases. |
265 |
> |
*/ |
266 |
> |
private final AtomicReference<QNode> evenQ = new AtomicReference<QNode>(); |
267 |
> |
private final AtomicReference<QNode> oddQ = new AtomicReference<QNode>(); |
268 |
> |
|
269 |
> |
private AtomicReference<QNode> queueFor(int phase) { |
270 |
> |
return (phase & 1) == 0? evenQ : oddQ; |
271 |
> |
} |
272 |
> |
|
273 |
> |
/** |
274 |
> |
* Returns current state, first resolving lagged propagation from |
275 |
> |
* root if necessary. |
276 |
> |
*/ |
277 |
> |
private long getReconciledState() { |
278 |
> |
return parent == null? state : reconcileState(); |
279 |
> |
} |
280 |
> |
|
281 |
> |
/** |
282 |
> |
* Recursively resolves state. |
283 |
> |
*/ |
284 |
> |
private long reconcileState() { |
285 |
> |
Phaser p = parent; |
286 |
> |
long s = state; |
287 |
> |
if (p != null) { |
288 |
> |
while (unarrivedOf(s) == 0 && phaseOf(s) != phaseOf(root.state)) { |
289 |
> |
long parentState = p.getReconciledState(); |
290 |
> |
int parentPhase = phaseOf(parentState); |
291 |
> |
int phase = phaseOf(s = state); |
292 |
> |
if (phase != parentPhase) { |
293 |
> |
long next = trippedStateFor(parentPhase, partiesOf(s)); |
294 |
> |
if (casState(s, next)) { |
295 |
> |
releaseWaiters(phase); |
296 |
> |
s = next; |
297 |
> |
} |
298 |
> |
} |
299 |
> |
} |
300 |
> |
} |
301 |
> |
return s; |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
|
* Creates a new Phaser without any initially registered parties, |
306 |
< |
* and initial phase number 0. |
306 |
> |
* initial phase number 0, and no parent. Any thread using this |
307 |
> |
* Phaser will need to first register for it. |
308 |
|
*/ |
309 |
|
public Phaser() { |
310 |
< |
state = new AtomicLong(stateFor(0, 0, 0)); |
310 |
> |
this(null); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Creates a new Phaser with the given numbers of registered |
315 |
< |
* unarrived parties and initial phase number 0. |
315 |
> |
* unarrived parties, initial phase number 0, and no parent. |
316 |
|
* @param parties the number of parties required to trip barrier. |
317 |
|
* @throws IllegalArgumentException if parties less than zero |
318 |
|
* or greater than the maximum number of parties supported. |
319 |
|
*/ |
320 |
|
public Phaser(int parties) { |
321 |
+ |
this(null, parties); |
322 |
+ |
} |
323 |
+ |
|
324 |
+ |
/** |
325 |
+ |
* Creates a new Phaser with the given parent, without any |
326 |
+ |
* initially registered parties. If parent is non-null this phaser |
327 |
+ |
* is registered with the parent and its initial phase number is |
328 |
+ |
* the same as that of parent phaser. |
329 |
+ |
* @param parent the parent phaser. |
330 |
+ |
*/ |
331 |
+ |
public Phaser(Phaser parent) { |
332 |
+ |
int phase = 0; |
333 |
+ |
this.parent = parent; |
334 |
+ |
if (parent != null) { |
335 |
+ |
this.root = parent.root; |
336 |
+ |
phase = parent.register(); |
337 |
+ |
} |
338 |
+ |
else |
339 |
+ |
this.root = this; |
340 |
+ |
this.state = trippedStateFor(phase, 0); |
341 |
+ |
} |
342 |
+ |
|
343 |
+ |
/** |
344 |
+ |
* Creates a new Phaser with the given parent and numbers of |
345 |
+ |
* registered unarrived parties. If parent is non-null this phaser |
346 |
+ |
* is registered with the parent and its initial phase number is |
347 |
+ |
* the same as that of parent phaser. |
348 |
+ |
* @param parent the parent phaser. |
349 |
+ |
* @param parties the number of parties required to trip barrier. |
350 |
+ |
* @throws IllegalArgumentException if parties less than zero |
351 |
+ |
* or greater than the maximum number of parties supported. |
352 |
+ |
*/ |
353 |
+ |
public Phaser(Phaser parent, int parties) { |
354 |
|
if (parties < 0 || parties > ushortMask) |
355 |
|
throw new IllegalArgumentException("Illegal number of parties"); |
356 |
< |
state = new AtomicLong(stateFor(0, parties, parties)); |
356 |
> |
int phase = 0; |
357 |
> |
this.parent = parent; |
358 |
> |
if (parent != null) { |
359 |
> |
this.root = parent.root; |
360 |
> |
phase = parent.register(); |
361 |
> |
} |
362 |
> |
else |
363 |
> |
this.root = this; |
364 |
> |
this.state = trippedStateFor(phase, parties); |
365 |
|
} |
366 |
|
|
367 |
|
/** |
370 |
|
* @throws IllegalStateException if attempting to register more |
371 |
|
* than the maximum supported number of parties. |
372 |
|
*/ |
373 |
< |
public int register() { // increment both parties and unarrived |
374 |
< |
final AtomicLong state = this.state; |
373 |
> |
public int register() { |
374 |
> |
return doRegister(1); |
375 |
> |
} |
376 |
> |
|
377 |
> |
/** |
378 |
> |
* Adds the given number of new unarrived parties to this phaser. |
379 |
> |
* @param parties the number of parties required to trip barrier. |
380 |
> |
* @return the current barrier phase number upon registration |
381 |
> |
* @throws IllegalStateException if attempting to register more |
382 |
> |
* than the maximum supported number of parties. |
383 |
> |
*/ |
384 |
> |
public int bulkRegister(int parties) { |
385 |
> |
if (parties < 0) |
386 |
> |
throw new IllegalArgumentException(); |
387 |
> |
if (parties == 0) |
388 |
> |
return getPhase(); |
389 |
> |
return doRegister(parties); |
390 |
> |
} |
391 |
> |
|
392 |
> |
/** |
393 |
> |
* Shared code for register, bulkRegister |
394 |
> |
*/ |
395 |
> |
private int doRegister(int registrations) { |
396 |
> |
int phase; |
397 |
|
for (;;) { |
398 |
< |
long s = state.get(); |
399 |
< |
int phase = phaseOf(s); |
400 |
< |
int parties = partiesOf(s) + 1; |
401 |
< |
int unarrived = unarrivedOf(s) + 1; |
398 |
> |
long s = getReconciledState(); |
399 |
> |
phase = phaseOf(s); |
400 |
> |
int unarrived = unarrivedOf(s) + registrations; |
401 |
> |
int parties = partiesOf(s) + registrations; |
402 |
> |
if (phase < 0) |
403 |
> |
break; |
404 |
|
if (parties > ushortMask || unarrived > ushortMask) |
405 |
< |
throw badBounds(parties, unarrived); |
406 |
< |
if (state.compareAndSet(s, stateFor(phase, parties, unarrived))) |
407 |
< |
return phase; |
405 |
> |
throw new IllegalStateException(badBounds(parties, unarrived)); |
406 |
> |
if (phase == phaseOf(root.state) && |
407 |
> |
casState(s, stateFor(phase, parties, unarrived))) |
408 |
> |
break; |
409 |
|
} |
410 |
+ |
return phase; |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
|
* Arrives at the barrier, but does not wait for others. (You can |
415 |
|
* in turn wait for others via {@link #awaitAdvance}). |
416 |
|
* |
417 |
< |
* @return the current barrier phase number upon entry to |
418 |
< |
* this method, or a negative value if terminated; |
419 |
< |
* @throws IllegalStateException if the number of unarrived |
420 |
< |
* parties would become negative. |
417 |
> |
* @return the barrier phase number upon entry to this method, or a |
418 |
> |
* negative value if terminated; |
419 |
> |
* @throws IllegalStateException if not terminated and the number |
420 |
> |
* of unarrived parties would become negative. |
421 |
|
*/ |
422 |
< |
public int arrive() { // decrement unarrived. If zero, trip |
423 |
< |
final AtomicLong state = this.state; |
422 |
> |
public int arrive() { |
423 |
> |
int phase; |
424 |
|
for (;;) { |
425 |
< |
long s = state.get(); |
426 |
< |
int phase = phaseOf(s); |
425 |
> |
long s = state; |
426 |
> |
phase = phaseOf(s); |
427 |
> |
if (phase < 0) |
428 |
> |
break; |
429 |
|
int parties = partiesOf(s); |
430 |
|
int unarrived = unarrivedOf(s) - 1; |
431 |
< |
if (unarrived < 0) |
432 |
< |
throw badBounds(parties, unarrived); |
433 |
< |
if (unarrived == 0 && phase >= 0) { |
236 |
< |
trip(phase, parties); |
237 |
< |
return phase; |
431 |
> |
if (unarrived > 0) { // Not the last arrival |
432 |
> |
if (casState(s, s - 1)) // s-1 adds one arrival |
433 |
> |
break; |
434 |
|
} |
435 |
< |
if (state.compareAndSet(s, stateFor(phase, parties, unarrived))) |
436 |
< |
return phase; |
435 |
> |
else if (unarrived == 0) { // the last arrival |
436 |
> |
Phaser par = parent; |
437 |
> |
if (par == null) { // directly trip |
438 |
> |
if (casState |
439 |
> |
(s, |
440 |
> |
trippedStateFor(onAdvance(phase, parties)? -1 : |
441 |
> |
((phase + 1) & phaseMask), parties))) { |
442 |
> |
releaseWaiters(phase); |
443 |
> |
break; |
444 |
> |
} |
445 |
> |
} |
446 |
> |
else { // cascade to parent |
447 |
> |
if (casState(s, s - 1)) { // zeroes unarrived |
448 |
> |
par.arrive(); |
449 |
> |
reconcileState(); |
450 |
> |
break; |
451 |
> |
} |
452 |
> |
} |
453 |
> |
} |
454 |
> |
else if (phase != phaseOf(root.state)) // or if unreconciled |
455 |
> |
reconcileState(); |
456 |
> |
else |
457 |
> |
throw new IllegalStateException(badBounds(parties, unarrived)); |
458 |
|
} |
459 |
+ |
return phase; |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
|
* Arrives at the barrier, and deregisters from it, without |
464 |
< |
* waiting for others. |
464 |
> |
* waiting for others. Deregistration reduces number of parties |
465 |
> |
* required to trip the barrier in future phases. If this phaser |
466 |
> |
* has a parent, and deregistration causes this phaser to have |
467 |
> |
* zero parties, this phaser is also deregistered from its parent. |
468 |
|
* |
469 |
|
* @return the current barrier phase number upon entry to |
470 |
|
* this method, or a negative value if terminated; |
471 |
< |
* @throws IllegalStateException if the number of registered or |
472 |
< |
* unarrived parties would become negative. |
471 |
> |
* @throws IllegalStateException if not terminated and the number |
472 |
> |
* of registered or unarrived parties would become negative. |
473 |
|
*/ |
474 |
< |
public int arriveAndDeregister() { // Same as arrive, plus decrement parties |
475 |
< |
final AtomicLong state = this.state; |
474 |
> |
public int arriveAndDeregister() { |
475 |
> |
// similar code to arrive, but too different to merge |
476 |
> |
Phaser par = parent; |
477 |
> |
int phase; |
478 |
|
for (;;) { |
479 |
< |
long s = state.get(); |
480 |
< |
int phase = phaseOf(s); |
479 |
> |
long s = state; |
480 |
> |
phase = phaseOf(s); |
481 |
> |
if (phase < 0) |
482 |
> |
break; |
483 |
|
int parties = partiesOf(s) - 1; |
484 |
|
int unarrived = unarrivedOf(s) - 1; |
485 |
< |
if (parties < 0 || unarrived < 0) |
486 |
< |
throw badBounds(parties, unarrived); |
487 |
< |
if (unarrived == 0 && phase >= 0) { |
488 |
< |
trip(phase, parties); |
489 |
< |
return phase; |
485 |
> |
if (parties >= 0) { |
486 |
> |
if (unarrived > 0 || (unarrived == 0 && par != null)) { |
487 |
> |
if (casState |
488 |
> |
(s, |
489 |
> |
stateFor(phase, parties, unarrived))) { |
490 |
> |
if (unarrived == 0) { |
491 |
> |
par.arriveAndDeregister(); |
492 |
> |
reconcileState(); |
493 |
> |
} |
494 |
> |
break; |
495 |
> |
} |
496 |
> |
continue; |
497 |
> |
} |
498 |
> |
if (unarrived == 0) { |
499 |
> |
if (casState |
500 |
> |
(s, |
501 |
> |
trippedStateFor(onAdvance(phase, parties)? -1 : |
502 |
> |
((phase + 1) & phaseMask), parties))) { |
503 |
> |
releaseWaiters(phase); |
504 |
> |
break; |
505 |
> |
} |
506 |
> |
continue; |
507 |
> |
} |
508 |
> |
if (par != null && phase != phaseOf(root.state)) { |
509 |
> |
reconcileState(); |
510 |
> |
continue; |
511 |
> |
} |
512 |
|
} |
513 |
< |
if (state.compareAndSet(s, stateFor(phase, parties, unarrived))) |
267 |
< |
return phase; |
513 |
> |
throw new IllegalStateException(badBounds(parties, unarrived)); |
514 |
|
} |
515 |
+ |
return phase; |
516 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
< |
* Arrives at the barrier and awaits others. Unlike other arrival |
520 |
< |
* methods, this method returns the arrival index of the |
521 |
< |
* caller. The caller tripping the barrier returns zero, the |
522 |
< |
* previous caller 1, and so on. |
523 |
< |
* @return the arrival index |
524 |
< |
* @throws IllegalStateException if the number of unarrived |
525 |
< |
* parties would become negative. |
519 |
> |
* Arrives at the barrier and awaits others. Equivalent in effect |
520 |
> |
* to {@code awaitAdvance(arrive())}. If you instead need to |
521 |
> |
* await with interruption of timeout, and/or deregister upon |
522 |
> |
* arrival, you can arrange them using analogous constructions. |
523 |
> |
* @return the phase on entry to this method |
524 |
> |
* @throws IllegalStateException if not terminated and the number |
525 |
> |
* of unarrived parties would become negative. |
526 |
|
*/ |
527 |
|
public int arriveAndAwaitAdvance() { |
528 |
< |
final AtomicLong state = this.state; |
282 |
< |
for (;;) { |
283 |
< |
long s = state.get(); |
284 |
< |
int phase = phaseOf(s); |
285 |
< |
int parties = partiesOf(s); |
286 |
< |
int unarrived = unarrivedOf(s) - 1; |
287 |
< |
if (unarrived < 0) |
288 |
< |
throw badBounds(parties, unarrived); |
289 |
< |
if (unarrived == 0 && phase >= 0) { |
290 |
< |
trip(phase, parties); |
291 |
< |
return 0; |
292 |
< |
} |
293 |
< |
if (state.compareAndSet(s, stateFor(phase, parties, unarrived))) { |
294 |
< |
awaitAdvance(phase); |
295 |
< |
return unarrived; |
296 |
< |
} |
297 |
< |
} |
528 |
> |
return awaitAdvance(arrive()); |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
|
* Awaits the phase of the barrier to advance from the given |
533 |
< |
* value, or returns immediately if this barrier is terminated. |
533 |
> |
* value, or returns immediately if argument is negative or this |
534 |
> |
* barrier is terminated. |
535 |
|
* @param phase the phase on entry to this method |
536 |
|
* @return the phase on exit from this method |
537 |
|
*/ |
538 |
|
public int awaitAdvance(int phase) { |
539 |
|
if (phase < 0) |
540 |
|
return phase; |
541 |
< |
Thread current = Thread.currentThread(); |
542 |
< |
if (current instanceof ForkJoinWorkerThread) |
543 |
< |
return helpingWait(phase); |
544 |
< |
if (untimedWait(current, phase, false)) |
545 |
< |
current.interrupt(); |
546 |
< |
return phaseOf(state.get()); |
541 |
> |
long s = getReconciledState(); |
542 |
> |
int p = phaseOf(s); |
543 |
> |
if (p != phase) |
544 |
> |
return p; |
545 |
> |
if (unarrivedOf(s) == 0 && parent != null) |
546 |
> |
parent.awaitAdvance(phase); |
547 |
> |
// Fall here even if parent waited, to reconcile and help release |
548 |
> |
return untimedWait(phase); |
549 |
|
} |
550 |
|
|
551 |
|
/** |
552 |
|
* Awaits the phase of the barrier to advance from the given |
553 |
< |
* value, or returns immediately if this barrier is terminated, or |
554 |
< |
* throws InterruptedException if interrupted while waiting. |
553 |
> |
* value, or returns immediately if argument is negative or this |
554 |
> |
* barrier is terminated, or throws InterruptedException if |
555 |
> |
* interrupted while waiting. |
556 |
|
* @param phase the phase on entry to this method |
557 |
|
* @return the phase on exit from this method |
558 |
|
* @throws InterruptedException if thread interrupted while waiting |
559 |
|
*/ |
560 |
< |
public int awaitAdvanceInterruptibly(int phase) throws InterruptedException { |
560 |
> |
public int awaitAdvanceInterruptibly(int phase) |
561 |
> |
throws InterruptedException { |
562 |
|
if (phase < 0) |
563 |
|
return phase; |
564 |
< |
Thread current = Thread.currentThread(); |
565 |
< |
if (current instanceof ForkJoinWorkerThread) |
566 |
< |
return helpingWait(phase); |
567 |
< |
else if (Thread.interrupted() || untimedWait(current, phase, true)) |
568 |
< |
throw new InterruptedException(); |
569 |
< |
else |
570 |
< |
return phaseOf(state.get()); |
564 |
> |
long s = getReconciledState(); |
565 |
> |
int p = phaseOf(s); |
566 |
> |
if (p != phase) |
567 |
> |
return p; |
568 |
> |
if (unarrivedOf(s) == 0 && parent != null) |
569 |
> |
parent.awaitAdvanceInterruptibly(phase); |
570 |
> |
return interruptibleWait(phase); |
571 |
|
} |
572 |
|
|
573 |
|
/** |
574 |
|
* Awaits the phase of the barrier to advance from the given value |
575 |
< |
* or the given timeout elapses, or returns immediately if this |
576 |
< |
* barrier is terminated. |
575 |
> |
* or the given timeout elapses, or returns immediately if |
576 |
> |
* argument is negative or this barrier is terminated. |
577 |
|
* @param phase the phase on entry to this method |
578 |
|
* @return the phase on exit from this method |
579 |
|
* @throws InterruptedException if thread interrupted while waiting |
583 |
|
throws InterruptedException, TimeoutException { |
584 |
|
if (phase < 0) |
585 |
|
return phase; |
586 |
< |
long nanos = unit.toNanos(timeout); |
587 |
< |
Thread current = Thread.currentThread(); |
588 |
< |
if (current instanceof ForkJoinWorkerThread) |
589 |
< |
return timedHelpingWait(phase, nanos); |
590 |
< |
timedWait(current, phase, nanos); |
591 |
< |
return phaseOf(state.get()); |
586 |
> |
long s = getReconciledState(); |
587 |
> |
int p = phaseOf(s); |
588 |
> |
if (p != phase) |
589 |
> |
return p; |
590 |
> |
if (unarrivedOf(s) == 0 && parent != null) |
591 |
> |
parent.awaitAdvanceInterruptibly(phase, timeout, unit); |
592 |
> |
return timedWait(phase, unit.toNanos(timeout)); |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
|
* Forces this barrier to enter termination state. Counts of |
597 |
< |
* arrived and registered parties are unaffected. This method may |
598 |
< |
* be useful for coordinating recovery after one or more tasks |
599 |
< |
* encounter unexpected exceptions. |
597 |
> |
* arrived and registered parties are unaffected. If this phaser |
598 |
> |
* has a parent, it too is terminated. This method may be useful |
599 |
> |
* for coordinating recovery after one or more tasks encounter |
600 |
> |
* unexpected exceptions. |
601 |
|
*/ |
602 |
|
public void forceTermination() { |
365 |
– |
final AtomicLong state = this.state; |
603 |
|
for (;;) { |
604 |
< |
long s = state.get(); |
604 |
> |
long s = getReconciledState(); |
605 |
|
int phase = phaseOf(s); |
606 |
|
int parties = partiesOf(s); |
607 |
|
int unarrived = unarrivedOf(s); |
608 |
|
if (phase < 0 || |
609 |
< |
state.compareAndSet(s, stateFor(-1, parties, unarrived))) { |
610 |
< |
if (head.get() != null) |
611 |
< |
releaseWaiters(-1); |
609 |
> |
casState(s, stateFor(-1, parties, unarrived))) { |
610 |
> |
releaseWaiters(0); |
611 |
> |
releaseWaiters(1); |
612 |
> |
if (parent != null) |
613 |
> |
parent.forceTermination(); |
614 |
|
return; |
615 |
|
} |
616 |
|
} |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
< |
* Resets the barrier with the given numbers of registered unarrived |
621 |
< |
* parties and phase number 0. This method allows repeated reuse |
622 |
< |
* of this barrier, but only if it is somehow known not to be in |
623 |
< |
* use for other purposes. |
385 |
< |
* @param parties the number of parties required to trip barrier. |
386 |
< |
* @throws IllegalArgumentException if parties less than zero |
387 |
< |
* or greater than the maximum number of parties supported. |
620 |
> |
* Returns the current phase number. The maximum phase number is |
621 |
> |
* {@code Integer.MAX_VALUE}, after which it restarts at |
622 |
> |
* zero. Upon termination, the phase number is negative. |
623 |
> |
* @return the phase number, or a negative value if terminated |
624 |
|
*/ |
625 |
< |
public void reset(int parties) { |
626 |
< |
if (parties < 0 || parties > ushortMask) |
391 |
< |
throw new IllegalArgumentException("Illegal number of parties"); |
392 |
< |
state.set(stateFor(0, parties, parties)); |
393 |
< |
if (head.get() != null) |
394 |
< |
releaseWaiters(0); |
625 |
> |
public final int getPhase() { |
626 |
> |
return phaseOf(getReconciledState()); |
627 |
|
} |
628 |
|
|
629 |
|
/** |
630 |
< |
* Returns the current phase number. The maximum phase number is |
631 |
< |
* <tt>Integer.MAX_VALUE</tt>, after which it restarts at |
632 |
< |
* zero. Upon termination, the phase number is negative. |
401 |
< |
* @return the phase number, or a negative value if terminated |
630 |
> |
* Returns {@code true} if the current phase number equals the given phase. |
631 |
> |
* @param phase the phase |
632 |
> |
* @return {@code true} if the current phase number equals the given phase |
633 |
|
*/ |
634 |
< |
public int getPhase() { |
635 |
< |
return phaseOf(state.get()); |
634 |
> |
public final boolean hasPhase(int phase) { |
635 |
> |
return phaseOf(getReconciledState()) == phase; |
636 |
|
} |
637 |
|
|
638 |
|
/** |
640 |
|
* @return the number of parties |
641 |
|
*/ |
642 |
|
public int getRegisteredParties() { |
643 |
< |
return partiesOf(state.get()); |
643 |
> |
return partiesOf(state); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
649 |
|
* @return the number of arrived parties |
650 |
|
*/ |
651 |
|
public int getArrivedParties() { |
652 |
< |
return arrivedOf(state.get()); |
652 |
> |
return arrivedOf(state); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
658 |
|
* @return the number of unarrived parties |
659 |
|
*/ |
660 |
|
public int getUnarrivedParties() { |
661 |
< |
return unarrivedOf(state.get()); |
661 |
> |
return unarrivedOf(state); |
662 |
> |
} |
663 |
> |
|
664 |
> |
/** |
665 |
> |
* Returns the parent of this phaser, or null if none. |
666 |
> |
* @return the parent of this phaser, or null if none |
667 |
> |
*/ |
668 |
> |
public Phaser getParent() { |
669 |
> |
return parent; |
670 |
|
} |
671 |
|
|
672 |
|
/** |
673 |
< |
* Returns true if this barrier has been terminated. |
674 |
< |
* @return true if this barrier has been terminated |
673 |
> |
* Returns the root ancestor of this phaser, which is the same as |
674 |
> |
* this phaser if it has no parent. |
675 |
> |
* @return the root ancestor of this phaser |
676 |
> |
*/ |
677 |
> |
public Phaser getRoot() { |
678 |
> |
return root; |
679 |
> |
} |
680 |
> |
|
681 |
> |
/** |
682 |
> |
* Returns {@code true} if this barrier has been terminated. |
683 |
> |
* @return {@code true} if this barrier has been terminated |
684 |
|
*/ |
685 |
|
public boolean isTerminated() { |
686 |
< |
return phaseOf(state.get()) < 0; |
686 |
> |
return getPhase() < 0; |
687 |
|
} |
688 |
|
|
689 |
|
/** |
692 |
|
* barrier is tripped (and thus all other waiting parties are |
693 |
|
* dormant). If it returns true, then, rather than advance the |
694 |
|
* phase number, this barrier will be set to a final termination |
695 |
< |
* state, and subsequent calls to <tt>isTerminated</tt> will |
695 |
> |
* state, and subsequent calls to {@code isTerminated} will |
696 |
|
* return true. |
697 |
|
* |
698 |
|
* <p> The default version returns true when the number of |
700 |
|
* termination for other reasons should also preserve this |
701 |
|
* property. |
702 |
|
* |
703 |
+ |
* <p> You may override this method to perform an action with side |
704 |
+ |
* effects visible to participating tasks, but it is in general |
705 |
+ |
* only sensible to do so in designs where all parties register |
706 |
+ |
* before any arrive, and all {@code awaitAdvance} at each phase. |
707 |
+ |
* Otherwise, you cannot ensure lack of interference. In |
708 |
+ |
* particular, this method may be invoked more than once per |
709 |
+ |
* transition if other parties successfully register while the |
710 |
+ |
* invocation of this method is in progress, thus postponing the |
711 |
+ |
* transition until those parties also arrive, re-triggering this |
712 |
+ |
* method. |
713 |
+ |
* |
714 |
|
* @param phase the phase number on entering the barrier |
715 |
< |
* @param registeredParties the current number of registered |
716 |
< |
* parties. |
458 |
< |
* @return true if this barrier should terminate |
715 |
> |
* @param registeredParties the current number of registered parties |
716 |
> |
* @return {@code true} if this barrier should terminate |
717 |
|
*/ |
718 |
|
protected boolean onAdvance(int phase, int registeredParties) { |
719 |
|
return registeredParties <= 0; |
720 |
|
} |
721 |
|
|
722 |
|
/** |
723 |
< |
* Returns a string identifying this barrier, as well as its |
723 |
> |
* Returns a string identifying this phaser, as well as its |
724 |
|
* state. The state, in brackets, includes the String {@code |
725 |
< |
* "phase ="} followed by the phase number, {@code "parties ="} |
725 |
> |
* "phase = "} followed by the phase number, {@code "parties = "} |
726 |
|
* followed by the number of registered parties, and {@code |
727 |
< |
* "arrived ="} followed by the number of arrived parties |
727 |
> |
* "arrived = "} followed by the number of arrived parties. |
728 |
|
* |
729 |
|
* @return a string identifying this barrier, as well as its state |
730 |
|
*/ |
731 |
|
public String toString() { |
732 |
< |
long s = state.get(); |
733 |
< |
return super.toString() + "[phase = " + phaseOf(s) + " parties = " + partiesOf(s) + " arrived = " + arrivedOf(s) + "]"; |
732 |
> |
long s = getReconciledState(); |
733 |
> |
return super.toString() + |
734 |
> |
"[phase = " + phaseOf(s) + |
735 |
> |
" parties = " + partiesOf(s) + |
736 |
> |
" arrived = " + arrivedOf(s) + "]"; |
737 |
|
} |
738 |
|
|
739 |
< |
// methods for tripping and waiting |
739 |
> |
// methods for waiting |
740 |
|
|
741 |
|
/** |
742 |
< |
* Advance the current phase (or terminate) |
742 |
> |
* Wait nodes for Treiber stack representing wait queue |
743 |
|
*/ |
744 |
< |
private void trip(int phase, int parties) { |
745 |
< |
int next = onAdvance(phase, parties)? -1 : ((phase + 1) & phaseMask); |
746 |
< |
state.set(stateFor(next, parties, parties)); |
747 |
< |
if (head.get() != null) |
748 |
< |
releaseWaiters(next); |
749 |
< |
} |
750 |
< |
|
751 |
< |
private int helpingWait(int phase) { |
752 |
< |
final AtomicLong state = this.state; |
753 |
< |
int p; |
754 |
< |
while ((p = phaseOf(state.get())) == phase) { |
755 |
< |
ForkJoinTask<?> t = ForkJoinWorkerThread.pollTask(); |
756 |
< |
if (t != null) { |
757 |
< |
if ((p = phaseOf(state.get())) == phase) |
758 |
< |
t.exec(); |
759 |
< |
else { // push task and exit if barrier advanced |
760 |
< |
t.fork(); |
761 |
< |
break; |
762 |
< |
} |
744 |
> |
static final class QNode implements ForkJoinPool.ManagedBlocker { |
745 |
> |
final Phaser phaser; |
746 |
> |
final int phase; |
747 |
> |
final long startTime; |
748 |
> |
final long nanos; |
749 |
> |
final boolean timed; |
750 |
> |
final boolean interruptible; |
751 |
> |
volatile boolean wasInterrupted = false; |
752 |
> |
volatile Thread thread; // nulled to cancel wait |
753 |
> |
QNode next; |
754 |
> |
QNode(Phaser phaser, int phase, boolean interruptible, |
755 |
> |
boolean timed, long startTime, long nanos) { |
756 |
> |
this.phaser = phaser; |
757 |
> |
this.phase = phase; |
758 |
> |
this.timed = timed; |
759 |
> |
this.interruptible = interruptible; |
760 |
> |
this.startTime = startTime; |
761 |
> |
this.nanos = nanos; |
762 |
> |
thread = Thread.currentThread(); |
763 |
> |
} |
764 |
> |
public boolean isReleasable() { |
765 |
> |
return (thread == null || |
766 |
> |
phaser.getPhase() != phase || |
767 |
> |
(interruptible && wasInterrupted) || |
768 |
> |
(timed && (nanos - (System.nanoTime() - startTime)) <= 0)); |
769 |
> |
} |
770 |
> |
public boolean block() { |
771 |
> |
if (Thread.interrupted()) { |
772 |
> |
wasInterrupted = true; |
773 |
> |
if (interruptible) |
774 |
> |
return true; |
775 |
> |
} |
776 |
> |
if (!timed) |
777 |
> |
LockSupport.park(this); |
778 |
> |
else { |
779 |
> |
long waitTime = nanos - (System.nanoTime() - startTime); |
780 |
> |
if (waitTime <= 0) |
781 |
> |
return true; |
782 |
> |
LockSupport.parkNanos(this, waitTime); |
783 |
|
} |
784 |
+ |
return isReleasable(); |
785 |
|
} |
786 |
< |
return p; |
787 |
< |
} |
506 |
< |
|
507 |
< |
private int timedHelpingWait(int phase, long nanos) throws TimeoutException { |
508 |
< |
final AtomicLong state = this.state; |
509 |
< |
long lastTime = System.nanoTime(); |
510 |
< |
int p; |
511 |
< |
while ((p = phaseOf(state.get())) == phase) { |
512 |
< |
long now = System.nanoTime(); |
513 |
< |
nanos -= now - lastTime; |
514 |
< |
lastTime = now; |
515 |
< |
if (nanos <= 0) { |
516 |
< |
if ((p = phaseOf(state.get())) == phase) |
517 |
< |
throw new TimeoutException(); |
518 |
< |
else |
519 |
< |
break; |
520 |
< |
} |
521 |
< |
ForkJoinTask<?> t = ForkJoinWorkerThread.pollTask(); |
786 |
> |
void signal() { |
787 |
> |
Thread t = thread; |
788 |
|
if (t != null) { |
789 |
< |
if ((p = phaseOf(state.get())) == phase) |
790 |
< |
t.exec(); |
525 |
< |
else { // push task and exit if barrier advanced |
526 |
< |
t.fork(); |
527 |
< |
break; |
528 |
< |
} |
789 |
> |
thread = null; |
790 |
> |
LockSupport.unpark(t); |
791 |
|
} |
792 |
|
} |
793 |
< |
return p; |
794 |
< |
} |
795 |
< |
|
796 |
< |
/** |
797 |
< |
* Wait nodes for Treiber stack representing wait queue for non-FJ |
798 |
< |
* tasks. The waiting scheme is an adaptation of the one used in |
537 |
< |
* forkjoin.PoolBarrier. |
538 |
< |
*/ |
539 |
< |
static final class QNode { |
540 |
< |
QNode next; |
541 |
< |
volatile Thread thread; // nulled to cancel wait |
542 |
< |
final int phase; |
543 |
< |
QNode(Thread t, int c) { |
544 |
< |
thread = t; |
545 |
< |
phase = c; |
546 |
< |
} |
547 |
< |
} |
548 |
< |
|
549 |
< |
private void releaseWaiters(int currentPhase) { |
550 |
< |
final AtomicReference<QNode> head = this.head; |
551 |
< |
QNode p; |
552 |
< |
while ((p = head.get()) != null && p.phase != currentPhase) { |
553 |
< |
if (head.compareAndSet(p, null)) { |
554 |
< |
do { |
555 |
< |
Thread t = p.thread; |
556 |
< |
if (t != null) { |
557 |
< |
p.thread = null; |
558 |
< |
LockSupport.unpark(t); |
559 |
< |
} |
560 |
< |
} while ((p = p.next) != null); |
793 |
> |
boolean doWait() { |
794 |
> |
if (thread != null) { |
795 |
> |
try { |
796 |
> |
ForkJoinPool.managedBlock(this, false); |
797 |
> |
} catch (InterruptedException ie) { |
798 |
> |
} |
799 |
|
} |
800 |
+ |
return wasInterrupted; |
801 |
|
} |
563 |
– |
} |
802 |
|
|
803 |
< |
/** The number of CPUs, for spin control */ |
566 |
< |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
803 |
> |
} |
804 |
|
|
805 |
|
/** |
806 |
< |
* The number of times to spin before blocking in timed waits. |
570 |
< |
* The value is empirically derived. |
806 |
> |
* Removes and signals waiting threads from wait queue |
807 |
|
*/ |
808 |
< |
static final int maxTimedSpins = (NCPUS < 2)? 0 : 32; |
808 |
> |
private void releaseWaiters(int phase) { |
809 |
> |
AtomicReference<QNode> head = queueFor(phase); |
810 |
> |
QNode q; |
811 |
> |
while ((q = head.get()) != null) { |
812 |
> |
if (head.compareAndSet(q, q.next)) |
813 |
> |
q.signal(); |
814 |
> |
} |
815 |
> |
} |
816 |
|
|
817 |
|
/** |
818 |
< |
* The number of times to spin before blocking in untimed waits. |
819 |
< |
* This is greater than timed value because untimed waits spin |
577 |
< |
* faster since they don't need to check times on each spin. |
818 |
> |
* Tries to enqueue given node in the appropriate wait queue |
819 |
> |
* @return true if successful |
820 |
|
*/ |
821 |
< |
static final int maxUntimedSpins = maxTimedSpins * 32; |
821 |
> |
private boolean tryEnqueue(QNode node) { |
822 |
> |
AtomicReference<QNode> head = queueFor(node.phase); |
823 |
> |
return head.compareAndSet(node.next = head.get(), node); |
824 |
> |
} |
825 |
|
|
826 |
|
/** |
827 |
< |
* The number of nanoseconds for which it is faster to spin |
828 |
< |
* rather than to use timed park. A rough estimate suffices. |
827 |
> |
* Enqueues node and waits unless aborted or signalled. |
828 |
> |
* @return current phase |
829 |
|
*/ |
830 |
< |
static final long spinForTimeoutThreshold = 1000L; |
830 |
> |
private int untimedWait(int phase) { |
831 |
> |
QNode node = null; |
832 |
> |
boolean queued = false; |
833 |
> |
boolean interrupted = false; |
834 |
> |
int p; |
835 |
> |
while ((p = getPhase()) == phase) { |
836 |
> |
if (Thread.interrupted()) |
837 |
> |
interrupted = true; |
838 |
> |
else if (node == null) |
839 |
> |
node = new QNode(this, phase, false, false, 0, 0); |
840 |
> |
else if (!queued) |
841 |
> |
queued = tryEnqueue(node); |
842 |
> |
else |
843 |
> |
interrupted = node.doWait(); |
844 |
> |
} |
845 |
> |
if (node != null) |
846 |
> |
node.thread = null; |
847 |
> |
releaseWaiters(phase); |
848 |
> |
if (interrupted) |
849 |
> |
Thread.currentThread().interrupt(); |
850 |
> |
return p; |
851 |
> |
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* Enqueues node and waits unless aborted or signalled. |
854 |
> |
* Interruptible version |
855 |
> |
* @return current phase |
856 |
|
*/ |
857 |
< |
private boolean untimedWait(Thread thread, int currentPhase, |
591 |
< |
boolean abortOnInterrupt) { |
592 |
< |
final AtomicReference<QNode> head = this.head; |
593 |
< |
final AtomicLong state = this.state; |
594 |
< |
boolean wasInterrupted = false; |
857 |
> |
private int interruptibleWait(int phase) throws InterruptedException { |
858 |
|
QNode node = null; |
859 |
|
boolean queued = false; |
860 |
< |
int spins = maxUntimedSpins; |
861 |
< |
while (phaseOf(state.get()) == currentPhase) { |
862 |
< |
QNode h; |
863 |
< |
if (node != null && queued) { |
864 |
< |
if (node.thread != null) { |
865 |
< |
LockSupport.park(); |
866 |
< |
if (Thread.interrupted()) { |
867 |
< |
wasInterrupted = true; |
868 |
< |
if (abortOnInterrupt) |
606 |
< |
break; |
607 |
< |
} |
608 |
< |
} |
609 |
< |
} |
610 |
< |
else if ((h = head.get()) != null && h.phase != currentPhase) { |
611 |
< |
if (phaseOf(state.get()) == currentPhase) { // must recheck |
612 |
< |
if (head.compareAndSet(h, h.next)) { |
613 |
< |
Thread t = h.thread; // help clear out old waiters |
614 |
< |
if (t != null) { |
615 |
< |
h.thread = null; |
616 |
< |
LockSupport.unpark(t); |
617 |
< |
} |
618 |
< |
} |
619 |
< |
} |
620 |
< |
else |
621 |
< |
break; |
622 |
< |
} |
623 |
< |
else if (node != null) |
624 |
< |
queued = head.compareAndSet(node.next = h, node); |
625 |
< |
else if (spins <= 0) |
626 |
< |
node = new QNode(thread, currentPhase); |
860 |
> |
boolean interrupted = false; |
861 |
> |
int p; |
862 |
> |
while ((p = getPhase()) == phase && !interrupted) { |
863 |
> |
if (Thread.interrupted()) |
864 |
> |
interrupted = true; |
865 |
> |
else if (node == null) |
866 |
> |
node = new QNode(this, phase, true, false, 0, 0); |
867 |
> |
else if (!queued) |
868 |
> |
queued = tryEnqueue(node); |
869 |
|
else |
870 |
< |
--spins; |
870 |
> |
interrupted = node.doWait(); |
871 |
|
} |
872 |
|
if (node != null) |
873 |
|
node.thread = null; |
874 |
< |
return wasInterrupted; |
874 |
> |
if (p != phase || (p = getPhase()) != phase) |
875 |
> |
releaseWaiters(phase); |
876 |
> |
if (interrupted) |
877 |
> |
throw new InterruptedException(); |
878 |
> |
return p; |
879 |
|
} |
880 |
|
|
881 |
|
/** |
882 |
< |
* Messier timeout version |
882 |
> |
* Timeout version. |
883 |
> |
* @return current phase |
884 |
|
*/ |
885 |
< |
private void timedWait(Thread thread, int currentPhase, long nanos) |
885 |
> |
private int timedWait(int phase, long nanos) |
886 |
|
throws InterruptedException, TimeoutException { |
887 |
< |
final AtomicReference<QNode> head = this.head; |
641 |
< |
final AtomicLong state = this.state; |
642 |
< |
long lastTime = System.nanoTime(); |
887 |
> |
long startTime = System.nanoTime(); |
888 |
|
QNode node = null; |
889 |
|
boolean queued = false; |
890 |
< |
int spins = maxTimedSpins; |
891 |
< |
while (phaseOf(state.get()) == currentPhase) { |
892 |
< |
QNode h; |
893 |
< |
long now = System.nanoTime(); |
894 |
< |
nanos -= now - lastTime; |
895 |
< |
lastTime = now; |
896 |
< |
if (nanos <= 0) { |
897 |
< |
if (node != null) |
898 |
< |
node.thread = null; |
899 |
< |
if (phaseOf(state.get()) == currentPhase) |
900 |
< |
throw new TimeoutException(); |
656 |
< |
else |
657 |
< |
break; |
658 |
< |
} |
659 |
< |
else if (node != null && queued) { |
660 |
< |
if (node.thread != null && |
661 |
< |
nanos > spinForTimeoutThreshold) { |
662 |
< |
// LockSupport.parkNanos(this, nanos); |
663 |
< |
LockSupport.parkNanos(nanos); |
664 |
< |
if (Thread.interrupted()) { |
665 |
< |
node.thread = null; |
666 |
< |
throw new InterruptedException(); |
667 |
< |
} |
668 |
< |
} |
669 |
< |
} |
670 |
< |
else if ((h = head.get()) != null && h.phase != currentPhase) { |
671 |
< |
if (phaseOf(state.get()) == currentPhase) { // must recheck |
672 |
< |
if (head.compareAndSet(h, h.next)) { |
673 |
< |
Thread t = h.thread; // help clear out old waiters |
674 |
< |
if (t != null) { |
675 |
< |
h.thread = null; |
676 |
< |
LockSupport.unpark(t); |
677 |
< |
} |
678 |
< |
} |
679 |
< |
} |
680 |
< |
else |
681 |
< |
break; |
682 |
< |
} |
683 |
< |
else if (node != null) |
684 |
< |
queued = head.compareAndSet(node.next = h, node); |
685 |
< |
else if (spins <= 0) |
686 |
< |
node = new QNode(thread, currentPhase); |
890 |
> |
boolean interrupted = false; |
891 |
> |
int p; |
892 |
> |
while ((p = getPhase()) == phase && !interrupted) { |
893 |
> |
if (Thread.interrupted()) |
894 |
> |
interrupted = true; |
895 |
> |
else if (nanos - (System.nanoTime() - startTime) <= 0) |
896 |
> |
break; |
897 |
> |
else if (node == null) |
898 |
> |
node = new QNode(this, phase, true, true, startTime, nanos); |
899 |
> |
else if (!queued) |
900 |
> |
queued = tryEnqueue(node); |
901 |
|
else |
902 |
< |
--spins; |
902 |
> |
interrupted = node.doWait(); |
903 |
|
} |
904 |
|
if (node != null) |
905 |
|
node.thread = null; |
906 |
+ |
if (p != phase || (p = getPhase()) != phase) |
907 |
+ |
releaseWaiters(phase); |
908 |
+ |
if (interrupted) |
909 |
+ |
throw new InterruptedException(); |
910 |
+ |
if (p == phase) |
911 |
+ |
throw new TimeoutException(); |
912 |
+ |
return p; |
913 |
|
} |
914 |
|
|
915 |
+ |
// Temporary Unsafe mechanics for preliminary release |
916 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
917 |
+ |
try { |
918 |
+ |
return Unsafe.getUnsafe(); |
919 |
+ |
} catch (SecurityException se) { |
920 |
+ |
try { |
921 |
+ |
return java.security.AccessController.doPrivileged |
922 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
923 |
+ |
public Unsafe run() throws Exception { |
924 |
+ |
return getUnsafePrivileged(); |
925 |
+ |
}}); |
926 |
+ |
} catch (java.security.PrivilegedActionException e) { |
927 |
+ |
throw e.getCause(); |
928 |
+ |
} |
929 |
+ |
} |
930 |
+ |
} |
931 |
+ |
|
932 |
+ |
private static Unsafe getUnsafePrivileged() |
933 |
+ |
throws NoSuchFieldException, IllegalAccessException { |
934 |
+ |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
935 |
+ |
f.setAccessible(true); |
936 |
+ |
return (Unsafe)f.get(null); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
private static long fieldOffset(String fieldName) |
940 |
+ |
throws NoSuchFieldException { |
941 |
+ |
return _unsafe.objectFieldOffset |
942 |
+ |
(Phaser.class.getDeclaredField(fieldName)); |
943 |
+ |
} |
944 |
+ |
|
945 |
+ |
static final Unsafe _unsafe; |
946 |
+ |
static final long stateOffset; |
947 |
+ |
|
948 |
+ |
static { |
949 |
+ |
try { |
950 |
+ |
_unsafe = getUnsafe(); |
951 |
+ |
stateOffset = fieldOffset("state"); |
952 |
+ |
} catch (Exception e) { |
953 |
+ |
throw new RuntimeException("Could not initialize intrinsics", e); |
954 |
+ |
} |
955 |
+ |
} |
956 |
+ |
|
957 |
+ |
final boolean casState(long cmp, long val) { |
958 |
+ |
return _unsafe.compareAndSwapLong(this, stateOffset, cmp, val); |
959 |
+ |
} |
960 |
|
} |