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  * @bug 8240918
28  * @summary This test tries to stress direct handshakes between threads while suspending them.
29  * @library /testlibrary /test/lib
30  * @build HandshakeDirectTest
31  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UseBiasedLocking -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest
32  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+UseBiasedLocking -XX:GuaranteedSafepointInterval=10 -XX:+HandshakeALot -XX:+SafepointALot -XX:BiasedLockingDecayTime=100000000 -XX:BiasedLockingBulkRebiasThreshold=1000000 -XX:BiasedLockingBulkRevokeThreshold=1000000 HandshakeDirectTest
33  */
34 
35 import java.util.concurrent.atomic.AtomicInteger;
36 import java.util.concurrent.ThreadLocalRandom;
37 import java.util.concurrent.Semaphore;
38 import java.io.*;
39 
40 public class HandshakeDirectTest  implements Runnable {
41     static final int WORKING_THREADS = 32;
42     static final int DIRECT_HANDSHAKES_MARK = 50000;
43     static Thread[] workingThreads = new Thread[WORKING_THREADS];
44     static Semaphore[] handshakeSem = new Semaphore[WORKING_THREADS];
45     static Object[] locks = new Object[WORKING_THREADS];
46     static boolean[] isBiased = new boolean[WORKING_THREADS];
47     static AtomicInteger handshakeCount = new AtomicInteger(0);
48 
49     @Override
run()50     public void run() {
51         int me = Integer.parseInt(Thread.currentThread().getName());
52 
53         while (true) {
54             try {
55                 if (!isBiased[me]) {
56                     handshakeSem[me].acquire();
57                     synchronized(locks[me]) {
58                         isBiased[me] = true;
59                     }
60                     handshakeSem[me].release();
61                 }
62 
63                 // Handshake directly some other worker
64                 int handshakee = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS - 1);
65                 if (handshakee == me) {
66                     // Pick another thread instead of me.
67                     handshakee = handshakee != 0 ? handshakee - 1 : handshakee + 1;
68                 }
69                 handshakeSem[handshakee].acquire();
70                 if (isBiased[handshakee]) {
71                     // Revoke biased lock
72                     synchronized(locks[handshakee]) {
73                         handshakeCount.incrementAndGet();
74                     }
75                     // Create new lock to be biased
76                     locks[handshakee] = new Object();
77                     isBiased[handshakee] = false;
78                 }
79                 handshakeSem[handshakee].release();
80                 if (handshakeCount.get() >= DIRECT_HANDSHAKES_MARK) {
81                     break;
82                 }
83             } catch(InterruptedException ie) {
84                 throw new Error("Unexpected interrupt");
85             }
86         }
87     }
88 
main(String... args)89     public static void main(String... args) throws Exception {
90         HandshakeDirectTest test = new HandshakeDirectTest();
91 
92         // Initialize semaphores
93         for (int i = 0; i < WORKING_THREADS; i++) {
94             handshakeSem[i] = new Semaphore(1);
95         }
96 
97         // Initialize locks
98         for (int i = 0; i < WORKING_THREADS; i++) {
99             locks[i] = new Object();
100         }
101 
102         // Fire-up working threads.
103         for (int i = 0; i < WORKING_THREADS; i++) {
104             workingThreads[i] = new Thread(test, Integer.toString(i));
105             workingThreads[i].start();
106         }
107 
108         // Fire-up suspend-resume thread
109         Thread suspendResumeThread = new Thread() {
110             @Override
111             public void run() {
112                 while (true) {
113                     int i = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS - 1);
114                     workingThreads[i].suspend();
115                     try {
116                         Thread.sleep(1); // sleep for 1 ms
117                     } catch(InterruptedException ie) {
118                     }
119                     workingThreads[i].resume();
120                 }
121             }
122         };
123         suspendResumeThread.setDaemon(true);
124         suspendResumeThread.start();
125 
126         // Wait until the desired number of direct handshakes is reached
127         // and check that all workers exited
128         for (int i = 0; i < WORKING_THREADS; i++) {
129             workingThreads[i].join();
130         }
131     }
132 }
133