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