-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split PortableThreadPool.WorkerThread start and loop body (#84490)
This is Part 1 of #84489 - landing support for async JS interop on threadpool threads in multi-threaded WebAssembly. We will need to start the threadpool worker threads on the browser in a special way, such that they can exit back to the JS event loop, and use callbacks to run the worker loop body. The current PR splits into a separate file the logic for starting threadpool worker threads, and the outer loop that waits for the semaphore that signals that work is available for the worker. The loop body (to be shared with the callback-based approach in a future PR) remains in PortableThreadPool.WorkerThread.cs as several new toplevel functions. Current PR is just refactoring existing code. No functional change. * Split PortableThreadPool.WorkerThread start and loop body For browser-wasm we will need to start the worker thread in a special way, and use callbacks to run the loop body. Current PR is just refactoring existing code. No functional change. * Change loop to use return instead of break * rename utility method to ShouldExitWorker
- Loading branch information
1 parent
790b14b
commit a4e2609
Showing
3 changed files
with
170 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...System.Private.CoreLib/src/System/Threading/PortableThreadPool.WorkerThread.NonBrowser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.Tracing; | ||
|
||
namespace System.Threading | ||
{ | ||
internal sealed partial class PortableThreadPool | ||
{ | ||
/// <summary> | ||
/// The worker thread infastructure for the CLR thread pool. | ||
/// </summary> | ||
private static partial class WorkerThread | ||
{ | ||
|
||
/// <summary> | ||
/// Semaphore for controlling how many threads are currently working. | ||
/// </summary> | ||
private static readonly LowLevelLifoSemaphore s_semaphore = | ||
new LowLevelLifoSemaphore( | ||
0, | ||
MaxPossibleThreadCount, | ||
AppContextConfigHelper.GetInt32Config( | ||
"System.Threading.ThreadPool.UnfairSemaphoreSpinLimit", | ||
SemaphoreSpinCountDefault, | ||
false), | ||
onWait: () => | ||
{ | ||
if (NativeRuntimeEventSource.Log.IsEnabled()) | ||
{ | ||
NativeRuntimeEventSource.Log.ThreadPoolWorkerThreadWait( | ||
(uint)ThreadPoolInstance._separated.counts.VolatileRead().NumExistingThreads); | ||
} | ||
}); | ||
|
||
private static readonly ThreadStart s_workerThreadStart = WorkerThreadStart; | ||
|
||
private static void WorkerThreadStart() | ||
{ | ||
Thread.CurrentThread.SetThreadPoolWorkerThreadName(); | ||
|
||
PortableThreadPool threadPoolInstance = ThreadPoolInstance; | ||
|
||
if (NativeRuntimeEventSource.Log.IsEnabled()) | ||
{ | ||
NativeRuntimeEventSource.Log.ThreadPoolWorkerThreadStart( | ||
(uint)threadPoolInstance._separated.counts.VolatileRead().NumExistingThreads); | ||
} | ||
|
||
LowLevelLock threadAdjustmentLock = threadPoolInstance._threadAdjustmentLock; | ||
LowLevelLifoSemaphore semaphore = s_semaphore; | ||
|
||
while (true) | ||
{ | ||
bool spinWait = true; | ||
while (semaphore.Wait(ThreadPoolThreadTimeoutMs, spinWait)) | ||
{ | ||
WorkerDoWork(threadPoolInstance, ref spinWait); | ||
} | ||
|
||
if (ShouldExitWorker(threadPoolInstance, threadAdjustmentLock)) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
private static void CreateWorkerThread() | ||
{ | ||
// Thread pool threads must start in the default execution context without transferring the context, so | ||
// using UnsafeStart() instead of Start() | ||
Thread workerThread = new Thread(s_workerThreadStart); | ||
workerThread.IsThreadPoolThread = true; | ||
workerThread.IsBackground = true; | ||
// thread name will be set in thread proc | ||
workerThread.UnsafeStart(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters