001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.jpf.jvm;
020:
021: /**
022: * signal test (wait/notify)
023: */
024: public class TestWait {
025: boolean cond;
026: boolean done;
027:
028: public static void main(String[] args) {
029: TestWait t = new TestWait();
030:
031: if (args.length > 0) {
032: // just run the specified tests
033: for (int i = 0; i < args.length; i++) {
034: String func = args[i];
035:
036: // note that we don't use reflection here because this would
037: // blow up execution/test scope under JPF
038: if ("testSimpleWait".equals(func)) {
039: t.testSimpleWait();
040: } else if ("testLoopedWait".equals(func)) {
041: t.testLoopedWait();
042: } else if ("testInterruptedWait".equals(func)) {
043: t.testInterruptedWait();
044: } else {
045: throw new IllegalArgumentException(
046: "unknown test function");
047: }
048: }
049: } else {
050: // quite useless to call the others, it locks up
051: t.testSimpleWait();
052: t.testLoopedWait();
053: t.testInterruptedWait();
054: }
055: }
056:
057: public void testSimpleWait() {
058: Runnable notifier = new Runnable() {
059: public void run() {
060: synchronized (TestWait.this ) {
061: System.out.println("notifying");
062: cond = true;
063: TestWait.this .notify();
064: }
065: }
066: };
067:
068: Thread t = new Thread(notifier);
069:
070: cond = false;
071:
072: synchronized (this ) {
073: t.start();
074:
075: try {
076: System.out.println("waiting");
077: wait();
078: System.out.println("notified");
079: if (!cond) {
080: throw new RuntimeException(
081: "'cond' not set, premature wait return");
082: }
083: } catch (InterruptedException ix) {
084: }
085: }
086: }
087:
088: public void testLoopedWait() {
089: Runnable notifier = new Runnable() {
090: public void run() {
091: while (!done) {
092: synchronized (TestWait.this ) {
093: System.out.println("notifying");
094: cond = true;
095: TestWait.this .notify();
096: }
097: }
098: }
099: };
100:
101: Thread t = new Thread(notifier);
102:
103: cond = false;
104: done = false;
105:
106: t.start();
107: synchronized (this ) {
108: for (int i = 0; i < 5; i++) {
109: try {
110: System.out.println("waiting " + i);
111: wait();
112: System.out.println("notified " + i);
113: if (!cond) {
114: throw new RuntimeException(
115: "'cond' not set, premature wait return");
116: }
117: cond = false;
118: } catch (InterruptedException ix) {
119: }
120: }
121: done = true;
122: }
123: }
124:
125: public void testInterruptedWait() {
126: final Thread current = Thread.currentThread();
127:
128: Runnable notifier = new Runnable() {
129: public void run() {
130: synchronized (TestWait.this ) {
131: System.out.println("interrupting");
132: cond = true;
133: current.interrupt();
134: }
135: }
136: };
137: Thread t = new Thread(notifier);
138:
139: cond = false;
140:
141: synchronized (this ) {
142: t.start();
143:
144: try {
145: System.out.println("waiting");
146: wait();
147: System.out.println("notified");
148: throw new RuntimeException("notified, not interrupted");
149: } catch (InterruptedException ix) {
150: if (!cond) {
151: throw new RuntimeException(
152: "'cond' not set, premature wait return");
153: }
154: }
155: }
156: }
157: }
|