2003/04/20 06:29:50
[org.ibex.core.git] / src / org / xwt / ThreadMessage.java
index 558c49d..bbd1f24 100644 (file)
@@ -38,7 +38,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 void newthread(Function f) {
+    public static synchronized void newthread(Function f) {
         ThreadMessage ret = (ThreadMessage)spare.remove(false);
         if (ret == null) {
             if (threadcount < Platform.maxThreads()) ret = new ThreadMessage();
@@ -50,6 +50,30 @@ public class ThreadMessage extends Thread implements Message {
         ret.f = f;
         MessageQueue.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() {
+        // put ourselves in the background
+        Thread thread = Thread.currentThread();
+        if (!(thread instanceof ThreadMessage)) {
+            Context cx = Context.enter();
+            if (Log.on) Log.log(ThreadMessage.class, "attempt to perform background-only operation in a foreground thread at " +
+                                cx.interpreterSourceFile + ":" + cx.interpreterLine);
+            return false;
+        }
+        ThreadMessage mythread = (ThreadMessage)thread;
+        mythread.setPriority(Thread.MIN_PRIORITY);
+        mythread.done.release();
+        return true;
+    }
+
+    /** re-enqueues this thread */
+    public static void resumeThread() {
+        ThreadMessage mythread = (ThreadMessage)Thread.currentThread();
+        MessageQueue.add(mythread);
+        mythread.setPriority(Thread.NORM_PRIORITY);
+        mythread.go.block();        
+    }
     
     public void run() {
         try {