2004/01/13 03:59:35
[org.ibex.core.git] / src / org / xwt / Scheduler.java
index 95fd6bf..4dc85bf 100644 (file)
@@ -1,34 +1,96 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt;
 
 import java.util.*;
 import org.xwt.js.*;
 import org.xwt.util.*;
 
-// FEATURE: reimplement Watcher
 /** Implements cooperative multitasking */
 public class Scheduler {
 
-    private static Scheduler singleton = new Scheduler();
-    public static void run() { singleton.do_run(); }
-    protected Scheduler() { }
+    // FIXME: prepending events messes with keysate -- make a "no re-ordering" invariant?
 
-    public static abstract class Task implements Callback { public abstract Object call(Object o); }
+    // Public API Exposed to org.xwt /////////////////////////////////////////////////
 
-    private static Queue runnable = new Queue(50);
+    private static Scheduler singleton;
+    public static interface Task { public abstract void perform() throws Exception; }
 
+    /** adds a task to the back of the queue */
     public static void add(Task t) { singleton.runnable.append(t); }
-    public void do_run() {
+
+    /** adds a task to the front of the queue (guaranteed to run next) */
+    public static void addAtFront(Task t) { singleton.runnable.prepend(t); }
+
+    public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
+
+    private static Task currentTask = null;
+    public static Task current() { return currentTask; }
+
+    /** synchronizd so that we can safely call it from an event-delivery thread, in-context */
+    private static volatile boolean rendering = false;
+    private static volatile boolean again = false;
+    public static void renderAll() {
+        if (rendering) { again = true; return; }
+        synchronized(Scheduler.class) {
+            try {
+                rendering = true;
+                do {
+                    again = false;
+                    for(int i=0; i<Surface.allSurfaces.size(); i++) {
+                        Surface s = ((Surface)Surface.allSurfaces.elementAt(i));
+                        do { s.render(); } while(s.abort);
+                    }
+                } while(again);
+            } finally {
+                rendering = false;
+            }
+        }
+    }
+
+    
+
+    // API which must be supported by subclasses /////////////////////////////////////
+
+    /**
+     *  SCHEDULER INVARIANT: all scheduler implementations MUST invoke
+     *  Surface.renderAll() after performing a Task if no tasks remain
+     *  in the queue.  A scheduler may choose to invoke
+     *  Surface.renderAll() more often than that if it so chooses.
+     */
+    public void run() { defaultRun(); }
+    protected Scheduler() { }
+
+
+    // Default Implementation //////////////////////////////////////////////////////
+
+    protected static Queue runnable = new Queue(50);
+    public void defaultRun() {
         while(true) {
-            Task t = (Task)runnable.remove(true);
+            currentTask = (Task)runnable.remove(true);
             try {
-                t.call(null);
-                for(int i=0; i<Surface.allSurfaces.size(); i++)
-                    ((Surface)Surface.allSurfaces.elementAt(i)).render();
+                synchronized(this) {
+                    for(int i=0; i<Surface.allSurfaces.size(); i++) {
+                        Surface s = (Surface)Surface.allSurfaces.elementAt(i);
+                        if (currentTask instanceof JSFunction) {
+                            s._mousex = Integer.MAX_VALUE;
+                            s._mousey = Integer.MAX_VALUE;
+                        } else {
+                            s._mousex = s.mousex;
+                            s._mousey = s.mousey;
+                        }
+                    }
+                    currentTask.perform();
+                }
+                renderAll();
+            } catch (JSExn e) {
+                Log.info(Scheduler.class, "a JavaScript thread spawned with xwt.thread() threw an exception:");
+                Log.info(Scheduler.class, "JS Exception: " + e.getObject() + "\n" + e.backtrace());
+                Log.info(Scheduler.class,e);
             } catch (Exception e) {
-                Log.log(Scheduler.class, "Task threw an exception: " + e);
-                Log.log(Scheduler.class, e);
+                Log.info(Scheduler.class, "a Task threw an exception which was caught by the scheduler:");
+                Log.info(Scheduler.class, e);
             }
+            // if an Error is thrown it will cause the engine to quit
         }
     }
 }