Skip to content

Commit

Permalink
WASM: Add an internal method to pump the threadpool (#38690)
Browse files Browse the repository at this point in the history
This can by used by the xharness xunit runner to make sure async tasks are executed.

We also need to tweak how `ThreadPoolTaskScheduler` handles `TaskCreationOptions.LongRunning` since the usual mode of starting a new thread doesn't work, instead we treat it like the option wasn't set and queue the task on the threadpool.
  • Loading branch information
akoeplinger authored Jul 2, 2020
1 parent 267e2cb commit fbd5d66
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ partial void ThreadNameChanged(string? value)
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern DeserializationTracker GetThreadDeserializationTracker(ref StackCrawlMark stackMark);

internal const bool IsThreadStartSupported = true;

/// <summary>Returns true if the thread has been started and is not dead.</summary>
public extern bool IsAlive
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ internal static void ResetThreadPoolThread(Thread currentThread)
[System.Diagnostics.Conditional("DEBUG")]
internal static void CheckThreadPoolAndContextsAreDefault()
{
Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
Debug.Assert(!Thread.IsThreadStartSupported || Thread.CurrentThread.IsThreadPoolThread); // there are no dedicated threadpool threads on runtimes where we can't start threads
Debug.Assert(Thread.CurrentThread._executionContext == null, "ThreadPool thread not on Default ExecutionContext.");
Debug.Assert(Thread.CurrentThread._synchronizationContext == null, "ThreadPool thread not on Default SynchronizationContext.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal ThreadPoolTaskScheduler()
protected internal override void QueueTask(Task task)
{
TaskCreationOptions options = task.Options;
if ((options & TaskCreationOptions.LongRunning) != 0)
if (Thread.IsThreadStartSupported && (options & TaskCreationOptions.LongRunning) != 0)
{
// Run LongRunning tasks on their own dedicated thread.
Thread thread = new Thread(s_longRunningThreadWork);
Expand Down
12 changes: 12 additions & 0 deletions src/mono/mono/mini/mini-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ extern void mono_set_timeout (int t, int d);
extern void mono_wasm_queue_tp_cb (void);
G_END_DECLS

extern void mono_wasm_pump_threadpool (void);
void mono_background_exec (void);

#endif // HOST_WASM

gpointer
Expand Down Expand Up @@ -620,12 +623,21 @@ mono_wasm_queue_tp_cb (void)
#endif
}

void
mono_wasm_pump_threadpool (void)
{
#ifdef HOST_WASM
mono_background_exec ();
#endif
}

void
mono_arch_register_icall (void)
{
#ifdef ENABLE_NETCORE
mono_add_internal_call_internal ("System.Threading.TimerQueue::SetTimeout", mono_wasm_set_timeout);
mono_add_internal_call_internal ("System.Threading.ThreadPool::QueueCallback", mono_wasm_queue_tp_cb);
mono_add_internal_call_internal ("System.Threading.ThreadPool::PumpThreadPool", mono_wasm_pump_threadpool);
#else
mono_add_internal_call_internal ("System.Threading.WasmRuntime::SetTimeout", mono_wasm_set_timeout);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,12 @@
<ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
<Compile Include="$(BclSourcesRoot)\System\Threading\TimerQueue.Browser.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\ThreadPool.Browser.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\Thread.Browser.Mono.cs" />
<Compile Include="$(LibrariesProjectRoot)\System.Private.CoreLib\src\System\Threading\ThreadPoolBoundHandle.PlatformNotSupported.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsBrowser)' != 'true'">
<Compile Include="$(BclSourcesRoot)\System\Threading\Thread.UnixOrWindows.Mono.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(BclSourcesRoot)\Mono\RuntimeStructs.cs" />
<Compile Include="$(BclSourcesRoot)\Mono\RuntimeMarshal.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Threading
{
public partial class Thread
{
internal const bool IsThreadStartSupported = false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Threading
{
public partial class Thread
{
internal const bool IsThreadStartSupported = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ private static RegisteredWaitHandle RegisterWaitForSingleObject(
}

[DynamicDependency("Callback")]
[DynamicDependency("PumpThreadPool")]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void QueueCallback();

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void PumpThreadPool(); // NOTE: this method is called via reflection by test code

private static void Callback()
{
_callbackQueued = false;
Expand Down

0 comments on commit fbd5d66

Please sign in to comment.