test/jdk/java/nio/channels/Selector/SelectorUtils.java
author mli
Fri, 31 Aug 2018 10:00:22 +0800
changeset 51602 dbb0e798deeb
permissions -rw-r--r--
8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set" Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51602
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     1
/*
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     4
 *
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     8
 *
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    13
 * accompanied this code).
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    14
 *
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    18
 *
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    21
 * questions.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    22
 */
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    23
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    24
import java.lang.management.ManagementFactory;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    25
import java.lang.management.MonitorInfo;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    26
import java.lang.management.ThreadInfo;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    27
import java.nio.channels.Selector;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    28
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    29
public class SelectorUtils {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    30
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    31
    /**
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    32
     * tell if the monitor of an Object is held by a Thread.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    33
     * @param t    the Thread to hold the monitor of the selected-key set
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    34
     * @param lock the Object
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    35
     * @return
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    36
     */
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    37
    public static boolean mightHoldLock(Thread t, Object lock) {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    38
        long tid = t.getId();
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    39
        int hash = System.identityHashCode(lock);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    40
        ThreadInfo ti = ManagementFactory.getThreadMXBean().
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    41
                getThreadInfo(new long[]{ tid} , true, false, 100)[0];
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    42
        if (ti != null) {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    43
            for (MonitorInfo mi : ti.getLockedMonitors()) {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    44
                if (mi.getIdentityHashCode() == hash)
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    45
                    return true;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    46
            }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    47
        }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    48
        return false;
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    49
    }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    50
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    51
    /**
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    52
     * Spin until the monitor of the selected-key set is likely held
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    53
     * as selected operations are specified to synchronize on the
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    54
     * selected-key set.
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    55
     * @param t   the Thread to hold the monitor of the selected-key set
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    56
     * @param sel the Selector
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    57
     * @throws Exception
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    58
     */
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    59
    public static void spinUntilLocked(Thread t, Selector sel) throws Exception {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    60
        while (!mightHoldLock(t, sel.selectedKeys())) {
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    61
            Thread.sleep(50);
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    62
        }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    63
    }
dbb0e798deeb 8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
mli
parents:
diff changeset
    64
}