From 28dcc44eef69c4f323f801967c4ec3c6e9b72cff Mon Sep 17 00:00:00 2001 From: Pavel Savara Date: Thu, 9 Nov 2023 08:32:16 +0100 Subject: [PATCH] [browser][MT] GC, more testing and cleanup (#94466) --- .../tests/WasmTestRunner/WasmTestRunner.cs | 1 + .../tests/HostFactoryResolverTests.cs | 1 - .../JsonConsoleFormatterTests.cs | 9 ----- ...ft.Extensions.Logging.Console.Tests.csproj | 3 +- .../tests/ProducerConsumerCollectionTests.cs | 1 + .../tests/ExchangeTests.cs | 1 + .../tests/CancellationTokenTests.cs | 1 + .../tests/MethodCoverage.cs | 2 + .../tests/System.Threading.Tasks.Tests.csproj | 1 + ...syncEnumerableToBlockingEnumerableTests.cs | 3 ++ .../tests/Task/TaskContinueWithTests.cs | 1 + .../System.Threading.ThreadPool.Tests.csproj | 1 - src/libraries/tests.proj | 9 ----- src/mono/mono/component/diagnostics_server.c | 2 +- src/mono/mono/metadata/gc.c | 19 +--------- src/mono/mono/metadata/sgen-mono.c | 2 +- src/mono/mono/metadata/sgen-stw.c | 2 +- src/mono/mono/metadata/threads.c | 2 +- src/mono/mono/sgen/sgen-gc.c | 2 +- src/mono/mono/utils/mono-threads-wasm.c | 38 +++++++++++-------- src/mono/mono/utils/mono-threads-wasm.h | 13 ++++--- src/mono/mono/utils/mono-threads.h | 2 + src/mono/wasm/runtime/corebindings.c | 2 - src/mono/wasm/runtime/cwraps.ts | 8 ++-- .../runtime/diagnostics/browser/controller.ts | 2 +- .../diagnostics/server_pthread/index.ts | 2 +- .../server_pthread/streaming-session.ts | 2 +- .../diagnostics/shared/create-session.ts | 2 +- src/mono/wasm/runtime/driver.c | 2 +- 29 files changed, 61 insertions(+), 75 deletions(-) diff --git a/src/libraries/Common/tests/WasmTestRunner/WasmTestRunner.cs b/src/libraries/Common/tests/WasmTestRunner/WasmTestRunner.cs index 1307a18a9eb51d..c68d939a1fffe6 100644 --- a/src/libraries/Common/tests/WasmTestRunner/WasmTestRunner.cs +++ b/src/libraries/Common/tests/WasmTestRunner/WasmTestRunner.cs @@ -64,6 +64,7 @@ public static async Task Main(string[] args) IncludedMethods = includedMethods }; + await Task.Yield(); return await runner.Run(); } } diff --git a/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs b/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs index fee56f79fb003e..3982ae29846862 100644 --- a/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs +++ b/src/libraries/Microsoft.Extensions.HostFactoryResolver/tests/HostFactoryResolverTests.cs @@ -279,7 +279,6 @@ public void ApplicationNameSetFromArgument() [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(NoSpecialEntryPointPattern.Program))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void NoSpecialEntryPointPatternCanRunInParallel() { var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPattern.Program).Assembly, s_WaitTimeout); diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs index 153ba5503eeb47..0ef491abbc1723 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/JsonConsoleFormatterTests.cs @@ -18,7 +18,6 @@ namespace Microsoft.Extensions.Logging.Console.Test public class JsonConsoleFormatterTests : ConsoleFormatterTests { [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void NoLogScope_DoesNotWriteAnyScopeContentToOutput_Json() { // Arrange @@ -80,7 +79,6 @@ public void Log_TimestampFormatSet_ContainsTimestamp() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_NullMessage_LogsWhenMessageIsNotProvided() { // Arrange @@ -125,7 +123,6 @@ public void Log_NullMessage_LogsWhenMessageIsNotProvided() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_ExceptionWithMessage_ExtractsInfo() { // Arrange @@ -180,7 +177,6 @@ public void Log_ExceptionWithMessage_ExtractsInfo() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_IncludeScopes_ContainsDuplicateNamedPropertiesInScope_AcceptableJson() { // Arrange @@ -214,7 +210,6 @@ public void Log_IncludeScopes_ContainsDuplicateNamedPropertiesInScope_Acceptable } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_StateAndScopeAreCollections_IncludesMessageAndCollectionValues() { // Arrange @@ -250,7 +245,6 @@ public void Log_StateAndScopeAreCollections_IncludesMessageAndCollectionValues() } [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] [MemberData(nameof(SpecialCaseValues))] public void Log_StateAndScopeContainsSpecialCaseValue_SerializesValueAsExpected(object value, string expectedJsonValue) { @@ -281,7 +275,6 @@ public void Log_StateAndScopeContainsSpecialCaseValue_SerializesValueAsExpected( } [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] [MemberData(nameof(FloatingPointValues))] public void Log_StateAndScopeContainsFloatingPointType_SerializesValue(object value) { @@ -321,7 +314,6 @@ static void AssertMessageValue(string message, string propertyName) } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_StateAndScopeContainsNullValue_SerializesNull() { // Arrange @@ -351,7 +343,6 @@ public void Log_StateAndScopeContainsNullValue_SerializesNull() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] - [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void Log_ScopeIsIEnumerable_SerializesKeyValuePair() { // Arrange diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/Microsoft.Extensions.Logging.Console.Tests.csproj b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/Microsoft.Extensions.Logging.Console.Tests.csproj index c1f8f924a4b2ee..b2801823f13f5b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/Microsoft.Extensions.Logging.Console.Tests.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/Microsoft.Extensions.Logging.Console.Tests.csproj @@ -5,10 +5,11 @@ true true true + <_WasmPThreadPoolSize Condition="'$(MonoWasmBuildVariant)' == 'multithread'">100 - \ No newline at end of file + diff --git a/src/libraries/System.Collections.Concurrent/tests/ProducerConsumerCollectionTests.cs b/src/libraries/System.Collections.Concurrent/tests/ProducerConsumerCollectionTests.cs index 7aae42fcae3eca..f7a9c219aceb56 100644 --- a/src/libraries/System.Collections.Concurrent/tests/ProducerConsumerCollectionTests.cs +++ b/src/libraries/System.Collections.Concurrent/tests/ProducerConsumerCollectionTests.cs @@ -483,6 +483,7 @@ public void ICollectionCopyTo_InvalidArgs_Throws() [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] [InlineData(100, 1, 10)] [InlineData(4, 100000, 10)] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public void BlockingCollection_WrappingCollection_ExpectedElementsTransferred(int numThreadsPerConsumerProducer, int numItemsPerThread, int producerSpin) { var bc = new BlockingCollection(CreateProducerConsumerCollection()); diff --git a/src/libraries/System.Linq.Parallel/tests/ExchangeTests.cs b/src/libraries/System.Linq.Parallel/tests/ExchangeTests.cs index 6dcaffbf60a5a5..5c36f46c5d2073 100644 --- a/src/libraries/System.Linq.Parallel/tests/ExchangeTests.cs +++ b/src/libraries/System.Linq.Parallel/tests/ExchangeTests.cs @@ -90,6 +90,7 @@ public static IEnumerable AllMergeOptions_Multiple() [ConditionalTheory] [MemberData(nameof(PartitioningData), new[] { 0, 1, 2, 16, 1024 })] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void Partitioning_Default(Labeled> labeled, int count, int partitions) { if (partitions > 1 && !PlatformDetection.IsThreadingSupported) diff --git a/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs b/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs index 72df26fa4c9886..5759199752ad66 100644 --- a/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs +++ b/src/libraries/System.Threading.Tasks/tests/CancellationTokenTests.cs @@ -874,6 +874,7 @@ static void FinalizeHelper(DisposeTracker disposeTracker) // Several tests for deriving custom user types from CancellationTokenSource [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/94486", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void DerivedCancellationTokenSource() { // Verify that a derived CTS is functional diff --git a/src/libraries/System.Threading.Tasks/tests/MethodCoverage.cs b/src/libraries/System.Threading.Tasks/tests/MethodCoverage.cs index 923875f6e43cee..88ec6cdca29481 100644 --- a/src/libraries/System.Threading.Tasks/tests/MethodCoverage.cs +++ b/src/libraries/System.Threading.Tasks/tests/MethodCoverage.cs @@ -279,6 +279,7 @@ public static async Task Task_WhenAny_TwoTasks_WakesOnFirstCompletion() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void CancellationTokenRegitration() { ManualResetEvent mre = new ManualResetEvent(false); @@ -296,6 +297,7 @@ public static void CancellationTokenRegitration() /// verify that the taskawaiter.UnsafeOnCompleted is invoked /// [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void TaskAwaiter() { ManualResetEvent mre = new ManualResetEvent(false); diff --git a/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj b/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj index eb1b13766e64f9..dfd17dfa6e6d45 100644 --- a/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj +++ b/src/libraries/System.Threading.Tasks/tests/System.Threading.Tasks.Tests.csproj @@ -3,6 +3,7 @@ true true $(NetCoreAppCurrent) + <_WasmPThreadPoolSize Condition="'$(MonoWasmBuildVariant)' == 'multithread'">64 diff --git a/src/libraries/System.Threading.Tasks/tests/Task/AsyncEnumerableToBlockingEnumerableTests.cs b/src/libraries/System.Threading.Tasks/tests/Task/AsyncEnumerableToBlockingEnumerableTests.cs index 0692aedb514f9e..7f7aca4a8611fb 100644 --- a/src/libraries/System.Threading.Tasks/tests/Task/AsyncEnumerableToBlockingEnumerableTests.cs +++ b/src/libraries/System.Threading.Tasks/tests/Task/AsyncEnumerableToBlockingEnumerableTests.cs @@ -70,6 +70,7 @@ static async IAsyncEnumerable CreateSourceEnumerable() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void AsyncEnumerableWithDelays() { var source = new InstrumentedAsyncEnumerable(CreateSourceEnumerable()); @@ -104,6 +105,7 @@ static async IAsyncEnumerable CreateSourceEnumerable() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void AsyncEnumerableWithException() { var source = new InstrumentedAsyncEnumerable(CreateSourceEnumerable()); @@ -132,6 +134,7 @@ static async IAsyncEnumerable CreateSourceEnumerable() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91538", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void AsyncEnumerableWithCancellation() { var source = new InstrumentedAsyncEnumerable(CreateSourceEnumerable()); diff --git a/src/libraries/System.Threading.Tasks/tests/Task/TaskContinueWithTests.cs b/src/libraries/System.Threading.Tasks/tests/Task/TaskContinueWithTests.cs index b426a576d982be..af2b79e07b2c3a 100644 --- a/src/libraries/System.Threading.Tasks/tests/Task/TaskContinueWithTests.cs +++ b/src/libraries/System.Threading.Tasks/tests/Task/TaskContinueWithTests.cs @@ -1255,6 +1255,7 @@ public static void LongContinuationChain_Unwrap_DoesNotStackOverflow() } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] + [ActiveIssue("https://github.com/dotnet/runtime/issues/91541", typeof(PlatformDetection), nameof(PlatformDetection.IsWasmThreadingSupported))] public static void LongContinuationChain_Await_DoesNotStackOverflow() { const int DiveDepth = 12_000; diff --git a/src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj b/src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj index b32acd02135625..0cb21c9d38492b 100644 --- a/src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj +++ b/src/libraries/System.Threading.ThreadPool/tests/System.Threading.ThreadPool.Tests.csproj @@ -3,7 +3,6 @@ true $(NetCoreAppCurrent) true - <_WasmPThreadPoolSize Condition="'$(MonoWasmBuildVariant)' == 'multithread'">64 diff --git a/src/libraries/tests.proj b/src/libraries/tests.proj index 6bb6f0858f7b03..88ac0d5f0aac1e 100644 --- a/src/libraries/tests.proj +++ b/src/libraries/tests.proj @@ -382,19 +382,10 @@ - - - - - - - - - diff --git a/src/mono/mono/component/diagnostics_server.c b/src/mono/mono/component/diagnostics_server.c index f81de18046f23b..4bea3d722625c5 100644 --- a/src/mono/mono/component/diagnostics_server.c +++ b/src/mono/mono/component/diagnostics_server.c @@ -250,7 +250,7 @@ queue_push_sync (WasmIpcStreamQueue *q, const uint8_t *buf, uint32_t buf_size, u gboolean is_browser_thread = FALSE; while (mono_atomic_load_i32 (&q->buf_full) != 0) { if (G_UNLIKELY (!is_browser_thread_inited)) { - is_browser_thread = mono_threads_wasm_is_browser_thread (); + is_browser_thread = mono_threads_wasm_is_ui_thread (); is_browser_thread_inited = TRUE; } if (G_UNLIKELY (is_browser_thread)) { diff --git a/src/mono/mono/metadata/gc.c b/src/mono/mono/metadata/gc.c index 9f8f4918214daa..a5c67062b0e78b 100644 --- a/src/mono/mono/metadata/gc.c +++ b/src/mono/mono/metadata/gc.c @@ -686,21 +686,6 @@ ves_icall_System_GCHandle_InternalSet (MonoGCHandle handle, MonoObjectHandle obj static MonoCoopSem finalizer_sem; static volatile gboolean finished; -#ifdef HOST_WASM - -static void -mono_wasm_gc_finalize_notify (void) -{ -#if 0 - /* use this if we are going to start the finalizer thread on wasm. */ - mono_coop_sem_post (&finalizer_sem); -#else - mono_main_thread_schedule_background_job (mono_runtime_do_background_work); -#endif -} - -#endif /* HOST_WASM */ - /* * mono_gc_finalize_notify: * @@ -720,8 +705,8 @@ mono_gc_finalize_notify (void) #if defined(HOST_WASI) // TODO: Schedule the background job on WASI. Threads aren't yet supported in this build. -#elif defined(HOST_WASM) - mono_wasm_gc_finalize_notify (); +#elif defined(HOST_WASM) && defined(DISABLE_THREADS) + mono_main_thread_schedule_background_job (mono_runtime_do_background_work); #else mono_coop_sem_post (&finalizer_sem); #endif diff --git a/src/mono/mono/metadata/sgen-mono.c b/src/mono/mono/metadata/sgen-mono.c index c98dcf7c0cd464..a650acb6ed7253 100644 --- a/src/mono/mono/metadata/sgen-mono.c +++ b/src/mono/mono/metadata/sgen-mono.c @@ -2893,7 +2893,7 @@ sgen_client_binary_protocol_collection_end (int minor_gc_count, int generation, MONO_PROFILER_RAISE (gc_event, (MONO_GC_EVENT_END, generation, generation == GENERATION_OLD && sgen_concurrent_collection_in_progress)); } -#ifdef HOST_WASM +#if defined(HOST_WASM) && defined(DISABLE_THREADS) void sgen_client_schedule_background_job (void (*cb)(void)) { diff --git a/src/mono/mono/metadata/sgen-stw.c b/src/mono/mono/metadata/sgen-stw.c index 3d8db9ef02522c..daaafba2aef7c3 100644 --- a/src/mono/mono/metadata/sgen-stw.c +++ b/src/mono/mono/metadata/sgen-stw.c @@ -503,7 +503,7 @@ mono_wasm_gc_lock(void) MONO_ENTER_GC_UNSAFE; #ifndef DISABLE_THREADS /* only the browser thread is allowed to take the GC lock */ - g_assert (mono_threads_wasm_is_browser_thread ()); + g_assert (mono_threads_wasm_is_ui_thread ()); LOCK_GC; acquire_gc_locks(); #else diff --git a/src/mono/mono/metadata/threads.c b/src/mono/mono/metadata/threads.c index 5679a04f63907b..dbed9f92e7f883 100644 --- a/src/mono/mono/metadata/threads.c +++ b/src/mono/mono/metadata/threads.c @@ -1237,7 +1237,7 @@ start_wrapper_internal (StartInfo *start_info, gsize *stack_ptr) if (G_UNLIKELY (external_eventloop)) { /* if the thread wants to stay alive in an external eventloop, don't clean up after it */ if (mono_thread_platform_external_eventloop_keepalive_check ()) - return 0; + return 0; // MONO_ENTER_GC_SAFE_UNBALANCED is done in start_wrapper } /* Do any cleanup needed for apartment state. This diff --git a/src/mono/mono/sgen/sgen-gc.c b/src/mono/mono/sgen/sgen-gc.c index 5024faa35173cb..07cf39ffd9dfc8 100644 --- a/src/mono/mono/sgen/sgen-gc.c +++ b/src/mono/mono/sgen/sgen-gc.c @@ -2740,7 +2740,7 @@ extern gboolean mono_wasm_enable_gc; void sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean forced_serial, gboolean stw) { -#ifdef HOST_BROWSER +#if defined(HOST_BROWSER) && defined(DISABLE_THREADS) if (!mono_wasm_enable_gc) { g_assert (stw); //can't handle non-stw mode (IE, domain unload) //we ignore forced_serial diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index daf5967de030b0..b31faae8e1173f 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -22,6 +22,7 @@ #include #ifndef DISABLE_THREADS #include +#include #endif @@ -352,20 +353,15 @@ extern void schedule_background_exec (void); // when this is called from JSSynchronizationContext, the cb would be System.Runtime.InteropServices.JavaScript.JSSynchronizationContext.BackgroundJobHandler // when this is called from sgen it would be wrapper of sgen_perform_collection_inner // when this is called from gc, it would be mono_runtime_do_background_work +#ifdef DISABLE_THREADS void mono_main_thread_schedule_background_job (background_job_cb cb) { g_assert (cb); -#ifndef DISABLE_THREADS - if (!mono_threads_wasm_is_browser_thread ()) { - THREADS_DEBUG ("mono_main_thread_schedule_background_job1: thread %p queued job %p to main thread\n", (gpointer)pthread_self(), (gpointer) cb); - mono_threads_wasm_async_run_in_main_thread_vi ((void (*)(gpointer))mono_current_thread_schedule_background_job, cb); - return; - } -#endif /*DISABLE_THREADS*/ THREADS_DEBUG ("mono_main_thread_schedule_background_job2: thread %p queued job %p to current thread\n", (gpointer)pthread_self(), (gpointer) cb); mono_current_thread_schedule_background_job (cb); } +#endif /*DISABLE_THREADS*/ #ifndef DISABLE_THREADS MonoNativeTlsKey jobs_key; @@ -456,7 +452,7 @@ mono_threads_platform_is_main_thread (void) } gboolean -mono_threads_wasm_is_browser_thread (void) +mono_threads_wasm_is_ui_thread (void) { #ifdef DISABLE_THREADS return TRUE; @@ -466,7 +462,7 @@ mono_threads_wasm_is_browser_thread (void) } MonoNativeThreadId -mono_threads_wasm_browser_thread_tid (void) +mono_threads_wasm_ui_thread_tid (void) { #ifdef DISABLE_THREADS return (MonoNativeThreadId)1; @@ -486,10 +482,13 @@ mono_threads_wasm_on_thread_attached (void) #ifdef DISABLE_THREADS return; #else - if (mono_threads_wasm_is_browser_thread ()) { + if (mono_threads_wasm_is_ui_thread ()) { + // FIXME: we should not be attaching UI thread with deputy design + // but right now we do, because mono_wasm_load_runtime is running in UI thread + // g_assert(!mono_threads_wasm_is_ui_thread ()); return; } - // Notify JS that the pthread attachd to Mono + // Notify JS that the pthread attached to Mono pthread_t id = pthread_self (); MONO_ENTER_GC_SAFE; mono_wasm_pthread_on_pthread_attached (id); @@ -503,10 +502,10 @@ mono_threads_wasm_on_thread_detached (void) #ifdef DISABLE_THREADS return; #else - if (mono_threads_wasm_is_browser_thread ()) { + if (mono_threads_wasm_is_ui_thread ()) { return; } - // Notify JS that the pthread attachd to Mono + // Notify JS that the pthread detached from Mono pthread_t id = pthread_self (); mono_wasm_pthread_on_pthread_detached (id); @@ -515,19 +514,19 @@ mono_threads_wasm_on_thread_detached (void) #ifndef DISABLE_THREADS void -mono_threads_wasm_async_run_in_main_thread (void (*func) (void)) +mono_threads_wasm_async_run_in_ui_thread (void (*func) (void)) { emscripten_async_run_in_main_runtime_thread (EM_FUNC_SIG_V, func); } void -mono_threads_wasm_async_run_in_main_thread_vi (void (*func) (gpointer), gpointer user_data) +mono_threads_wasm_async_run_in_ui_thread_vi (void (*func) (gpointer), gpointer user_data) { emscripten_async_run_in_main_runtime_thread (EM_FUNC_SIG_VI, func, user_data); } void -mono_threads_wasm_async_run_in_main_thread_vii (void (*func) (gpointer, gpointer), gpointer user_data1, gpointer user_data2) +mono_threads_wasm_async_run_in_ui_thread_vii (void (*func) (gpointer, gpointer), gpointer user_data1, gpointer user_data2) { emscripten_async_run_in_main_runtime_thread (EM_FUNC_SIG_VII, func, user_data1, user_data2); } @@ -544,6 +543,13 @@ mono_threads_wasm_async_run_in_target_thread_vi (pthread_t target_thread, void ( emscripten_dispatch_to_thread_async (target_thread, EM_FUNC_SIG_VI, func, NULL, user_data); } +void +mono_threads_wasm_async_run_in_target_thread_vii (pthread_t target_thread, void (*func) (gpointer, gpointer), gpointer user_data1, gpointer user_data2) +{ + emscripten_dispatch_to_thread_async (target_thread, EM_FUNC_SIG_VII, func, NULL, user_data1, user_data2); +} + + #endif /* DISABLE_THREADS */ #endif /* HOST_BROWSER */ diff --git a/src/mono/mono/utils/mono-threads-wasm.h b/src/mono/mono/utils/mono-threads-wasm.h index 38b63118c4acc8..157fc22af8859a 100644 --- a/src/mono/mono/utils/mono-threads-wasm.h +++ b/src/mono/mono/utils/mono-threads-wasm.h @@ -21,10 +21,10 @@ */ gboolean -mono_threads_wasm_is_browser_thread (void); +mono_threads_wasm_is_ui_thread (void); MonoNativeThreadId -mono_threads_wasm_browser_thread_tid (void); +mono_threads_wasm_ui_thread_tid (void); #ifndef DISABLE_THREADS /** @@ -32,16 +32,16 @@ mono_threads_wasm_browser_thread_tid (void); * See emscripten/threading.h emscripten_async_run_in_main_runtime_thread */ void -mono_threads_wasm_async_run_in_main_thread (void (*func) (void)); +mono_threads_wasm_async_run_in_ui_thread (void (*func) (void)); /* * Variant that takes an argument. Add more variants as needed. */ void -mono_threads_wasm_async_run_in_main_thread_vi (void (*func)(gpointer), gpointer user_data); +mono_threads_wasm_async_run_in_ui_thread_vi (void (*func)(gpointer), gpointer user_data); void -mono_threads_wasm_async_run_in_main_thread_vii (void (*func)(gpointer, gpointer), gpointer user_data1, gpointer user_data2); +mono_threads_wasm_async_run_in_ui_thread_vii (void (*func)(gpointer, gpointer), gpointer user_data1, gpointer user_data2); void mono_threads_wasm_async_run_in_target_thread (pthread_t target_thread, void (*func) (void)); @@ -49,6 +49,9 @@ mono_threads_wasm_async_run_in_target_thread (pthread_t target_thread, void (*fu void mono_threads_wasm_async_run_in_target_thread_vi (pthread_t target_thread, void (*func) (gpointer), gpointer user_data); +void +mono_threads_wasm_async_run_in_target_thread_vii (pthread_t target_thread, void (*func) (gpointer, gpointer), gpointer user_data1, gpointer user_data2); + static inline int32_t mono_wasm_atomic_wait_i32 (volatile int32_t *addr, int32_t expected, int32_t timeout_ns) diff --git a/src/mono/mono/utils/mono-threads.h b/src/mono/mono/utils/mono-threads.h index 2189f7e70f39c3..a738460f58950f 100644 --- a/src/mono/mono/utils/mono-threads.h +++ b/src/mono/mono/utils/mono-threads.h @@ -848,7 +848,9 @@ void mono_threads_join_unlock (void); #ifdef HOST_WASM typedef void (*background_job_cb)(void); +#ifdef DISABLE_THREADS void mono_main_thread_schedule_background_job (background_job_cb cb); +#endif // DISABLE_THREADS void mono_current_thread_schedule_background_job (background_job_cb cb); void mono_target_thread_schedule_background_job (MonoNativeThreadId target_thread, background_job_cb cb); #endif diff --git a/src/mono/wasm/runtime/corebindings.c b/src/mono/wasm/runtime/corebindings.c index dd15493724e939..4ec5d3abc4af2c 100644 --- a/src/mono/wasm/runtime/corebindings.c +++ b/src/mono/wasm/runtime/corebindings.c @@ -25,8 +25,6 @@ extern void mono_wasm_bind_cs_function(MonoString **fully_qualified_name, int si extern void mono_wasm_resolve_or_reject_promise(void *data); typedef void (*background_job_cb)(void); -void mono_main_thread_schedule_background_job (background_job_cb cb); -void mono_current_thread_schedule_background_job (background_job_cb cb); #ifndef DISABLE_LEGACY_JS_INTEROP extern void mono_wasm_invoke_js_with_args_ref (int js_handle, MonoString **method, MonoArray **args, int *is_exception, MonoObject **result); diff --git a/src/mono/wasm/runtime/cwraps.ts b/src/mono/wasm/runtime/cwraps.ts index 14dbed3af14d05..4026be432c3004 100644 --- a/src/mono/wasm/runtime/cwraps.ts +++ b/src/mono/wasm/runtime/cwraps.ts @@ -32,7 +32,7 @@ const legacy_interop_cwraps: SigLine[] = WasmEnableLegacyJsInterop ? [ [true, "mono_wasm_array_length_ref", "number", ["number"]], ] : []; -const diagnostics_cwraps: SigLine[] = MonoWasmThreads ? [ +const threading_cwraps: SigLine[] = MonoWasmThreads ? [ // MONO.diagnostics [true, "mono_wasm_event_pipe_enable", "bool", ["string", "number", "number", "string", "bool", "number"]], [true, "mono_wasm_event_pipe_session_start_streaming", "bool", ["number"]], @@ -151,7 +151,7 @@ const fn_signatures: SigLine[] = [ [true, "mono_interp_pgo_load_table", "number", ["number", "number"]], [true, "mono_interp_pgo_save_table", "number", ["number", "number"]], - ...diagnostics_cwraps, + ...threading_cwraps, ...legacy_interop_cwraps, ]; @@ -172,7 +172,7 @@ export interface t_LegacyCwraps { mono_wasm_array_length_ref(array: MonoObjectRef): number; } -export interface t_DiagnosticsCwraps { +export interface t_ThreadingCwraps { // MONO.diagnostics mono_wasm_event_pipe_enable(outputPath: string | null, stream: VoidPtr, bufferSizeInMB: number, providers: string, rundownRequested: boolean, outSessionId: VoidPtr): boolean; mono_wasm_event_pipe_session_start_streaming(sessionId: number): boolean; @@ -303,7 +303,7 @@ const wrapped_c_functions: t_Cwraps = {}; export default wrapped_c_functions; export const legacy_c_functions: t_LegacyCwraps & t_Cwraps = wrapped_c_functions as any; -export const diagnostics_c_functions: t_DiagnosticsCwraps & t_Cwraps = wrapped_c_functions as any; +export const threads_c_functions: t_ThreadingCwraps & t_Cwraps = wrapped_c_functions as any; export const profiler_c_functions: t_ProfilerCwraps & t_Cwraps = wrapped_c_functions as any; // see src/mono/wasm/driver.c I52_ERROR_xxx diff --git a/src/mono/wasm/runtime/diagnostics/browser/controller.ts b/src/mono/wasm/runtime/diagnostics/browser/controller.ts index d891710b8763b9..6286da2b50539f 100644 --- a/src/mono/wasm/runtime/diagnostics/browser/controller.ts +++ b/src/mono/wasm/runtime/diagnostics/browser/controller.ts @@ -3,7 +3,7 @@ import MonoWasmThreads from "consts:monoWasmThreads"; -import { diagnostics_c_functions as cwraps } from "../../cwraps"; +import { threads_c_functions as cwraps } from "../../cwraps"; import { INTERNAL, mono_assert } from "../../globals"; import { mono_log_info, mono_log_debug, mono_log_warn } from "../../logging"; import { withStackAlloc, getI32 } from "../../memory"; diff --git a/src/mono/wasm/runtime/diagnostics/server_pthread/index.ts b/src/mono/wasm/runtime/diagnostics/server_pthread/index.ts index 4023df9e5134eb..1bec0f6b3efd98 100644 --- a/src/mono/wasm/runtime/diagnostics/server_pthread/index.ts +++ b/src/mono/wasm/runtime/diagnostics/server_pthread/index.ts @@ -8,7 +8,7 @@ import monoDiagnosticsMock from "consts:monoDiagnosticsMock"; import { PromiseAndController, assertNever } from "../../types/internal"; import { pthread_self } from "../../pthreads/worker"; import { createPromiseController, mono_assert } from "../../globals"; -import { diagnostics_c_functions as cwraps } from "../../cwraps"; +import { threads_c_functions as cwraps } from "../../cwraps"; import { EventPipeSessionIDImpl } from "../shared/types"; import { CharPtr } from "../../types/emscripten"; import { diff --git a/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts b/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts index b8830ae488b84f..aa60222ed657d5 100644 --- a/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts +++ b/src/mono/wasm/runtime/diagnostics/server_pthread/streaming-session.ts @@ -7,7 +7,7 @@ import { EventPipeSocketConnection, takeOverSocket } from "./socket-connection"; import { StreamQueue, allocateQueue } from "./stream-queue"; import type { MockRemoteSocket } from "../mock"; import type { VoidPtr } from "../../types/emscripten"; -import { diagnostics_c_functions as cwraps } from "../../cwraps"; +import { threads_c_functions as cwraps } from "../../cwraps"; import { EventPipeCommandCollectTracing2, EventPipeCollectTracingCommandProvider, diff --git a/src/mono/wasm/runtime/diagnostics/shared/create-session.ts b/src/mono/wasm/runtime/diagnostics/shared/create-session.ts index e6d80d2cb3a246..17af0cbc5bebba 100644 --- a/src/mono/wasm/runtime/diagnostics/shared/create-session.ts +++ b/src/mono/wasm/runtime/diagnostics/shared/create-session.ts @@ -3,7 +3,7 @@ import * as memory from "../../memory"; import { VoidPtr } from "../../types/emscripten"; -import { diagnostics_c_functions as cwraps } from "../../cwraps"; +import { threads_c_functions as cwraps } from "../../cwraps"; import type { EventPipeSessionIDImpl } from "./types"; const sizeOfInt32 = 4; diff --git a/src/mono/wasm/runtime/driver.c b/src/mono/wasm/runtime/driver.c index ba27e78f538a59..b3bac44d0d6858 100644 --- a/src/mono/wasm/runtime/driver.c +++ b/src/mono/wasm/runtime/driver.c @@ -651,7 +651,7 @@ mono_wasm_invoke_method_bound (MonoMethod *method, void* args /*JSMarshalerArgum // this failure is unlikely because it would be runtime error, not application exception. // the application exception is passed inside JSMarshalerArguments `args` - if (temp_exc) { + if (temp_exc && out_exc) { PVOLATILE(MonoObject) exc2 = NULL; store_volatile((MonoObject**)out_exc, (MonoObject*)mono_object_to_string ((MonoObject*)temp_exc, (MonoObject **)&exc2)); if (exc2)