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

