current.set(this);
w.handle(evs);
} catch(Throwable t) {
- error(w, t);
+ error(w, t, "handling event");
+ } finally {
+ current.remove();
+ }
+ }
+
+ protected void close(Watcher w) {
+ try {
+ current.set(this);
+ w.close();
+ } catch(Throwable t) {
+ error(w, t, "closing");
} finally {
current.remove();
}
worker.submit(task);
}
- protected void error(Watcher w, Throwable t) {
- hlog.log(Level.WARNING, w + ": uncaught error when handling event", t);
+ protected void error(Watcher w, Throwable t, String thing) {
+ hlog.log(Level.WARNING, w + ": uncaught error when " + thing, t);
remove(w);
}
}
void handle(Watcher w, int evs) {
+ if(!watching.containsKey(w))
+ return;
try {
pause(w);
submit(() -> {
boolean quit = false;
Throwable error = null;
try {
+ double now = time();
while(true) {
- double now = time();
long timeout = 0;
synchronized(selectors) {
Double first = timeheap.keypeek();
return;
}
if(first != null)
- timeout = (long)Math.ceil((first - now) * 1000);
+ timeout = Math.max((long)Math.ceil((first - now) * 1000), 0);
}
poll.selectedKeys().clear();
try {
}
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;
int evs = w.events();
double timeout = w.timeout();
boolean hastime = timeout < Double.POSITIVE_INFINITY;
- if((evs == 0) && !hastime) {
+ if(evs < 0) {
remove(w);
return;
}
int evs = w.events();
double timeout = w.timeout();
boolean hastime = timeout < Double.POSITIVE_INFINITY;
- if((evs == 0) && !hastime) {
- w.close();
+ if(evs < 0) {
+ submit(() -> close(w));
return;
}
+ w.added(Driver.this);
try {
watching.put(w, ch.register(poll, evs, w));
} catch(ClosedChannelException e) {
throw(new RuntimeException(w + ": inconsistent internal state"));
if(wc == null)
throw(new IllegalStateException(w + ": not registered"));
- w.close();
+ submit(() -> close(w));
poll.wakeup();
}
int evs = w.events();
double timeout = w.timeout();
boolean hastime = timeout < Double.POSITIVE_INFINITY;
- if((evs == 0) && !hastime) {
+ if(evs < 0) {
remove(w);
return;
}