前言
對於寫過 ASP.NET Core 的童鞋來說,可以通過 HttpContextAccessor 在 Controller 之外的地方獲取到HttpContext,而它實現的關鍵其實是在於一個AsyncLocal<HttpContextHolder> 類型的靜態字段。接下來就和大家來一起探討下這個 AsyncLocal 的具體實現原理。如果有講得不清晰或不准確的地方,還望指出。
public class HttpContextAccessor : IHttpContextAccessor
{
private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>();
// 其他代碼這里不展示
}
本文源碼參考為發文時間點為止最新的 github 開源代碼,和之前實現有些許不同,但設計思想基本一致。
代碼庫地址:https://github.com/dotnet/runtime
1、線程本地存儲
如果想要整個.NET程序中共享一個變量,我們可以將想要共享的變量放在某個類的靜態屬性上來實現。
而在多線程的運行環境中,則可能會希望能將這個變量的共享范圍縮小到單個線程內。例如在web應用中,服務器為每個同時訪問的請求分配一個獨立的線程,我們要在這些獨立的線程中維護自己的當前訪問用戶的信息時,就需要需要線程本地存儲了。
例如下面這樣一個例子。
class Program
{
[ThreadStatic]
private static string _value;
static void Main(string[] args)
{
Parallel.For(0, 4, _ =>
{
var threadId = Thread.CurrentThread.ManagedThreadId;
_value ??= $"這是來自線程{threadId}的數據";
Console.WriteLine($"Thread:{threadId}; Value:{_value}");
});
}
}
輸出結果:
Thread:4; Value:這是來自線程4的數據
Thread:1; Value:這是來自線程1的數據
Thread:5; Value:這是來自線程5的數據
Thread:6; Value:這是來自線程6的數據
除了可以使用 ThreadStaticAttribute 外,我們還可以使用 ThreadLocal<T> 、CallContext 、AsyncLocal<T> 來實現一樣的功能。由於 .NET Core 不再實現 CallContext,所以下列代碼只能在 .NET Framework 中執行。
class Program
{
[ThreadStatic]
private static string _threadStatic;
private static ThreadLocal<string> _threadLocal = new ThreadLocal<string>();
private static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();
static void Main(string[] args)
{
Parallel.For(0, 4, _ =>
{
var threadId = Thread.CurrentThread.ManagedThreadId;
var value = $"這是來自線程{threadId}的數據";
_threadStatic ??= value;
CallContext.SetData("value", value);
_threadLocal.Value ??= value;
_asyncLocal.Value ??= value;
Console.WriteLine($"Use ThreadStaticAttribute; Thread:{threadId}; Value:{_threadStatic}");
Console.WriteLine($"Use CallContext; Thread:{threadId}; Value:{CallContext.GetData("value")}");
Console.WriteLine($"Use ThreadLocal; Thread:{threadId}; Value:{_threadLocal.Value}");
Console.WriteLine($"Use AsyncLocal; Thread:{threadId}; Value:{_asyncLocal.Value}");
});
Console.Read();
}
}
輸出結果:
Use ThreadStaticAttribute; Thread:3; Value:這是來自線程3的數據
Use ThreadStaticAttribute; Thread:4; Value:這是來自線程4的數據
Use ThreadStaticAttribute; Thread:1; Value:這是來自線程1的數據
Use CallContext; Thread:1; Value:這是來自線程1的數據
Use ThreadLocal; Thread:1; Value:這是來自線程1的數據
Use AsyncLocal; Thread:1; Value:這是來自線程1的數據
Use ThreadStaticAttribute; Thread:5; Value:這是來自線程5的數據
Use CallContext; Thread:5; Value:這是來自線程5的數據
Use ThreadLocal; Thread:5; Value:這是來自線程5的數據
Use AsyncLocal; Thread:5; Value:這是來自線程5的數據
Use CallContext; Thread:3; Value:這是來自線程3的數據
Use CallContext; Thread:4; Value:這是來自線程4的數據
Use ThreadLocal; Thread:4; Value:這是來自線程4的數據
Use AsyncLocal; Thread:4; Value:這是來自線程4的數據
Use ThreadLocal; Thread:3; Value:這是來自線程3的數據
Use AsyncLocal; Thread:3; Value:這是來自線程3的數據
上面的例子都只是在同一個線程中對線程進行存和取,但日常開發的過程中,我們會有很多異步的場景,這些場景可能會導致執行代碼的線程發生切換。
比如下面的例子
class Program
{
[ThreadStatic]
private static string _threadStatic;
private static ThreadLocal<string> _threadLocal = new ThreadLocal<string>();
private static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();
static void Main(string[] args)
{
_threadStatic = "ThreadStatic保存的數據";
_threadLocal.Value = "ThreadLocal保存的數據";
_asyncLocal.Value = "AsyncLocal保存的數據";
PrintValuesInAnotherThread();
Console.ReadKey();
}
private static void PrintValuesInAnotherThread()
{
Task.Run(() =>
{
Console.WriteLine($"ThreadStatic: {_threadStatic}");
Console.WriteLine($"ThreadLocal: {_threadLocal.Value}");
Console.WriteLine($"AsyncLocal: {_asyncLocal.Value}");
});
}
}
輸出結果:
ThreadStatic:
ThreadLocal:
AsyncLocal: AsyncLocal保存的數據
在線程發生了切換之后,只有 AsyncLocal 還能夠保留原來的值,當然,.NET Framework 中的 CallContext 也可以實現這個需求,下面給出一個相對完整的總結。
實現方式 | .NET FrameWork 可用 | .NET Core 可用 | 是否支持數據流向輔助線程 |
---|---|---|---|
ThreadStaticAttribute | 是 | 是 | 否 |
ThreadLocal<T> | 是 | 是 | 否 |
CallContext.SetData(string name, object data) | 是 | 否 | 僅當參數 data 對應的類型實現了 ILogicalThreadAffinative 接口時支持 |
CallContext.LogicalSetData(string name, object data) | 是 | 否 | 是 |
AsyncLocal<T> | 是 | 是 | 是 |
2、AsyncLocal 實現
我們主要對照 .NET Core 源碼進行學習,源碼地址:https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Threading/AsyncLocal.cs
2.1、主體 AsyncLocal<T>
AsyncLocal<T> 為我們提供了兩個功能
- 通過 Value 屬性存取值
- 通過構造函數注冊回調函數監聽任意線程中對值做出的改動,需記着這個功能,后面介紹源碼的時候會有很多地方涉及
其內部代碼相對簡單
public sealed class AsyncLocal<T> : IAsyncLocal
{
private readonly Action<AsyncLocalValueChangedArgs<T>>? m_valueChangedHandler;
// 無參構造
public AsyncLocal()
{
}
// 可以注冊回調的構造函數,當 Value 在任意線程被改動,將調用回調
public AsyncLocal(Action<AsyncLocalValueChangedArgs<T>>? valueChangedHandler)
{
m_valueChangedHandler = valueChangedHandler;
}
[MaybeNull]
public T Value
{
get
{
// 從 ExecutionContext 中以自身為 Key 獲取值
object? obj = ExecutionContext.GetLocalValue(this);
return (obj == null) ? default : (T)obj;
}
// 是否注冊回調將回影響到 ExecutionContext 是否保存其引用
set => ExecutionContext.SetLocalValue(this, value, m_valueChangedHandler != null);
}
// 在 ExecutionContext 如果判斷到值發生了變化,此方法將被調用
void IAsyncLocal.OnValueChanged(object? previousValueObj, object? currentValueObj, bool contextChanged)
{
Debug.Assert(m_valueChangedHandler != null);
T previousValue = previousValueObj == null ? default! : (T)previousValueObj;
T currentValue = currentValueObj == null ? default! : (T)currentValueObj;
m_valueChangedHandler(new AsyncLocalValueChangedArgs<T>(previousValue, currentValue, contextChanged));
}
}
internal interface IAsyncLocal
{
void OnValueChanged(object? previousValue, object? currentValue, bool contextChanged);
}
真正的數據存取是通過 ExecutionContext.GetLocalValue
和 ExecutionContext.SetLocalValue
實現的。
public class ExecutionContext
{
internal static object? GetLocalValue(IAsyncLocal local);
internal static void SetLocalValue(
IAsyncLocal local,
object? newValue,
bool needChangeNotifications);
}
需要注意的是這邊通過 IAsyncLocal 這一接口實現了 AsyncLocal 與 ExcutionContext 的解耦。 ExcutionContext 只關注數據的存取本身,接口定義的類型都是 object,而不關心具體的類型 T。
2.2、AsyncLocal<T> 在 ExecutionContext 中的數據存取實現
在.NET 中,每個線程都關聯着一個 執行上下文(execution context) 。 可以通過Thread.CurrentThread.ExecutionContext 屬性進行訪問,或者通過 ExecutionContext.Capture() 獲取(前者的實現) 。
AsyncLocal 最終就是把數據保存在 ExecutionContext 上的,為了更深入地理解 AsyncLocal 我們需要先理解一下它。
2.2.1、 ExecutionContext 與 線程的綁定關系
ExecutionContext 被保存 Thread 的 internal 修飾的 _executionContext 字段上。但Thread.CurrentThread.ExecutionContext 並不直接暴露 _executionContext 而與 ExecutionContext.Capture() 共用一套邏輯。
class ExecutionContext
{
public static ExecutionContext? Capture()
{
ExecutionContext? executionContext = Thread.CurrentThread._executionContext;
if (executionContext == null)
{
executionContext = Default;
}
else if (executionContext.m_isFlowSuppressed)
{
executionContext = null;
}
return executionContext;
}
}
下面是經過整理的 Thread 的與 ExecutionContext 相關的部分,Thread 屬於部分類,_executionContext 字段定義在 Thread.CoreCLR.cs 文件中
class Thread
{
// 保存當前線程所關聯的 執行上下文
internal ExecutionContext? _executionContext;
[ThreadStatic]
private static Thread? t_currentThread;
public static Thread CurrentThread => t_currentThread ?? InitializeCurrentThread();
public ExecutionContext? ExecutionContext => ExecutionContext.Capture();
}
2.2.2、ExecutionContext 的私有變量
public sealed class ExecutionContext : IDisposable, ISerializable
{
// 默認執行上下文
internal static readonly ExecutionContext Default = new ExecutionContext(isDefault: true);
// 執行上下文禁止流動后的默認上下文
internal static readonly ExecutionContext DefaultFlowSuppressed = new ExecutionContext(AsyncLocalValueMap.Empty, Array.Empty<IAsyncLocal>(), isFlowSuppressed: true);
// 保存所有注冊了修改回調的 AsyncLocal 的 Value 值,本文暫不涉及對此字段的具體討論
private readonly IAsyncLocalValueMap? m_localValues;
// 保存所有注冊了回調的 AsyncLocal 的對象引用
private readonly IAsyncLocal[]? m_localChangeNotifications;
// 當前線程是否禁止上下文流動
private readonly bool m_isFlowSuppressed;
// 當前上下文是否是默認上下文
private readonly bool m_isDefault;
}
2.2.3、IAsyncLocalValueMap 接口及其實現
在同一個線程中,所有 AsyncLocal 所保存的 Value 都保存在 ExecutionContext 的 m_localValues 字段上。
public class ExecutionContext
{
private readonly IAsyncLocalValueMap m_localValues;
}
為了優化查找值時的性能,微軟為 IAsyncLocalValueMap 提供了6個實現
類型 | 元素個數 |
---|---|
EmptyAsyncLocalValueMap | 0 |
OneElementAsyncLocalValueMap | 1 |
TwoElementAsyncLocalValueMap | 2 |
ThreeElementAsyncLocalValueMap | 3 |
MultiElementAsyncLocalValueMap | 4 ~ 16 |
ManyElementAsyncLocalValueMap | > 16 |
隨着 ExecutionContext 所關聯的 AsyncLocal 數量的增加,IAsyncLocalValueMap 的實現將會在ExecutionContext的SetLocalValue方法中被不斷替換。查詢的時間復雜度和空間復雜度依次遞增。代碼的實現與 AsyncLocal 同屬於 一個文件。當然元素數量減少時也會替換成之前的實現。
// 這個接口是用來在 ExecutionContext 中保存 IAsyncLocal => object 的映射關系。
// 其實現被設定為不可變的(immutable),隨着元素的數量增加而變化,空間復雜度和時間復雜度也隨之增加。
internal interface IAsyncLocalValueMap
{
bool TryGetValue(IAsyncLocal key, out object? value);
// 通過此方法新增 AsyncLocal 或修改現有的 AsyncLocal
// 如果數量無變化,返回同類型的 IAsyncLocalValueMap 實現類實例
// 如果數量發生變化(增加或減少,將value設值為null時會減少),則可能返回不同類型的 IAsyncLocalValueMap 實現類實例
IAsyncLocalValueMap Set(IAsyncLocal key, object? value, bool treatNullValueAsNonexistent);
}
Map 的創建是以靜態類 AsyncLocalValueMap 的 Create 方法作為創建的入口的。
internal static class AsyncLocalValueMap
{
// EmptyAsyncLocalValueMap 設計上只在這邊實例化,其他地方當作常量使用
public static IAsyncLocalValueMap Empty { get; } = new EmptyAsyncLocalValueMap();
public static bool IsEmpty(IAsyncLocalValueMap asyncLocalValueMap)
{
Debug.Assert(asyncLocalValueMap != null);
Debug.Assert(asyncLocalValueMap == Empty || asyncLocalValueMap.GetType() != typeof(EmptyAsyncLocalValueMap));
return asyncLocalValueMap == Empty;
}
public static IAsyncLocalValueMap Create(IAsyncLocal key, object? value, bool treatNullValueAsNonexistent)
{
// 創建最初的實例
// 如果 AsyncLocal 注冊了回調,則需要保存 null 的 Value,以便下次設置非null的值時因為值發生變化而觸發回調
return value != null || !treatNullValueAsNonexistent ?
new OneElementAsyncLocalValueMap(key, value) :
Empty;
}
}
此后每次更新元素時都必須調用 IAsyncLocalValueMap 實現類的 Set 方法,原實例是不會發生變化的,需保存 Set 的返回值。
接下來以 ThreeElementAsyncLocalValueMap 為例進行解釋
private sealed class ThreeElementAsyncLocalValueMap : IAsyncLocalValueMap
{
// 申明三個私有字段保存 key
private readonly IAsyncLocal _key1, _key2, _key3;
// 申明三個私有字段保存
private readonly object? _value1, _value2, _value3;
public ThreeElementAsyncLocalValueMap(IAsyncLocal key1, object? value1, IAsyncLocal key2, object? value2, IAsyncLocal key3, object? value3)
{
_key1 = key1; _value1 = value1;
_key2 = key2; _value2 = value2;
_key3 = key3; _value3 = value3;
}
public IAsyncLocalValueMap Set(IAsyncLocal key, object? value, bool treatNullValueAsNonexistent)
{
// 如果 AsyncLocal 注冊過回調,treatNullValueAsNonexistent 的值是 false,
// 意思是就算 value 是 null,也認為它是有效的
if (value != null || !treatNullValueAsNonexistent)
{
// 如果現在的 map 已經保存過傳入的 key ,則返回一個更新了 value 值的新 map 實例
if (ReferenceEquals(key, _key1)) return new ThreeElementAsyncLocalValueMap(key, value, _key2, _value2, _key3, _value3);
if (ReferenceEquals(key, _key2)) return new ThreeElementAsyncLocalValueMap(_key1, _value1, key, value, _key3, _value3);
if (ReferenceEquals(key, _key3)) return new ThreeElementAsyncLocalValueMap(_key1, _value1, _key2, _value2, key, value);
// 如果當前Key不存在map里,則需要一個能存放第四個key的map
var multi = new MultiElementAsyncLocalValueMap(4);
multi.UnsafeStore(0, _key1, _value1);
multi.UnsafeStore(1, _key2, _value2);
multi.UnsafeStore(2, _key3, _value3);
multi.UnsafeStore(3, key, value);
return multi;
}
else
{
// value 是 null,對應的 key 會被忽略或者從 map 中去除,這邊會有兩種情況
// 1、如果當前的 key 存在於 map 當中,則將這個 key 去除,map 類型降級為 TwoElementAsyncLocalValueMap
return
ReferenceEquals(key, _key1) ? new TwoElementAsyncLocalValueMap(_key2, _value2, _key3, _value3) :
ReferenceEquals(key, _key2) ? new TwoElementAsyncLocalValueMap(_key1, _value1, _key3, _value3) :
ReferenceEquals(key, _key3) ? new TwoElementAsyncLocalValueMap(_key1, _value1, _key2, _value2) :
// 2、當前 key 不存在於 map 中,則會被直接忽略
(IAsyncLocalValueMap)this;
}
}
// 至多對比三次就能找到對應的 value
public bool TryGetValue(IAsyncLocal key, out object? value)
{
if (ReferenceEquals(key, _key1))
{
value = _value1;
return true;
}
else if (ReferenceEquals(key, _key2))
{
value = _value2;
return true;
}
else if (ReferenceEquals(key, _key3))
{
value = _value3;
return true;
}
else
{
value = null;
return false;
}
}
}
2.2.4、ExecutionContext - SetLocalValue
需要注意的是這邊會涉及到兩個 Immutable 結構,一個是 ExecutionContext 本身,另一個是 IAsyncLocalValueMap 的實現類。同一個 key 前后兩次 value 發生變化后,會產生新的 ExecutionContext 的實例和 IAsyncLocalMap 實現類實例(在 IAsyncLocalValueMap 實現類的 Set 方法中完成)。
internal static void SetLocalValue(IAsyncLocal local, object? newValue, bool needChangeNotifications)
{
// 獲取當前執行上下文
ExecutionContext? current = Thread.CurrentThread._executionContext;
object? previousValue = null;
bool hadPreviousValue = false;
if (current != null)
{
Debug.Assert(!current.IsDefault);
Debug.Assert(current.m_localValues != null, "Only the default context should have null, and we shouldn't be here on the default context");
// 判斷當前作為 Key 的 AsyncLocal 是否已經有對應的 Value
hadPreviousValue = current.m_localValues.TryGetValue(local, out previousValue);
}
// 如果前后兩次 Value 沒有發生變化,則繼續處理
if (previousValue == newValue)
{
return;
}
// 對於 treatNullValueAsNonexistent: !needChangeNotifications 的說明
// 如果 AsyncLocal 注冊了回調,則 needChangeNotifications 為 ture,m_localValues 會保存 null 值以便下次觸發change回調
IAsyncLocal[]? newChangeNotifications = null;
IAsyncLocalValueMap newValues;
bool isFlowSuppressed = false;
if (current != null)
{
Debug.Assert(!current.IsDefault);
Debug.Assert(current.m_localValues != null, "Only the default context should have null, and we shouldn't be here on the default context");
isFlowSuppressed = current.m_isFlowSuppressed;
// 這一步很關鍵,通過調用 m_localValues.Set 對 map 進行修改,這會產生一個新的 map 實例。
newValues = current.m_localValues.Set(local, newValue, treatNullValueAsNonexistent: !needChangeNotifications);
newChangeNotifications = current.m_localChangeNotifications;
}
else
{
// 如果當前上下文不存在,創建第一個 IAsyncLocalValueMap 實例
newValues = AsyncLocalValueMap.Create(local, newValue, treatNullValueAsNonexistent: !needChangeNotifications);
}
// 如果 AsyncLocal 注冊了回調,則需要保存 AsyncLocal 的引用
// 這邊會有兩種情況,一個是數組未創建過,一個是數組已存在
if (needChangeNotifications)
{
if (hadPreviousValue)
{
Debug.Assert(newChangeNotifications != null);
Debug.Assert(Array.IndexOf(newChangeNotifications, local) >= 0);
}
else if (newChangeNotifications == null)
{
newChangeNotifications = new IAsyncLocal[1] { local };
}
else
{
int newNotificationIndex = newChangeNotifications.Length;
// 這個方法會創建一個新數組並將原來的元素拷貝過去
Array.Resize(ref newChangeNotifications, newNotificationIndex + 1);
newChangeNotifications[newNotificationIndex] = local;
}
}
// 如果 AsyncLocal 存在有效值,且允許執行上下文流動,則創建新的 ExecutionContext實例,新實例會保存所有的AsyncLocal的值和所有需要通知的 AsyncLocal 引用。
Thread.CurrentThread._executionContext =
(!isFlowSuppressed && AsyncLocalValueMap.IsEmpty(newValues)) ?
null : // No values, return to Default context
new ExecutionContext(newValues, newChangeNotifications, isFlowSuppressed);
if (needChangeNotifications)
{
// 調用先前注冊好的委托
local.OnValueChanged(previousValue, newValue, contextChanged: false);
}
}
2.2.5、ExecutionContext - GetLocalValue
值的獲取實現相對簡單
internal static object? GetLocalValue(IAsyncLocal local)
{
ExecutionContext? current = Thread.CurrentThread._executionContext;
if (current == null)
{
return null;
}
Debug.Assert(!current.IsDefault);
Debug.Assert(current.m_localValues != null, "Only the default context should have null, and we shouldn't be here on the default context");
current.m_localValues.TryGetValue(local, out object? value);
return value;
}
3、ExecutionContext 的流動
在線程發生切換的時候,ExecutionContext 會在前一個線程中被默認捕獲,流向下一個線程,它所保存的數據也就隨之流動。
在所有會發生線程切換的地方,基礎類庫(BCL) 都為我們封裝好了對執行上下文的捕獲。
例如:
- new Thread(ThreadStart start).Start()
- Task.Run(Action action)
- ThreadPool.QueueUserWorkItem(WaitCallback callBack)
- await 語法糖
class Program
{
static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();
static async Task Main(string[] args)
{
_asyncLocal.Value = "AsyncLocal保存的數據";
new Thread(() =>
{
Console.WriteLine($"new Thread: {_asyncLocal.Value}");
})
{
IsBackground = true
}.Start();
ThreadPool.QueueUserWorkItem(_ =>
{
Console.WriteLine($"ThreadPool.QueueUserWorkItem: {_asyncLocal.Value}");
});
Task.Run(() =>
{
Console.WriteLine($"Task.Run: {_asyncLocal.Value}");
});
await Task.Delay(100);
Console.WriteLine($"after await: {_asyncLocal.Value}");
}
}
輸出結果:
new Thread: AsyncLocal保存的數據
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數據
Task.Run: AsyncLocal保存的數據
after await: AsyncLocal保存的數據
3.1、流動的禁止和恢復
ExecutionContext 為我們提供了 SuppressFlow(禁止流動) 和 RestoreFlow (恢復流動)這兩個靜態方法來控制當前線程的執行上下文是否像輔助線程流動。並可以通過 IsFlowSuppressed 靜態方法來進行判斷。
class Program
{
static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();
static async Task Main(string[] args)
{
_asyncLocal.Value = "AsyncLocal保存的數據";
Console.WriteLine("默認:");
PrintAsync(); // 不 await,后面的線程不會發生切換
Thread.Sleep(1000); // 確保上面的方法內的所有線程都執行完
ExecutionContext.SuppressFlow();
Console.WriteLine("SuppressFlow:");
PrintAsync();
Thread.Sleep(1000);
Console.WriteLine("RestoreFlow:");
ExecutionContext.RestoreFlow();
await PrintAsync();
Console.Read();
}
static async ValueTask PrintAsync()
{
new Thread(() =>
{
Console.WriteLine($" new Thread: {_asyncLocal.Value}");
})
{
IsBackground = true
}.Start();
Thread.Sleep(100); // 保證輸出順序
ThreadPool.QueueUserWorkItem(_ =>
{
Console.WriteLine($" ThreadPool.QueueUserWorkItem: {_asyncLocal.Value}");
});
Thread.Sleep(100);
Task.Run(() =>
{
Console.WriteLine($" Task.Run: {_asyncLocal.Value}");
});
await Task.Delay(100);
Console.WriteLine($" after await: {_asyncLocal.Value}");
Console.WriteLine();
}
}
輸出結果:
默認:
new Thread: AsyncLocal保存的數據
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數據
Task.Run: AsyncLocal保存的數據
after await: AsyncLocal保存的數據SuppressFlow:
new Thread:
ThreadPool.QueueUserWorkItem:
Task.Run:
after await:RestoreFlow:
new Thread: AsyncLocal保存的數據
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數據
Task.Run: AsyncLocal保存的數據
after await: AsyncLocal保存的數據
需要注意的是,在線程A中創建線程B之前調用 ExecutionContext.SuppressFlow 只會影響 ExecutionContext 從線程A => 線程B的傳遞,線程B => 線程C 不受影響。
class Program
{
static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>();
static void Main(string[] args)
{
_asyncLocal.Value = "A => B";
ExecutionContext.SuppressFlow();
new Thread((() =>
{
Console.WriteLine($"線程B:{_asyncLocal.Value}"); // 輸出線程B:
_asyncLocal.Value = "B => C";
new Thread((() =>
{
Console.WriteLine($"線程C:{_asyncLocal.Value}"); // 輸出線程C:B => C
}))
{
IsBackground = true
}.Start();
}))
{
IsBackground = true
}.Start();
Console.Read();
}
}
3.2、ExcutionContext 的流動實現
上面舉例了四種場景,由於每一種場景的傳遞過程都比較復雜,目前先介紹其中一個。
但不管什么場景,都會涉及到 ExcutionContext 的 Run 方法。在Run 方法中會調用 RunInternal 方法,
public static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
{
if (executionContext == null)
{
ThrowNullContext();
}
// 內部會調用 RestoreChangedContextToThread 方法
RunInternal(executionContext, callback, state);
}
RunInternal 調用下面一個 RestoreChangedContextToThread 方法將 ExcutionContext.Run 方法傳入的 ExcutionContext 賦值給當前線程的 _executionContext 字段。
internal static void RestoreChangedContextToThread(Thread currentThread, ExecutionContext? contextToRestore, ExecutionContext? currentContext)
{
Debug.Assert(currentThread == Thread.CurrentThread);
Debug.Assert(contextToRestore != currentContext);
// 在這邊把之前的 ExecutionContext 賦值給了當前線程
currentThread._executionContext = contextToRestore;
if ((currentContext != null && currentContext.HasChangeNotifications) ||
(contextToRestore != null && contextToRestore.HasChangeNotifications))
{
OnValuesChanged(currentContext, contextToRestore);
}
}
3.2.1、new Thread(ThreadStart start).Start() 為例說明 ExecutionContext 的流動
這邊可以分為三個步驟:
在 Thread 的 Start 方法中捕獲當前的 ExecutionContext,將其傳遞給 Thread 的構造函數中實例化的 ThreadHelper 實例,ExecutionContext 會暫存在 ThreadHelper 的實例字段中,線程創建完成后會調用ExecutionContext.RunInternal 將其賦值給新創建的線程。
代碼位置:
public void Start()
{
#if FEATURE_COMINTEROP_APARTMENT_SUPPORT
// Eagerly initialize the COM Apartment state of the thread if we're allowed to.
StartupSetApartmentStateInternal();
#endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
// Attach current thread's security principal object to the new
// thread. Be careful not to bind the current thread to a principal
// if it's not already bound.
if (_delegate != null)
{
// If we reach here with a null delegate, something is broken. But we'll let the StartInternal method take care of
// reporting an error. Just make sure we don't try to dereference a null delegate.
Debug.Assert(_delegate.Target is ThreadHelper);
// 由於 _delegate 指向 ThreadHelper 的實例方法,所以 _delegate.Target 指向 ThreadHelper 實例。
var t = (ThreadHelper)_delegate.Target;
ExecutionContext? ec = ExecutionContext.Capture();
t.SetExecutionContextHelper(ec);
}
StartInternal();
}
class ThreadHelper
{
internal ThreadHelper(Delegate start)
{
_start = start;
}
internal void SetExecutionContextHelper(ExecutionContext? ec)
{
_executionContext = ec;
}
// 這個方法是對 Thread 構造函數傳入的委托的包裝
internal void ThreadStart()
{
Debug.Assert(_start is ThreadStart);
ExecutionContext? context = _executionContext;
if (context != null)
{
// 將 ExecutionContext 與 CurrentThread 進行綁定
ExecutionContext.RunInternal(context, s_threadStartContextCallback, this);
}
else
{
InitializeCulture();
((ThreadStart)_start)();
}
}
}
4、總結
-
AsyncLocal 本身不保存數據,數據保存在 ExecutionContext 實例的 m_localValues 的私有字段上,字段類型定義是 IAsyncLocalMap ,以 IAsyncLocal => object 的 Map 結構進行保存,且實現類型隨着元素數量的變化而變化。
-
ExecutionContext 實例 保存在 Thread.CurrentThread._executionContext 上,實現與當前線程的關聯。
-
對於 IAsyncLocalMap 的實現類,如果 AsyncLocal 注冊了回調,value 傳 null 不會被忽略。
沒注冊回調時分為兩種情況:如果 key 存在,則做刪除處理,map 類型可能出現降級。如果 key 不存在,則直接忽略。
-
ExecutionContext 和 IAsyncLocalMap 的實現類都被設計成不可變(immutable)。同一個 key 前后兩次 value 發生變化后,會產生新的 ExecutionContext 的實例和 IAsyncLocalMap 實現類實例。
-
ExecutionContext 與當前線程綁定,默認流動到輔助線程,可以禁止流動和恢復流動,且禁止流動僅影響當前線程向其輔助線程的傳遞,不影響后續。
5、參考
- https://devblogs.microsoft.com/pfxteam/executioncontext-vs-synchronizationcontext/
- 《CLR via C#》27.3 章節
- github 代碼庫 https://github.com/dotnet/runtime