1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
8 /** Implements cooperative multitasking */
9 public class Scheduler {
11 // Public API Exposed to org.xwt /////////////////////////////////////////////////
13 private static Scheduler singleton;
14 public static interface Task { public abstract void perform() throws Exception; }
15 public static void add(Task t) { singleton.runnable.append(t); }
16 public static void init() { if (singleton == null) (singleton = Platform.getScheduler()).run(); }
18 // API which must be supported by subclasses /////////////////////////////////////
21 * SCHEDULER INVARIANT: all scheduler implementations MUST invoke
22 * Surface.renderAll() after performing a Task if no tasks remain
23 * in the queue. A scheduler may choose to invoke
24 * Surface.renderAll() more often than that if it so chooses.
26 public void run() { defaultRun(); }
27 protected Scheduler() { }
30 // Default Implementation //////////////////////////////////////////////////////
32 protected static Queue runnable = new Queue(50);
33 public void defaultRun() {
35 Task t = (Task)runnable.remove(true);
38 // FEATURE: be smarter about this
39 if (t != Surface.renderAll) add(Surface.renderAll);
41 Log.log(Scheduler.class, "a JavaScript thread spawned with xwt.thread() threw an exception:");
42 Log.log(Scheduler.class, e.toString());
43 } catch (Exception e) {
44 Log.log(Scheduler.class, "a Task threw an exception which was caught by the scheduler:");
45 Log.log(Scheduler.class, e);
47 // if an Error is thrown it will cause the engine to quit