bugfix in url parsing for HTTP.java
[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     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(); }
19
20     private static Task current = null;
21
22     private static volatile boolean rendering = false;
23     private static volatile boolean again = false;
24
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) {
29             try {
30                 rendering = true;
31                 do {
32                     // FEATURE: this could be cleaner
33                     again = false;
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);
37                     }
38                 } while(again);
39             } finally {
40                 rendering = false;
41             }
42         }
43     }
44
45     
46
47     // API which must be supported by subclasses /////////////////////////////////////
48
49     /**
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.
54      */
55     public void run() { defaultRun(); }
56     public Scheduler() { }
57
58
59     // Default Implementation //////////////////////////////////////////////////////
60
61     protected static Queue runnable = new Queue(50);
62     public void defaultRun() {
63         while(true) {
64             current = (Task)runnable.remove(true);
65             try {
66                 // FIXME hideous
67                 synchronized(this) {
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;
73                         } else {
74                             s._mousex = s.mousex;
75                             s._mousey = s.mousey;
76                         }
77                     }
78                     Log.debug(Scheduler.class, "performing " + current);
79                     current.perform();
80                 }
81                 renderAll();
82             } catch (JSExn e) {
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) {
89                 t.printStackTrace();
90             }
91             // if an Error is thrown it will cause the engine to quit
92         }
93     }
94 }