1 /*
 2  * Copyright (c) 2024, 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  * @test id=default
26  * @summary Test virtual thread entering (and reentering) a lot of monitors with no contention
27  * @library /test/lib
28  * @run main/othervm LotsOfUncontendedMonitorEnter
29  */
30 
31 /*
32  * @test id=LM_LEGACY
33  * @library /test/lib
34  * @run main/othervm -XX:LockingMode=1 LotsOfUncontendedMonitorEnter
35  */
36 
37 /*
38  * @test id=LM_LIGHTWEIGHT
39  * @library /test/lib
40  * @run main/othervm -XX:LockingMode=2 LotsOfUncontendedMonitorEnter
41  */
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.concurrent.ThreadLocalRandom;
46 import jdk.test.lib.thread.VThreadRunner;
47 
48 public class LotsOfUncontendedMonitorEnter {
49 
50     public static void main(String[] args) throws Exception {
51         int depth;
52         if (args.length > 0) {
53             depth = Integer.parseInt(args[0]);
54         } else {
55             depth = 24; // 33554430 enters
56         }
57         VThreadRunner.run(() -> {
58             testEnter(List.of(), depth);
59         });
60     }
61 
62     /**
63      * Enter the monitor for a new object, reenter a monitor that is already held, and
64      * repeat to the given depth.
65      */
66     private static void testEnter(List<Object> ownedMonitors, int depthRemaining) {
67         if (depthRemaining > 0) {
68             var lock = new Object();
69             synchronized (lock) {
70                 // new list of owned monitors
71                 var monitors = concat(ownedMonitors, lock);
72                 testEnter(monitors, depthRemaining - 1);
73 
74                 // reenter a monitor that is already owned
75                 int index = ThreadLocalRandom.current().nextInt(monitors.size());
76                 var otherLock = monitors.get(index);
77 
78                 synchronized (otherLock) {
79                     testEnter(monitors, depthRemaining - 1);
80                 }
81             }
82         }
83     }
84 
85     /**
86      * Adds an element to a list, returning a new list.
87      */
88     private static <T> List<T> concat(List<T> list, T object) {
89         var newList = new ArrayList<>(list);
90         newList.add(object);
91         return newList;
92     }
93 }