Skip to content

Commit 80b08ed

Browse files
authored
[browser][MT] use portable timer (#94180)
1 parent 5df92ec commit 80b08ed

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<FeatureMono>true</FeatureMono>
124124
<FeatureWasmThreads Condition="('$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true') and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
125125
<FeatureWasmPerfTracing Condition="('$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true') and ('$(MonoWasmBuildVariant)' == 'multithread')">true</FeatureWasmPerfTracing>
126-
<FeaturePortableTimer Condition="'$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true'">true</FeaturePortableTimer>
126+
<FeaturePortableTimer Condition="('$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true') or '$(FeatureWasmThreads)' == 'true'">true</FeaturePortableTimer>
127127
<FeaturePortableThreadPool Condition="('$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true') or '$(FeatureWasmThreads)' == 'true'">true</FeaturePortableThreadPool>
128128
<FeaturePerfTracing Condition="('$(TargetsBrowser)' != 'true' and '$(TargetsWasi)' != 'true') or '$(FeatureWasmPerftracing)' == 'true'">true</FeaturePerfTracing>
129129
<FeatureObjCMarshal Condition="'$(TargetsOSX)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">true</FeatureObjCMarshal>
@@ -273,9 +273,6 @@
273273
<ItemGroup Condition="('$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true') and '$(FeaturePortableThreadPool)' == 'true'">
274274
<Compile Include="$(BclSourcesRoot)\System\Threading\LowLevelLifoSemaphore.Unix.Mono.cs" />
275275
</ItemGroup>
276-
<ItemGroup Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">
277-
<Compile Include="$(BclSourcesRoot)\System\Threading\TimerQueue.Browser.Mono.cs" />
278-
</ItemGroup>
279276
<ItemGroup Condition="('$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true') and '$(FeatureWasmThreads)' == 'true'">
280277
<Compile Include="$(BclSourcesRoot)\System\Threading\ThreadPoolBoundHandle.Browser.Threads.Mono.cs" />
281278
<Compile Include="$(BclSourcesRoot)\System\Threading\LowLevelLifoAsyncWaitSemaphore.Browser.Threads.Mono.cs" />
@@ -288,6 +285,7 @@
288285
<Compile Include="$(BclSourcesRoot)\System\Threading\ThreadPoolBoundHandle.Browser.Mono.cs" />
289286
<Compile Include="$(BclSourcesRoot)\System\Threading\PreAllocatedOverlapped.Browser.Mono.cs" />
290287
<Compile Include="$(BclSourcesRoot)\System\Threading\ThreadPool.Browser.Mono.cs" />
288+
<Compile Include="$(BclSourcesRoot)\System\Threading\TimerQueue.Browser.Mono.cs" />
291289
</ItemGroup>
292290
<ItemGroup>
293291
<Compile Include="$(BclSourcesRoot)\Mono\HotReload.cs" />

src/mono/mono/mini/mini-wasm.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,6 @@ mono_init_native_crash_info (void)
557557

558558
#endif
559559

560-
// this points to System.Threading.TimerQueue.TimerHandler C# method
561-
static void *timer_handler;
562-
563560
#ifdef HOST_BROWSER
564561

565562
void
@@ -583,6 +580,9 @@ mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo
583580
return FALSE;
584581
}
585582

583+
// this points to System.Threading.TimerQueue.TimerHandler C# method
584+
static void *timer_handler;
585+
586586
EMSCRIPTEN_KEEPALIVE void
587587
mono_wasm_execute_timer (void)
588588
{
@@ -595,33 +595,25 @@ mono_wasm_execute_timer (void)
595595
cb ();
596596
}
597597

598-
599-
#endif
600-
598+
#ifdef DISABLE_THREADS
601599
void
602600
mono_wasm_main_thread_schedule_timer (void *timerHandler, int shortestDueTimeMs)
603601
{
604602
// NOTE: here the `timerHandler` callback is [UnmanagedCallersOnly] which wraps it with MONO_ENTER_GC_UNSAFE/MONO_EXIT_GC_UNSAFE
605603

606604
g_assert (timerHandler);
607605
timer_handler = timerHandler;
608-
#ifdef HOST_BROWSER
609-
#ifndef DISABLE_THREADS
610-
if (!mono_threads_wasm_is_browser_thread ()) {
611-
mono_threads_wasm_async_run_in_main_thread_vi ((void (*)(gpointer))mono_wasm_schedule_timer, GINT_TO_POINTER(shortestDueTimeMs));
612-
return;
613-
}
614-
#endif
615606
mono_wasm_schedule_timer (shortestDueTimeMs);
616-
#endif
617607
}
608+
#endif
609+
#endif
618610

619611
void
620612
mono_arch_register_icall (void)
621613
{
622614
#ifdef HOST_BROWSER
623-
mono_add_internal_call_internal ("System.Threading.TimerQueue::MainThreadScheduleTimer", mono_wasm_main_thread_schedule_timer);
624615
#ifdef DISABLE_THREADS
616+
mono_add_internal_call_internal ("System.Threading.TimerQueue::MainThreadScheduleTimer", mono_wasm_main_thread_schedule_timer);
625617
mono_add_internal_call_internal ("System.Threading.ThreadPool::MainThreadScheduleBackgroundJob", mono_main_thread_schedule_background_job);
626618
#else
627619
mono_add_internal_call_internal ("System.Runtime.InteropServices.JavaScript.JSSynchronizationContext::TargetThreadScheduleBackgroundJob", mono_target_thread_schedule_background_job);

src/mono/mono/mini/mini-wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ typedef struct {
100100
// sdks/wasm/driver.c is C and uses this
101101
G_EXTERN_C void mono_wasm_enable_debugging (int log_level);
102102

103+
#ifdef DISABLE_THREADS
103104
void mono_wasm_main_thread_schedule_timer (void *timerHandler, int shortestDueTimeMs);
105+
#endif // DISABLE_THREADS
104106

105107
void mono_wasm_print_stack_trace (void);
106108

0 commit comments

Comments
 (0)