当完全.NET监视器进入内核模式? [英] When exactly .NET Monitor goes to kernel-mode?

查看:300
本文介绍了当完全.NET监视器进入内核模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编译使显示器进入内核模式下,所有可能条件的列表/使用内核同步对象。

I would like to compile a list of all possible conditions making Monitor go to kernel-mode / use kernel sync object.

同步块有一个字段来引用内核对象,因此我认为扣除锁定将进入内核模式的某个时候。

Sync block has a field to reference kernel object hence I deducted that lock will go to kernel-mode sometime.

我发现这一点:锁(监视器)在.NET 内部实现

I found this : Lock (Monitor) internal implementation in .NET

但它有太多的问题需要回答,唯一有用的信息是,OP通过简单地指出锁定将进入内核级回答了自己的问题模式的某个时候。也没有指向任何东西来支持这个答案。

but it has too many questions to be answered and the only useful information is that the OP answered his own question by simply stating that the lock will go to the kernel-mode sometime. Also there is no links to anything to support that answer.

我的问题是不同的 - 我想知道究竟锁定将进入内核模式(如果没有,不为什么 - 当)

My question is different - I want to know when exactly lock will go to kernel-mode (not if and not why - when).

我比较有兴趣了解.NET 4和4.5是否存在与旧版本的

Edit:从里氏书:一个同步块包含一个内核对象,拥有线程的ID,递归数和等待的线程数场

推荐答案

大多数这类问题可以通过在CLR源$ C ​​$ C面色可以通过的 SSCLI20分布。它越来越受过时了pretty的,这是.NET 2.0的年份,但很多核心CLR的功能都没有太大的改变。

Most of these kind of questions can be answered by looking at the CLR source code as available through the SSCLI20 distribution. It is getting pretty dated by now, it is .NET 2.0 vintage, but a lot of the core CLR features haven't changed much.

您想看看是CLR / src目录/ VM / syncblk.cpp源$ C ​​$ C文件。三类在这方面发挥的作用,AwareLock是低级别的锁实现,需要获取锁的照顾,的SyncBlock是实现了正在等待进入一个锁的线程队列中的类,CLREvent是包装操作系统同步对象,则是问有关的人。

The source code file you want to look at is clr/src/vm/syncblk.cpp. Three classes play a role here, AwareLock is the low-level lock implementation that takes care of acquiring the lock, SyncBlock is the class that implements the queue of threads that are waiting to enter a lock, CLREvent is the wrapper for the operating system synchronization object, the one you are asking about.

这是C ++ code和抽象的水平是相当高的,这code大量交互与垃圾回收器,并有大量的测试code包括在内。所以,我给这个过程的简要说明。

This is C++ code and the level of abstraction is quite high, this code heavily interacts with the garbage collector and there's a lot of testing code included. So I'll give a brief description of the process.

的SyncBlock具有m_Monitor构件,其存储所述AwareLock实例。的SyncBlock ::回车()直接调用AwareLock ::回车()。它首先会尝试尽可能便宜获取锁。首先检查是否线程已经拥有该锁,只是递增的锁计数,如果是这样的话。接下来使用FastInterlockCompareExchange(),内部的功能非常类似于Interlocked.CompareExchange()。如果锁没有争然后​​这个成功非常迅速和Monitor.Enter()返回。如果没有,那么另一个线程已经拥有该锁,AwareLock :: EnterEpilog使用。还有一个需要得到操作系统的线程调度所涉及的一个CLREvent使用。它是动态如果需要创建,其的WaitOne()方法被调用。这将涉及到内核的过渡。

SyncBlock has the m_Monitor member that stores the AwareLock instance. SyncBlock::Enter() directly calls AwareLock::Enter(). It first tries to acquire the lock as cheaply as possible. First checking if the thread already owns the lock and just incrementing the lock count if that's the case. Next using FastInterlockCompareExchange(), an internal function that's very similar to Interlocked.CompareExchange(). If the lock is not contended then this succeeds very quickly and Monitor.Enter() returns. If not then another thread already owns the lock, AwareLock::EnterEpilog is used. There's a need to get the operating system's thread scheduler involved so a CLREvent is used. It is dynamically created if necessary and its WaitOne() method is called. Which will involve a kernel transition.

所以有足够的回答你的问题:Monitor类进入时,锁争用内核模式和线程必须等待

So enough there to answer your question: the Monitor class enters kernel mode when the lock is contended and the thread has to wait.

这篇关于当完全.NET监视器进入内核模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆