Skip to content

Commit

Permalink
[wasm] Don't use SyncTextWriter in single-threaded wasm builds (#101221)
Browse files Browse the repository at this point in the history
Bypass SyncTextWriter on single-threaded wasm to avoid compiling synchronization code during startup when console is touched
  • Loading branch information
kg authored Jul 5, 2024
1 parent 02bd1da commit 5b7e77d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/libraries/System.Console/tests/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public static async Task OutWriteAndWriteLineOverloads()
Console.SetOut(sw);
TextWriter writer = Console.Out;
Assert.NotNull(writer);
Assert.NotEqual(writer, sw); // the writer we provide gets wrapped
// Browser bypasses SyncTextWriter for faster startup
if (!OperatingSystem.IsBrowser())
Assert.NotEqual(writer, sw); // the writer we provide gets wrapped

// We just want to ensure none of these throw exceptions, we don't actually validate
// what was written.
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Console/tests/SyncTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

public class SyncTextWriter
{
[Fact]
// Browser bypasses SyncTextWriter for faster startup
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void SyncTextWriterLockedOnThis()
{
TextWriter oldWriter = Console.Out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<IsBigEndian Condition="'$(Platform)' == 's390x'">true</IsBigEndian>
<Is64Bit Condition="'$(Platform)' == 'arm64' or '$(Platform)' == 'x64' or '$(Platform)' == 's390x' or '$(Platform)' == 'loongarch64' or '$(Platform)' == 'ppc64le' or '$(Platform)' == 'riscv64'">true</Is64Bit>
<UseMinimalGlobalizationData Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">true</UseMinimalGlobalizationData>
<FeatureWasmManagedThreads Condition="'$(WasmEnableThreads)' == 'true'">true</FeatureWasmManagedThreads>
<DefineConstants Condition="'$(FeatureWasmManagedThreads)' == 'true'">$(DefineConstants);FEATURE_WASM_MANAGED_THREADS</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(IsBigEndian)' == 'true'">$(DefineConstants);BIGENDIAN</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,11 @@ public static TextWriter Synchronized(TextWriter writer)
{
ArgumentNullException.ThrowIfNull(writer);

#if !TARGET_BROWSER || FEATURE_WASM_MANAGED_THREADS
return writer is SyncTextWriter ? writer : new SyncTextWriter(writer);
#else
return writer;
#endif
}

internal sealed class SyncTextWriter : TextWriter, IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public void ObjectClosedReadLineBaseStream()
Assert.Throws<ObjectDisposedException>(() => sr.ReadLine());
}

[Fact]
// Browser bypasses SyncTextWriter for faster startup
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void Synchronized_NewObject()
{
using (Stream str = GetLargeStream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace System.IO.Tests
{
public partial class WriteTests
{
[Fact]
// Browser bypasses SyncTextWriter for faster startup
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void Synchronized_NewObject()
{
using (Stream str = CreateStream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ public void DisposeAsync_ExceptionReturnedInTask()
Assert.Same(e, vt.AsTask().Exception.InnerException);
}

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

0 comments on commit 5b7e77d

Please sign in to comment.