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  * @run main WriteToReleasesCarrier
 31  */
 32 
 33 import java.io.ByteArrayOutputStream;
 34 import java.io.IOException;
 35 import java.io.OutputStream;
 36 import java.nio.charset.StandardCharsets;
 37 import java.lang.reflect.Constructor;
 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 
 46 public class WriteToReleasesCarrier {
 47     public static void main(String[] args) throws Exception {
 48         byte[] bytes = "Hello".getBytes(StandardCharsets.UTF_8);
 49 
 50         var baos = new ByteArrayOutputStream();
 51         baos.write(bytes);
 52 
 53         var target = new ParkingOutputStream();
 54 
 55         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
 56             Thread.Builder builder = virtualThreadBuilder(scheduler);
 57             var started = new CountDownLatch(1);
 58             var vthread1 = builder.start(() -> {
 59                 started.countDown();
 60                 try {
 61                     baos.writeTo(target);
 62                 } catch (IOException ioe) { }
 63             });
 64             try {
 65                 started.await();
 66                 await(vthread1, Thread.State.WAITING);
 67 
 68                 // carrier should be released, use it for another thread
 69                 var executed = new AtomicBoolean();
 70                 var vthread2 = builder.start(() -> {
 71                     executed.set(true);
 72                 });
 73                 vthread2.join();
 74                 if (!executed.get()) {
 75                     throw new RuntimeException("Second virtual thread did not run");
 76                 }
 77             } finally {
 78                 LockSupport.unpark(vthread1);
 79                 vthread1.join();
 80             }
 81         }
 82 
 83         if (!Arrays.equals(target.toByteArray(), bytes)) {
 84             throw new RuntimeException("Expected bytes not written");
 85         }
 86     }
 87 
 88     /**
 89      * Waits for a thread to get to the expected state.
 90      */
 91     private static void await(Thread thread, Thread.State expectedState) throws Exception {
 92         Thread.State state = thread.getState();
 93         while (state != expectedState) {
 94             Thread.sleep(10);
 95             state = thread.getState();
 96         }
 97     }
 98 
 99     /**
100      * An OutputStream that parks when writing.
101      */
102     static class ParkingOutputStream extends OutputStream {
103         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
104 
105         @Override
106         public void write(int i) {
107             LockSupport.park();
108             baos.write(i);
109         }
110 
111         @Override
112         public void write(byte[] b, int off, int len) {
113             LockSupport.park();
114             baos.write(b, off, len);
115         }
116 
117         byte[] toByteArray() {
118             return baos.toByteArray();
119         }
120     }
121 
122     /**
123      * Returns a builder to create virtual threads that use the given scheduler.
124      */
125     static Thread.Builder.OfVirtual virtualThreadBuilder(Executor scheduler) throws Exception {
126         Class<?> clazz = Class.forName("java.lang.ThreadBuilders$VirtualThreadBuilder");
127         Constructor<?> ctor = clazz.getDeclaredConstructor(Executor.class);
128         ctor.setAccessible(true);
129         return (Thread.Builder.OfVirtual) ctor.newInstance(scheduler);
130     }
131 }