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  * @bug 8330748
 27  * @summary Test ByteArrayOutputStream.writeTo releases carrier thread
 28  * @requires vm.continuations
 29  * @modules java.base/java.lang:+open
 30  * @library /test/lib
 31  * @run main WriteToReleasesCarrier
 32  */
 33 
 34 import java.io.ByteArrayOutputStream;
 35 import java.io.IOException;
 36 import java.io.OutputStream;
 37 import java.nio.charset.StandardCharsets;
 38 import java.util.Arrays;
 39 import java.util.concurrent.CountDownLatch;
 40 import java.util.concurrent.ExecutorService;
 41 import java.util.concurrent.Executor;
 42 import java.util.concurrent.Executors;
 43 import java.util.concurrent.atomic.AtomicBoolean;
 44 import java.util.concurrent.locks.LockSupport;
 45 import jdk.test.lib.thread.VThreadScheduler;
 46 
 47 public class WriteToReleasesCarrier {
 48     public static void main(String[] args) throws Exception {
 49         byte[] bytes = "Hello".getBytes(StandardCharsets.UTF_8);
 50 
 51         var baos = new ByteArrayOutputStream();
 52         baos.write(bytes);
 53 
 54         var target = new ParkingOutputStream();
 55 
 56         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
 57             Thread.Builder builder = VThreadScheduler.virtualThreadBuilder(scheduler);
 58             var started = new CountDownLatch(1);
 59             var vthread1 = builder.start(() -> {
 60                 started.countDown();
 61                 try {
 62                     baos.writeTo(target);
 63                 } catch (IOException ioe) { }
 64             });
 65             try {
 66                 started.await();
 67                 await(vthread1, Thread.State.WAITING);
 68 
 69                 // carrier should be released, use it for another thread
 70                 var executed = new AtomicBoolean();
 71                 var vthread2 = builder.start(() -> {
 72                     executed.set(true);
 73                 });
 74                 vthread2.join();
 75                 if (!executed.get()) {
 76                     throw new RuntimeException("Second virtual thread did not run");
 77                 }
 78             } finally {
 79                 LockSupport.unpark(vthread1);
 80                 vthread1.join();
 81             }
 82         }
 83 
 84         if (!Arrays.equals(target.toByteArray(), bytes)) {
 85             throw new RuntimeException("Expected bytes not written");
 86         }
 87     }
 88 
 89     /**
 90      * Waits for a thread to get to the expected state.
 91      */
 92     private static void await(Thread thread, Thread.State expectedState) throws Exception {
 93         Thread.State state = thread.getState();
 94         while (state != expectedState) {
 95             Thread.sleep(10);
 96             state = thread.getState();
 97         }
 98     }
 99 
100     /**
101      * An OutputStream that parks when writing.
102      */
103     static class ParkingOutputStream extends OutputStream {
104         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
105 
106         @Override
107         public void write(int i) {
108             LockSupport.park();
109             baos.write(i);
110         }
111 
112         @Override
113         public void write(byte[] b, int off, int len) {
114             LockSupport.park();
115             baos.write(b, off, len);
116         }
117 
118         byte[] toByteArray() {
119             return baos.toByteArray();
120         }
121     }
122 }