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.
  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.util.concurrent.Semaphore;
  36 import java.io.*;
  37 
  38 public class HandshakeDirectTest  implements Runnable {
  39     static final int WORKING_THREADS = 32;
  40     static final int DIRECT_HANDSHAKES_MARK = 50000;
  41     static Thread[] workingThreads = new Thread[WORKING_THREADS];
  42     static Semaphore[] handshakeSem = new Semaphore[WORKING_THREADS];
  43     static Object[] locks = new Object[WORKING_THREADS];
  44     static boolean[] isBiased = new boolean[WORKING_THREADS];
  45     static AtomicInteger handshakeCount = 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 (!isBiased[me]) {
  54                     handshakeSem[me].acquire();
  55                     synchronized(locks[me]) {
  56                         isBiased[me] = true;
  57                     }
  58                     handshakeSem[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                 handshakeSem[handshakee].acquire();
  67                 if (isBiased[handshakee]) {
  68                     // Revoke biased lock
  69                     synchronized(locks[handshakee]) {
  70                         handshakeCount.incrementAndGet();
  71                     }
  72                     // Create new lock to be biased
  73                     locks[handshakee] = new Object();
  74                     isBiased[handshakee] = false;
  75                 }
  76                 handshakeSem[handshakee].release();
  77                 if (handshakeCount.get() >= DIRECT_HANDSHAKES_MARK) {
  78                     break;
  79                 }
  80             } catch(InterruptedException ie) {
  81                 throw new Error("Unexpected interrupt");
  82             }
  83         }
  84     }
  85 
  86     public static void main(String... args) throws Exception {
  87         HandshakeDirectTest test = new HandshakeDirectTest();
  88 
  89         // Initialize semaphores
  90         for (int i = 0; i < WORKING_THREADS; i++) {
  91             handshakeSem[i] = new Semaphore(1);
  92         }
  93 
  94         // Initialize locks
  95         for (int i = 0; i < WORKING_THREADS; i++) {
  96             locks[i] = new Object();
  97         }
  98 
  99         // Fire-up working threads.
 100         for (int i = 0; i < WORKING_THREADS; i++) {
 101             workingThreads[i] = new Thread(test, Integer.toString(i));
 102             workingThreads[i].setDaemon(true);
 103             workingThreads[i].start();
 104         }
 105 
 106         // Wait until the desired number of direct handshakes is reached
 107         workingThreads[0].join();
 108     }
 109 }