Skip to content

Commit

Permalink
Undo changes to Monitor.Enter/Exit
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 committed Apr 19, 2024
1 parent f8b8967 commit e946d7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
33 changes: 23 additions & 10 deletions src/libraries/System.Console/src/System/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,23 @@ static TextWriter EnsureInitialized()

private static TextWriter CreateOutputWriter(Stream outputStream)
{
return outputStream == Stream.Null ?
TextWriter.Null :
TextWriter.Synchronized(new StreamWriter(
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
bufferSize: WriteBufferSize,
leaveOpen: true)
{
AutoFlush = true
});
if (outputStream == Stream.Null)
return TextWriter.Null;

StreamWriter writer = new StreamWriter(
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
bufferSize: WriteBufferSize,
leaveOpen: true
) {
AutoFlush = true
};

#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
return writer;
#else
return TextWriter.Synchronized(writer);
#endif
}

private static StrongBox<bool>? _isStdInRedirected;
Expand Down Expand Up @@ -702,7 +709,10 @@ public static void SetOut(TextWriter newOut)
// are nops.
if (newOut != TextWriter.Null)
{
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
#else
newOut = TextWriter.Synchronized(newOut);
#endif
}

lock (s_syncObject)
Expand All @@ -719,7 +729,10 @@ public static void SetError(TextWriter newError)
// Ensure all access to the writer is synchronized. See comment in SetOut.
if (newError != TextWriter.Null)
{
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
#else
newError = TextWriter.Synchronized(newError);
#endif
}

lock (s_syncObject)
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Console/tests/SyncTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class SyncTextWriter
{
[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Browser bypasses SyncTextWriter for faster startup")]
public void SyncTextWriterLockedOnThis()
{
TextWriter oldWriter = Console.Out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ public static void Exit(object obj)
{
if (obj == null)
ArgumentNullException.ThrowIfNull(obj);
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
#else
if (ObjectHeader.TryExitChecked(obj))
return;

InternalExit(obj);
#endif
}

public static bool TryEnter(object obj)
Expand Down Expand Up @@ -160,16 +157,13 @@ private static void ReliableEnterTimeout(object obj, int timeout, ref bool lockT
if (timeout < 0 && timeout != (int)Timeout.Infinite)
throw new ArgumentOutOfRangeException(nameof(timeout));

#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
#else
// fast path
if (ObjectHeader.TryEnterFast(obj)) {
lockTaken = true;
return;
}

try_enter_with_atomic_var(obj, timeout, true, ref lockTaken);
#endif
}

public static long LockContentionCount => Monitor_get_lock_contention_count() + Lock.ContentionCount;
Expand Down

0 comments on commit e946d7e

Please sign in to comment.