author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
4110 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
4110 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8544
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
38 |
import java.util.concurrent.TimeUnit; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
39 |
import java.util.concurrent.TimeoutException; |
4110 | 40 |
import java.util.concurrent.atomic.AtomicReference; |
41 |
import java.util.concurrent.locks.LockSupport; |
|
42 |
||
43 |
/** |
|
44 |
* A reusable synchronization barrier, similar in functionality to |
|
45 |
* {@link java.util.concurrent.CyclicBarrier CyclicBarrier} and |
|
46 |
* {@link java.util.concurrent.CountDownLatch CountDownLatch} |
|
47 |
* but supporting more flexible usage. |
|
48 |
* |
|
18768 | 49 |
* <p><b>Registration.</b> Unlike the case for other barriers, the |
4110 | 50 |
* number of parties <em>registered</em> to synchronize on a phaser |
51 |
* may vary over time. Tasks may be registered at any time (using |
|
52 |
* methods {@link #register}, {@link #bulkRegister}, or forms of |
|
53 |
* constructors establishing initial numbers of parties), and |
|
54 |
* optionally deregistered upon any arrival (using {@link |
|
55 |
* #arriveAndDeregister}). As is the case with most basic |
|
56 |
* synchronization constructs, registration and deregistration affect |
|
57 |
* only internal counts; they do not establish any further internal |
|
58 |
* bookkeeping, so tasks cannot query whether they are registered. |
|
59 |
* (However, you can introduce such bookkeeping by subclassing this |
|
60 |
* class.) |
|
61 |
* |
|
18768 | 62 |
* <p><b>Synchronization.</b> Like a {@code CyclicBarrier}, a {@code |
4110 | 63 |
* Phaser} may be repeatedly awaited. Method {@link |
64 |
* #arriveAndAwaitAdvance} has effect analogous to {@link |
|
65 |
* java.util.concurrent.CyclicBarrier#await CyclicBarrier.await}. Each |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
66 |
* generation of a phaser has an associated phase number. The phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
67 |
* number starts at zero, and advances when all parties arrive at the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
68 |
* phaser, wrapping around to zero after reaching {@code |
4110 | 69 |
* Integer.MAX_VALUE}. The use of phase numbers enables independent |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
70 |
* control of actions upon arrival at a phaser and upon awaiting |
4110 | 71 |
* others, via two kinds of methods that may be invoked by any |
72 |
* registered party: |
|
73 |
* |
|
74 |
* <ul> |
|
75 |
* |
|
76 |
* <li> <b>Arrival.</b> Methods {@link #arrive} and |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
77 |
* {@link #arriveAndDeregister} record arrival. These methods |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
78 |
* do not block, but return an associated <em>arrival phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
79 |
* number</em>; that is, the phase number of the phaser to which |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
80 |
* the arrival applied. When the final party for a given phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
81 |
* arrives, an optional action is performed and the phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
82 |
* advances. These actions are performed by the party |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
83 |
* triggering a phase advance, and are arranged by overriding |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
84 |
* method {@link #onAdvance(int, int)}, which also controls |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
85 |
* termination. Overriding this method is similar to, but more |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
86 |
* flexible than, providing a barrier action to a {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
87 |
* CyclicBarrier}. |
4110 | 88 |
* |
89 |
* <li> <b>Waiting.</b> Method {@link #awaitAdvance} requires an |
|
90 |
* argument indicating an arrival phase number, and returns when |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
91 |
* the phaser advances to (or is already at) a different phase. |
4110 | 92 |
* Unlike similar constructions using {@code CyclicBarrier}, |
93 |
* method {@code awaitAdvance} continues to wait even if the |
|
94 |
* waiting thread is interrupted. Interruptible and timeout |
|
95 |
* versions are also available, but exceptions encountered while |
|
96 |
* tasks wait interruptibly or with timeout do not change the |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
97 |
* state of the phaser. If necessary, you can perform any |
4110 | 98 |
* associated recovery within handlers of those exceptions, |
99 |
* often after invoking {@code forceTermination}. Phasers may |
|
100 |
* also be used by tasks executing in a {@link ForkJoinPool}, |
|
101 |
* which will ensure sufficient parallelism to execute tasks |
|
102 |
* when others are blocked waiting for a phase to advance. |
|
103 |
* |
|
104 |
* </ul> |
|
105 |
* |
|
18768 | 106 |
* <p><b>Termination.</b> A phaser may enter a <em>termination</em> |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
107 |
* state, that may be checked using method {@link #isTerminated}. Upon |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
108 |
* termination, all synchronization methods immediately return without |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
109 |
* waiting for advance, as indicated by a negative return value. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
110 |
* Similarly, attempts to register upon termination have no effect. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
111 |
* Termination is triggered when an invocation of {@code onAdvance} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
112 |
* returns {@code true}. The default implementation returns {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
113 |
* true} if a deregistration has caused the number of registered |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
114 |
* parties to become zero. As illustrated below, when phasers control |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
115 |
* actions with a fixed number of iterations, it is often convenient |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
116 |
* to override this method to cause termination when the current phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
117 |
* number reaches a threshold. Method {@link #forceTermination} is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
118 |
* also available to abruptly release waiting threads and allow them |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
119 |
* to terminate. |
4110 | 120 |
* |
18768 | 121 |
* <p><b>Tiering.</b> Phasers may be <em>tiered</em> (i.e., |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
122 |
* constructed in tree structures) to reduce contention. Phasers with |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
123 |
* large numbers of parties that would otherwise experience heavy |
4110 | 124 |
* synchronization contention costs may instead be set up so that |
125 |
* groups of sub-phasers share a common parent. This may greatly |
|
126 |
* increase throughput even though it incurs greater per-operation |
|
127 |
* overhead. |
|
128 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
129 |
* <p>In a tree of tiered phasers, registration and deregistration of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
130 |
* child phasers with their parent are managed automatically. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
131 |
* Whenever the number of registered parties of a child phaser becomes |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
132 |
* non-zero (as established in the {@link #Phaser(Phaser,int)} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
133 |
* constructor, {@link #register}, or {@link #bulkRegister}), the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
134 |
* child phaser is registered with its parent. Whenever the number of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
135 |
* registered parties becomes zero as the result of an invocation of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
136 |
* {@link #arriveAndDeregister}, the child phaser is deregistered |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
137 |
* from its parent. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
138 |
* |
4110 | 139 |
* <p><b>Monitoring.</b> While synchronization methods may be invoked |
140 |
* only by registered parties, the current state of a phaser may be |
|
141 |
* monitored by any caller. At any given moment there are {@link |
|
142 |
* #getRegisteredParties} parties in total, of which {@link |
|
143 |
* #getArrivedParties} have arrived at the current phase ({@link |
|
144 |
* #getPhase}). When the remaining ({@link #getUnarrivedParties}) |
|
145 |
* parties arrive, the phase advances. The values returned by these |
|
146 |
* methods may reflect transient states and so are not in general |
|
147 |
* useful for synchronization control. Method {@link #toString} |
|
148 |
* returns snapshots of these state queries in a form convenient for |
|
149 |
* informal monitoring. |
|
150 |
* |
|
151 |
* <p><b>Sample usages:</b> |
|
152 |
* |
|
153 |
* <p>A {@code Phaser} may be used instead of a {@code CountDownLatch} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
154 |
* to control a one-shot action serving a variable number of parties. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
155 |
* The typical idiom is for the method setting this up to first |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
156 |
* register, then start the actions, then deregister, as in: |
4110 | 157 |
* |
158 |
* <pre> {@code |
|
159 |
* void runTasks(List<Runnable> tasks) { |
|
160 |
* final Phaser phaser = new Phaser(1); // "1" to register self |
|
161 |
* // create and start threads |
|
9697 | 162 |
* for (final Runnable task : tasks) { |
4110 | 163 |
* phaser.register(); |
164 |
* new Thread() { |
|
165 |
* public void run() { |
|
166 |
* phaser.arriveAndAwaitAdvance(); // await all creation |
|
167 |
* task.run(); |
|
168 |
* } |
|
169 |
* }.start(); |
|
170 |
* } |
|
171 |
* |
|
172 |
* // allow threads to start and deregister self |
|
173 |
* phaser.arriveAndDeregister(); |
|
174 |
* }}</pre> |
|
175 |
* |
|
176 |
* <p>One way to cause a set of threads to repeatedly perform actions |
|
177 |
* for a given number of iterations is to override {@code onAdvance}: |
|
178 |
* |
|
179 |
* <pre> {@code |
|
180 |
* void startTasks(List<Runnable> tasks, final int iterations) { |
|
181 |
* final Phaser phaser = new Phaser() { |
|
182 |
* protected boolean onAdvance(int phase, int registeredParties) { |
|
183 |
* return phase >= iterations || registeredParties == 0; |
|
184 |
* } |
|
185 |
* }; |
|
186 |
* phaser.register(); |
|
187 |
* for (final Runnable task : tasks) { |
|
188 |
* phaser.register(); |
|
189 |
* new Thread() { |
|
190 |
* public void run() { |
|
191 |
* do { |
|
192 |
* task.run(); |
|
193 |
* phaser.arriveAndAwaitAdvance(); |
|
194 |
* } while (!phaser.isTerminated()); |
|
195 |
* } |
|
196 |
* }.start(); |
|
197 |
* } |
|
198 |
* phaser.arriveAndDeregister(); // deregister self, don't wait |
|
199 |
* }}</pre> |
|
200 |
* |
|
201 |
* If the main task must later await termination, it |
|
202 |
* may re-register and then execute a similar loop: |
|
203 |
* <pre> {@code |
|
204 |
* // ... |
|
205 |
* phaser.register(); |
|
206 |
* while (!phaser.isTerminated()) |
|
207 |
* phaser.arriveAndAwaitAdvance();}</pre> |
|
208 |
* |
|
209 |
* <p>Related constructions may be used to await particular phase numbers |
|
210 |
* in contexts where you are sure that the phase will never wrap around |
|
211 |
* {@code Integer.MAX_VALUE}. For example: |
|
212 |
* |
|
213 |
* <pre> {@code |
|
214 |
* void awaitPhase(Phaser phaser, int phase) { |
|
215 |
* int p = phaser.register(); // assumes caller not already registered |
|
216 |
* while (p < phase) { |
|
217 |
* if (phaser.isTerminated()) |
|
218 |
* // ... deal with unexpected termination |
|
219 |
* else |
|
220 |
* p = phaser.arriveAndAwaitAdvance(); |
|
221 |
* } |
|
222 |
* phaser.arriveAndDeregister(); |
|
223 |
* }}</pre> |
|
224 |
* |
|
225 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
226 |
* <p>To create a set of {@code n} tasks using a tree of phasers, you |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
227 |
* could use code of the following form, assuming a Task class with a |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
228 |
* constructor accepting a {@code Phaser} that it registers with upon |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
229 |
* construction. After invocation of {@code build(new Task[n], 0, n, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
230 |
* new Phaser())}, these tasks could then be started, for example by |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
231 |
* submitting to a pool: |
4110 | 232 |
* |
233 |
* <pre> {@code |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
234 |
* void build(Task[] tasks, int lo, int hi, Phaser ph) { |
4110 | 235 |
* if (hi - lo > TASKS_PER_PHASER) { |
236 |
* for (int i = lo; i < hi; i += TASKS_PER_PHASER) { |
|
237 |
* int j = Math.min(i + TASKS_PER_PHASER, hi); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
238 |
* build(tasks, i, j, new Phaser(ph)); |
4110 | 239 |
* } |
240 |
* } else { |
|
241 |
* for (int i = lo; i < hi; ++i) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
242 |
* tasks[i] = new Task(ph); |
4110 | 243 |
* // assumes new Task(ph) performs ph.register() |
244 |
* } |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
245 |
* }}</pre> |
4110 | 246 |
* |
247 |
* The best value of {@code TASKS_PER_PHASER} depends mainly on |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
248 |
* expected synchronization rates. A value as low as four may |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
249 |
* be appropriate for extremely small per-phase task bodies (thus |
4110 | 250 |
* high rates), or up to hundreds for extremely large ones. |
251 |
* |
|
252 |
* <p><b>Implementation notes</b>: This implementation restricts the |
|
253 |
* maximum number of parties to 65535. Attempts to register additional |
|
254 |
* parties result in {@code IllegalStateException}. However, you can and |
|
255 |
* should create tiered phasers to accommodate arbitrarily large sets |
|
256 |
* of participants. |
|
257 |
* |
|
258 |
* @since 1.7 |
|
259 |
* @author Doug Lea |
|
260 |
*/ |
|
261 |
public class Phaser { |
|
262 |
/* |
|
263 |
* This class implements an extension of X10 "clocks". Thanks to |
|
264 |
* Vijay Saraswat for the idea, and to Vivek Sarkar for |
|
265 |
* enhancements to extend functionality. |
|
266 |
*/ |
|
267 |
||
268 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
269 |
* Primary state representation, holding four bit-fields: |
4110 | 270 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
271 |
* unarrived -- the number of parties yet to hit barrier (bits 0-15) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
272 |
* parties -- the number of parties to wait (bits 16-31) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
273 |
* phase -- the generation of the barrier (bits 32-62) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
274 |
* terminated -- set if barrier is terminated (bit 63 / sign) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
275 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
276 |
* Except that a phaser with no registered parties is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
277 |
* distinguished by the otherwise illegal state of having zero |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
278 |
* parties and one unarrived parties (encoded as EMPTY below). |
4110 | 279 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
280 |
* To efficiently maintain atomicity, these values are packed into |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
281 |
* a single (atomic) long. Good performance relies on keeping |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
282 |
* state decoding and encoding simple, and keeping race windows |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
283 |
* short. |
4110 | 284 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
285 |
* All state updates are performed via CAS except initial |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
286 |
* registration of a sub-phaser (i.e., one with a non-null |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
287 |
* parent). In this (relatively rare) case, we use built-in |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
288 |
* synchronization to lock while first registering with its |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
289 |
* parent. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
290 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
291 |
* The phase of a subphaser is allowed to lag that of its |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
292 |
* ancestors until it is actually accessed -- see method |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
293 |
* reconcileState. |
4110 | 294 |
*/ |
295 |
private volatile long state; |
|
296 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
297 |
private static final int MAX_PARTIES = 0xffff; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
298 |
private static final int MAX_PHASE = Integer.MAX_VALUE; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
299 |
private static final int PARTIES_SHIFT = 16; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
300 |
private static final int PHASE_SHIFT = 32; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
301 |
private static final int UNARRIVED_MASK = 0xffff; // to mask ints |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
302 |
private static final long PARTIES_MASK = 0xffff0000L; // to mask longs |
18768 | 303 |
private static final long COUNTS_MASK = 0xffffffffL; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
304 |
private static final long TERMINATION_BIT = 1L << 63; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
305 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
306 |
// some special values |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
307 |
private static final int ONE_ARRIVAL = 1; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
308 |
private static final int ONE_PARTY = 1 << PARTIES_SHIFT; |
18768 | 309 |
private static final int ONE_DEREGISTER = ONE_ARRIVAL|ONE_PARTY; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
310 |
private static final int EMPTY = 1; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
311 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
312 |
// The following unpacking methods are usually manually inlined |
4110 | 313 |
|
314 |
private static int unarrivedOf(long s) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
315 |
int counts = (int)s; |
18768 | 316 |
return (counts == EMPTY) ? 0 : (counts & UNARRIVED_MASK); |
4110 | 317 |
} |
318 |
||
319 |
private static int partiesOf(long s) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
320 |
return (int)s >>> PARTIES_SHIFT; |
4110 | 321 |
} |
322 |
||
323 |
private static int phaseOf(long s) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
324 |
return (int)(s >>> PHASE_SHIFT); |
4110 | 325 |
} |
326 |
||
327 |
private static int arrivedOf(long s) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
328 |
int counts = (int)s; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
329 |
return (counts == EMPTY) ? 0 : |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
330 |
(counts >>> PARTIES_SHIFT) - (counts & UNARRIVED_MASK); |
4110 | 331 |
} |
332 |
||
333 |
/** |
|
334 |
* The parent of this phaser, or null if none |
|
335 |
*/ |
|
336 |
private final Phaser parent; |
|
337 |
||
338 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
339 |
* The root of phaser tree. Equals this if not in a tree. |
4110 | 340 |
*/ |
341 |
private final Phaser root; |
|
342 |
||
343 |
/** |
|
344 |
* Heads of Treiber stacks for waiting threads. To eliminate |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
345 |
* contention when releasing some threads while adding others, we |
4110 | 346 |
* use two of them, alternating across even and odd phases. |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
347 |
* Subphasers share queues with root to speed up releases. |
4110 | 348 |
*/ |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
349 |
private final AtomicReference<QNode> evenQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
350 |
private final AtomicReference<QNode> oddQ; |
4110 | 351 |
|
352 |
private AtomicReference<QNode> queueFor(int phase) { |
|
353 |
return ((phase & 1) == 0) ? evenQ : oddQ; |
|
354 |
} |
|
355 |
||
356 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
357 |
* Returns message string for bounds exceptions on arrival. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
358 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
359 |
private String badArrive(long s) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
360 |
return "Attempted arrival of unregistered party for " + |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
361 |
stateToString(s); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
362 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
363 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
364 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
365 |
* Returns message string for bounds exceptions on registration. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
366 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
367 |
private String badRegister(long s) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
368 |
return "Attempt to register more than " + |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
369 |
MAX_PARTIES + " parties for " + stateToString(s); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
370 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
371 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
372 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
373 |
* Main implementation for methods arrive and arriveAndDeregister. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
374 |
* Manually tuned to speed up and minimize race windows for the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
375 |
* common case of just decrementing unarrived field. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
376 |
* |
18768 | 377 |
* @param adjust value to subtract from state; |
378 |
* ONE_ARRIVAL for arrive, |
|
379 |
* ONE_DEREGISTER for arriveAndDeregister |
|
4110 | 380 |
*/ |
18768 | 381 |
private int doArrive(int adjust) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
382 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
383 |
for (;;) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
384 |
long s = (root == this) ? state : reconcileState(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
385 |
int phase = (int)(s >>> PHASE_SHIFT); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
386 |
if (phase < 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
387 |
return phase; |
18768 | 388 |
int counts = (int)s; |
389 |
int unarrived = (counts == EMPTY) ? 0 : (counts & UNARRIVED_MASK); |
|
390 |
if (unarrived <= 0) |
|
391 |
throw new IllegalStateException(badArrive(s)); |
|
392 |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, s-=adjust)) { |
|
393 |
if (unarrived == 1) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
394 |
long n = s & PARTIES_MASK; // base of next state |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
395 |
int nextUnarrived = (int)n >>> PARTIES_SHIFT; |
18768 | 396 |
if (root == this) { |
397 |
if (onAdvance(phase, nextUnarrived)) |
|
398 |
n |= TERMINATION_BIT; |
|
399 |
else if (nextUnarrived == 0) |
|
400 |
n |= EMPTY; |
|
401 |
else |
|
402 |
n |= nextUnarrived; |
|
403 |
int nextPhase = (phase + 1) & MAX_PHASE; |
|
404 |
n |= (long)nextPhase << PHASE_SHIFT; |
|
405 |
UNSAFE.compareAndSwapLong(this, stateOffset, s, n); |
|
406 |
releaseWaiters(phase); |
|
407 |
} |
|
408 |
else if (nextUnarrived == 0) { // propagate deregistration |
|
409 |
phase = parent.doArrive(ONE_DEREGISTER); |
|
410 |
UNSAFE.compareAndSwapLong(this, stateOffset, |
|
411 |
s, s | EMPTY); |
|
412 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
413 |
else |
18768 | 414 |
phase = parent.doArrive(ONE_ARRIVAL); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
415 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
416 |
return phase; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
417 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
418 |
} |
4110 | 419 |
} |
420 |
||
421 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
422 |
* Implementation of register, bulkRegister |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
423 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
424 |
* @param registrations number to add to both parties and |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
425 |
* unarrived fields. Must be greater than zero. |
4110 | 426 |
*/ |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
427 |
private int doRegister(int registrations) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
428 |
// adjustment to state |
18768 | 429 |
long adjust = ((long)registrations << PARTIES_SHIFT) | registrations; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
430 |
final Phaser parent = this.parent; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
431 |
int phase; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
432 |
for (;;) { |
18768 | 433 |
long s = (parent == null) ? state : reconcileState(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
434 |
int counts = (int)s; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
435 |
int parties = counts >>> PARTIES_SHIFT; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
436 |
int unarrived = counts & UNARRIVED_MASK; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
437 |
if (registrations > MAX_PARTIES - parties) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
438 |
throw new IllegalStateException(badRegister(s)); |
18768 | 439 |
phase = (int)(s >>> PHASE_SHIFT); |
440 |
if (phase < 0) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
441 |
break; |
18768 | 442 |
if (counts != EMPTY) { // not 1st registration |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
443 |
if (parent == null || reconcileState() == s) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
444 |
if (unarrived == 0) // wait out advance |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
445 |
root.internalAwaitAdvance(phase, null); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
446 |
else if (UNSAFE.compareAndSwapLong(this, stateOffset, |
18768 | 447 |
s, s + adjust)) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
448 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
449 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
450 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
451 |
else if (parent == null) { // 1st root registration |
18768 | 452 |
long next = ((long)phase << PHASE_SHIFT) | adjust; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
453 |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, next)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
454 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
455 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
456 |
else { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
457 |
synchronized (this) { // 1st sub registration |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
458 |
if (state == s) { // recheck under lock |
18768 | 459 |
phase = parent.doRegister(1); |
460 |
if (phase < 0) |
|
461 |
break; |
|
462 |
// finish registration whenever parent registration |
|
463 |
// succeeded, even when racing with termination, |
|
464 |
// since these are part of the same "transaction". |
|
465 |
while (!UNSAFE.compareAndSwapLong |
|
466 |
(this, stateOffset, s, |
|
467 |
((long)phase << PHASE_SHIFT) | adjust)) { |
|
468 |
s = state; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
469 |
phase = (int)(root.state >>> PHASE_SHIFT); |
18768 | 470 |
// assert (int)s == EMPTY; |
471 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
472 |
break; |
4110 | 473 |
} |
474 |
} |
|
475 |
} |
|
476 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
477 |
return phase; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
478 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
479 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
480 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
481 |
* Resolves lagged phase propagation from root if necessary. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
482 |
* Reconciliation normally occurs when root has advanced but |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
483 |
* subphasers have not yet done so, in which case they must finish |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
484 |
* their own advance by setting unarrived to parties (or if |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
485 |
* parties is zero, resetting to unregistered EMPTY state). |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
486 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
487 |
* @return reconciled state |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
488 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
489 |
private long reconcileState() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
490 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
491 |
long s = state; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
492 |
if (root != this) { |
18768 | 493 |
int phase, p; |
494 |
// CAS to root phase with current parties, tripping unarrived |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
495 |
while ((phase = (int)(root.state >>> PHASE_SHIFT)) != |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
496 |
(int)(s >>> PHASE_SHIFT) && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
497 |
!UNSAFE.compareAndSwapLong |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
498 |
(this, stateOffset, s, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
499 |
s = (((long)phase << PHASE_SHIFT) | |
18768 | 500 |
((phase < 0) ? (s & COUNTS_MASK) : |
501 |
(((p = (int)s >>> PARTIES_SHIFT) == 0) ? EMPTY : |
|
502 |
((s & PARTIES_MASK) | p)))))) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
503 |
s = state; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
504 |
} |
4110 | 505 |
return s; |
506 |
} |
|
507 |
||
508 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
509 |
* Creates a new phaser with no initially registered parties, no |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
510 |
* parent, and initial phase number 0. Any thread using this |
4110 | 511 |
* phaser will need to first register for it. |
512 |
*/ |
|
513 |
public Phaser() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
514 |
this(null, 0); |
4110 | 515 |
} |
516 |
||
517 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
518 |
* Creates a new phaser with the given number of registered |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
519 |
* unarrived parties, no parent, and initial phase number 0. |
4110 | 520 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
521 |
* @param parties the number of parties required to advance to the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
522 |
* next phase |
4110 | 523 |
* @throws IllegalArgumentException if parties less than zero |
524 |
* or greater than the maximum number of parties supported |
|
525 |
*/ |
|
526 |
public Phaser(int parties) { |
|
527 |
this(null, parties); |
|
528 |
} |
|
529 |
||
530 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
531 |
* Equivalent to {@link #Phaser(Phaser, int) Phaser(parent, 0)}. |
4110 | 532 |
* |
533 |
* @param parent the parent phaser |
|
534 |
*/ |
|
535 |
public Phaser(Phaser parent) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
536 |
this(parent, 0); |
4110 | 537 |
} |
538 |
||
539 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
540 |
* Creates a new phaser with the given parent and number of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
541 |
* registered unarrived parties. When the given parent is non-null |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
542 |
* and the given number of parties is greater than zero, this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
543 |
* child phaser is registered with its parent. |
4110 | 544 |
* |
545 |
* @param parent the parent phaser |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
546 |
* @param parties the number of parties required to advance to the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
547 |
* next phase |
4110 | 548 |
* @throws IllegalArgumentException if parties less than zero |
549 |
* or greater than the maximum number of parties supported |
|
550 |
*/ |
|
551 |
public Phaser(Phaser parent, int parties) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
552 |
if (parties >>> PARTIES_SHIFT != 0) |
4110 | 553 |
throw new IllegalArgumentException("Illegal number of parties"); |
554 |
int phase = 0; |
|
555 |
this.parent = parent; |
|
556 |
if (parent != null) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
557 |
final Phaser root = parent.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
558 |
this.root = root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
559 |
this.evenQ = root.evenQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
560 |
this.oddQ = root.oddQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
561 |
if (parties != 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
562 |
phase = parent.doRegister(1); |
4110 | 563 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
564 |
else { |
4110 | 565 |
this.root = this; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
566 |
this.evenQ = new AtomicReference<QNode>(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
567 |
this.oddQ = new AtomicReference<QNode>(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
568 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
569 |
this.state = (parties == 0) ? (long)EMPTY : |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
570 |
((long)phase << PHASE_SHIFT) | |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
571 |
((long)parties << PARTIES_SHIFT) | |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
572 |
((long)parties); |
4110 | 573 |
} |
574 |
||
575 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
576 |
* Adds a new unarrived party to this phaser. If an ongoing |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
577 |
* invocation of {@link #onAdvance} is in progress, this method |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
578 |
* may await its completion before returning. If this phaser has |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
579 |
* a parent, and this phaser previously had no registered parties, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
580 |
* this child phaser is also registered with its parent. If |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
581 |
* this phaser is terminated, the attempt to register has |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
582 |
* no effect, and a negative value is returned. |
4110 | 583 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
584 |
* @return the arrival phase number to which this registration |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
585 |
* applied. If this value is negative, then this phaser has |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
586 |
* terminated, in which case registration has no effect. |
4110 | 587 |
* @throws IllegalStateException if attempting to register more |
588 |
* than the maximum supported number of parties |
|
589 |
*/ |
|
590 |
public int register() { |
|
591 |
return doRegister(1); |
|
592 |
} |
|
593 |
||
594 |
/** |
|
595 |
* Adds the given number of new unarrived parties to this phaser. |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
596 |
* If an ongoing invocation of {@link #onAdvance} is in progress, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
597 |
* this method may await its completion before returning. If this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
598 |
* phaser has a parent, and the given number of parties is greater |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
599 |
* than zero, and this phaser previously had no registered |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
600 |
* parties, this child phaser is also registered with its parent. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
601 |
* If this phaser is terminated, the attempt to register has no |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
602 |
* effect, and a negative value is returned. |
4110 | 603 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
604 |
* @param parties the number of additional parties required to |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
605 |
* advance to the next phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
606 |
* @return the arrival phase number to which this registration |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
607 |
* applied. If this value is negative, then this phaser has |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
608 |
* terminated, in which case registration has no effect. |
4110 | 609 |
* @throws IllegalStateException if attempting to register more |
610 |
* than the maximum supported number of parties |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
611 |
* @throws IllegalArgumentException if {@code parties < 0} |
4110 | 612 |
*/ |
613 |
public int bulkRegister(int parties) { |
|
614 |
if (parties < 0) |
|
615 |
throw new IllegalArgumentException(); |
|
616 |
if (parties == 0) |
|
617 |
return getPhase(); |
|
618 |
return doRegister(parties); |
|
619 |
} |
|
620 |
||
621 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
622 |
* Arrives at this phaser, without waiting for others to arrive. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
623 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
624 |
* <p>It is a usage error for an unregistered party to invoke this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
625 |
* method. However, this error may result in an {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
626 |
* IllegalStateException} only upon some subsequent operation on |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
627 |
* this phaser, if ever. |
4110 | 628 |
* |
629 |
* @return the arrival phase number, or a negative value if terminated |
|
630 |
* @throws IllegalStateException if not terminated and the number |
|
631 |
* of unarrived parties would become negative |
|
632 |
*/ |
|
633 |
public int arrive() { |
|
18768 | 634 |
return doArrive(ONE_ARRIVAL); |
4110 | 635 |
} |
636 |
||
637 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
638 |
* Arrives at this phaser and deregisters from it without waiting |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
639 |
* for others to arrive. Deregistration reduces the number of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
640 |
* parties required to advance in future phases. If this phaser |
4110 | 641 |
* has a parent, and deregistration causes this phaser to have |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
642 |
* zero parties, this phaser is also deregistered from its parent. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
643 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
644 |
* <p>It is a usage error for an unregistered party to invoke this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
645 |
* method. However, this error may result in an {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
646 |
* IllegalStateException} only upon some subsequent operation on |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
647 |
* this phaser, if ever. |
4110 | 648 |
* |
649 |
* @return the arrival phase number, or a negative value if terminated |
|
650 |
* @throws IllegalStateException if not terminated and the number |
|
651 |
* of registered or unarrived parties would become negative |
|
652 |
*/ |
|
653 |
public int arriveAndDeregister() { |
|
18768 | 654 |
return doArrive(ONE_DEREGISTER); |
4110 | 655 |
} |
656 |
||
657 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
658 |
* Arrives at this phaser and awaits others. Equivalent in effect |
4110 | 659 |
* to {@code awaitAdvance(arrive())}. If you need to await with |
660 |
* interruption or timeout, you can arrange this with an analogous |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
661 |
* construction using one of the other forms of the {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
662 |
* awaitAdvance} method. If instead you need to deregister upon |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
663 |
* arrival, use {@code awaitAdvance(arriveAndDeregister())}. |
4110 | 664 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
665 |
* <p>It is a usage error for an unregistered party to invoke this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
666 |
* method. However, this error may result in an {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
667 |
* IllegalStateException} only upon some subsequent operation on |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
668 |
* this phaser, if ever. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
669 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
670 |
* @return the arrival phase number, or the (negative) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
671 |
* {@linkplain #getPhase() current phase} if terminated |
4110 | 672 |
* @throws IllegalStateException if not terminated and the number |
673 |
* of unarrived parties would become negative |
|
674 |
*/ |
|
675 |
public int arriveAndAwaitAdvance() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
676 |
// Specialization of doArrive+awaitAdvance eliminating some reads/paths |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
677 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
678 |
for (;;) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
679 |
long s = (root == this) ? state : reconcileState(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
680 |
int phase = (int)(s >>> PHASE_SHIFT); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
681 |
if (phase < 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
682 |
return phase; |
18768 | 683 |
int counts = (int)s; |
684 |
int unarrived = (counts == EMPTY) ? 0 : (counts & UNARRIVED_MASK); |
|
685 |
if (unarrived <= 0) |
|
686 |
throw new IllegalStateException(badArrive(s)); |
|
687 |
if (UNSAFE.compareAndSwapLong(this, stateOffset, s, |
|
688 |
s -= ONE_ARRIVAL)) { |
|
689 |
if (unarrived > 1) |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
690 |
return root.internalAwaitAdvance(phase, null); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
691 |
if (root != this) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
692 |
return parent.arriveAndAwaitAdvance(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
693 |
long n = s & PARTIES_MASK; // base of next state |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
694 |
int nextUnarrived = (int)n >>> PARTIES_SHIFT; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
695 |
if (onAdvance(phase, nextUnarrived)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
696 |
n |= TERMINATION_BIT; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
697 |
else if (nextUnarrived == 0) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
698 |
n |= EMPTY; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
699 |
else |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
700 |
n |= nextUnarrived; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
701 |
int nextPhase = (phase + 1) & MAX_PHASE; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
702 |
n |= (long)nextPhase << PHASE_SHIFT; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
703 |
if (!UNSAFE.compareAndSwapLong(this, stateOffset, s, n)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
704 |
return (int)(state >>> PHASE_SHIFT); // terminated |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
705 |
releaseWaiters(phase); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
706 |
return nextPhase; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
707 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
708 |
} |
4110 | 709 |
} |
710 |
||
711 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
712 |
* Awaits the phase of this phaser to advance from the given phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
713 |
* value, returning immediately if the current phase is not equal |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
714 |
* to the given phase value or this phaser is terminated. |
4110 | 715 |
* |
716 |
* @param phase an arrival phase number, or negative value if |
|
717 |
* terminated; this argument is normally the value returned by a |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
718 |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
719 |
* @return the next arrival phase number, or the argument if it is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
720 |
* negative, or the (negative) {@linkplain #getPhase() current phase} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
721 |
* if terminated |
4110 | 722 |
*/ |
723 |
public int awaitAdvance(int phase) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
724 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
725 |
long s = (root == this) ? state : reconcileState(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
726 |
int p = (int)(s >>> PHASE_SHIFT); |
4110 | 727 |
if (phase < 0) |
728 |
return phase; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
729 |
if (p == phase) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
730 |
return root.internalAwaitAdvance(phase, null); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
731 |
return p; |
4110 | 732 |
} |
733 |
||
734 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
735 |
* Awaits the phase of this phaser to advance from the given phase |
4110 | 736 |
* value, throwing {@code InterruptedException} if interrupted |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
737 |
* while waiting, or returning immediately if the current phase is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
738 |
* not equal to the given phase value or this phaser is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
739 |
* terminated. |
4110 | 740 |
* |
741 |
* @param phase an arrival phase number, or negative value if |
|
742 |
* terminated; this argument is normally the value returned by a |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
743 |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
744 |
* @return the next arrival phase number, or the argument if it is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
745 |
* negative, or the (negative) {@linkplain #getPhase() current phase} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
746 |
* if terminated |
4110 | 747 |
* @throws InterruptedException if thread interrupted while waiting |
748 |
*/ |
|
749 |
public int awaitAdvanceInterruptibly(int phase) |
|
750 |
throws InterruptedException { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
751 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
752 |
long s = (root == this) ? state : reconcileState(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
753 |
int p = (int)(s >>> PHASE_SHIFT); |
4110 | 754 |
if (phase < 0) |
755 |
return phase; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
756 |
if (p == phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
757 |
QNode node = new QNode(this, phase, true, false, 0L); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
758 |
p = root.internalAwaitAdvance(phase, node); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
759 |
if (node.wasInterrupted) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
760 |
throw new InterruptedException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
761 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
762 |
return p; |
4110 | 763 |
} |
764 |
||
765 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
766 |
* Awaits the phase of this phaser to advance from the given phase |
4110 | 767 |
* value or the given timeout to elapse, throwing {@code |
768 |
* InterruptedException} if interrupted while waiting, or |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
769 |
* returning immediately if the current phase is not equal to the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
770 |
* given phase value or this phaser is terminated. |
4110 | 771 |
* |
772 |
* @param phase an arrival phase number, or negative value if |
|
773 |
* terminated; this argument is normally the value returned by a |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
774 |
* previous call to {@code arrive} or {@code arriveAndDeregister}. |
4110 | 775 |
* @param timeout how long to wait before giving up, in units of |
776 |
* {@code unit} |
|
777 |
* @param unit a {@code TimeUnit} determining how to interpret the |
|
778 |
* {@code timeout} parameter |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
779 |
* @return the next arrival phase number, or the argument if it is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
780 |
* negative, or the (negative) {@linkplain #getPhase() current phase} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
781 |
* if terminated |
4110 | 782 |
* @throws InterruptedException if thread interrupted while waiting |
783 |
* @throws TimeoutException if timed out while waiting |
|
784 |
*/ |
|
785 |
public int awaitAdvanceInterruptibly(int phase, |
|
786 |
long timeout, TimeUnit unit) |
|
787 |
throws InterruptedException, TimeoutException { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
788 |
long nanos = unit.toNanos(timeout); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
789 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
790 |
long s = (root == this) ? state : reconcileState(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
791 |
int p = (int)(s >>> PHASE_SHIFT); |
4110 | 792 |
if (phase < 0) |
793 |
return phase; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
794 |
if (p == phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
795 |
QNode node = new QNode(this, phase, true, true, nanos); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
796 |
p = root.internalAwaitAdvance(phase, node); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
797 |
if (node.wasInterrupted) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
798 |
throw new InterruptedException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
799 |
else if (p == phase) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
800 |
throw new TimeoutException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
801 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
802 |
return p; |
4110 | 803 |
} |
804 |
||
805 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
806 |
* Forces this phaser to enter termination state. Counts of |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
807 |
* registered parties are unaffected. If this phaser is a member |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
808 |
* of a tiered set of phasers, then all of the phasers in the set |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
809 |
* are terminated. If this phaser is already terminated, this |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
810 |
* method has no effect. This method may be useful for |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
811 |
* coordinating recovery after one or more tasks encounter |
4110 | 812 |
* unexpected exceptions. |
813 |
*/ |
|
814 |
public void forceTermination() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
815 |
// Only need to change root state |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
816 |
final Phaser root = this.root; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
817 |
long s; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
818 |
while ((s = root.state) >= 0) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
819 |
if (UNSAFE.compareAndSwapLong(root, stateOffset, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
820 |
s, s | TERMINATION_BIT)) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
821 |
// signal all threads |
18768 | 822 |
releaseWaiters(0); // Waiters on evenQ |
823 |
releaseWaiters(1); // Waiters on oddQ |
|
4110 | 824 |
return; |
825 |
} |
|
826 |
} |
|
827 |
} |
|
828 |
||
829 |
/** |
|
830 |
* Returns the current phase number. The maximum phase number is |
|
831 |
* {@code Integer.MAX_VALUE}, after which it restarts at |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
832 |
* zero. Upon termination, the phase number is negative, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
833 |
* in which case the prevailing phase prior to termination |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
834 |
* may be obtained via {@code getPhase() + Integer.MIN_VALUE}. |
4110 | 835 |
* |
836 |
* @return the phase number, or a negative value if terminated |
|
837 |
*/ |
|
838 |
public final int getPhase() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
839 |
return (int)(root.state >>> PHASE_SHIFT); |
4110 | 840 |
} |
841 |
||
842 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
843 |
* Returns the number of parties registered at this phaser. |
4110 | 844 |
* |
845 |
* @return the number of parties |
|
846 |
*/ |
|
847 |
public int getRegisteredParties() { |
|
848 |
return partiesOf(state); |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Returns the number of registered parties that have arrived at |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
853 |
* the current phase of this phaser. If this phaser has terminated, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
854 |
* the returned value is meaningless and arbitrary. |
4110 | 855 |
* |
856 |
* @return the number of arrived parties |
|
857 |
*/ |
|
858 |
public int getArrivedParties() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
859 |
return arrivedOf(reconcileState()); |
4110 | 860 |
} |
861 |
||
862 |
/** |
|
863 |
* Returns the number of registered parties that have not yet |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
864 |
* arrived at the current phase of this phaser. If this phaser has |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
865 |
* terminated, the returned value is meaningless and arbitrary. |
4110 | 866 |
* |
867 |
* @return the number of unarrived parties |
|
868 |
*/ |
|
869 |
public int getUnarrivedParties() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
870 |
return unarrivedOf(reconcileState()); |
4110 | 871 |
} |
872 |
||
873 |
/** |
|
874 |
* Returns the parent of this phaser, or {@code null} if none. |
|
875 |
* |
|
876 |
* @return the parent of this phaser, or {@code null} if none |
|
877 |
*/ |
|
878 |
public Phaser getParent() { |
|
879 |
return parent; |
|
880 |
} |
|
881 |
||
882 |
/** |
|
883 |
* Returns the root ancestor of this phaser, which is the same as |
|
884 |
* this phaser if it has no parent. |
|
885 |
* |
|
886 |
* @return the root ancestor of this phaser |
|
887 |
*/ |
|
888 |
public Phaser getRoot() { |
|
889 |
return root; |
|
890 |
} |
|
891 |
||
892 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
893 |
* Returns {@code true} if this phaser has been terminated. |
4110 | 894 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
895 |
* @return {@code true} if this phaser has been terminated |
4110 | 896 |
*/ |
897 |
public boolean isTerminated() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
898 |
return root.state < 0L; |
4110 | 899 |
} |
900 |
||
901 |
/** |
|
902 |
* Overridable method to perform an action upon impending phase |
|
903 |
* advance, and to control termination. This method is invoked |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
904 |
* upon arrival of the party advancing this phaser (when all other |
4110 | 905 |
* waiting parties are dormant). If this method returns {@code |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
906 |
* true}, this phaser will be set to a final termination state |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
907 |
* upon advance, and subsequent calls to {@link #isTerminated} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
908 |
* will return true. Any (unchecked) Exception or Error thrown by |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
909 |
* an invocation of this method is propagated to the party |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
910 |
* attempting to advance this phaser, in which case no advance |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
911 |
* occurs. |
4110 | 912 |
* |
913 |
* <p>The arguments to this method provide the state of the phaser |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
914 |
* prevailing for the current transition. The effects of invoking |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
915 |
* arrival, registration, and waiting methods on this phaser from |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
916 |
* within {@code onAdvance} are unspecified and should not be |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
917 |
* relied on. |
4110 | 918 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
919 |
* <p>If this phaser is a member of a tiered set of phasers, then |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
920 |
* {@code onAdvance} is invoked only for its root phaser on each |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
921 |
* advance. |
4110 | 922 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
923 |
* <p>To support the most common use cases, the default |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
924 |
* implementation of this method returns {@code true} when the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
925 |
* number of registered parties has become zero as the result of a |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
926 |
* party invoking {@code arriveAndDeregister}. You can disable |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
927 |
* this behavior, thus enabling continuation upon future |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
928 |
* registrations, by overriding this method to always return |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
929 |
* {@code false}: |
4110 | 930 |
* |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
931 |
* <pre> {@code |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
932 |
* Phaser phaser = new Phaser() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
933 |
* protected boolean onAdvance(int phase, int parties) { return false; } |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
934 |
* }}</pre> |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
935 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
936 |
* @param phase the current phase number on entry to this method, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
937 |
* before this phaser is advanced |
4110 | 938 |
* @param registeredParties the current number of registered parties |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
939 |
* @return {@code true} if this phaser should terminate |
4110 | 940 |
*/ |
941 |
protected boolean onAdvance(int phase, int registeredParties) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
942 |
return registeredParties == 0; |
4110 | 943 |
} |
944 |
||
945 |
/** |
|
946 |
* Returns a string identifying this phaser, as well as its |
|
947 |
* state. The state, in brackets, includes the String {@code |
|
948 |
* "phase = "} followed by the phase number, {@code "parties = "} |
|
949 |
* followed by the number of registered parties, and {@code |
|
950 |
* "arrived = "} followed by the number of arrived parties. |
|
951 |
* |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
952 |
* @return a string identifying this phaser, as well as its state |
4110 | 953 |
*/ |
954 |
public String toString() { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
955 |
return stateToString(reconcileState()); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
956 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
957 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
958 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
959 |
* Implementation of toString and string-based error messages |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
960 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
961 |
private String stateToString(long s) { |
4110 | 962 |
return super.toString() + |
963 |
"[phase = " + phaseOf(s) + |
|
964 |
" parties = " + partiesOf(s) + |
|
965 |
" arrived = " + arrivedOf(s) + "]"; |
|
966 |
} |
|
967 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
968 |
// Waiting mechanics |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
969 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
970 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
971 |
* Removes and signals threads from queue for phase. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
972 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
973 |
private void releaseWaiters(int phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
974 |
QNode q; // first element of queue |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
975 |
Thread t; // its thread |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
976 |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
977 |
while ((q = head.get()) != null && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
978 |
q.phase != (int)(root.state >>> PHASE_SHIFT)) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
979 |
if (head.compareAndSet(q, q.next) && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
980 |
(t = q.thread) != null) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
981 |
q.thread = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
982 |
LockSupport.unpark(t); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
983 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
984 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
985 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
986 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
987 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
988 |
* Variant of releaseWaiters that additionally tries to remove any |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
989 |
* nodes no longer waiting for advance due to timeout or |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
990 |
* interrupt. Currently, nodes are removed only if they are at |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
991 |
* head of queue, which suffices to reduce memory footprint in |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
992 |
* most usages. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
993 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
994 |
* @return current phase on exit |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
995 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
996 |
private int abortWait(int phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
997 |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
998 |
for (;;) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
999 |
Thread t; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1000 |
QNode q = head.get(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1001 |
int p = (int)(root.state >>> PHASE_SHIFT); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1002 |
if (q == null || ((t = q.thread) != null && q.phase == p)) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1003 |
return p; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1004 |
if (head.compareAndSet(q, q.next) && t != null) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1005 |
q.thread = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1006 |
LockSupport.unpark(t); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1007 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1008 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1009 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1010 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1011 |
/** The number of CPUs, for spin control */ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1012 |
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1013 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1014 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1015 |
* The number of times to spin before blocking while waiting for |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1016 |
* advance, per arrival while waiting. On multiprocessors, fully |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1017 |
* blocking and waking up a large number of threads all at once is |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1018 |
* usually a very slow process, so we use rechargeable spins to |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1019 |
* avoid it when threads regularly arrive: When a thread in |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1020 |
* internalAwaitAdvance notices another arrival before blocking, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1021 |
* and there appear to be enough CPUs available, it spins |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1022 |
* SPINS_PER_ARRIVAL more times before blocking. The value trades |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1023 |
* off good-citizenship vs big unnecessary slowdowns. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1024 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1025 |
static final int SPINS_PER_ARRIVAL = (NCPU < 2) ? 1 : 1 << 8; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1026 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1027 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1028 |
* Possibly blocks and waits for phase to advance unless aborted. |
18768 | 1029 |
* Call only on root phaser. |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1030 |
* |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1031 |
* @param phase current phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1032 |
* @param node if non-null, the wait node to track interrupt and timeout; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1033 |
* if null, denotes noninterruptible wait |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1034 |
* @return current phase |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1035 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1036 |
private int internalAwaitAdvance(int phase, QNode node) { |
18768 | 1037 |
// assert root == this; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1038 |
releaseWaiters(phase-1); // ensure old queue clean |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1039 |
boolean queued = false; // true when node is enqueued |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1040 |
int lastUnarrived = 0; // to increase spins upon change |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1041 |
int spins = SPINS_PER_ARRIVAL; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1042 |
long s; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1043 |
int p; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1044 |
while ((p = (int)((s = state) >>> PHASE_SHIFT)) == phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1045 |
if (node == null) { // spinning in noninterruptible mode |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1046 |
int unarrived = (int)s & UNARRIVED_MASK; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1047 |
if (unarrived != lastUnarrived && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1048 |
(lastUnarrived = unarrived) < NCPU) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1049 |
spins += SPINS_PER_ARRIVAL; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1050 |
boolean interrupted = Thread.interrupted(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1051 |
if (interrupted || --spins < 0) { // need node to record intr |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1052 |
node = new QNode(this, phase, false, false, 0L); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1053 |
node.wasInterrupted = interrupted; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1054 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1055 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1056 |
else if (node.isReleasable()) // done or aborted |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1057 |
break; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1058 |
else if (!queued) { // push onto queue |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1059 |
AtomicReference<QNode> head = (phase & 1) == 0 ? evenQ : oddQ; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1060 |
QNode q = node.next = head.get(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1061 |
if ((q == null || q.phase == phase) && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1062 |
(int)(state >>> PHASE_SHIFT) == phase) // avoid stale enq |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1063 |
queued = head.compareAndSet(q, node); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1064 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1065 |
else { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1066 |
try { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1067 |
ForkJoinPool.managedBlock(node); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1068 |
} catch (InterruptedException ie) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1069 |
node.wasInterrupted = true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1070 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1071 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1072 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1073 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1074 |
if (node != null) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1075 |
if (node.thread != null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1076 |
node.thread = null; // avoid need for unpark() |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1077 |
if (node.wasInterrupted && !node.interruptible) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1078 |
Thread.currentThread().interrupt(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1079 |
if (p == phase && (p = (int)(state >>> PHASE_SHIFT)) == phase) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1080 |
return abortWait(phase); // possibly clean up on abort |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1081 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1082 |
releaseWaiters(phase); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1083 |
return p; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1084 |
} |
4110 | 1085 |
|
1086 |
/** |
|
1087 |
* Wait nodes for Treiber stack representing wait queue |
|
1088 |
*/ |
|
1089 |
static final class QNode implements ForkJoinPool.ManagedBlocker { |
|
1090 |
final Phaser phaser; |
|
1091 |
final int phase; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1092 |
final boolean interruptible; |
4110 | 1093 |
final boolean timed; |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1094 |
boolean wasInterrupted; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1095 |
long nanos; |
18768 | 1096 |
final long deadline; |
4110 | 1097 |
volatile Thread thread; // nulled to cancel wait |
1098 |
QNode next; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1099 |
|
4110 | 1100 |
QNode(Phaser phaser, int phase, boolean interruptible, |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1101 |
boolean timed, long nanos) { |
4110 | 1102 |
this.phaser = phaser; |
1103 |
this.phase = phase; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1104 |
this.interruptible = interruptible; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1105 |
this.nanos = nanos; |
4110 | 1106 |
this.timed = timed; |
18768 | 1107 |
this.deadline = timed ? System.nanoTime() + nanos : 0L; |
4110 | 1108 |
thread = Thread.currentThread(); |
1109 |
} |
|
1110 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1111 |
public boolean isReleasable() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1112 |
if (thread == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1113 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1114 |
if (phaser.getPhase() != phase) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1115 |
thread = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1116 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1117 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1118 |
if (Thread.interrupted()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1119 |
wasInterrupted = true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1120 |
if (wasInterrupted && interruptible) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1121 |
thread = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1122 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1123 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1124 |
if (timed) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1125 |
if (nanos > 0L) { |
18768 | 1126 |
nanos = deadline - System.nanoTime(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1127 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1128 |
if (nanos <= 0L) { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1129 |
thread = null; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1130 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1131 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1132 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1133 |
return false; |
4110 | 1134 |
} |
1135 |
||
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1136 |
public boolean block() { |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1137 |
if (isReleasable()) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1138 |
return true; |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1139 |
else if (!timed) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1140 |
LockSupport.park(this); |
18768 | 1141 |
else if (nanos > 0L) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1142 |
LockSupport.parkNanos(this, nanos); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6543
diff
changeset
|
1143 |
return isReleasable(); |
4110 | 1144 |
} |
1145 |
} |
|
1146 |
||
1147 |
// Unsafe mechanics |
|
1148 |
||
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1149 |
private static final sun.misc.Unsafe UNSAFE; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1150 |
private static final long stateOffset; |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1151 |
static { |
4110 | 1152 |
try { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1153 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
11279 | 1154 |
Class<?> k = Phaser.class; |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1155 |
stateOffset = UNSAFE.objectFieldOffset |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1156 |
(k.getDeclaredField("state")); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1157 |
} catch (Exception e) { |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1158 |
throw new Error(e); |
4110 | 1159 |
} |
1160 |
} |
|
1161 |
} |