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