Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Don't use SyncTextWriter in single-threaded wasm builds #101221

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Comment on lines +161 to +162
Copy link
Member

@akoeplinger akoeplinger Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use PlatformDetection.IsThreadingSupported too instead of checking for browser so it applies to single-threaded wasi too

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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicates the conditions from src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj, I don't think we need them here

</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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also check for WASI

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
Loading