ThreadLocal
字段成員:
private Func<T>? _valueFactory;
一個獲取默認值的委託 不同線程共享此成員。
[ThreadStatic]
private static LinkedSlotVolatile[]? ts_slotArray;
ThreadStatic特性,這不就是我們熟悉的ThreadStaticAttribute嗎,
🤯🤯🤯所以ThreadLocal 就是一個ThreadStatic的封裝類,簡化了tls操作
[ThreadStatic]
private static FinalizationHelper? ts_finalizationHelper;
見名思義,用於釋放的幫助類
private int _idComplement;
Slot ID of this ThreadLocal<instance.
這個ThreadLocal<>實例的槽ID。
We store a bitwise complement of the ID (that is ~ID), which allows us to distinguish
我們存儲ID的位補碼(即~ID),這使我們能夠區分
between the case when ID is 0 and an incompletely initialized object, either due to a thread abort in the constructor, or
在ID爲0的情況和未完全初始化的對象之間,原因可能是構造函數中的線程中止,也可能是
possibly due to a memory model issue in user code.
可能是由於用戶代碼中的內存模型問題。
用於區分是否初始化。
private volatile bool _initialized;
表示對象是否完全初始化..
private volatile bool _initialized;
是否初始化-構造函數
private static readonly IdManager s_idManager = new IdManager();
IdManager assigns and reuses slot IDs.
IdManager分配和重用插槽id。
Additionally, the object is also used as a global lock.
此外,該對象還用作全局鎖。
private LinkedSlot? _linkedSlot = new LinkedSlot(null);
僞頭節點
private bool _trackAllValues;
是否支持Values屬性
方法
private void Initialize(Func<T>? valueFactory, bool trackAllValues)
{
_valueFactory = valueFactory;
_trackAllValues = trackAllValues;
// Assign the ID and mark the instance as initialized. To avoid leaking IDs, we assign the ID and set _initialized
// in a finally block, to avoid a thread abort in between the two statements.
try { }
finally
{
_idComplement = ~s_idManager.GetId();
// As the last step, mark the instance as fully initialized. (Otherwise, if _initialized=false, we know that an exception
// occurred in the constructor.)
_initialized = true;
}
}
初始化方法,所有構造通過此方法初始化。
查看IdManager的GetId方法:
internal int GetId()
{
List<bool> freeIds = this.m_freeIds;
lock (freeIds)
{
int nextIdToTry = this.m_nextIdToTry;
while (nextIdToTry < this.m_freeIds.Count)
{
if (this.m_freeIds[nextIdToTry])
{
break;
}
nextIdToTry++;
}
if (nextIdToTry == this.m_freeIds.Count)
{
this.m_freeIds.Add(false);
}
else
{
this.m_freeIds[nextIdToTry] = false;
}
this.m_nextIdToTry = nextIdToTry + 1;
return nextIdToTry;
}
}
具體就不說明了,類似於數據庫中的自增標識
注:由於ThreadLocal爲泛型類,僅當構造同類型的ThreadLocal纔會觸發自增
這裏我們也可以知道爲何需要一個LinkedSlotVolatile數組
當線程中存在多個ThreadLocal
public T Value
{
get
{
LinkedSlotVolatile[]? slotArray = ts_slotArray;
LinkedSlot? slot;
int id = ~_idComplement;
//
// Attempt to get the value using the fast path
//
if (slotArray != null // Has the slot array been initialized?
&& id >= 0 // Is the ID non-negative (i.e., instance is not disposed)?
&& id < slotArray.Length // Is the table large enough?
&& (slot = slotArray[id].Value) != null // Has a LinkedSlot object has been allocated for this ID?
&& _initialized // Has the instance *still* not been disposed (important for a race condition with Dispose)?
)
{
// We verified that the instance has not been disposed *after* we got a reference to the slot.
// This guarantees that we have a reference to the right slot.
//
// Volatile read of the LinkedSlotVolatile.Value property ensures that the m_initialized read
// will not be reordered before the read of slotArray[id].
return slot._value;
}
return GetValueSlow();
}
set
{
LinkedSlotVolatile[]? slotArray = ts_slotArray;
LinkedSlot? slot;
int id = ~_idComplement;
// Attempt to set the value using the fast path
if (slotArray != null // Has the slot array been initialized?
&& id >= 0 // Is the ID non-negative (i.e., instance is not disposed)?
&& id < slotArray.Length // Is the table large enough?
&& (slot = slotArray[id].Value) != null // Has a LinkedSlot object has been allocated for this ID?
&& _initialized // Has the instance *still* not been disposed (important for a race condition with Dispose)?
)
{
// We verified that the instance has not been disposed *after* we got a reference to the slot.
// This guarantees that we have a reference to the right slot.
//
// Volatile read of the LinkedSlotVolatile.Value property ensures that the m_initialized read
// will not be reordered before the read of slotArray[id].
slot._value = value;
}
else
{
SetValueSlow(value, slotArray);
}
}
}
如果slotArray中有值就操作slotArray ,否則就
寫-更新slotArray
讀-從_valueFactory 取值
到這裏就差不多了,over~