前言 看過不少關於 await 的原理的文章,也知道背後是編譯器給轉成了狀態機實現的,但是具體是怎麼完成的,回調又是如何銜接的,一直都沒有搞清楚,這次下定決心把源碼自己跑了下,終於豁然開朗了 本文的演示代碼基於 VS2022 + .NET 6 示例 public class Program { st ...
前言
看過不少關於 await 的原理的文章,也知道背後是編譯器給轉成了狀態機實現的,但是具體是怎麼完成的,回調又是如何銜接的,一直都沒有搞清楚,這次下定決心把源碼自己跑了下,終於豁然開朗了
本文的演示代碼基於 VS2022 + .NET 6
示例
public class Program
{
static int Work()
{
Console.WriteLine("In Task.Run");
return 1;
}
static async Task TestAsync()
{
Console.WriteLine("Before Task.Run");
await Task.Run(Work);
Console.WriteLine("After Task.Run");
}
static void Main()
{
_ = TestAsync();
Console.WriteLine("End");
Console.ReadKey();
}
}
- 很簡單的非同步代碼,我們來看下,編譯器把它變成了啥
class Program
{
static int Work()
{
Console.WriteLine("In Task.Run");
return 1;
}
static Task TestAsync()
{
var stateMachine = new StateMachine()
{
_builder = AsyncTaskMethodBuilder.Create(),
_state = -1
};
stateMachine._builder.Start(ref stateMachine);
return stateMachine._builder.Task;
}
static void Main()
{
_ = TestAsync();
Console.WriteLine("End");
Console.ReadKey();
}
class StateMachine : IAsyncStateMachine
{
public int _state;
public AsyncTaskMethodBuilder _builder;
private TaskAwaiter<int> _awaiter;
void IAsyncStateMachine.MoveNext()
{
int num = _state;
try
{
TaskAwaiter<int> awaiter;
if (num != 0)
{
Console.WriteLine("Before Task.Run");
awaiter = Task.Run(Work).GetAwaiter();
if (!awaiter.IsCompleted)
{
_state = 0;
_awaiter = awaiter;
StateMachine stateMachine = this;
_builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = _awaiter;
_awaiter = default;
_state = -1;
}
awaiter.GetResult();
Console.WriteLine("After Task.Run");
}
catch (Exception exception)
{
_state = -2;
_builder.SetException(exception);
return;
}
_state = -2;
_builder.SetResult();
}
void IAsyncStateMachine.SetStateMachine(IAsyncStateMachine stateMachine) { }
}
}
- 編譯後的代碼經過我的整理,命名簡化了,更容易理解
狀態機實現
-
我們看到實際是生成了一個隱藏的狀態機類
StateMachine
-
把狀態機的初始狀態
_state
設置 -1 -
stateMachine._builder.Start(ref stateMachine);
啟動狀態機,內部實際調用的就是狀態機的MoveNext
方法 -
Task.Run
創建一個任務, 把委托放在Task.m_action
欄位,丟到線程池,等待調度 -
任務線上程池內被調度完成後,是怎麼回到這個狀態機繼續執行後續代碼的呢?
_builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
就是關鍵了, 跟下去,到瞭如下的代碼:if (!this.AddTaskContinuation(stateMachineBox, false)) { ThreadPool.UnsafeQueueUserWorkItemInternal(stateMachineBox, true); } bool AddTaskContinuation(object tc, bool addBeforeOthers) { return !this.IsCompleted && ((this.m_continuationObject == null && Interlocked.CompareExchange(ref this.m_continuationObject, tc, null) == null) || this.AddTaskContinuationComplex(tc, addBeforeOthers)); }
- 這裡很清楚的看到,嘗試把狀態機對象(實際是狀態機的包裝類),賦值到
Task.m_continuationObject
, 如果操作失敗,則把狀態機對象丟進線程池等待調度,這裡為什麼這麼實現,看一下線程池是怎麼執行的就清楚了
- 這裡很清楚的看到,嘗試把狀態機對象(實際是狀態機的包裝類),賦值到
線程池實現
- .NET6 的線程池實現,實際是放到了
PortableThreadPool
, 具體調試步驟我就不放了,直接說結果就是, 線程池線程從任務隊列中拿到任務後都執行了DispatchWorkItem
方法
static void DispatchWorkItem(object workItem, Thread currentThread)
{
Task task = workItem as Task;
if (task != null)
{
task.ExecuteFromThreadPool(currentThread);
return;
}
Unsafe.As<IThreadPoolWorkItem>(workItem).Execute();
}
virtual void ExecuteFromThreadPool(Thread threadPoolThread)
{
this.ExecuteEntryUnsafe(threadPoolThread);
}
-
我們看到, 線程池隊列中的任務都是 object 類型的, 這裡進行了類型判斷, 如果是 Task , 直接執行
task.ExecuteFromThreadPool
, 更有意思的這個方法是個虛方法,後面說明 -
ExecuteFromThreadPool
繼續追下去,我們來到了這裡,代碼做了簡化private void ExecuteWithThreadLocal(ref Task currentTaskSlot, Thread threadPoolThread = null) { this.InnerInvoke(); this.Finish(true); } virtual void InnerInvoke() { Action action = this.m_action as Action; if (action != null) { action(); return; } }
-
很明顯
this.InnerInvoke
就是執行了最開始Task.Run(Work)
封裝的委托了, 在m_action
欄位 -
this.Finish(true);
跟下去會發現會調用FinishStageTwo
設置任務的完成狀態,異常等, 繼續調用FinishStageThree
就來了重點:FinishContinuations
這個方法就是銜接後續回調的核心internal void FinishContinuations() { object obj = Interlocked.Exchange(ref this.m_continuationObject, Task.s_taskCompletionSentinel); if (obj != null) { this.RunContinuations(obj); } }
-
還記得狀態機實現麽,
Task.m_continuationObject
欄位實際存儲的就是狀態機的包裝類,這裡線程池線程也會判斷這個欄位有值的話,就直接使用它執行後續代碼了void RunContinuations(object continuationObject) { var asyncStateMachineBox = continuationObject as IAsyncStateMachineBox; if (asyncStateMachineBox != null) { AwaitTaskContinuation.RunOrScheduleAction(asyncStateMachineBox, flag2); return; } } static void RunOrScheduleAction(IAsyncStateMachineBox box, bool allowInlining) { if (allowInlining && AwaitTaskContinuation.IsValidLocationForInlining) { box.MoveNext(); return; } }
總結
Task.Run
創建Task
, 把委托放在m_action
欄位, 把Task
壓入線程池隊列,等待調度_builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
嘗試把狀態機對象放在Task.m_continuationObject
欄位上,等待線程池線程調度完成任務後使用(用來執行後續),若操作失敗,直接把狀態機對象壓入線程池隊列,等待調度- 線程池線程調度任務完成後,會判斷
Task.m_continuationObject
有值,直接執行它的MoveNext
備註
-
狀態機實現中,嘗試修改
Task.m_continuationObject
,可能會失敗,
就會直接把狀態機對象壓入線程池, 但是線程池調度,不都是判斷是不是Task
類型麽, 其實狀態機的包裝類是Task
的子類,哈哈,是不是明白了class AsyncStateMachineBox<TStateMachine> : Task<TResult>, IAsyncStateMachineBox where TStateMachine : IAsyncStateMachine static void DispatchWorkItem(object workItem, Thread currentThread) { Task task = workItem as Task; if (task != null) { task.ExecuteFromThreadPool(currentThread); return; } Unsafe.As<IThreadPoolWorkItem>(workItem).Execute(); }
- 還有就是狀態機包裝類,重寫了
Task.ExecuteFromThreadPool
,所以線程池調用task.ExecuteFromThreadPool
就是直接調用了狀態機的MoveNext
了, Soga ^_^override void ExecuteFromThreadPool(Thread threadPoolThread) { this.MoveNext(threadPoolThread); }
參考鏈接
- 關於線程池和非同步的更深刻的原理,大家可以參考下麵的文章
概述 .NET 6 ThreadPool 實現: https://www.cnblogs.com/eventhorizon/p/15316955.html
.NET Task 揭秘(2):Task 的回調執行與 await: https://www.cnblogs.com/eventhorizon/p/15912383.html