aboutsummaryrefslogtreecommitdiffstats
path: root/tests/new/NestedSyncWithResult.java
blob: 6acf814b5bb2be8ba256276846ecdeba4b8e8820 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import org.aspectj.testing.Tester;

/** @testcase PR#601 VerifyError if nested sync returning result */
public class NestedSyncWithResult {
	public static boolean holdA;
	public static boolean aWaiterDone;
	public static boolean aHolderDone;
	public static void main(String[] args) {
		int result = Bug.bug();
		Tester.check(0 == result, "0 == result");
		checkSynchronization();
		Tester.checkAllEvents();
	}

	public static void checkSynchronization() {
		final StringBuffer sb = new StringBuffer();
		Tester.expectEvent("holding A; releasing A; invoked bug; ");
		final boolean[] holdAstarted = new boolean[] { false };
		holdA = true;
		Runnable aHolder = new Runnable() {
			public void run() {
				boolean wroteWait = false;
				synchronized (Bug.lockB) {
					holdAstarted[0] = true;
					while (holdA) {
						if (!wroteWait) {
							wroteWait = true;
							sb.append("holding A; ");
						}
						sleep();
					}
					sb.append("releasing A; ");
				}
				aHolderDone = true;
			}
		};
		Runnable aWaiter = new Runnable() {
			public void run() {
				while (!holdAstarted[0]) {
					sleep();
				}
				Bug.bug();
				sb.append("invoked bug; ");
				aWaiterDone = true;
			}
		};
		new Thread(aHolder).start();
		new Thread(aWaiter).start();
		sleep();
		holdA = false;
		while (!aWaiterDone && !aHolderDone) {
			sleep();
		}
		Tester.event(sb.toString());
		//System.err.println("got: " + sb.toString());
	}

	public static void sleep() {
		try {
			Thread.currentThread().sleep(300);
		} catch (InterruptedException e) {
		} // ignore
	}
}

class Bug {
	public static Object lockA = new Object();
	public static Object lockB = new Object();

	static int bug() {
		synchronized (lockA) {
			synchronized (lockB) {
				return 0;
			}
		}
	}
}