2003/12/22 04:50:08
[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     // API which must be supported by subclasses /////////////////////////////////////
30
31     /**
32      *  SCHEDULER INVARIANT: all scheduler implementations MUST invoke
33      *  Surface.renderAll() after performing a Task if no tasks remain
34      *  in the queue.  A scheduler may choose to invoke
35      *  Surface.renderAll() more often than that if it so chooses.
36      */
37     public void run() { defaultRun(); }
38     protected Scheduler() { }
39
40
41     // Default Implementation //////////////////////////////////////////////////////
42
43     protected static Queue runnable = new Queue(50);
44     public void defaultRun() {
45         while(true) {
46             current = (Task)runnable.remove(true);
47             try {
48                 for(int i=0; i<Surface.allSurfaces.size(); i++) {
49                     Surface s = (Surface)Surface.allSurfaces.elementAt(i);
50                     if (current instanceof JSFunction) {
51                         s._mousex = Integer.MAX_VALUE;
52                         s._mousey = Integer.MAX_VALUE;
53                     } else {
54                         s._mousex = s.mousex;
55                         s._mousey = s.mousey;
56                     }
57                 }
58                 current.perform();
59                 if (runnable.size() == 0 && Surface.needRender) Surface.renderAll.perform();
60             } catch (JSExn e) {
61                 Log.log(Scheduler.class, "a JavaScript thread spawned with xwt.thread() threw an exception:");
62                 Log.log(Scheduler.class, e.toString());
63             } catch (Exception e) {
64                 Log.log(Scheduler.class, "a Task threw an exception which was caught by the scheduler:");
65                 Log.log(Scheduler.class, e);
66             }
67             // if an Error is thrown it will cause the engine to quit
68         }
69     }
70 }