+ /**
+ * Spinlock for the primary and secondary event handlers.
+ * WidgetEventHandler.run() is responsible for setting this value.
+ */
+ private volatile boolean insideHandleEvent = false;
+
+ /**
+ * Set the insideHandleEvent flag to true. lockoutEventHandlers() will
+ * spin indefinitely until unlockHandleEvent() is called.
+ *
+ * @return the old value of insideHandleEvent
+ */
+ private boolean lockHandleEvent() {
+ if (debugThreads) {
+ System.err.printf(" >> lockHandleEvent(): oldValue %s",
+ insideHandleEvent);
+ }
+ boolean oldValue = true;
+
+ synchronized (this) {
+ // Wait for TApplication.run() to finish using the global state
+ // before allowing further event processing.
+ while (lockoutHandleEvent == true);
+
+ oldValue = insideHandleEvent;
+ insideHandleEvent = true;
+ }
+
+ if (debugThreads) {
+ System.err.printf(" ***\n");
+ }
+ return oldValue;
+ }
+
+ /**
+ * Set the insideHandleEvent flag to false. lockoutEventHandlers() will
+ * spin indefinitely until unlockHandleEvent() is called.
+ *
+ * @return the old value of insideHandleEvent
+ */
+ private boolean unlockHandleEvent() {
+ if (debugThreads) {
+ System.err.printf(" << unlockHandleEvent(): oldValue %s\n",
+ insideHandleEvent);
+ }
+ synchronized (this) {
+ boolean oldValue = insideHandleEvent;
+ insideHandleEvent = false;
+ return oldValue;
+ }
+ }
+
+ /**
+ * Spinlock for the primary and secondary event handlers. When true, the
+ * event handlers will spinlock wait before calling handleEvent().
+ */
+ private volatile boolean lockoutHandleEvent = false;
+
+ /**
+ * TApplication.run() needs to be able rely on the global data structures
+ * being intact when calling doIdle() and drawAll(). Tell the event
+ * handlers to wait for an unlock before handling their events.
+ */
+ private void stopEventHandlers() {
+ if (debugThreads) {
+ System.err.printf(">> stopEventHandlers()");
+ }
+
+ lockoutHandleEvent = true;
+ // Wait for the last event to finish processing before returning
+ // control to TApplication.run().
+ while (insideHandleEvent == true);
+
+ if (debugThreads) {
+ System.err.printf(" XXX\n");
+ }
+ }
+
+ /**
+ * TApplication.run() needs to be able rely on the global data structures
+ * being intact when calling doIdle() and drawAll(). Tell the event
+ * handlers that it is now OK to handle their events.
+ */
+ private void startEventHandlers() {
+ if (debugThreads) {
+ System.err.printf("<< startEventHandlers()\n");
+ }
+ lockoutHandleEvent = false;
+ }
+