淺析.NET中AsyncLocal的實(shí)現(xiàn)原理
前言
對(duì)于寫過(guò) ASP.NET Core 的童鞋來(lái)說(shuō),可以通過(guò) HttpContextAccessor 在 Controller 之外的地方獲取到HttpContext,而它實(shí)現(xiàn)的關(guān)鍵其實(shí)是在于一個(gè)AsyncLocal<HttpContextHolder> 類型的靜態(tài)字段。接下來(lái)就和大家來(lái)一起探討下這個(gè) AsyncLocal 的具體實(shí)現(xiàn)原理。如果有講得不清晰或不準(zhǔn)確的地方,還望指出。
public class HttpContextAccessor : IHttpContextAccessor { private static AsyncLocal<HttpContextHolder> _httpContextCurrent = new AsyncLocal<HttpContextHolder>(); // 其他代碼這里不展示 }
本文源碼參考為發(fā)文時(shí)間點(diǎn)為止最新的 github 開源代碼,和之前實(shí)現(xiàn)有些許不同,但設(shè)計(jì)思想基本一致。
代碼庫(kù)地址:https://github.com/dotnet/runtime
1、線程本地存儲(chǔ)
如果想要整個(gè).NET程序中共享一個(gè)變量,我們可以將想要共享的變量放在某個(gè)類的靜態(tài)屬性上來(lái)實(shí)現(xiàn)。
而在多線程的運(yùn)行環(huán)境中,則可能會(huì)希望能將這個(gè)變量的共享范圍縮小到單個(gè)線程內(nèi)。例如在web應(yīng)用中,服務(wù)器為每個(gè)同時(shí)訪問(wèn)的請(qǐng)求分配一個(gè)獨(dú)立的線程,我們要在這些獨(dú)立的線程中維護(hù)自己的當(dāng)前訪問(wèn)用戶的信息時(shí),就需要需要線程本地存儲(chǔ)了。
例如下面這樣一個(gè)例子。
class Program { [ThreadStatic] private static string _value; static void Main(string[] args) { Parallel.For(0, 4, _ => { var threadId = Thread.CurrentThread.ManagedThreadId; _value ??= $"這是來(lái)自線程{threadId}的數(shù)據(jù)"; Console.WriteLine($"Thread:{threadId}; Value:{_value}"); }); } }
輸出結(jié)果:
Thread:4; Value:這是來(lái)自線程4的數(shù)據(jù)
Thread:1; Value:這是來(lái)自線程1的數(shù)據(jù)
Thread:5; Value:這是來(lái)自線程5的數(shù)據(jù)
Thread:6; Value:這是來(lái)自線程6的數(shù)據(jù)
除了可以使用 ThreadStaticAttribute 外,我們還可以使用 ThreadLocal<T> 、CallContext 、AsyncLocal<T> 來(lái)實(shí)現(xiàn)一樣的功能。由于 .NET Core 不再實(shí)現(xiàn) CallContext,所以下列代碼只能在 .NET Framework 中執(zhí)行。
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 = $"這是來(lái)自線程{threadId}的數(shù)據(jù)"; _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(); } }
輸出結(jié)果:
Use ThreadStaticAttribute; Thread:3; Value:這是來(lái)自線程3的數(shù)據(jù)
Use ThreadStaticAttribute; Thread:4; Value:這是來(lái)自線程4的數(shù)據(jù)
Use ThreadStaticAttribute; Thread:1; Value:這是來(lái)自線程1的數(shù)據(jù)
Use CallContext; Thread:1; Value:這是來(lái)自線程1的數(shù)據(jù)
Use ThreadLocal; Thread:1; Value:這是來(lái)自線程1的數(shù)據(jù)
Use AsyncLocal; Thread:1; Value:這是來(lái)自線程1的數(shù)據(jù)
Use ThreadStaticAttribute; Thread:5; Value:這是來(lái)自線程5的數(shù)據(jù)
Use CallContext; Thread:5; Value:這是來(lái)自線程5的數(shù)據(jù)
Use ThreadLocal; Thread:5; Value:這是來(lái)自線程5的數(shù)據(jù)
Use AsyncLocal; Thread:5; Value:這是來(lái)自線程5的數(shù)據(jù)
Use CallContext; Thread:3; Value:這是來(lái)自線程3的數(shù)據(jù)
Use CallContext; Thread:4; Value:這是來(lái)自線程4的數(shù)據(jù)
Use ThreadLocal; Thread:4; Value:這是來(lái)自線程4的數(shù)據(jù)
Use AsyncLocal; Thread:4; Value:這是來(lái)自線程4的數(shù)據(jù)
Use ThreadLocal; Thread:3; Value:這是來(lái)自線程3的數(shù)據(jù)
Use AsyncLocal; Thread:3; Value:這是來(lái)自線程3的數(shù)據(jù)
上面的例子都只是在同一個(gè)線程中對(duì)線程進(jìn)行存和取,但日常開發(fā)的過(guò)程中,我們會(huì)有很多異步的場(chǎng)景,這些場(chǎng)景可能會(huì)導(dǎo)致執(zhí)行代碼的線程發(fā)生切換。
比如下面的例子
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保存的數(shù)據(jù)"; _threadLocal.Value = "ThreadLocal保存的數(shù)據(jù)"; _asyncLocal.Value = "AsyncLocal保存的數(shù)據(jù)"; PrintValuesInAnotherThread(); Console.ReadKey(); } private static void PrintValuesInAnotherThread() { Task.Run(() => { Console.WriteLine($"ThreadStatic: {_threadStatic}"); Console.WriteLine($"ThreadLocal: {_threadLocal.Value}"); Console.WriteLine($"AsyncLocal: {_asyncLocal.Value}"); }); } }
輸出結(jié)果:
ThreadStatic:
ThreadLocal:
AsyncLocal: AsyncLocal保存的數(shù)據(jù)
在線程發(fā)生了切換之后,只有 AsyncLocal 還能夠保留原來(lái)的值,當(dāng)然,.NET Framework 中的 CallContext 也可以實(shí)現(xiàn)這個(gè)需求,下面給出一個(gè)相對(duì)完整的總結(jié)。
實(shí)現(xiàn)方式 | .NET FrameWork 可用 | .NET Core 可用 | 是否支持?jǐn)?shù)據(jù)流向輔助線程 |
---|---|---|---|
ThreadStaticAttribute | 是 | 是 | 否 |
ThreadLocal<T> | 是 | 是 | 否 |
CallContext.SetData(string name, object data) | 是 | 否 | 僅當(dāng)參數(shù) data 對(duì)應(yīng)的類型實(shí)現(xiàn)了 ILogicalThreadAffinative 接口時(shí)支持 |
CallContext.LogicalSetData(string name, object data) | 是 | 否 | 是 |
AsyncLocal<T> | 是 | 是 | 是 |
2、AsyncLocal 實(shí)現(xiàn)
我們主要對(duì)照 .NET Core 源碼進(jìn)行學(xué)習(xí),源碼地址:https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Threading/AsyncLocal.cs
2.1、主體 AsyncLocal<T>
AsyncLocal<T> 為我們提供了兩個(gè)功能
- 通過(guò) Value 屬性存取值
- 通過(guò)構(gòu)造函數(shù)注冊(cè)回調(diào)函數(shù)監(jiān)聽任意線程中對(duì)值做出的改動(dòng),需記著這個(gè)功能,后面介紹源碼的時(shí)候會(huì)有很多地方涉及
其內(nèi)部代碼相對(duì)簡(jiǎn)單
public sealed class AsyncLocal<T> : IAsyncLocal { private readonly Action<AsyncLocalValueChangedArgs<T>>? m_valueChangedHandler; // 無(wú)參構(gòu)造 public AsyncLocal() { } // 可以注冊(cè)回調(diào)的構(gòu)造函數(shù),當(dāng) Value 在任意線程被改動(dòng),將調(diào)用回調(diào) 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; } // 是否注冊(cè)回調(diào)將回影響到 ExecutionContext 是否保存其引用 set => ExecutionContext.SetLocalValue(this, value, m_valueChangedHandler != null); } // 在 ExecutionContext 如果判斷到值發(fā)生了變化,此方法將被調(diào)用 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); }
真正的數(shù)據(jù)存取是通過(guò) ExecutionContext.GetLocalValue
和 ExecutionContext.SetLocalValue
實(shí)現(xiàn)的。
public class ExecutionContext { internal static object? GetLocalValue(IAsyncLocal local); internal static void SetLocalValue( IAsyncLocal local, object? newValue, bool needChangeNotifications); }
需要注意的是這邊通過(guò) IAsyncLocal 這一接口實(shí)現(xiàn)了 AsyncLocal 與 ExcutionContext 的解耦。 ExcutionContext 只關(guān)注數(shù)據(jù)的存取本身,接口定義的類型都是 object,而不關(guān)心具體的類型 T。
2.2、AsyncLocal<T> 在 ExecutionContext 中的數(shù)據(jù)存取實(shí)現(xiàn)
在.NET 中,每個(gè)線程都關(guān)聯(lián)著一個(gè) 執(zhí)行上下文(execution context) 。 可以通過(guò)Thread.CurrentThread.ExecutionContext 屬性進(jìn)行訪問(wèn),或者通過(guò) ExecutionContext.Capture() 獲?。ㄇ罢叩膶?shí)現(xiàn)) 。
AsyncLocal 最終就是把數(shù)據(jù)保存在 ExecutionContext 上的,為了更深入地理解 AsyncLocal 我們需要先理解一下它。
2.2.1、 ExecutionContext 與 線程的綁定關(guān)系
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; } }
下面是經(jīng)過(guò)整理的 Thread 的與 ExecutionContext 相關(guān)的部分,Thread 屬于部分類,_executionContext 字段定義在 Thread.CoreCLR.cs 文件中
class Thread { // 保存當(dāng)前線程所關(guān)聯(lián)的 執(zhí)行上下文 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 { // 默認(rèn)執(zhí)行上下文 internal static readonly ExecutionContext Default = new ExecutionContext(isDefault: true); // 執(zhí)行上下文禁止流動(dòng)后的默認(rèn)上下文 internal static readonly ExecutionContext DefaultFlowSuppressed = new ExecutionContext(AsyncLocalValueMap.Empty, Array.Empty<IAsyncLocal>(), isFlowSuppressed: true); // 保存所有注冊(cè)了修改回調(diào)的 AsyncLocal 的 Value 值,本文暫不涉及對(duì)此字段的具體討論 private readonly IAsyncLocalValueMap? m_localValues; // 保存所有注冊(cè)了回調(diào)的 AsyncLocal 的對(duì)象引用 private readonly IAsyncLocal[]? m_localChangeNotifications; // 當(dāng)前線程是否禁止上下文流動(dòng) private readonly bool m_isFlowSuppressed; // 當(dāng)前上下文是否是默認(rèn)上下文 private readonly bool m_isDefault; }
2.2.3、IAsyncLocalValueMap 接口及其實(shí)現(xiàn)
在同一個(gè)線程中,所有 AsyncLocal 所保存的 Value 都保存在 ExecutionContext 的 m_localValues 字段上。
public class ExecutionContext { private readonly IAsyncLocalValueMap m_localValues; }
為了優(yōu)化查找值時(shí)的性能,微軟為 IAsyncLocalValueMap 提供了6個(gè)實(shí)現(xiàn)
類型 | 元素個(gè)數(shù) |
---|---|
EmptyAsyncLocalValueMap | 0 |
OneElementAsyncLocalValueMap | 1 |
TwoElementAsyncLocalValueMap | 2 |
ThreeElementAsyncLocalValueMap | 3 |
MultiElementAsyncLocalValueMap | 4 ~ 16 |
ManyElementAsyncLocalValueMap | > 16 |
隨著 ExecutionContext 所關(guān)聯(lián)的 AsyncLocal 數(shù)量的增加,IAsyncLocalValueMap 的實(shí)現(xiàn)將會(huì)在ExecutionContext的SetLocalValue方法中被不斷替換。查詢的時(shí)間復(fù)雜度和空間復(fù)雜度依次遞增。代碼的實(shí)現(xiàn)與 AsyncLocal 同屬于 一個(gè)文件。當(dāng)然元素?cái)?shù)量減少時(shí)也會(huì)替換成之前的實(shí)現(xiàn)。
// 這個(gè)接口是用來(lái)在 ExecutionContext 中保存 IAsyncLocal => object 的映射關(guān)系。 // 其實(shí)現(xiàn)被設(shè)定為不可變的(immutable),隨著元素的數(shù)量增加而變化,空間復(fù)雜度和時(shí)間復(fù)雜度也隨之增加。 internal interface IAsyncLocalValueMap { bool TryGetValue(IAsyncLocal key, out object? value); // 通過(guò)此方法新增 AsyncLocal 或修改現(xiàn)有的 AsyncLocal // 如果數(shù)量無(wú)變化,返回同類型的 IAsyncLocalValueMap 實(shí)現(xiàn)類實(shí)例 // 如果數(shù)量發(fā)生變化(增加或減少,將value設(shè)值為null時(shí)會(huì)減少),則可能返回不同類型的 IAsyncLocalValueMap 實(shí)現(xiàn)類實(shí)例 IAsyncLocalValueMap Set(IAsyncLocal key, object? value, bool treatNullValueAsNonexistent); }
Map 的創(chuàng)建是以靜態(tài)類 AsyncLocalValueMap 的 Create 方法作為創(chuàng)建的入口的。
internal static class AsyncLocalValueMap { // EmptyAsyncLocalValueMap 設(shè)計(jì)上只在這邊實(shí)例化,其他地方當(dāng)作常量使用 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) { // 創(chuàng)建最初的實(shí)例 // 如果 AsyncLocal 注冊(cè)了回調(diào),則需要保存 null 的 Value,以便下次設(shè)置非null的值時(shí)因?yàn)橹蛋l(fā)生變化而觸發(fā)回調(diào) return value != null || !treatNullValueAsNonexistent ? new OneElementAsyncLocalValueMap(key, value) : Empty; } }
此后每次更新元素時(shí)都必須調(diào)用 IAsyncLocalValueMap 實(shí)現(xiàn)類的 Set 方法,原實(shí)例是不會(huì)發(fā)生變化的,需保存 Set 的返回值。
接下來(lái)以 ThreeElementAsyncLocalValueMap 為例進(jìn)行解釋
private sealed class ThreeElementAsyncLocalValueMap : IAsyncLocalValueMap { // 申明三個(gè)私有字段保存 key private readonly IAsyncLocal _key1, _key2, _key3; // 申明三個(gè)私有字段保存 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 注冊(cè)過(guò)回調(diào),treatNullValueAsNonexistent 的值是 false, // 意思是就算 value 是 null,也認(rèn)為它是有效的 if (value != null || !treatNullValueAsNonexistent) { // 如果現(xiàn)在的 map 已經(jīng)保存過(guò)傳入的 key ,則返回一個(gè)更新了 value 值的新 map 實(shí)例 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); // 如果當(dāng)前Key不存在map里,則需要一個(gè)能存放第四個(gè)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,對(duì)應(yīng)的 key 會(huì)被忽略或者從 map 中去除,這邊會(huì)有兩種情況 // 1、如果當(dāng)前的 key 存在于 map 當(dāng)中,則將這個(gè) key 去除,map 類型降級(jí)為 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、當(dāng)前 key 不存在于 map 中,則會(huì)被直接忽略 (IAsyncLocalValueMap)this; } } // 至多對(duì)比三次就能找到對(duì)應(yīng)的 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
需要注意的是這邊會(huì)涉及到兩個(gè) Immutable 結(jié)構(gòu),一個(gè)是 ExecutionContext 本身,另一個(gè)是 IAsyncLocalValueMap 的實(shí)現(xiàn)類。同一個(gè) key 前后兩次 value 發(fā)生變化后,會(huì)產(chǎn)生新的 ExecutionContext 的實(shí)例和 IAsyncLocalMap 實(shí)現(xiàn)類實(shí)例(在 IAsyncLocalValueMap 實(shí)現(xiàn)類的 Set 方法中完成)。
internal static void SetLocalValue(IAsyncLocal local, object? newValue, bool needChangeNotifications) { // 獲取當(dāng)前執(zhí)行上下文 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"); // 判斷當(dāng)前作為 Key 的 AsyncLocal 是否已經(jīng)有對(duì)應(yīng)的 Value hadPreviousValue = current.m_localValues.TryGetValue(local, out previousValue); } // 如果前后兩次 Value 沒有發(fā)生變化,則繼續(xù)處理 if (previousValue == newValue) { return; } // 對(duì)于 treatNullValueAsNonexistent: !needChangeNotifications 的說(shuō)明 // 如果 AsyncLocal 注冊(cè)了回調(diào),則 needChangeNotifications 為 ture,m_localValues 會(huì)保存 null 值以便下次觸發(fā)change回調(diào) 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; // 這一步很關(guān)鍵,通過(guò)調(diào)用 m_localValues.Set 對(duì) map 進(jìn)行修改,這會(huì)產(chǎn)生一個(gè)新的 map 實(shí)例。 newValues = current.m_localValues.Set(local, newValue, treatNullValueAsNonexistent: !needChangeNotifications); newChangeNotifications = current.m_localChangeNotifications; } else { // 如果當(dāng)前上下文不存在,創(chuàng)建第一個(gè) IAsyncLocalValueMap 實(shí)例 newValues = AsyncLocalValueMap.Create(local, newValue, treatNullValueAsNonexistent: !needChangeNotifications); } // 如果 AsyncLocal 注冊(cè)了回調(diào),則需要保存 AsyncLocal 的引用 // 這邊會(huì)有兩種情況,一個(gè)是數(shù)組未創(chuàng)建過(guò),一個(gè)是數(shù)組已存在 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; // 這個(gè)方法會(huì)創(chuàng)建一個(gè)新數(shù)組并將原來(lái)的元素拷貝過(guò)去 Array.Resize(ref newChangeNotifications, newNotificationIndex + 1); newChangeNotifications[newNotificationIndex] = local; } } // 如果 AsyncLocal 存在有效值,且允許執(zhí)行上下文流動(dòng),則創(chuàng)建新的 ExecutionContext實(shí)例,新實(shí)例會(huì)保存所有的AsyncLocal的值和所有需要通知的 AsyncLocal 引用。 Thread.CurrentThread._executionContext = (!isFlowSuppressed && AsyncLocalValueMap.IsEmpty(newValues)) ? null : // No values, return to Default context new ExecutionContext(newValues, newChangeNotifications, isFlowSuppressed); if (needChangeNotifications) { // 調(diào)用先前注冊(cè)好的委托 local.OnValueChanged(previousValue, newValue, contextChanged: false); } }
2.2.5、ExecutionContext - GetLocalValue
值的獲取實(shí)現(xiàn)相對(duì)簡(jiǎn)單
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 的流動(dòng)
在線程發(fā)生切換的時(shí)候,ExecutionContext 會(huì)在前一個(gè)線程中被默認(rèn)捕獲,流向下一個(gè)線程,它所保存的數(shù)據(jù)也就隨之流動(dòng)。
在所有會(huì)發(fā)生線程切換的地方,基礎(chǔ)類庫(kù)(BCL) 都為我們封裝好了對(duì)執(zhí)行上下文的捕獲。
例如:
- new Thread(ThreadStart start).Start()
- Task.Run(Action action)
- ThreadPool.QueueUserWorkItem(WaitCallback callBack)
- await 語(yǔ)法糖
class Program { static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>(); static async Task Main(string[] args) { _asyncLocal.Value = "AsyncLocal保存的數(shù)據(jù)"; 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}"); } }
輸出結(jié)果:
new Thread: AsyncLocal保存的數(shù)據(jù)
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數(shù)據(jù)
Task.Run: AsyncLocal保存的數(shù)據(jù)
after await: AsyncLocal保存的數(shù)據(jù)
3.1、流動(dòng)的禁止和恢復(fù)
ExecutionContext 為我們提供了 SuppressFlow(禁止流動(dòng)) 和 RestoreFlow (恢復(fù)流動(dòng))這兩個(gè)靜態(tài)方法來(lái)控制當(dāng)前線程的執(zhí)行上下文是否像輔助線程流動(dòng)。并可以通過(guò) IsFlowSuppressed 靜態(tài)方法來(lái)進(jìn)行判斷。
class Program { static AsyncLocal<string> _asyncLocal = new AsyncLocal<string>(); static async Task Main(string[] args) { _asyncLocal.Value = "AsyncLocal保存的數(shù)據(jù)"; Console.WriteLine("默認(rèn):"); PrintAsync(); // 不 await,后面的線程不會(huì)發(fā)生切換 Thread.Sleep(1000); // 確保上面的方法內(nèi)的所有線程都執(zhí)行完 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(); } }
輸出結(jié)果:
默認(rèn):
new Thread: AsyncLocal保存的數(shù)據(jù)
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數(shù)據(jù)
Task.Run: AsyncLocal保存的數(shù)據(jù)
after await: AsyncLocal保存的數(shù)據(jù)
SuppressFlow:
new Thread:
ThreadPool.QueueUserWorkItem:
Task.Run:
after await:
RestoreFlow:
new Thread: AsyncLocal保存的數(shù)據(jù)
ThreadPool.QueueUserWorkItem: AsyncLocal保存的數(shù)據(jù)
Task.Run: AsyncLocal保存的數(shù)據(jù)
after await: AsyncLocal保存的數(shù)據(jù)
需要注意的是,在線程A中創(chuàng)建線程B之前調(diào)用 ExecutionContext.SuppressFlow 只會(huì)影響 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 的流動(dòng)實(shí)現(xiàn)
上面舉例了四種場(chǎng)景,由于每一種場(chǎng)景的傳遞過(guò)程都比較復(fù)雜,目前先介紹其中一個(gè)。
但不管什么場(chǎng)景,都會(huì)涉及到 ExcutionContext 的 Run 方法。在Run 方法中會(huì)調(diào)用 RunInternal 方法,
public static void Run(ExecutionContext executionContext, ContextCallback callback, object? state) { if (executionContext == null) { ThrowNullContext(); } // 內(nèi)部會(huì)調(diào)用 RestoreChangedContextToThread 方法 RunInternal(executionContext, callback, state); }
RunInternal 調(diào)用下面一個(gè) RestoreChangedContextToThread 方法將 ExcutionContext.Run 方法傳入的 ExcutionContext 賦值給當(dāng)前線程的 _executionContext 字段。
internal static void RestoreChangedContextToThread(Thread currentThread, ExecutionContext? contextToRestore, ExecutionContext? currentContext) { Debug.Assert(currentThread == Thread.CurrentThread); Debug.Assert(contextToRestore != currentContext); // 在這邊把之前的 ExecutionContext 賦值給了當(dāng)前線程 currentThread._executionContext = contextToRestore; if ((currentContext != null && currentContext.HasChangeNotifications) || (contextToRestore != null && contextToRestore.HasChangeNotifications)) { OnValuesChanged(currentContext, contextToRestore); } }
3.2.1、new Thread(ThreadStart start).Start() 為例說(shuō)明 ExecutionContext 的流動(dòng)
這邊可以分為三個(gè)步驟:
在 Thread 的 Start 方法中捕獲當(dāng)前的 ExecutionContext,將其傳遞給 Thread 的構(gòu)造函數(shù)中實(shí)例化的 ThreadHelper 實(shí)例,ExecutionContext 會(huì)暫存在 ThreadHelper 的實(shí)例字段中,線程創(chuàng)建完成后會(huì)調(diào)用ExecutionContext.RunInternal 將其賦值給新創(chuàng)建的線程。
代碼位置:
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 的實(shí)例方法,所以 _delegate.Target 指向 ThreadHelper 實(shí)例。 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; } // 這個(gè)方法是對(duì) Thread 構(gòu)造函數(shù)傳入的委托的包裝 internal void ThreadStart() { Debug.Assert(_start is ThreadStart); ExecutionContext? context = _executionContext; if (context != null) { // 將 ExecutionContext 與 CurrentThread 進(jìn)行綁定 ExecutionContext.RunInternal(context, s_threadStartContextCallback, this); } else { InitializeCulture(); ((ThreadStart)_start)(); } } }
4、總結(jié)
AsyncLocal 本身不保存數(shù)據(jù),數(shù)據(jù)保存在 ExecutionContext 實(shí)例的 m_localValues 的私有字段上,字段類型定義是 IAsyncLocalMap ,以 IAsyncLocal => object 的 Map 結(jié)構(gòu)進(jìn)行保存,且實(shí)現(xiàn)類型隨著元素?cái)?shù)量的變化而變化。
ExecutionContext 實(shí)例 保存在 Thread.CurrentThread._executionContext 上,實(shí)現(xiàn)與當(dāng)前線程的關(guān)聯(lián)。
對(duì)于 IAsyncLocalMap 的實(shí)現(xiàn)類,如果 AsyncLocal 注冊(cè)了回調(diào),value 傳 null 不會(huì)被忽略。
沒注冊(cè)回調(diào)時(shí)分為兩種情況:如果 key 存在,則做刪除處理,map 類型可能出現(xiàn)降級(jí)。如果 key 不存在,則直接忽略。
ExecutionContext 和 IAsyncLocalMap 的實(shí)現(xiàn)類都被設(shè)計(jì)成不可變(immutable)。同一個(gè) key 前后兩次 value 發(fā)生變化后,會(huì)產(chǎn)生新的 ExecutionContext 的實(shí)例和 IAsyncLocalMap 實(shí)現(xiàn)類實(shí)例。
ExecutionContext 與當(dāng)前線程綁定,默認(rèn)流動(dòng)到輔助線程,可以禁止流動(dòng)和恢復(fù)流動(dòng),且禁止流動(dòng)僅影響當(dāng)前線程向其輔助線程的傳遞,不影響后續(xù)。
到此這篇關(guān)于淺析.NET中AsyncLocal的實(shí)現(xiàn)原理的文章就介紹到這了,更多相關(guān).NET AsyncLocal內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
提權(quán)函數(shù)之RtlAdjustPrivilege()使用說(shuō)明
RtlAdjustPrivilege() 這玩意是在 NTDLL.DLL 里的一個(gè)不為人知的函數(shù),MS沒有公開,原因就是這玩意實(shí)在是太NB了,以至于不需要任何其他函數(shù)的幫助,僅憑這一個(gè)函數(shù)就可以獲得進(jìn)程ACL的任意權(quán)限!2011-06-06關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法詳解
這篇文章主要給大家介紹了關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法,Directory類位于System.IO 命名空間,Directory類提供了在目錄和子目錄中進(jìn)行創(chuàng)建移動(dòng)和列舉操作的靜態(tài)方法,需要的朋友可以參考下2021-08-08