2003/10/31 09:50:08
[org.ibex.core.git] / src / org / xwt / Scheduler.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.util.*;
5 import org.xwt.js.*;
6 import org.xwt.util.*;
7
8 // FEATURE: reimplement Watcher
9 /** Implements cooperative multitasking */
10 public class Scheduler {
11
12     private static Scheduler singleton = new Scheduler();
13     public static void run() { singleton.do_run(); }
14     protected Scheduler() { }
15
16     public static abstract class Task implements Callback { public abstract Object call(Object o); }
17
18     private static Queue runnable = new Queue(50);
19
20     public static void add(Task t) { singleton.runnable.append(t); }
21     public void do_run() {
22         while(true) {
23             Task t = (Task)runnable.remove(true);
24             try {
25                 t.call(null);
26                 for(int i=0; i<Surface.allSurfaces.size(); i++)
27                     ((Surface)Surface.allSurfaces.elementAt(i)).render();
28             } catch (Exception e) {
29                 Log.log(Scheduler.class, "Task threw an exception: " + e);
30                 Log.log(Scheduler.class, e);
31             }
32         }
33     }
34 }