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
 26  * @summary Test that a virtual thread waiting to enter a monitor, while pinning its
 27  *   carrier, will retry until it enters the monitor. This avoids starvation when the
 28  *   monitor is exited, an unmounted thread is the chosen successor, and the successor
 29  *   can't continue because there are no carriers available.
 30  * @modules java.base/java.lang:+open
 31  * @library /test/lib
 32  * @requires vm.opt.LockingMode != 1
 33  * @run main/othervm --enable-native-access=ALL-UNNAMED RetryMonitorEnterWhenPinned
 34  */
 35 
 36 import java.time.Duration;
 37 import java.time.Instant;
 38 import java.util.ArrayList;
 39 import java.util.concurrent.CountDownLatch;
 40 import java.util.concurrent.TimeUnit;
 41 import jdk.test.lib.thread.VThreadPinner;
 42 
 43 public class RetryMonitorEnterWhenPinned {
 44     public static void main(String[] args) throws Exception {
 45         int iterations = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
 46         for (int i = 1; i <= iterations; i++) {
 47             System.out.printf("%s -- iteration %d --%n", Instant.now(), i);
 48             run();
 49             System.out.println();
 50         }
 51     }
 52 
 53     static void run() throws Exception {
 54         var threads = new ArrayList<Thread>();
 55 
 56         Object lock = new Object();
 57         synchronized (lock) {
 58 
 59             // start virtual threads that block on monitorenter
 60             for (int i = 0; i < 100; i++) {
 61                 var started = new CountDownLatch(1);
 62                 Thread thread = Thread.startVirtualThread(() -> {
 63                     started.countDown();
 64                     synchronized (lock) {
 65                         spin(20);
 66                     }
 67                 });
 68 
 69                 // wait for thread to start and block
 70                 started.await();
 71                 await(thread, Thread.State.BLOCKED);
 72                 threads.add(thread);
 73             }
 74 
 75             // start virtual threads that block on monitorenter while pinned
 76             int carriersAvailable = Runtime.getRuntime().availableProcessors();
 77             if (Thread.currentThread().isVirtual()) {
 78                 carriersAvailable--;
 79             }
 80             for (int i = 0; i < 100; i++) {
 81                 var started = new CountDownLatch(1);
 82                 Thread thread = Thread.startVirtualThread(() -> {
 83                     started.countDown();
 84                     VThreadPinner.runPinned(() -> {
 85                         synchronized (lock) {
 86                             spin(20);
 87                         }
 88                     });
 89                 });
 90 
 91                 // if there are carriers available when wait until the thread blocks.
 92                 if (carriersAvailable > 0) {
 93                     System.out.printf("%s waiting for thread #%d to block%n",
 94                             Instant.now(), thread.threadId());
 95                     started.await();
 96                     await(thread, Thread.State.BLOCKED);
 97                     carriersAvailable--;
 98                 }
 99                 threads.add(thread);
100             }
101 
102         } // exit monitor
103 
104         // wait for all threads to terminate
105         int threadsRemaining = threads.size();
106         while (threadsRemaining > 0) {
107             System.out.printf("%s waiting for %d threads to terminate%n",
108                     Instant.now(), threadsRemaining);
109             int terminated = 0;
110             for (Thread t : threads) {
111                 if (t.join(Duration.ofSeconds(1))) {
112                     terminated++;
113                 }
114             }
115             threadsRemaining = threads.size() - terminated;
116         }
117         System.out.printf("%s done%n", Instant.now());
118     }
119 
120     /**
121      * Spin for the given number of milliseconds.
122      */
123     static void spin(long millis) {
124         long nanos = TimeUnit.MILLISECONDS.toNanos(millis);
125         long start = System.nanoTime();
126         while ((System.nanoTime() - start) < nanos) {
127             Thread.onSpinWait();
128         }
129     }
130 
131     /**
132      * Wait for a thread to reach the expected state.
133      */
134     static void await(Thread thread, Thread.State expectedState) throws InterruptedException {
135         Thread.State state = thread.getState();
136         while (state != expectedState) {
137             assert state != Thread.State.TERMINATED : "Thread has terminated";
138             Thread.sleep(10);
139             state = thread.getState();
140         }
141     }
142 }