Fixed a bug where NioWorker.register() hangs up with high CPU consumption when it failed to open a selector.

This commit is contained in:
Trustin Lee 2008-11-06 11:39:17 +00:00
parent 7f1ff668f7
commit 00ec5be6dd

View File

@ -86,30 +86,39 @@ class NioWorker implements Runnable {
} }
void register(NioSocketChannel channel, ChannelFuture future) { void register(NioSocketChannel channel, ChannelFuture future) {
boolean firstChannel = started.compareAndSet(false, true); boolean firstChannel;
Selector selector; Selector selector;
if (firstChannel) { for (;;) {
boolean success = false; firstChannel = started.compareAndSet(false, true);
selectorGuard.writeLock().lock(); if (firstChannel) {
try { boolean success = false;
this.selector = selector = Selector.open(); selectorGuard.writeLock().lock();
success = true; try {
} catch (IOException e) { this.selector = selector = Selector.open();
throw new ChannelException( success = true;
"Failed to create a selector.", e); } catch (IOException e) {
} finally { throw new ChannelException(
selectorGuard.writeLock().unlock(); "Failed to create a selector.", e);
if (!success) { } finally {
started.compareAndSet(true, false); selectorGuard.writeLock().unlock();
if (!success) {
started.compareAndSet(true, false);
}
}
break;
} else {
selector = this.selector;
if (selector == null) {
do {
Thread.yield();
selector = this.selector;
} while (selector == null && started.get());
}
if (selector != null) {
break;
} }
}
} else {
selector = this.selector;
if (selector == null) {
do {
Thread.yield();
selector = this.selector;
} while (selector == null);
} }
} }