001: package tests;
002:
003: import tcl.lang.*;
004:
005: // This class is designed to test the mutual exclusion approach
006: // of the tcl.lang.Notifier class. An event should be added to
007: // the queue right away, even if another time consuming event
008: // is being processed. This class is used in
009: // tcljava/TclEvent.test in test TclEvent-1.5.
010:
011: public class EventQueueLockThread extends Thread {
012: private Interp interpInOtherThread;
013:
014: public boolean slow_event_started = false;
015: public boolean slow_event_finished = false;
016: public boolean thread_finished = false;
017: public boolean test_passed = false;
018:
019: private static final boolean debug = false;
020:
021: public EventQueueLockThread(Interp i) {
022: interpInOtherThread = i;
023: }
024:
025: public void run() {
026: if (debug) {
027: System.out.println("running EventQueueLockThread");
028: }
029:
030: // In this new thread, queue an event that will take a
031: // long time to process, it will actually be processed
032: // in the original thread.
033:
034: TclEvent event = new TclEvent() {
035: public int processEvent(int flags) {
036: if (debug) {
037: System.out.println("started processing slow event");
038: }
039: slow_event_started = true;
040: try {
041: Thread.sleep(10000);
042: } catch (InterruptedException e) {
043: }
044: slow_event_finished = true;
045: if (debug) {
046: System.out
047: .println("finished processing slow event");
048: }
049: return 1;
050: }
051: };
052:
053: // Add the event to the thread safe event queue
054: if (debug) {
055: System.out.println("about to add slow event to queue");
056: }
057: interpInOtherThread.getNotifier().queueEvent(event,
058: TCL.QUEUE_TAIL);
059:
060: // Now wait around a bit to give the notifier a
061: // chance to start processing the slow event.
062:
063: try {
064: Thread.sleep(2000);
065: } catch (InterruptedException e) {
066: }
067:
068: if (slow_event_started == false) {
069: throw new RuntimeException("slow event was not started");
070: }
071: if (slow_event_finished == true) {
072: throw new RuntimeException(
073: "slow event was already finished");
074: }
075:
076: TclEvent other_event = new TclEvent() {
077: public int processEvent(int flags) {
078: if (debug) {
079: System.err.println("processed other event");
080: }
081: return 1;
082: }
083: };
084:
085: // This event should get added to the queue right away,
086: // it should not get queued after the slow event has
087: // finished running.
088:
089: if (debug) {
090: System.err.println("about to add other event to queue");
091: }
092: interpInOtherThread.getNotifier().queueEvent(other_event,
093: TCL.QUEUE_TAIL);
094: if (debug) {
095: System.err.println("added other event to queue");
096: }
097:
098: if (slow_event_finished == false) {
099: if (debug) {
100: System.err
101: .println("queueEvent returned before slow event finished");
102: }
103: test_passed = true;
104: } else {
105: if (debug) {
106: System.err
107: .println("slow event finished before queueEvent returned");
108: }
109: }
110:
111: thread_finished = true;
112:
113: if (debug) {
114: System.err.println("done running EventQueueLockThread");
115: }
116: }
117: }
|