6089N/A/*
6089N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6089N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6089N/A *
6089N/A * This code is free software; you can redistribute it and/or modify it
6089N/A * under the terms of the GNU General Public License version 2 only, as
6089N/A * published by the Free Software Foundation.
6089N/A *
6089N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6089N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6089N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6089N/A * version 2 for more details (a copy is included in the LICENSE file that
6089N/A * accompanied this code).
6089N/A *
6089N/A * You should have received a copy of the GNU General Public License version
6089N/A * 2 along with this work; if not, write to the Free Software Foundation,
6089N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6089N/A *
6089N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6089N/A * or visit www.oracle.com if you need additional information or have any
6089N/A * questions.
6089N/A */
6089N/A
6089N/A/*
6089N/A * Portions Copyright (c) 2012 IBM Corporation
6089N/A */
6089N/A
6089N/Aimport java.net.InetAddress;
6089N/Aimport java.net.InetSocketAddress;
6089N/Aimport java.nio.channels.SelectionKey;
6089N/Aimport java.nio.channels.Selector;
6089N/Aimport java.nio.channels.ServerSocketChannel;
6089N/Aimport java.nio.channels.SocketChannel;
6089N/A
6089N/A/*
6089N/A * @test
6089N/A * @bug 6429204
6089N/A * @summary SelectionKey.interestOps does not update interest set on Windows.
6089N/A * @author Frank Ding
6089N/A */
6089N/Apublic class RacyDeregister {
6089N/A
6089N/A static boolean notified;
6089N/A static final Object selectorLock = new Object();
6089N/A static final Object notifyLock = new Object();
6089N/A /**
6089N/A * null: not terminated
6089N/A * true: passed
6089N/A * false: failed
6089N/A */
6089N/A static volatile Boolean succTermination = null;
6089N/A
6089N/A public static void main(String[] args) throws Exception {
6089N/A InetAddress addr = InetAddress.getByName(null);
6089N/A ServerSocketChannel sc = ServerSocketChannel.open();
6089N/A sc.socket().bind(new InetSocketAddress(addr, 0));
6089N/A
6089N/A SocketChannel.open(new InetSocketAddress(addr,
6089N/A sc.socket().getLocalPort()));
6089N/A
6089N/A SocketChannel accepted = sc.accept();
6089N/A accepted.configureBlocking(false);
6089N/A
6089N/A SocketChannel.open(new InetSocketAddress(addr,
6089N/A sc.socket().getLocalPort()));
6089N/A SocketChannel accepted2 = sc.accept();
6089N/A accepted2.configureBlocking(false);
6089N/A
6089N/A final Selector sel = Selector.open();
6089N/A SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ);
6089N/A final SelectionKey[] key = new SelectionKey[]{
6089N/A accepted.register(sel, SelectionKey.OP_READ)};
6089N/A
6089N/A
6089N/A // thread that will be changing key[0].interestOps to OP_READ | OP_WRITE
6089N/A new Thread() {
6089N/A
6089N/A public void run() {
6089N/A try {
6089N/A for (int k = 0; k < 15; k++) {
6089N/A for (int i = 0; i < 10000; i++) {
6089N/A synchronized (notifyLock) {
6089N/A synchronized (selectorLock) {
6089N/A sel.wakeup();
6089N/A key[0].interestOps(SelectionKey.OP_READ
6089N/A | SelectionKey.OP_WRITE);
6089N/A }
6089N/A notified = false;
6089N/A long beginTime = System.currentTimeMillis();
6089N/A while (true) {
6089N/A notifyLock.wait(5000);
6089N/A if (notified) {
6089N/A break;
6089N/A }
6089N/A long endTime = System.currentTimeMillis();
6089N/A if (endTime - beginTime > 5000) {
6089N/A succTermination = false;
6089N/A // wake up main thread doing select()
6089N/A sel.wakeup();
6089N/A return;
6089N/A }
6089N/A }
6089N/A }
6089N/A }
6089N/A }
6089N/A succTermination = true;
6089N/A // wake up main thread doing select()
6089N/A sel.wakeup();
6089N/A } catch (Exception e) {
6089N/A System.out.println(e);
6089N/A succTermination = true;
6089N/A // wake up main thread doing select()
6089N/A sel.wakeup();
6089N/A }
6089N/A }
6089N/A }.start();
6089N/A
6089N/A // main thread will be doing registering/deregistering with the sel
6089N/A while (true) {
6089N/A sel.select();
6089N/A if (Boolean.TRUE.equals(succTermination)) {
6089N/A System.out.println("Test passed");
6089N/A sel.close();
6089N/A sc.close();
6089N/A break;
6089N/A } else if (Boolean.FALSE.equals(succTermination)) {
6089N/A System.out.println("Failed to pass the test");
6089N/A sel.close();
6089N/A sc.close();
6089N/A throw new RuntimeException("Failed to pass the test");
6089N/A }
6089N/A synchronized (selectorLock) {
6089N/A }
6089N/A if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) {
6089N/A synchronized (notifyLock) {
6089N/A notified = true;
6089N/A notifyLock.notify();
6089N/A key[0].cancel();
6089N/A sel.selectNow();
6089N/A key2 = accepted2.register(sel, SelectionKey.OP_READ);
6089N/A key[0] = accepted.register(sel, SelectionKey.OP_READ);
6089N/A }
6089N/A }
6089N/A key2.cancel();
6089N/A sel.selectedKeys().clear();
6089N/A }
6089N/A }
6089N/A}