Potention fix for NPE in NioWorker.RegisterTask.run()

This commit is contained in:
Trustin Lee 2008-11-07 17:37:10 +00:00
parent 14a49ed042
commit 0976673ac2

View File

@ -79,8 +79,6 @@ class NioWorker implements Runnable {
private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>(); private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>();
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>(); private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
final Object registerLock = new Object();
NioWorker(int bossId, int id, Executor executor) { NioWorker(int bossId, int id, Executor executor) {
this.bossId = bossId; this.bossId = bossId;
this.id = id; this.id = id;
@ -95,9 +93,7 @@ class NioWorker implements Runnable {
if (firstChannel) { if (firstChannel) {
boolean success = false; boolean success = false;
try { try {
synchronized (registerLock) { this.selector = selector = Selector.open();
this.selector = selector = Selector.open();
}
success = true; success = true;
} catch (IOException e) { } catch (IOException e) {
throw new ChannelException( throw new ChannelException(
@ -117,7 +113,7 @@ class NioWorker implements Runnable {
selector = this.selector; selector = this.selector;
} while (selector == null && started.get()); } while (selector == null && started.get());
if (selector != null) { if (selector != null && selector.isOpen()) {
break; break;
} }
} else { } else {
@ -128,20 +124,19 @@ class NioWorker implements Runnable {
boolean server = !(channel instanceof NioClientSocketChannel); boolean server = !(channel instanceof NioClientSocketChannel);
Runnable registerTask = new RegisterTask(selector, channel, future, server); Runnable registerTask = new RegisterTask(selector, channel, future, server);
synchronized (shutdownLock) {
registerTaskQueue.offer(registerTask);
}
if (wakenUp.compareAndSet(false, true)) {
selector.wakeup();
}
if (firstChannel) { if (firstChannel) {
registerTask.run();
String threadName = String threadName =
(server ? "New I/O server worker #" (server ? "New I/O server worker #"
: "New I/O client worker #") + bossId + '-' + id; : "New I/O client worker #") + bossId + '-' + id;
executor.execute(new ThreadRenamingRunnable(this, threadName)); executor.execute(new ThreadRenamingRunnable(this, threadName));
} else {
synchronized (shutdownLock) {
registerTaskQueue.offer(registerTask);
}
if (wakenUp.compareAndSet(false, true)) {
selector.wakeup();
}
} }
} }
@ -870,9 +865,7 @@ class NioWorker implements Runnable {
public void run() { public void run() {
try { try {
synchronized (registerLock) { channel.socket.register(selector, SelectionKey.OP_READ, channel);
channel.socket.register(selector, SelectionKey.OP_READ, channel);
}
if (future != null) { if (future != null) {
future.setSuccess(); future.setSuccess();
} }