Skip to content

Commit 5b7e77d

Browse files
authored
[wasm] Don't use SyncTextWriter in single-threaded wasm builds (#101221)
Bypass SyncTextWriter on single-threaded wasm to avoid compiling synchronization code during startup when console is touched
1 parent 02bd1da commit 5b7e77d

File tree

7 files changed

+17
-5
lines changed

7 files changed

+17
-5
lines changed

src/libraries/System.Console/tests/ReadAndWrite.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ public static async Task OutWriteAndWriteLineOverloads()
158158
Console.SetOut(sw);
159159
TextWriter writer = Console.Out;
160160
Assert.NotNull(writer);
161-
Assert.NotEqual(writer, sw); // the writer we provide gets wrapped
161+
// Browser bypasses SyncTextWriter for faster startup
162+
if (!OperatingSystem.IsBrowser())
163+
Assert.NotEqual(writer, sw); // the writer we provide gets wrapped
162164

163165
// We just want to ensure none of these throw exceptions, we don't actually validate
164166
// what was written.

src/libraries/System.Console/tests/SyncTextWriter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
public class SyncTextWriter
1414
{
15-
[Fact]
15+
// Browser bypasses SyncTextWriter for faster startup
16+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
1617
public void SyncTextWriterLockedOnThis()
1718
{
1819
TextWriter oldWriter = Console.Out;

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<IsBigEndian Condition="'$(Platform)' == 's390x'">true</IsBigEndian>
2323
<Is64Bit Condition="'$(Platform)' == 'arm64' or '$(Platform)' == 'x64' or '$(Platform)' == 's390x' or '$(Platform)' == 'loongarch64' or '$(Platform)' == 'ppc64le' or '$(Platform)' == 'riscv64'">true</Is64Bit>
2424
<UseMinimalGlobalizationData Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">true</UseMinimalGlobalizationData>
25+
<FeatureWasmManagedThreads Condition="'$(WasmEnableThreads)' == 'true'">true</FeatureWasmManagedThreads>
26+
<DefineConstants Condition="'$(FeatureWasmManagedThreads)' == 'true'">$(DefineConstants);FEATURE_WASM_MANAGED_THREADS</DefineConstants>
2527
</PropertyGroup>
2628
<PropertyGroup>
2729
<DefineConstants Condition="'$(IsBigEndian)' == 'true'">$(DefineConstants);BIGENDIAN</DefineConstants>

src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs

+4
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,11 @@ public static TextWriter Synchronized(TextWriter writer)
759759
{
760760
ArgumentNullException.ThrowIfNull(writer);
761761

762+
#if !TARGET_BROWSER || FEATURE_WASM_MANAGED_THREADS
762763
return writer is SyncTextWriter ? writer : new SyncTextWriter(writer);
764+
#else
765+
return writer;
766+
#endif
763767
}
764768

765769
internal sealed class SyncTextWriter : TextWriter, IDisposable

src/libraries/System.Runtime/tests/System.IO.Tests/StreamReader/StreamReader.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public void ObjectClosedReadLineBaseStream()
2929
Assert.Throws<ObjectDisposedException>(() => sr.ReadLine());
3030
}
3131

32-
[Fact]
32+
// Browser bypasses SyncTextWriter for faster startup
33+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
3334
public void Synchronized_NewObject()
3435
{
3536
using (Stream str = GetLargeStream())

src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace System.IO.Tests
77
{
88
public partial class WriteTests
99
{
10-
[Fact]
10+
// Browser bypasses SyncTextWriter for faster startup
11+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
1112
public void Synchronized_NewObject()
1213
{
1314
using (Stream str = CreateStream())

src/libraries/System.Runtime/tests/System.IO.Tests/TextWriter/TextWriterTests.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ public void DisposeAsync_ExceptionReturnedInTask()
690690
Assert.Same(e, vt.AsTask().Exception.InnerException);
691691
}
692692

693-
[Fact]
693+
// Browser bypasses SyncTextWriter for faster startup
694+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
694695
public async Task FlushAsync_Precanceled()
695696
{
696697
Assert.Equal(TaskStatus.RanToCompletion, TextWriter.Null.FlushAsync(new CancellationToken(true)).Status);

0 commit comments

Comments
 (0)