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