This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Support Scheduler de-virtualization - Fast fire-and-forget Tasks - Rename TaskRun -> ThreadPool
- Loading branch information
Showing
6 changed files
with
64 additions
and
27 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
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
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
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
21 changes: 0 additions & 21 deletions
21
src/System.IO.Pipelines/System/Threading/TaskRunScheduler.cs
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/System.IO.Pipelines/System/Threading/ThreadPoolScheduler.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,55 @@ | ||
// 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. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace System.Threading | ||
{ | ||
internal sealed class ThreadPoolScheduler : Scheduler | ||
{ | ||
public override void Schedule(Action action) | ||
{ | ||
#if NETCOREAPP2_1 | ||
// Queue to low contention local ThreadPool queue; rather than global queue as per Task | ||
Threading.ThreadPool.QueueUserWorkItem(_actionWaitCallback, action, preferLocal: true); | ||
#elif NETSTANDARD2_0 | ||
Threading.ThreadPool.QueueUserWorkItem(_actionWaitCallback, action); | ||
#else | ||
Task.Factory.StartNew(action); | ||
#endif | ||
} | ||
|
||
public override void Schedule(Action<object> action, object state) | ||
{ | ||
#if NETCOREAPP2_1 | ||
// Queue to low contention local ThreadPool queue; rather than global queue as per Task | ||
Threading.ThreadPool.QueueUserWorkItem(_actionObjectWaitCallback, new ActionObjectAsWaitCallback(action, state), preferLocal: true); | ||
#elif NETSTANDARD2_0 | ||
Threading.ThreadPool.QueueUserWorkItem(_actionObjectWaitCallback, new ActionObjectAsWaitCallback(action, state)); | ||
#else | ||
Task.Factory.StartNew(action, state); | ||
#endif | ||
} | ||
|
||
#if NETCOREAPP2_1 || NETSTANDARD2_0 | ||
private readonly static WaitCallback _actionWaitCallback = state => ((Action)state)(); | ||
|
||
private readonly static WaitCallback _actionObjectWaitCallback = state => ((ActionObjectAsWaitCallback)state).Run(); | ||
|
||
private sealed class ActionObjectAsWaitCallback | ||
{ | ||
private Action<object> _action; | ||
private object _state; | ||
|
||
public ActionObjectAsWaitCallback(Action<object> action, object state) | ||
{ | ||
_action = action; | ||
_state = state; | ||
} | ||
|
||
public void Run() => _action(_state); | ||
} | ||
#endif | ||
} | ||
} |