Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM: Add an internal method to pump the threadpool #38690

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
stephentoub marked this conversation as resolved.
Show resolved Hide resolved

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