--- /dev/null 2019-10-31 15:42:43.283000000 +0000 +++ new/test/hotspot/jtreg/runtime/handshake/HandshakeDirectTest.java 2020-01-13 15:15:57.882763728 +0000 @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +/* + * @test HandshakeDirectTest + * @summary This test tries to stress direct handshakes between threads while suspending them. + * @library /testlibrary /test/lib + * @build HandshakeDirectTest + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest + */ + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.ThreadLocalRandom; +import java.io.*; + +public class HandshakeDirectTest implements Runnable { + static final int WORKING_THREADS = 32; + static final int DIRECT_HANDSHAKES_MARK = 50000; + static Thread[] _working_threads = new Thread[WORKING_THREADS]; + static java.util.concurrent.Semaphore[] _handshake_sem = new java.util.concurrent.Semaphore[WORKING_THREADS]; + static Object[] _locks = new Object[WORKING_THREADS]; + static boolean[] _is_biased = new boolean[WORKING_THREADS]; + static Thread _suspendresume_thread = new Thread(); + static AtomicInteger _handshake_count = new AtomicInteger(0); + + @Override + public void run() { + int me = Integer.parseInt(Thread.currentThread().getName()); + + while (true) { + try { + if (_is_biased[me] == false) { + _handshake_sem[me].acquire(); + synchronized(_locks[me]) { + _is_biased[me] = true; + } + _handshake_sem[me].release(); + } + + // Handshake directly some other worker + int handshakee = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1); + if (handshakee == me) { + handshakee = handshakee != 0 ? handshakee - 1 : handshakee + 1; + } + _handshake_sem[handshakee].acquire(); + if (_is_biased[handshakee]) { + // Revoke biased lock + synchronized(_locks[handshakee]) { + _handshake_count.incrementAndGet(); + } + // Create new lock to be biased + _locks[handshakee] = new Object(); + _is_biased[handshakee] = false; + } + _handshake_sem[handshakee].release(); + if (_handshake_count.get() >= DIRECT_HANDSHAKES_MARK) { + break; + } + } catch(InterruptedException ie) { + } + } + } + + public static void main(String... args) throws Exception { + HandshakeDirectTest test = new HandshakeDirectTest(); + + // Initialize semaphores + for (int i = 0; i < WORKING_THREADS; i++) { + _handshake_sem[i] = new java.util.concurrent.Semaphore(1); + } + + // Initialize locks + for (int i = 0; i < WORKING_THREADS; i++) { + _locks[i] = new Object(); + } + + // Fire-up working threads. + for (int i = 0; i < WORKING_THREADS; i++) { + _working_threads[i] = new Thread(test, Integer.toString(i)); + _working_threads[i].setDaemon(true); + _working_threads[i].start(); + } + + // Fire-up suspend-resume thread + Thread _suspendresume_thread = new Thread() { + @Override + public void run() { + while (true) { + int i = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1); + _working_threads[i].suspend(); + try { + Thread.sleep(1); // sleep for 1 ms + } catch(InterruptedException ie) { + } + _working_threads[i].resume(); + } + } + }; + _suspendresume_thread.setDaemon(true); + _suspendresume_thread.start(); + + // Wait until the desired number of direct handshakes is reached + while (_handshake_count.get() < DIRECT_HANDSHAKES_MARK) { + Thread.sleep(10); // sleep for 10ms + } + } +}