implement Selector.selectNow() and select()

This commit is contained in:
Joel Dice 2009-07-23 13:08:41 -06:00
parent 77f1bddce2
commit 29858a5299
3 changed files with 29 additions and 1 deletions

View File

@ -626,7 +626,17 @@ Java_java_nio_channels_SocketSelector_natDoSocketSelect(JNIEnv *e, jclass,
}
#endif
timeval time = { interval / 1000, (interval % 1000) * 1000 };
timeval time;
if (interval > 0) {
time.tv_sec = interval / 1000;
time.tv_usec = (interval % 1000) * 1000;
} else if (interval < 0) {
time.tv_sec = 0;
time.tv_usec = 0;
} else {
time.tv_sec = INT32_MAX;
time.tv_usec = 0;
}
int r = ::select(max + 1, &(s->read), &(s->write), &(s->except), &time);
if (r < 0) {

View File

@ -42,7 +42,11 @@ public abstract class Selector {
public abstract Selector wakeup();
public abstract int selectNow() throws IOException;
public abstract int select(long interval) throws IOException;
public abstract int select() throws IOException;
public abstract void close();
}

View File

@ -48,7 +48,21 @@ class SocketSelector extends Selector {
}
}
public synchronized int selectNow() throws IOException {
return doSelect(-1);
}
public synchronized int select() throws IOException {
return doSelect(0);
}
public synchronized int select(long interval) throws IOException {
if (interval < 0) throw new IllegalArgumentException();
return doSelect(interval);
}
public int doSelect(long interval) throws IOException {
selectedKeys.clear();
if (clearWoken()) return 0;