1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import java.io.IOException;
7 import org.ibex.util.*;
8 import org.ibex.graphics.*;
9 import org.ibex.plat.*;
11 /** Implements cooperative multitasking */
12 public class Scheduler {
14 // Public API Exposed to org.ibex /////////////////////////////////////////////////
16 private static Scheduler singleton;
17 public static void add(Task t) { Log.debug(Scheduler.class, "scheduling " + t); Scheduler.runnable.append(t); }
18 public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
20 private static Task current = null;
22 private static volatile boolean rendering = false;
23 private static volatile boolean again = false;
25 /** synchronizd so that we can safely call it from an event-delivery thread, in-context */
26 public static void renderAll() {
27 if (rendering) { again = true; return; }
28 synchronized(Scheduler.class) {
32 // FEATURE: this could be cleaner
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);
47 // API which must be supported by subclasses /////////////////////////////////////
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.
55 public void run() { defaultRun(); }
56 public Scheduler() { }
59 // Default Implementation //////////////////////////////////////////////////////
61 protected static Queue runnable = new Queue(50);
62 public void defaultRun() {
64 current = (Task)runnable.remove(true);
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;
78 Log.debug(Scheduler.class, "performing " + current);
83 Log.info(Scheduler.class, "a JavaScript thread spawned with ibex.thread() threw an exception:");
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) {
91 // if an Error is thrown it will cause the engine to quit