2003/10/28 10:10:17
[org.ibex.core.git] / src / org / xwt / ThreadMessage.java
index f4f95a9..cfd6cb7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
 import java.util.*;
@@ -16,16 +16,18 @@ import org.xwt.js.*;
  *             this invariant.
  */
 public class ThreadMessage extends Thread implements Message {
+
+    public static boolean fakeBackground = false;
     
     private volatile static int threadcount = 0;
 
     /** the JavaScript function that we are executing */
-    volatile JS.Function f;
+    volatile JS.Callable f;
 
     /** the ThreadMessage thread blocks on this before executing any JavaScript */
     Semaphore go = new Semaphore();
 
-    /** The MessageQueue (main) thread blocks on this while the ThreadMessage thread is running JavaScript code */
+    /** The Message.Q (main) thread blocks on this while the ThreadMessage thread is running JavaScript code */
     Semaphore done = new Semaphore();
 
     /** used to pool ThreadMessages that are not in use */
@@ -38,7 +40,7 @@ public class ThreadMessage extends Thread implements Message {
     private static Object[] emptyobj = new Object[] { };
 
     /** creates a new thread to execute function <tt>f</tt> */
-    public static synchronized void newthread(JS.Function f) {
+    public static synchronized void newthread(JS.Callable f) {
         ThreadMessage ret = (ThreadMessage)spare.remove(false);
         if (ret == null) {
             if (threadcount < Platform.maxThreads()) ret = new ThreadMessage();
@@ -48,15 +50,16 @@ public class ThreadMessage extends Thread implements Message {
             }
         }
         ret.f = f;
-        MessageQueue.add(ret);
+        Message.Q.add(ret);
     }
 
     /** attempts to put this thread into the background to perform a blocking operation; returns false if unable to do so */
     public static boolean suspendThread() {
+        if (fakeBackground) return true;
         // put ourselves in the background
         Thread thread = Thread.currentThread();
         if (!(thread instanceof ThreadMessage)) {
-            if (Log.on) Log.log(ThreadMessage.class, "attempt to perform background-only operation in a foreground thread at " + JS.getCurrentFunctionSourceName());
+            if (Log.on) Log.logJS(ThreadMessage.class, "attempt to perform background-only operation in a foreground thread");
             return false;
         }
         ThreadMessage mythread = (ThreadMessage)thread;
@@ -67,8 +70,9 @@ public class ThreadMessage extends Thread implements Message {
 
     /** re-enqueues this thread */
     public static void resumeThread() {
+        if (fakeBackground) return;
         ThreadMessage mythread = (ThreadMessage)Thread.currentThread();
-        MessageQueue.add(mythread);
+        Message.Q.add(mythread);
         mythread.setPriority(Thread.NORM_PRIORITY);
         mythread.go.block();        
     }
@@ -86,8 +90,8 @@ public class ThreadMessage extends Thread implements Message {
                 done.release();
                 synchronized(waiting) {
                     if (waiting.size() > 0) {
-                        f = (JS.Function)waiting.remove(false);
-                        MessageQueue.add(this);
+                        f = (JS.Callable)waiting.remove(false);
+                        Message.Q.add(this);
                     } else if (spare.size() < 10) {
                         spare.append(this);
                     } else {
@@ -104,7 +108,7 @@ public class ThreadMessage extends Thread implements Message {
         }
     }
 
-    /** this is invoked in the MessageQueue thread */
+    /** this is invoked in the Message.Q thread */
     public void perform() {
         go.release();
         done.block();