author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 12308 | 48d57c925c0d |
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 |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
5506 | 18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
4110 | 21 |
*/ |
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7976
diff
changeset
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 32 |
*/ |
33 |
||
34 |
/* |
|
35 |
* @test |
|
36 |
* @bug 6445158 |
|
37 |
* @summary Basic tests for Phaser |
|
38 |
* @author Chris Hegarty |
|
39 |
*/ |
|
40 |
||
41 |
import java.util.Iterator; |
|
42 |
import java.util.LinkedList; |
|
43 |
import java.util.concurrent.Phaser; |
|
44 |
import java.util.concurrent.TimeUnit; |
|
45 |
import java.util.concurrent.TimeoutException; |
|
46 |
import java.util.concurrent.atomic.AtomicInteger; |
|
47 |
import static java.util.concurrent.TimeUnit.*; |
|
48 |
||
49 |
public class Basic { |
|
50 |
||
51 |
private static void checkTerminated(final Phaser phaser) { |
|
52 |
check(phaser.isTerminated()); |
|
53 |
int unarriverParties = phaser.getUnarrivedParties(); |
|
54 |
int registeredParties = phaser.getRegisteredParties(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
55 |
int phase = phaser.getPhase(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
56 |
check(phase < 0); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
57 |
equal(phase, phaser.arrive()); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
58 |
equal(phase, phaser.arriveAndDeregister()); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
59 |
equal(phase, phaser.arriveAndAwaitAdvance()); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
60 |
equal(phase, phaser.bulkRegister(10)); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
61 |
equal(phase, phaser.register()); |
4110 | 62 |
try { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
63 |
equal(phase, phaser.awaitAdvanceInterruptibly(0)); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
64 |
equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS)); |
4110 | 65 |
} catch (Exception ie) { |
66 |
unexpected(ie); |
|
67 |
} |
|
68 |
equal(phaser.getUnarrivedParties(), unarriverParties); |
|
69 |
equal(phaser.getRegisteredParties(), registeredParties); |
|
70 |
} |
|
71 |
||
72 |
private static void checkResult(Arriver a, Class<? extends Throwable> c) { |
|
73 |
Throwable t = a.result(); |
|
74 |
if (! ((t == null && c == null) || (c != null && c.isInstance(t)))) { |
|
75 |
// t.printStackTrace(); |
|
76 |
fail("Mismatch in thread " + |
|
77 |
a.getName() + ": " + |
|
78 |
t + ", " + |
|
79 |
(c == null ? "<null>" : c.getName())); |
|
80 |
} else { |
|
81 |
pass(); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
//---------------------------------------------------------------- |
|
86 |
// Mechanism to get all test threads into "running" mode. |
|
87 |
//---------------------------------------------------------------- |
|
88 |
private static Phaser atTheStartingGate = new Phaser(3); |
|
89 |
||
90 |
private static void toTheStartingGate() { |
|
91 |
try { |
|
92 |
boolean expectNextPhase = false; |
|
93 |
if (atTheStartingGate.getUnarrivedParties() == 1) { |
|
94 |
expectNextPhase = true; |
|
95 |
} |
|
96 |
int phase = atTheStartingGate.getPhase(); |
|
97 |
equal(phase, atTheStartingGate.arrive()); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
98 |
int awaitPhase = atTheStartingGate.awaitAdvanceInterruptibly |
12308
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
99 |
(phase, 30, SECONDS); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
100 |
if (expectNextPhase) check(awaitPhase == (phase + 1)); |
4110 | 101 |
|
102 |
pass(); |
|
103 |
} catch (Throwable t) { |
|
104 |
unexpected(t); |
|
105 |
// reset(atTheStartingGate); |
|
106 |
throw new Error(t); |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
//---------------------------------------------------------------- |
|
111 |
// Convenience methods for creating threads that call arrive, |
|
112 |
// awaitAdvance, arriveAndAwaitAdvance, awaitAdvanceInterruptibly |
|
113 |
//---------------------------------------------------------------- |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
12308
diff
changeset
|
114 |
private abstract static class Arriver extends Thread { |
4110 | 115 |
static AtomicInteger count = new AtomicInteger(1); |
116 |
||
117 |
Arriver() { |
|
118 |
this("Arriver"); |
|
119 |
} |
|
120 |
||
121 |
Arriver(String name) { |
|
122 |
this.setName(name + ":" + count.getAndIncrement()); |
|
123 |
this.setDaemon(true); |
|
124 |
} |
|
125 |
||
126 |
private volatile Throwable result; |
|
127 |
private volatile int phase; |
|
128 |
protected void result(Throwable result) { this.result = result; } |
|
129 |
public Throwable result() { return this.result; } |
|
130 |
protected void phase(int phase) { this.phase = phase; } |
|
131 |
public int phase() { return this.phase; } |
|
132 |
} |
|
133 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
12308
diff
changeset
|
134 |
private abstract static class Awaiter extends Arriver { |
4110 | 135 |
Awaiter() { super("Awaiter"); } |
136 |
Awaiter(String name) { super(name); } |
|
137 |
} |
|
138 |
||
139 |
private static Arriver arriver(final Phaser phaser) { |
|
140 |
return new Arriver() { public void run() { |
|
141 |
toTheStartingGate(); |
|
142 |
||
143 |
try { phase(phaser.arrive()); } |
|
144 |
catch (Throwable result) { result(result); }}}; |
|
145 |
} |
|
146 |
||
147 |
private static AtomicInteger cycleArriveAwaitAdvance = new AtomicInteger(1); |
|
148 |
||
149 |
private static Awaiter awaiter(final Phaser phaser) { |
|
150 |
return new Awaiter() { public void run() { |
|
151 |
toTheStartingGate(); |
|
152 |
||
153 |
try { |
|
154 |
if (cycleArriveAwaitAdvance.getAndIncrement() % 2 == 0) |
|
155 |
phase(phaser.awaitAdvance(phaser.arrive())); |
|
156 |
else |
|
157 |
phase(phaser.arriveAndAwaitAdvance()); |
|
158 |
} catch (Throwable result) { result(result); }}}; |
|
159 |
} |
|
160 |
||
161 |
private static Awaiter awaiter(final Phaser phaser, |
|
162 |
final long timeout, |
|
163 |
final TimeUnit unit) { |
|
164 |
return new Awaiter("InterruptibleWaiter") { public void run() { |
|
165 |
toTheStartingGate(); |
|
166 |
||
167 |
try { |
|
168 |
if (timeout < 0) |
|
169 |
phase(phaser.awaitAdvanceInterruptibly(phaser.arrive())); |
|
170 |
else |
|
171 |
phase(phaser.awaitAdvanceInterruptibly(phaser.arrive(), |
|
172 |
timeout, |
|
173 |
unit)); |
|
174 |
} catch (Throwable result) { result(result); }}}; |
|
175 |
} |
|
176 |
||
177 |
// Returns an infinite lazy list of all possible arriver/awaiter combinations. |
|
178 |
private static Iterator<Arriver> arriverIterator(final Phaser phaser) { |
|
179 |
return new Iterator<Arriver>() { |
|
180 |
int i = 0; |
|
181 |
public boolean hasNext() { return true; } |
|
182 |
public Arriver next() { |
|
183 |
switch ((i++)&7) { |
|
184 |
case 0: case 4: |
|
185 |
return arriver(phaser); |
|
186 |
case 1: case 5: |
|
187 |
return awaiter(phaser); |
|
188 |
case 2: case 6: case 7: |
|
189 |
return awaiter(phaser, -1, SECONDS); |
|
190 |
default: |
|
12308
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
191 |
return awaiter(phaser, 30, SECONDS); }} |
4110 | 192 |
public void remove() {throw new UnsupportedOperationException();}}; |
193 |
} |
|
194 |
||
195 |
// Returns an infinite lazy list of all possible awaiter only combinations. |
|
196 |
private static Iterator<Awaiter> awaiterIterator(final Phaser phaser) { |
|
197 |
return new Iterator<Awaiter>() { |
|
198 |
int i = 0; |
|
199 |
public boolean hasNext() { return true; } |
|
200 |
public Awaiter next() { |
|
201 |
switch ((i++)&7) { |
|
202 |
case 1: case 4: case 7: |
|
203 |
return awaiter(phaser); |
|
204 |
case 2: case 5: |
|
205 |
return awaiter(phaser, -1, SECONDS); |
|
206 |
default: |
|
12308
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
207 |
return awaiter(phaser, 30, SECONDS); }} |
4110 | 208 |
public void remove() {throw new UnsupportedOperationException();}}; |
209 |
} |
|
210 |
||
211 |
private static void realMain(String[] args) throws Throwable { |
|
212 |
||
213 |
Thread.currentThread().setName("mainThread"); |
|
214 |
||
215 |
//---------------------------------------------------------------- |
|
216 |
// Normal use |
|
217 |
//---------------------------------------------------------------- |
|
218 |
try { |
|
219 |
Phaser phaser = new Phaser(3); |
|
220 |
equal(phaser.getRegisteredParties(), 3); |
|
221 |
equal(phaser.getArrivedParties(), 0); |
|
222 |
equal(phaser.getPhase(), 0); |
|
223 |
check(phaser.getRoot().equals(phaser)); |
|
224 |
equal(phaser.getParent(), null); |
|
225 |
check(!phaser.isTerminated()); |
|
226 |
||
227 |
Iterator<Arriver> arrivers = arriverIterator(phaser); |
|
228 |
int phase = 0; |
|
229 |
for (int i = 0; i < 10; i++) { |
|
230 |
equal(phaser.getPhase(), phase++); |
|
231 |
Arriver a1 = arrivers.next(); a1.start(); |
|
232 |
Arriver a2 = arrivers.next(); a2.start(); |
|
233 |
toTheStartingGate(); |
|
234 |
phaser.arriveAndAwaitAdvance(); |
|
235 |
a1.join(); |
|
236 |
a2.join(); |
|
237 |
checkResult(a1, null); |
|
238 |
checkResult(a2, null); |
|
239 |
check(!phaser.isTerminated()); |
|
240 |
equal(phaser.getRegisteredParties(), 3); |
|
241 |
equal(phaser.getArrivedParties(), 0); |
|
242 |
} |
|
243 |
} catch (Throwable t) { unexpected(t); } |
|
244 |
||
245 |
//---------------------------------------------------------------- |
|
246 |
// One thread interrupted |
|
247 |
//---------------------------------------------------------------- |
|
248 |
try { |
|
249 |
Phaser phaser = new Phaser(3); |
|
250 |
Iterator<Arriver> arrivers = arriverIterator(phaser); |
|
251 |
int phase = phaser.getPhase(); |
|
252 |
for (int i = 0; i < 4; i++) { |
|
253 |
check(phaser.getPhase() == phase); |
|
12308
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
254 |
Awaiter a1 = awaiter(phaser, 30, SECONDS); a1.start(); |
4110 | 255 |
Arriver a2 = arrivers.next(); a2.start(); |
256 |
toTheStartingGate(); |
|
12308
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
257 |
// allow a1 to block in awaitAdvanceInterruptibly |
48d57c925c0d
6963841: java/util/concurrent/Phaser/Basic.java fails intermittently
chegar
parents:
9242
diff
changeset
|
258 |
Thread.sleep(2000); |
4110 | 259 |
a1.interrupt(); |
260 |
a1.join(); |
|
261 |
phaser.arriveAndAwaitAdvance(); |
|
262 |
a2.join(); |
|
263 |
checkResult(a1, InterruptedException.class); |
|
264 |
checkResult(a2, null); |
|
265 |
check(!phaser.isTerminated()); |
|
266 |
equal(phaser.getRegisteredParties(), 3); |
|
267 |
equal(phaser.getArrivedParties(), 0); |
|
268 |
phase++; |
|
269 |
} |
|
270 |
} catch (Throwable t) { unexpected(t); } |
|
271 |
||
272 |
//---------------------------------------------------------------- |
|
273 |
// Phaser is terminated while threads are waiting |
|
274 |
//---------------------------------------------------------------- |
|
275 |
try { |
|
276 |
for (int i = 0; i < 4; i++) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
277 |
Phaser phaser = new Phaser(3); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
278 |
Iterator<Awaiter> awaiters = awaiterIterator(phaser); |
4110 | 279 |
Arriver a1 = awaiters.next(); a1.start(); |
280 |
Arriver a2 = awaiters.next(); a2.start(); |
|
281 |
toTheStartingGate(); |
|
282 |
while (phaser.getArrivedParties() < 2) Thread.yield(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
283 |
equal(0, phaser.getPhase()); |
4110 | 284 |
phaser.forceTermination(); |
285 |
a1.join(); |
|
286 |
a2.join(); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
287 |
equal(0 + Integer.MIN_VALUE, a1.phase); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
5506
diff
changeset
|
288 |
equal(0 + Integer.MIN_VALUE, a2.phase); |
4110 | 289 |
int arrivedParties = phaser.getArrivedParties(); |
290 |
checkTerminated(phaser); |
|
291 |
equal(phaser.getArrivedParties(), arrivedParties); |
|
292 |
} |
|
293 |
} catch (Throwable t) { unexpected(t); } |
|
294 |
||
295 |
//---------------------------------------------------------------- |
|
296 |
// Adds new unarrived parties to this phaser |
|
297 |
//---------------------------------------------------------------- |
|
298 |
try { |
|
299 |
Phaser phaser = new Phaser(1); |
|
300 |
Iterator<Arriver> arrivers = arriverIterator(phaser); |
|
301 |
LinkedList<Arriver> arriverList = new LinkedList<Arriver>(); |
|
302 |
int phase = phaser.getPhase(); |
|
303 |
for (int i = 1; i < 5; i++) { |
|
304 |
atTheStartingGate = new Phaser(1+(3*i)); |
|
305 |
check(phaser.getPhase() == phase); |
|
306 |
// register 3 more |
|
307 |
phaser.register(); phaser.register(); phaser.register(); |
|
308 |
for (int z=0; z<(3*i); z++) { |
|
309 |
arriverList.add(arrivers.next()); |
|
310 |
} |
|
311 |
for (Arriver arriver : arriverList) |
|
312 |
arriver.start(); |
|
313 |
||
314 |
toTheStartingGate(); |
|
315 |
phaser.arriveAndAwaitAdvance(); |
|
316 |
||
317 |
for (Arriver arriver : arriverList) { |
|
318 |
arriver.join(); |
|
319 |
checkResult(arriver, null); |
|
320 |
} |
|
321 |
equal(phaser.getRegisteredParties(), 1 + (3*i)); |
|
322 |
equal(phaser.getArrivedParties(), 0); |
|
323 |
arriverList.clear(); |
|
324 |
phase++; |
|
325 |
} |
|
326 |
atTheStartingGate = new Phaser(3); |
|
327 |
} catch (Throwable t) { unexpected(t); } |
|
328 |
||
329 |
//---------------------------------------------------------------- |
|
330 |
// One thread timed out |
|
331 |
//---------------------------------------------------------------- |
|
332 |
try { |
|
333 |
Phaser phaser = new Phaser(3); |
|
334 |
Iterator<Arriver> arrivers = arriverIterator(phaser); |
|
335 |
for (long timeout : new long[] { 0L, 5L }) { |
|
336 |
for (int i = 0; i < 2; i++) { |
|
337 |
Awaiter a1 = awaiter(phaser, timeout, SECONDS); a1.start(); |
|
338 |
Arriver a2 = arrivers.next(); a2.start(); |
|
339 |
toTheStartingGate(); |
|
340 |
a1.join(); |
|
341 |
checkResult(a1, TimeoutException.class); |
|
342 |
phaser.arrive(); |
|
343 |
a2.join(); |
|
344 |
checkResult(a2, null); |
|
345 |
check(!phaser.isTerminated()); |
|
346 |
} |
|
347 |
} |
|
348 |
} catch (Throwable t) { unexpected(t); } |
|
349 |
||
350 |
//---------------------------------------------------------------- |
|
351 |
// Barrier action completed normally |
|
352 |
//---------------------------------------------------------------- |
|
353 |
try { |
|
354 |
final AtomicInteger count = new AtomicInteger(0); |
|
355 |
final Phaser[] kludge = new Phaser[1]; |
|
356 |
Phaser phaser = new Phaser(3) { |
|
357 |
@Override |
|
358 |
protected boolean onAdvance(int phase, int registeredParties) { |
|
359 |
int countPhase = count.getAndIncrement(); |
|
360 |
equal(countPhase, phase); |
|
361 |
equal(kludge[0].getPhase(), phase); |
|
362 |
equal(kludge[0].getRegisteredParties(), registeredParties); |
|
363 |
if (phase >= 3) |
|
364 |
return true; // terminate |
|
365 |
||
366 |
return false; |
|
367 |
} |
|
368 |
}; |
|
369 |
kludge[0] = phaser; |
|
370 |
equal(phaser.getRegisteredParties(), 3); |
|
371 |
Iterator<Awaiter> awaiters = awaiterIterator(phaser); |
|
372 |
for (int i = 0; i < 4; i++) { |
|
373 |
Awaiter a1 = awaiters.next(); a1.start(); |
|
374 |
Awaiter a2 = awaiters.next(); a2.start(); |
|
375 |
toTheStartingGate(); |
|
376 |
while (phaser.getArrivedParties() < 2) Thread.yield(); |
|
377 |
phaser.arrive(); |
|
378 |
a1.join(); |
|
379 |
a2.join(); |
|
380 |
checkResult(a1, null); |
|
381 |
checkResult(a2, null); |
|
382 |
equal(count.get(), i+1); |
|
383 |
if (i < 3) { |
|
384 |
check(!phaser.isTerminated()); |
|
385 |
equal(phaser.getRegisteredParties(), 3); |
|
386 |
equal(phaser.getArrivedParties(), 0); |
|
387 |
equal(phaser.getUnarrivedParties(), 3); |
|
388 |
equal(phaser.getPhase(), count.get()); |
|
389 |
} else |
|
390 |
checkTerminated(phaser); |
|
391 |
} |
|
392 |
} catch (Throwable t) { unexpected(t); } |
|
393 |
||
394 |
} |
|
395 |
||
396 |
//--------------------- Infrastructure --------------------------- |
|
397 |
static volatile int passed = 0, failed = 0; |
|
398 |
static void pass() {passed++;} |
|
399 |
static void fail() {failed++; Thread.dumpStack();} |
|
400 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
401 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
402 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
403 |
static void equal(Object x, Object y) { |
|
404 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
405 |
else fail(x + " not equal to " + y);} |
|
406 |
public static void main(String[] args) throws Throwable { |
|
407 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
408 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
409 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
410 |
} |