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