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