2cb7892ab5242aa7492bebca5cff4d20d2b99666
[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 /** Implements cooperative multitasking */
9 public class Scheduler {
10
11     public static final Scheduler singleton = Platform.getScheduler();
12     protected Scheduler() { }
13
14     public static abstract class Task { public abstract void perform(); }
15
16     protected static Queue runnable = new Queue(50);
17     public static void add(Task t) { singleton.runnable.append(t); }
18     public void run() {
19         while(true) {
20             Task t = (Task)runnable.remove(true);
21             try {
22                 t.perform();
23                 // FIXME: be smarter about this
24                 for(int i=0; i<Surface.allSurfaces.size(); i++)
25                     ((Surface)Surface.allSurfaces.elementAt(i)).render();
26             } catch (Exception e) {
27                 Log.log(Scheduler.class, "Task threw an exception: " + e);
28                 Log.log(Scheduler.class, e);
29             }
30         }
31     }
32 }