test/jdk/java/nio/channels/Selector/RacyDeregister.java
author bpb
Tue, 26 Jun 2018 09:33:55 -0700
changeset 50794 0ab1cac936b8
parent 50258 25f93c5406bf
child 55525 c9093341cfe2
permissions -rw-r--r--
8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     1
/*
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     4
 *
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     8
 *
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    13
 * accompanied this code).
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    14
 *
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    18
 *
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    21
 * questions.
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    22
 */
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    23
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    24
/*
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    25
 * Portions Copyright (c) 2012 IBM Corporation
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    26
 */
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    27
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    28
import java.net.InetAddress;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    29
import java.net.InetSocketAddress;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    30
import java.nio.channels.SelectionKey;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    31
import java.nio.channels.Selector;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    32
import java.nio.channels.ServerSocketChannel;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    33
import java.nio.channels.SocketChannel;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    34
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    35
/*
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    36
 * @test
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
    37
 * @bug 6429204 8203766 8205641
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    38
 * @summary SelectionKey.interestOps does not update interest set on Windows.
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    39
 * @author Frank Ding
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    40
 * @run main/timeout=1200 RacyDeregister
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    41
 */
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    42
public class RacyDeregister {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    43
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    44
    // FIXME: NUM_OUTER_LOOP_ITERATIONS should be reverted to the hard-coded
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    45
    // value 15 when JDK-8161083 is resolved as either a bug or a non-issue.
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    46
    static final int NUM_OUTER_LOOP_ITERATIONS =
40250
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
    47
        System.getProperty("os.name").startsWith("Windows") ? 150 : 15;
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
    48
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    49
    // 90% of 1200 second timeout as milliseconds
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    50
    static final int TIMEOUT_THRESHOLD_MILLIS = 1200*900;
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    51
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    52
    // Time at start of main().
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    53
    static long t0;
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    54
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    55
    static boolean notified;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    56
    static final Object selectorLock = new Object();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    57
    static final Object notifyLock = new Object();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    58
    /**
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    59
     * null: not terminated
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    60
     * true: passed
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    61
     * false: failed
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    62
     */
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    63
    static volatile Boolean succTermination = null;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    64
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    65
    public static void main(String[] args) throws Exception {
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    66
        t0 = System.currentTimeMillis();
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    67
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    68
        InetAddress addr = InetAddress.getByName(null);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    69
        ServerSocketChannel sc = ServerSocketChannel.open();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    70
        sc.socket().bind(new InetSocketAddress(addr, 0));
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    71
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    72
        SocketChannel.open(new InetSocketAddress(addr,
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    73
                sc.socket().getLocalPort()));
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    74
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    75
        SocketChannel accepted = sc.accept();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    76
        accepted.configureBlocking(false);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    77
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    78
        SocketChannel.open(new InetSocketAddress(addr,
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    79
                sc.socket().getLocalPort()));
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    80
        SocketChannel accepted2 = sc.accept();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    81
        accepted2.configureBlocking(false);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    82
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    83
        final Selector sel = Selector.open();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    84
        SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    85
        final SelectionKey[] key = new SelectionKey[]{
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    86
            accepted.register(sel, SelectionKey.OP_READ)};
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    87
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    88
        // thread that will be changing key[0].interestOps to OP_READ | OP_WRITE
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    89
        new Thread() {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    90
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    91
            public void run() {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    92
                try {
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
    93
                    for (int k = 0; k < NUM_OUTER_LOOP_ITERATIONS; k++) {
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
    94
                        System.out.format("outer loop %3d at %7d ms%n", k,
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
    95
                            System.currentTimeMillis() - t0);
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
    96
                        System.out.flush();
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    97
                        for (int i = 0; i < 10000; i++) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    98
                            synchronized (notifyLock) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
    99
                                synchronized (selectorLock) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   100
                                    sel.wakeup();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   101
                                    key[0].interestOps(SelectionKey.OP_READ
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   102
                                            | SelectionKey.OP_WRITE);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   103
                                }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   104
                                notified = false;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   105
                                long beginTime = System.currentTimeMillis();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   106
                                while (true) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   107
                                    notifyLock.wait(5000);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   108
                                    if (notified) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   109
                                        break;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   110
                                    }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   111
                                    long endTime = System.currentTimeMillis();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   112
                                    if (endTime - beginTime > 5000) {
40250
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   113
                                        for (int j = 0; j < 60; j++) {
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   114
                                            Thread.sleep(1000);
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   115
                                            if (notified) {
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   116
                                                long t =
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   117
                                                    System.currentTimeMillis();
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   118
                                                System.err.printf
40250
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   119
                                                    ("Notified after %d ms%n",
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   120
                                                     t - beginTime);
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
   121
                                                System.err.flush();
40250
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   122
                                                break;
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   123
                                            }
e645cab45a32 8163305: Add some print instrumentation to java/nio/channels/Selector/RacyDeregister
bpb
parents: 16004
diff changeset
   124
                                        }
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   125
                                        succTermination = false;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   126
                                        // wake up main thread doing select()
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   127
                                        sel.wakeup();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   128
                                        return;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   129
                                    }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   130
                                }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   131
                            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   132
                        }
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   133
                        long t = System.currentTimeMillis();
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   134
                        if (t - t0 > TIMEOUT_THRESHOLD_MILLIS) {
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   135
                            System.err.format
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   136
                                ("Timeout after %d outer loop iterations%n", k);
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
   137
                            System.err.flush();
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   138
                            succTermination = false;
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   139
                            // wake up main thread doing select()
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   140
                            sel.wakeup();
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   141
                            return;
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   142
                        }
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   143
                    }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   144
                    succTermination = true;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   145
                    // wake up main thread doing select()
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   146
                    sel.wakeup();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   147
                } catch (Exception e) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   148
                    System.out.println(e);
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
   149
                    System.out.flush();
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   150
                    succTermination = true;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   151
                    // wake up main thread doing select()
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   152
                    sel.wakeup();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   153
                }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   154
            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   155
        }.start();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   156
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   157
        // main thread will be doing registering/deregistering with the sel
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   158
        while (true) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   159
            sel.select();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   160
            if (Boolean.TRUE.equals(succTermination)) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   161
                System.out.println("Test passed");
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
   162
                System.out.flush();
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   163
                sel.close();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   164
                sc.close();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   165
                break;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   166
            } else if (Boolean.FALSE.equals(succTermination)) {
50258
25f93c5406bf 8203766: Add some instrumentation to jdk/java/nio/channels/Selector/RacyDeregister.java
bpb
parents: 47216
diff changeset
   167
                System.err.println("Failed to pass the test");
50794
0ab1cac936b8 8205641: Make java/nio/channels/Selector/RacyDeregister.java noisier and flush after prints
bpb
parents: 50258
diff changeset
   168
                System.err.flush();
16004
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   169
                sel.close();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   170
                sc.close();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   171
                throw new RuntimeException("Failed to pass the test");
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   172
            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   173
            synchronized (selectorLock) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   174
            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   175
            if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   176
                synchronized (notifyLock) {
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   177
                    notified = true;
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   178
                    notifyLock.notify();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   179
                    key[0].cancel();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   180
                    sel.selectNow();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   181
                    key2 = accepted2.register(sel, SelectionKey.OP_READ);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   182
                    key[0] = accepted.register(sel, SelectionKey.OP_READ);
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   183
                }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   184
            }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   185
            key2.cancel();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   186
            sel.selectedKeys().clear();
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   187
        }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   188
    }
6dcf0b33fe6f 6429204: (se) Concurrent Selector.register and SelectionKey.interestOps can ignore interestOps
dingxmin
parents:
diff changeset
   189
}