2004/01/12 06:38:30
[org.ibex.core.git] / src / org / xwt / Scheduler.java
index 55be6df..9ee8da2 100644 (file)
@@ -23,8 +23,31 @@ public class Scheduler {
 
     public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
 
-    private static Task current = null;
-    public static Task current() { return current; }
+    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 /////////////////////////////////////
 
@@ -43,26 +66,29 @@ public class Scheduler {
     protected static Queue runnable = new Queue(50);
     public void defaultRun() {
         while(true) {
-            current = (Task)runnable.remove(true);
+            currentTask = (Task)runnable.remove(true);
             try {
-                for(int i=0; i<Surface.allSurfaces.size(); i++) {
-                    Surface s = (Surface)Surface.allSurfaces.elementAt(i);
-                    if (current instanceof JSFunction) {
-                        s._mousex = Integer.MAX_VALUE;
-                        s._mousey = Integer.MAX_VALUE;
-                    } else {
-                        s._mousex = s.mousex;
-                        s._mousey = s.mousey;
+                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();
                 }
-                current.perform();
-                if (runnable.size() == 0 && Surface.needRender) Surface.renderAll.perform();
+                renderAll();
             } catch (JSExn e) {
-                Log.log(Scheduler.class, "a JavaScript thread spawned with xwt.thread() threw an exception:");
-                Log.log(Scheduler.class, e.toString());
+                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, "a Task threw an exception which was caught by the scheduler:");
-                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
         }