1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 /* 26 * @test HandshakeDirectTest 27 * @summary This test tries to stress direct handshakes between threads while suspending them. 28 * @library /testlibrary /test/lib 29 * @build HandshakeDirectTest 30 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest 31 */ 32 33 import java.util.concurrent.atomic.AtomicInteger; 34 import java.util.concurrent.ThreadLocalRandom; 35 import java.io.*; 36 37 public class HandshakeDirectTest implements Runnable { 38 static final int WORKING_THREADS = 32; 39 static final int DIRECT_HANDSHAKES_MARK = 50000; 40 static Thread[] _working_threads = new Thread[WORKING_THREADS]; 41 static java.util.concurrent.Semaphore[] _handshake_sem = new java.util.concurrent.Semaphore[WORKING_THREADS]; 42 static Object[] _locks = new Object[WORKING_THREADS]; 43 static boolean[] _is_biased = new boolean[WORKING_THREADS]; 44 static Thread _suspendresume_thread = new Thread(); 45 static AtomicInteger _handshake_count = new AtomicInteger(0); 46 47 @Override 48 public void run() { 49 int me = Integer.parseInt(Thread.currentThread().getName()); 50 51 while (true) { 52 try { 53 if (_is_biased[me] == false) { 54 _handshake_sem[me].acquire(); 55 synchronized(_locks[me]) { 56 _is_biased[me] = true; 57 } 58 _handshake_sem[me].release(); 59 } 60 61 // Handshake directly some other worker 62 int handshakee = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1); 63 if (handshakee == me) { 64 handshakee = handshakee != 0 ? handshakee - 1 : handshakee + 1; 65 } 66 _handshake_sem[handshakee].acquire(); 67 if (_is_biased[handshakee]) { 68 // Revoke biased lock 69 synchronized(_locks[handshakee]) { 70 _handshake_count.incrementAndGet(); 71 } 72 // Create new lock to be biased 73 _locks[handshakee] = new Object(); 74 _is_biased[handshakee] = false; 75 } 76 _handshake_sem[handshakee].release(); 77 if (_handshake_count.get() >= DIRECT_HANDSHAKES_MARK) { 78 break; 79 } 80 } catch(InterruptedException ie) { 81 } 82 } 83 } 84 85 public static void main(String... args) throws Exception { 86 HandshakeDirectTest test = new HandshakeDirectTest(); 87 88 // Initialize semaphores 89 for (int i = 0; i < WORKING_THREADS; i++) { 90 _handshake_sem[i] = new java.util.concurrent.Semaphore(1); 91 } 92 93 // Initialize locks 94 for (int i = 0; i < WORKING_THREADS; i++) { 95 _locks[i] = new Object(); 96 } 97 98 // Fire-up working threads. 99 for (int i = 0; i < WORKING_THREADS; i++) { 100 _working_threads[i] = new Thread(test, Integer.toString(i)); 101 _working_threads[i].setDaemon(true); 102 _working_threads[i].start(); 103 } 104 105 // Fire-up suspend-resume thread 106 Thread _suspendresume_thread = new Thread() { 107 @Override 108 public void run() { 109 while (true) { 110 int i = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS-1); 111 _working_threads[i].suspend(); 112 try { 113 Thread.sleep(1); // sleep for 1 ms 114 } catch(InterruptedException ie) { 115 } 116 _working_threads[i].resume(); 117 } 118 } 119 }; 120 _suspendresume_thread.setDaemon(true); 121 _suspendresume_thread.start(); 122 123 // Wait until the desired number of direct handshakes is reached 124 while (_handshake_count.get() < DIRECT_HANDSHAKES_MARK) { 125 Thread.sleep(10); // sleep for 10ms 126 } 127 } 128 }