X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fjagi%2Fevent%2FDriver.java;h=a8475b4ffc7ae726187b5c199f65406528de1664;hb=31f44c3f51ad7eb8aa5b0602e67eca92ce0d124b;hp=c9ca63856081003d49e1eec3b86e7e24cf148874;hpb=aac2f975859e9b0bbbf582c4d84bebccd2e27e51;p=jagi.git diff --git a/src/jagi/event/Driver.java b/src/jagi/event/Driver.java index c9ca638..a8475b4 100644 --- a/src/jagi/event/Driver.java +++ b/src/jagi/event/Driver.java @@ -59,6 +59,8 @@ public class Driver { } void handle(Watcher w, int evs) { + if(!watching.containsKey(w)) + return; try { pause(w); submit(() -> { @@ -88,8 +90,8 @@ public class Driver { boolean quit = false; Throwable error = null; try { + double now = time(); while(true) { - double now = time(); long timeout = 0; synchronized(selectors) { Double first = timeheap.keypeek(); @@ -109,6 +111,13 @@ public class Driver { } for(SelectionKey key : poll.selectedKeys()) handle((Watcher)key.attachment(), key.readyOps()); + now = time(); + while(true) { + Double first = timeheap.keypeek(); + if((first == null) || (first > now)) + break; + handle(timeheap.remove(), 0); + } } } catch(Throwable t) { error = t; @@ -138,7 +147,7 @@ public class Driver { int evs = w.events(); double timeout = w.timeout(); boolean hastime = timeout < Double.POSITIVE_INFINITY; - if((evs == 0) && !hastime) { + if(evs < 0) { remove(w); return; } @@ -154,10 +163,11 @@ public class Driver { int evs = w.events(); double timeout = w.timeout(); boolean hastime = timeout < Double.POSITIVE_INFINITY; - if((evs == 0) && !hastime) { - w.close(); + if(evs < 0) { + submit(w::close); return; } + w.added(Driver.this); try { watching.put(w, ch.register(poll, evs, w)); } catch(ClosedChannelException e) { @@ -178,7 +188,7 @@ public class Driver { throw(new RuntimeException(w + ": inconsistent internal state")); if(wc == null) throw(new IllegalStateException(w + ": not registered")); - w.close(); + submit(w::close); poll.wakeup(); } @@ -189,7 +199,7 @@ public class Driver { int evs = w.events(); double timeout = w.timeout(); boolean hastime = timeout < Double.POSITIVE_INFINITY; - if((evs == 0) && !hastime) { + if(evs < 0) { remove(w); return; }