Skip to content

Commit e946d7e

Browse files
committed
Undo changes to Monitor.Enter/Exit
Bypass SyncTextWriter on single-threaded wasm to avoid compiling synchronization code during startup when console is touched
1 parent f8b8967 commit e946d7e

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/libraries/System.Console/src/System/Console.cs

+23-10
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,23 @@ static TextWriter EnsureInitialized()
235235

236236
private static TextWriter CreateOutputWriter(Stream outputStream)
237237
{
238-
return outputStream == Stream.Null ?
239-
TextWriter.Null :
240-
TextWriter.Synchronized(new StreamWriter(
241-
stream: outputStream,
242-
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
243-
bufferSize: WriteBufferSize,
244-
leaveOpen: true)
245-
{
246-
AutoFlush = true
247-
});
238+
if (outputStream == Stream.Null)
239+
return TextWriter.Null;
240+
241+
StreamWriter writer = new StreamWriter(
242+
stream: outputStream,
243+
encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream.
244+
bufferSize: WriteBufferSize,
245+
leaveOpen: true
246+
) {
247+
AutoFlush = true
248+
};
249+
250+
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
251+
return writer;
252+
#else
253+
return TextWriter.Synchronized(writer);
254+
#endif
248255
}
249256

250257
private static StrongBox<bool>? _isStdInRedirected;
@@ -702,7 +709,10 @@ public static void SetOut(TextWriter newOut)
702709
// are nops.
703710
if (newOut != TextWriter.Null)
704711
{
712+
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
713+
#else
705714
newOut = TextWriter.Synchronized(newOut);
715+
#endif
706716
}
707717

708718
lock (s_syncObject)
@@ -719,7 +729,10 @@ public static void SetError(TextWriter newError)
719729
// Ensure all access to the writer is synchronized. See comment in SetOut.
720730
if (newError != TextWriter.Null)
721731
{
732+
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
733+
#else
722734
newError = TextWriter.Synchronized(newError);
735+
#endif
723736
}
724737

725738
lock (s_syncObject)

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

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class SyncTextWriter
1414
{
1515
[Fact]
16+
[SkipOnPlatform(TestPlatforms.Browser, "Browser bypasses SyncTextWriter for faster startup")]
1617
public void SyncTextWriterLockedOnThis()
1718
{
1819
TextWriter oldWriter = Console.Out;

src/mono/System.Private.CoreLib/src/System/Threading/Monitor.Mono.cs

-6
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ public static void Exit(object obj)
3030
{
3131
if (obj == null)
3232
ArgumentNullException.ThrowIfNull(obj);
33-
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
34-
#else
3533
if (ObjectHeader.TryExitChecked(obj))
3634
return;
3735

3836
InternalExit(obj);
39-
#endif
4037
}
4138

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

163-
#if TARGET_BROWSER && !FEATURE_WASM_MANAGED_THREADS
164-
#else
165160
// fast path
166161
if (ObjectHeader.TryEnterFast(obj)) {
167162
lockTaken = true;
168163
return;
169164
}
170165

171166
try_enter_with_atomic_var(obj, timeout, true, ref lockTaken);
172-
#endif
173167
}
174168

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

0 commit comments

Comments
 (0)