001: /*
002: * TclEvent.java --
003: *
004: * Abstract class for describing an event in the Tcl notifier
005: * API.
006: *
007: * Copyright (c) 1997 Cornell University.
008: * Copyright (c) 1997 Sun Microsystems, Inc.
009: *
010: * See the file "license.terms" for information on usage and
011: * redistribution of this file, and for a DISCLAIMER OF ALL
012: * WARRANTIES.
013: *
014: * RCS: @(#) $Id: TclEvent.java,v 1.3 2003/03/11 01:45:53 mdejong Exp $
015: *
016: */
017:
018: package tcl.lang;
019:
020: /*
021: * This is an abstract class that describes an event in the Jacl
022: * implementation of the notifier. It contains package protected
023: * fields and methods that are accessed by the Jacl notifier. Tcl Blend
024: * needs a different implementation of the TclEvent base class.
025: *
026: * The only public methods in this class are processEvent() and
027: * sync(). These methods must appear in both the Jacl and Tcl Blend versions
028: * of this class.
029: */
030:
031: public abstract class TclEvent {
032:
033: /*
034: * The notifier in which this event is queued.
035: */
036:
037: Notifier notifier = null;
038:
039: /*
040: * This flag is true if sync() has been called on this object.
041: */
042:
043: boolean needsNotify = false;
044:
045: /*
046: * True if this event is current being processing. This flag provents
047: * an event to be processed twice when the event loop is entered
048: * recursively.
049: */
050:
051: boolean isProcessing = false;
052:
053: /*
054: * True if this event has been processed.
055: */
056:
057: boolean isProcessed = false;
058:
059: /*
060: * Links to the next event in the event queue.
061: */
062:
063: TclEvent next;
064:
065: /*
066: *----------------------------------------------------------------------
067: *
068: * processEvent --
069: *
070: * Process the event. Override this method to implement new types
071: * of events.
072: *
073: * Note: this method is called by the primary thread of the
074: * notifier.
075: *
076: * Results:
077: * 1 means the event has been processed and can be removed from
078: * the event queue. 0 means the event should be deferred for
079: * processing later.
080: *
081: * Side effects:
082: * This method may have arbitrary side effects while processing
083: * the event.
084: *
085: *----------------------------------------------------------------------
086: */
087:
088: public abstract int processEvent(int flags); // Same as flags passed to Notifier.doOneEvent.
089:
090: /*
091: *----------------------------------------------------------------------
092: *
093: * sync --
094: *
095: * Wait until the event is processed.
096: *
097: * Results:
098: * None.
099: *
100: * Side effects:
101: * Arbitrary things may happen while this thread is waiting.
102: *
103: *----------------------------------------------------------------------
104: */
105:
106: public final void sync() {
107: if (notifier == null) {
108: throw new TclRuntimeError(
109: "TclEvent is not queued when sync() is called");
110: }
111:
112: if (Thread.currentThread() == notifier.primaryThread) {
113: while (!isProcessed) {
114: notifier.serviceEvent(0);
115: }
116: } else {
117: synchronized (this ) {
118: needsNotify = true;
119: while (!isProcessed) {
120: try {
121: wait(0);
122: } catch (InterruptedException e) {
123: // Another thread has sent us an "interrupt"
124: // signal. We ignore it and continue waiting until
125: // the event is processed.
126:
127: continue;
128: }
129: }
130: }
131: }
132: }
133:
134: } // end TclEvent
|