avoid crashes due to calling select or wakeup on a closed Selector

This commit is contained in:
Joel Dice 2012-01-31 09:48:05 -07:00
parent 807f2a8d51
commit b45528203d
2 changed files with 24 additions and 5 deletions

View File

@ -0,0 +1,13 @@
/* Copyright (c) 2012, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.nio.channels;
public class ClosedSelectorException extends IllegalStateException { }

View File

@ -15,7 +15,7 @@ import java.util.Iterator;
import java.net.Socket;
class SocketSelector extends Selector {
protected long state;
protected volatile long state;
protected final Object lock = new Object();
protected boolean woken = false;
@ -31,7 +31,7 @@ class SocketSelector extends Selector {
public Selector wakeup() {
synchronized (lock) {
if (! woken) {
if (isOpen() && (! woken)) {
woken = true;
natWakeup(state);
@ -66,6 +66,10 @@ class SocketSelector extends Selector {
}
public int doSelect(long interval) throws IOException {
if (! isOpen()) {
throw new ClosedSelectorException();
}
selectedKeys.clear();
if (clearWoken()) interval = -1;
@ -107,9 +111,11 @@ class SocketSelector extends Selector {
}
public synchronized void close() {
if (isOpen()) {
natClose(state);
state = 0;
synchronized (lock) {
if (isOpen()) {
natClose(state);
state = 0;
}
}
}