2004/01/07 20:37:32
[org.ibex.core.git] / src / org / xwt / Scheduler.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.util.*;
5 import org.xwt.js.*;
6 import org.xwt.util.*;
7
8 /** Implements cooperative multitasking */
9 public class Scheduler {
10
11     // FIXME: prepending events messes with keysate -- make a "no re-ordering" invariant?
12
13     // Public API Exposed to org.xwt /////////////////////////////////////////////////
14
15     private static Scheduler singleton;
16     public static interface Task { public abstract void perform() throws Exception; }
17
18     /** adds a task to the back of the queue */
19     public static void add(Task t) { singleton.runnable.append(t); }
20
21     /** adds a task to the front of the queue (guaranteed to run next) */
22     public static void addAtFront(Task t) { singleton.runnable.prepend(t); }
23
24     public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
25
26     private static Task current = null;
27     public static Task current() { return current; }
28
29     /** synchronizd so that we can safely call it from an event-delivery thread, in-context */
30     private static volatile boolean rendering = false;
31     private static volatile boolean again = false;
32     public static void renderAll() {
33         if (rendering) { again = true; return; }
34         synchronized(Scheduler.class) {
35             try {
36                 rendering = true;
37                 do {
38                     again = false;
39                     for(int i=0; i<Surface.allSurfaces.size(); i++) {
40                         Surface s = ((Surface)Surface.allSurfaces.elementAt(i));
41                         do { s.render(); } while(s.abort);
42                     }
43                 } while(again);
44             } finally {
45                 rendering = false;
46             }
47         }
48     }
49
50     
51
52     // API which must be supported by subclasses /////////////////////////////////////
53
54     /**
55      *  SCHEDULER INVARIANT: all scheduler implementations MUST invoke
56      *  Surface.renderAll() after performing a Task if no tasks remain
57      *  in the queue.  A scheduler may choose to invoke
58      *  Surface.renderAll() more often than that if it so chooses.
59      */
60     public void run() { defaultRun(); }
61     protected Scheduler() { }
62
63
64     // Default Implementation //////////////////////////////////////////////////////
65
66     protected static Queue runnable = new Queue(50);
67     public void defaultRun() {
68         while(true) {
69             current = (Task)runnable.remove(true);
70             try {
71                 synchronized(this) {
72                     for(int i=0; i<Surface.allSurfaces.size(); i++) {
73                         Surface s = (Surface)Surface.allSurfaces.elementAt(i);
74                         if (current instanceof JSFunction) {
75                             s._mousex = Integer.MAX_VALUE;
76                             s._mousey = Integer.MAX_VALUE;
77                         } else {
78                             s._mousex = s.mousex;
79                             s._mousey = s.mousey;
80                         }
81                     }
82                     current.perform();
83                 }
84                 renderAll();
85             } catch (JSExn e) {
86                 Log.info(Scheduler.class, "a JavaScript thread spawned with xwt.thread() threw an exception:");
87                 Log.info(Scheduler.class, "JS Exception: " + e.getObject() + "\n" + e.backtrace());
88                 Log.info(Scheduler.class,e);
89             } catch (Exception e) {
90                 Log.info(Scheduler.class, "a Task threw an exception which was caught by the scheduler:");
91                 Log.info(Scheduler.class, e);
92             }
93             // if an Error is thrown it will cause the engine to quit
94         }
95     }
96 }