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+ * @bug 8335362
27+ * @summary Test virtual thread usage with big stackChunks
28+ * @requires vm.continuations
29+ * @run junit/othervm BigStackChunk
30+ */
31+
32+ import java .util .concurrent .locks .ReentrantLock ;
33+
34+ import org .junit .jupiter .api .Test ;
35+ import static org .junit .jupiter .api .Assertions .*;
36+
37+ class BigStackChunk {
38+
39+ void recurse (int cnt , ReentrantLock rlock ) {
40+ int i1 = cnt ;
41+ int i2 = i1 + 1 ;
42+ int i3 = i2 + 1 ;
43+ int i4 = i3 + 1 ;
44+ int i5 = i4 + 1 ;
45+ int i6 = i5 + 1 ;
46+ int i7 = i6 + 1 ;
47+ long ll = 2 * (long )i1 ;
48+ float ff = ll + 1.2f ;
49+ double dd = ff + 1.3D ;
50+
51+ if (cnt > 0 ) {
52+ recurse (cnt - 1 , rlock );
53+ } else {
54+ rlock .lock ();
55+ rlock .unlock ();
56+ }
57+ }
58+
59+ @ Test
60+ void bigStackChunkTest () throws Exception {
61+ int VTHREAD_CNT = Runtime .getRuntime ().availableProcessors ();
62+ ReentrantLock rlock = new ReentrantLock ();
63+ Thread [] vthreads = new Thread [VTHREAD_CNT ];
64+
65+ rlock .lock ();
66+ for (int i = 0 ; i < VTHREAD_CNT ; i ++) {
67+ vthreads [i ] = Thread .ofVirtual ().start (() -> {
68+ // Set up things so that half of the carriers will commit lots of
69+ // pages in the stack while running the mounted vthread and half
70+ // will just commit very few ones.
71+ if (Math .random () < 0.5 ) {
72+ recurse (300 , rlock );
73+ } else {
74+ recurse (1 , rlock );
75+ }
76+ });
77+ }
78+ await (vthreads [0 ], Thread .State .WAITING );
79+ // Now we expect that some vthread that recursed a lot is mounted on
80+ // a carrier that previously run a vthread that didn't recurse at all.
81+ rlock .unlock ();
82+
83+ for (int i = 0 ; i < VTHREAD_CNT ; i ++) {
84+ vthreads [i ].join ();
85+ }
86+ }
87+
88+ private void await (Thread thread , Thread .State expectedState ) throws InterruptedException {
89+ Thread .State state = thread .getState ();
90+ while (state != expectedState ) {
91+ assertTrue (state != Thread .State .TERMINATED , "Thread has terminated" );
92+ Thread .sleep (10 );
93+ state = thread .getState ();
94+ }
95+ }
96+ }
0 commit comments