Skip to content

Commit 819f3d6

Browse files
author
Brian Burkhalter
committed
8330748: ByteArrayOutputStream.writeTo(OutputStream) pins carrier
Reviewed-by: alanb
1 parent eb88343 commit 819f3d6

File tree

2 files changed

+142
-3
lines changed

2 files changed

+142
-3
lines changed

src/java.base/share/classes/java/io/ByteArrayOutputStream.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -159,8 +159,16 @@ public void writeBytes(byte[] b) {
159159
* @throws NullPointerException if {@code out} is {@code null}.
160160
* @throws IOException if an I/O error occurs.
161161
*/
162-
public synchronized void writeTo(OutputStream out) throws IOException {
163-
out.write(buf, 0, count);
162+
public void writeTo(OutputStream out) throws IOException {
163+
if (Thread.currentThread().isVirtual()) {
164+
byte[] bytes;
165+
synchronized (this) {
166+
bytes = Arrays.copyOf(buf, count);
167+
}
168+
out.write(bytes);
169+
} else synchronized (this) {
170+
out.write(buf, 0, count);
171+
}
164172
}
165173

166174
/**
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

0 commit comments

Comments
 (0)