提交 4c231751 编写于 作者: S sherman

5025260: Register methods should throw ClosedChannelException instead of NPE

Summary: update the spec and implementation to throw ClosedSelectorException
Reviewed-by: alanb
上级 38db87eb
...@@ -191,6 +191,9 @@ public abstract class SelectableChannel ...@@ -191,6 +191,9 @@ public abstract class SelectableChannel
* @throws ClosedChannelException * @throws ClosedChannelException
* If this channel is closed * If this channel is closed
* *
* @throws ClosedSelectorException
* If the selector is closed
*
* @throws IllegalBlockingModeException * @throws IllegalBlockingModeException
* If this channel is in blocking mode * If this channel is in blocking mode
* *
...@@ -246,6 +249,9 @@ public abstract class SelectableChannel ...@@ -246,6 +249,9 @@ public abstract class SelectableChannel
* @throws ClosedChannelException * @throws ClosedChannelException
* If this channel is closed * If this channel is closed
* *
* @throws ClosedSelectorException
* If the selector is closed
*
* @throws IllegalBlockingModeException * @throws IllegalBlockingModeException
* If this channel is in blocking mode * If this channel is in blocking mode
* *
......
...@@ -175,6 +175,16 @@ public abstract class AbstractSelectableChannel ...@@ -175,6 +175,16 @@ public abstract class AbstractSelectableChannel
* the selector is invoked while holding the appropriate locks. The * the selector is invoked while holding the appropriate locks. The
* resulting key is added to this channel's key set before being returned. * resulting key is added to this channel's key set before being returned.
* </p> * </p>
*
* @throws ClosedSelectorException {@inheritDoc}
*
* @throws IllegalBlockingModeException {@inheritDoc}
*
* @throws IllegalSelectorException {@inheritDoc}
*
* @throws CancelledKeyException {@inheritDoc}
*
* @throws IllegalArgumentException {@inheritDoc}
*/ */
public final SelectionKey register(Selector sel, int ops, public final SelectionKey register(Selector sel, int ops,
Object att) Object att)
......
...@@ -58,6 +58,9 @@ abstract class AbstractPollSelectorImpl ...@@ -58,6 +58,9 @@ abstract class AbstractPollSelectorImpl
// True if this Selector has been closed // True if this Selector has been closed
private boolean closed = false; private boolean closed = false;
// Lock for close and cleanup
private Object closeLock = new Object();
AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) { AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) {
super(sp); super(sp);
this.totalChannels = channels; this.totalChannels = channels;
...@@ -65,8 +68,12 @@ abstract class AbstractPollSelectorImpl ...@@ -65,8 +68,12 @@ abstract class AbstractPollSelectorImpl
} }
void putEventOps(SelectionKeyImpl sk, int ops) { void putEventOps(SelectionKeyImpl sk, int ops) {
synchronized (closeLock) {
if (closed)
throw new ClosedSelectorException();
pollWrapper.putEventOps(sk.getIndex(), ops); pollWrapper.putEventOps(sk.getIndex(), ops);
} }
}
public Selector wakeup() { public Selector wakeup() {
pollWrapper.interrupt(); pollWrapper.interrupt();
...@@ -76,7 +83,9 @@ abstract class AbstractPollSelectorImpl ...@@ -76,7 +83,9 @@ abstract class AbstractPollSelectorImpl
protected abstract int doSelect(long timeout) throws IOException; protected abstract int doSelect(long timeout) throws IOException;
protected void implClose() throws IOException { protected void implClose() throws IOException {
if (!closed) { synchronized (closeLock) {
if (closed)
return;
closed = true; closed = true;
// Deregister channels // Deregister channels
for(int i=channelOffset; i<totalChannels; i++) { for(int i=channelOffset; i<totalChannels; i++) {
...@@ -129,6 +138,10 @@ abstract class AbstractPollSelectorImpl ...@@ -129,6 +138,10 @@ abstract class AbstractPollSelectorImpl
} }
protected void implRegister(SelectionKeyImpl ski) { protected void implRegister(SelectionKeyImpl ski) {
synchronized (closeLock) {
if (closed)
throw new ClosedSelectorException();
// Check to see if the array is large enough // Check to see if the array is large enough
if (channelArray.length == totalChannels) { if (channelArray.length == totalChannels) {
// Make a larger array // Make a larger array
...@@ -147,6 +160,7 @@ abstract class AbstractPollSelectorImpl ...@@ -147,6 +160,7 @@ abstract class AbstractPollSelectorImpl
totalChannels++; totalChannels++;
keys.add(ski); keys.add(ski);
} }
}
protected void implDereg(SelectionKeyImpl ski) throws IOException { protected void implDereg(SelectionKeyImpl ski) throws IOException {
// Algorithm: Copy the sc from the end of the list and put it into // Algorithm: Copy the sc from the end of the list and put it into
......
...@@ -46,15 +46,15 @@ class DevPollSelectorImpl ...@@ -46,15 +46,15 @@ class DevPollSelectorImpl
// The poll object // The poll object
DevPollArrayWrapper pollWrapper; DevPollArrayWrapper pollWrapper;
// The number of valid channels in this Selector's poll array
private int totalChannels;
// Maps from file descriptors to keys // Maps from file descriptors to keys
private Map<Integer,SelectionKeyImpl> fdToKey; private Map<Integer,SelectionKeyImpl> fdToKey;
// True if this Selector has been closed // True if this Selector has been closed
private boolean closed = false; private boolean closed = false;
// Lock for close/cleanup
private Object closeLock = new Object();
// Lock for interrupt triggering and clearing // Lock for interrupt triggering and clearing
private Object interruptLock = new Object(); private Object interruptLock = new Object();
private boolean interruptTriggered = false; private boolean interruptTriggered = false;
...@@ -72,7 +72,6 @@ class DevPollSelectorImpl ...@@ -72,7 +72,6 @@ class DevPollSelectorImpl
pollWrapper = new DevPollArrayWrapper(); pollWrapper = new DevPollArrayWrapper();
pollWrapper.initInterrupt(fd0, fd1); pollWrapper.initInterrupt(fd0, fd1);
fdToKey = new HashMap<Integer,SelectionKeyImpl>(); fdToKey = new HashMap<Integer,SelectionKeyImpl>();
totalChannels = 1;
} }
protected int doSelect(long timeout) protected int doSelect(long timeout)
...@@ -131,7 +130,8 @@ class DevPollSelectorImpl ...@@ -131,7 +130,8 @@ class DevPollSelectorImpl
} }
protected void implClose() throws IOException { protected void implClose() throws IOException {
if (!closed) { if (closed)
return;
closed = true; closed = true;
// prevent further wakeup // prevent further wakeup
...@@ -141,11 +141,9 @@ class DevPollSelectorImpl ...@@ -141,11 +141,9 @@ class DevPollSelectorImpl
FileDispatcher.closeIntFD(fd0); FileDispatcher.closeIntFD(fd0);
FileDispatcher.closeIntFD(fd1); FileDispatcher.closeIntFD(fd1);
if (pollWrapper != null) {
pollWrapper.release(fd0); pollWrapper.release(fd0);
pollWrapper.closeDevPollFD(); pollWrapper.closeDevPollFD();
pollWrapper = null;
selectedKeys = null; selectedKeys = null;
// Deregister channels // Deregister channels
...@@ -158,18 +156,13 @@ class DevPollSelectorImpl ...@@ -158,18 +156,13 @@ class DevPollSelectorImpl
((SelChImpl)selch).kill(); ((SelChImpl)selch).kill();
i.remove(); i.remove();
} }
totalChannels = 0;
}
fd0 = -1; fd0 = -1;
fd1 = -1; fd1 = -1;
} }
}
protected void implRegister(SelectionKeyImpl ski) { protected void implRegister(SelectionKeyImpl ski) {
int fd = IOUtil.fdVal(ski.channel.getFD()); int fd = IOUtil.fdVal(ski.channel.getFD());
fdToKey.put(Integer.valueOf(fd), ski); fdToKey.put(Integer.valueOf(fd), ski);
totalChannels++;
keys.add(ski); keys.add(ski);
} }
...@@ -179,7 +172,6 @@ class DevPollSelectorImpl ...@@ -179,7 +172,6 @@ class DevPollSelectorImpl
int fd = ski.channel.getFDVal(); int fd = ski.channel.getFDVal();
fdToKey.remove(Integer.valueOf(fd)); fdToKey.remove(Integer.valueOf(fd));
pollWrapper.release(fd); pollWrapper.release(fd);
totalChannels--;
ski.setIndex(-1); ski.setIndex(-1);
keys.remove(ski); keys.remove(ski);
selectedKeys.remove(ski); selectedKeys.remove(ski);
...@@ -190,6 +182,8 @@ class DevPollSelectorImpl ...@@ -190,6 +182,8 @@ class DevPollSelectorImpl
} }
void putEventOps(SelectionKeyImpl sk, int ops) { void putEventOps(SelectionKeyImpl sk, int ops) {
if (closed)
throw new ClosedSelectorException();
int fd = IOUtil.fdVal(sk.channel.getFD()); int fd = IOUtil.fdVal(sk.channel.getFD());
pollWrapper.setInterest(fd, ops); pollWrapper.setInterest(fd, ops);
} }
......
/* /*
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -31,7 +31,6 @@ import java.nio.channels.spi.*; ...@@ -31,7 +31,6 @@ import java.nio.channels.spi.*;
import java.util.*; import java.util.*;
import sun.misc.*; import sun.misc.*;
/** /**
* An implementation of Selector for Linux 2.6+ kernels that uses * An implementation of Selector for Linux 2.6+ kernels that uses
* the epoll event notification facility. * the epoll event notification facility.
...@@ -51,7 +50,7 @@ class EPollSelectorImpl ...@@ -51,7 +50,7 @@ class EPollSelectorImpl
private Map<Integer,SelectionKeyImpl> fdToKey; private Map<Integer,SelectionKeyImpl> fdToKey;
// True if this Selector has been closed // True if this Selector has been closed
private boolean closed = false; private volatile boolean closed = false;
// Lock for interrupt triggering and clearing // Lock for interrupt triggering and clearing
private Object interruptLock = new Object(); private Object interruptLock = new Object();
...@@ -128,7 +127,8 @@ class EPollSelectorImpl ...@@ -128,7 +127,8 @@ class EPollSelectorImpl
} }
protected void implClose() throws IOException { protected void implClose() throws IOException {
if (!closed) { if (closed)
return;
closed = true; closed = true;
// prevent further wakeup // prevent further wakeup
...@@ -138,11 +138,10 @@ class EPollSelectorImpl ...@@ -138,11 +138,10 @@ class EPollSelectorImpl
FileDispatcher.closeIntFD(fd0); FileDispatcher.closeIntFD(fd0);
FileDispatcher.closeIntFD(fd1); FileDispatcher.closeIntFD(fd1);
if (pollWrapper != null) {
pollWrapper.release(fd0); pollWrapper.release(fd0);
pollWrapper.closeEPollFD(); pollWrapper.closeEPollFD();
pollWrapper = null; // it is possible
selectedKeys = null; selectedKeys = null;
// Deregister channels // Deregister channels
...@@ -155,13 +154,14 @@ class EPollSelectorImpl ...@@ -155,13 +154,14 @@ class EPollSelectorImpl
((SelChImpl)selch).kill(); ((SelChImpl)selch).kill();
i.remove(); i.remove();
} }
}
fd0 = -1; fd0 = -1;
fd1 = -1; fd1 = -1;
} }
}
protected void implRegister(SelectionKeyImpl ski) { protected void implRegister(SelectionKeyImpl ski) {
if (closed)
throw new ClosedSelectorException();
int fd = IOUtil.fdVal(ski.channel.getFD()); int fd = IOUtil.fdVal(ski.channel.getFD());
fdToKey.put(Integer.valueOf(fd), ski); fdToKey.put(Integer.valueOf(fd), ski);
pollWrapper.add(fd); pollWrapper.add(fd);
...@@ -183,6 +183,8 @@ class EPollSelectorImpl ...@@ -183,6 +183,8 @@ class EPollSelectorImpl
} }
void putEventOps(SelectionKeyImpl sk, int ops) { void putEventOps(SelectionKeyImpl sk, int ops) {
if (closed)
throw new ClosedSelectorException();
int fd = IOUtil.fdVal(sk.channel.getFD()); int fd = IOUtil.fdVal(sk.channel.getFD());
pollWrapper.setInterest(fd, ops); pollWrapper.setInterest(fd, ops);
} }
......
/* /*
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -80,6 +80,9 @@ final class WindowsSelectorImpl extends SelectorImpl { ...@@ -80,6 +80,9 @@ final class WindowsSelectorImpl extends SelectorImpl {
// File descriptors corresponding to source and sink // File descriptors corresponding to source and sink
private final int wakeupSourceFd, wakeupSinkFd; private final int wakeupSourceFd, wakeupSinkFd;
// Lock for close cleanup
private Object closeLock = new Object();
// Maps file descriptors to their indices in pollArray // Maps file descriptors to their indices in pollArray
private final static class FdMap extends HashMap<Integer, MapEntry> { private final static class FdMap extends HashMap<Integer, MapEntry> {
static final long serialVersionUID = 0L; static final long serialVersionUID = 0L;
...@@ -473,6 +476,7 @@ final class WindowsSelectorImpl extends SelectorImpl { ...@@ -473,6 +476,7 @@ final class WindowsSelectorImpl extends SelectorImpl {
} }
protected void implClose() throws IOException { protected void implClose() throws IOException {
synchronized (closeLock) {
if (channelArray != null) { if (channelArray != null) {
if (pollWrapper != null) { if (pollWrapper != null) {
// prevent further wakeup // prevent further wakeup
...@@ -500,8 +504,12 @@ final class WindowsSelectorImpl extends SelectorImpl { ...@@ -500,8 +504,12 @@ final class WindowsSelectorImpl extends SelectorImpl {
} }
} }
} }
}
protected void implRegister(SelectionKeyImpl ski) { protected void implRegister(SelectionKeyImpl ski) {
synchronized (closeLock) {
if (pollWrapper == null)
throw new ClosedSelectorException();
growIfNeeded(); growIfNeeded();
channelArray[totalChannels] = ski; channelArray[totalChannels] = ski;
ski.setIndex(totalChannels); ski.setIndex(totalChannels);
...@@ -510,6 +518,7 @@ final class WindowsSelectorImpl extends SelectorImpl { ...@@ -510,6 +518,7 @@ final class WindowsSelectorImpl extends SelectorImpl {
pollWrapper.addEntry(totalChannels, ski); pollWrapper.addEntry(totalChannels, ski);
totalChannels++; totalChannels++;
} }
}
private void growIfNeeded() { private void growIfNeeded() {
if (channelArray.length == totalChannels) { if (channelArray.length == totalChannels) {
...@@ -554,8 +563,12 @@ final class WindowsSelectorImpl extends SelectorImpl { ...@@ -554,8 +563,12 @@ final class WindowsSelectorImpl extends SelectorImpl {
} }
void putEventOps(SelectionKeyImpl sk, int ops) { void putEventOps(SelectionKeyImpl sk, int ops) {
synchronized (closeLock) {
if (pollWrapper == null)
throw new ClosedSelectorException();
pollWrapper.putEventOps(sk.getIndex(), ops); pollWrapper.putEventOps(sk.getIndex(), ops);
} }
}
public Selector wakeup() { public Selector wakeup() {
synchronized (interruptLock) { synchronized (interruptLock) {
......
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 5025260
* @summary ClosedSelectorException is expected when register after close
*/
import java.net.*;
import java.nio.channels.*;
public class CloseThenRegister {
public static void main (String [] args) throws Exception {
try {
Selector s = Selector.open();
s.close();
ServerSocketChannel c = ServerSocketChannel.open();
c.socket().bind(new InetSocketAddress(40000));
c.configureBlocking(false);
c.register(s, SelectionKey.OP_ACCEPT);
} catch (ClosedSelectorException cse) {
return;
}
throw new RuntimeException("register after close does not cause CSE!");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册