1 /*
 2  * Copyright (c) 2023, 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 mutual exclusion of monitors with platform and virtual threads
27  * @run junit MonitorMutualExclusion
28  */
29 
30 import java.util.stream.IntStream;
31 import java.util.stream.Stream;
32 
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.Arguments;
35 import org.junit.jupiter.params.provider.MethodSource;
36 import static org.junit.jupiter.api.Assertions.*;
37 
38 class MonitorMutualExclusion {
39 
40     /**
41      * Returns a stream of elements that are ordered pairs of platform and virtual thread
42      * counts (0,2,4,..16 platform threads, 2,4,6,..32 virtual threads).
43      */
44     static Stream<Arguments> threadCounts() {
45         return IntStream.range(0, 17)
46                 .filter(i -> i % 2 == 0)
47                 .mapToObj(i -> i)
48                 .flatMap(np -> IntStream.range(2, 33)
49                         .filter(i -> i % 2 == 0)
50                         .mapToObj(vp -> Arguments.of(np, vp)));
51     }
52 
53     @ParameterizedTest
54     @MethodSource("threadCounts")
55     void testExclusion(int nPlatformThreads, int nVirtualThreads) throws Exception {
56         class Counter {
57             int count;
58             synchronized void increment() {
59                 count++;
60                 Thread.yield();
61             }
62         }
63         var counter = new Counter();
64         int nThreads = nPlatformThreads + nVirtualThreads;
65         var threads = new Thread[nThreads];
66         int index = 0;
67         for (int i = 0; i < nPlatformThreads; i++) {
68             threads[index++] = Thread.ofPlatform().unstarted(counter::increment);
69         }
70         for (int i = 0; i < nVirtualThreads; i++) {
71             threads[index++] = Thread.ofVirtual().unstarted(counter::increment);
72         }
73         // start all threads
74         for (Thread thread : threads) {
75             thread.start();
76         }
77         // wait for all threads to terminate
78         for (Thread thread : threads) {
79             thread.join();
80         }
81         assertEquals(nThreads, counter.count);
82     }
83 }
84