C# ThreadLocal源码追踪

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即存在多个泛型类型相同的ThreadLocal,就需要使用数组进行存储,而_idComplement就是充当一个数组下标的功能

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~


https://github.com/dotnet/coreclr/blob/9773db1e7b1acb3ec75c9cc0e36bd62dcbacd6d5/src/System.Private.CoreLib/shared/System/Threading/ThreadLocal.cs

转载于:https://www.cnblogs.com/monster17/p/10899794.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThreadLocal源码是Java中一个关键的类,它提供了一种在多线程环境下实现线程本地变量的机制。在JDK 8之前和之后,ThreadLocal的内部结构有所变化。ThreadLocal源码分为两部分:ThreadLocal类和ThreadLocalMap类。 ThreadLocal类是一个泛型类,它包含了两个核心方法:set()和get()。set()方法用于将一个值与当前线程关联起来,get()方法用于获取当前线程关联的值。 ThreadLocalMap类是ThreadLocal的内部类,它用于存储每个线程的本地变量。在JDK 8之前,ThreadLocalMap是通过线性探测法解决哈希冲突的,每个ThreadLocal对象都对应一个Entry对象,Entry对象包含了ThreadLocal对象和与之关联的值[2]。 在JDK 8之后,ThreadLocalMap的实现方式发生了改变。使用了类似于HashMap的方式,采用了分段锁的机制来提高并发性能。每个线程维护一个ThreadLocalMap对象,其中的Entry对象也是采用链表的形式来解决哈希冲突。 总结起来,ThreadLocal源码主要由ThreadLocal类和ThreadLocalMap类组成。ThreadLocal类提供了set()和get()方法来管理线程本地变量,而ThreadLocalMap类则负责存储每个线程的本地变量,并解决哈希冲突的问题。 史上最全ThreadLocal 详解 ThreadLocal源码分析_02 内核(ThreadLocalMap) 【JDK源码】线程系列之ThreadLocal 深挖ThreadLocal ThreadLocal原理及内存泄露预防 ThreadLocal原理详解——终于弄明白了ThreadLocal ThreadLocal使用与原理 史上最全ThreadLocal 详解。 ThreadLocal源码分析,主要有ThreadLocal源码以及ThreadLocal的内部结构在jdk8前后的变化。 使用方式非常简单,核心就两个方法set/get public class TestThreadLocal { private static final ThreadLocal<String> threadLocal = new ThreadLocal<>(); public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { try { threadLocal.set("aaa"); Thread.sleep(500); System.out.println("threadA:" threadLocal.get()); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { @Override public void run() { threadLocal.set("bbb"); System.out.println("threadB:" threadLocal.get()); } }).start(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值