.net c# Monitor.Enter如何工作?

How does Monitor.Enter work?
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我一直在做一些调查,以查找Monitor.Enter在内部的确切工作方式.我仔细检查了代码,似乎输入的实际机制是在外部函数内部:

[System.Security.SecuritySafeCritical]
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void ReliableEnterTimeout(Object obj, int timeout, ref bool lockTaken);

有谁知道此功能的作用或Monitor.Enter的一般机制是什么?如何知道哪些对象被锁定?

解决过程1

如何知道哪些对象被锁定?

.NET中的每个对象都有两个额外的(隐藏的..您看不到它们)开销成员.

  • 一个"类型对象指针".这只是Type对对象实例的引用.实际上,您可以通过调用来"访问"它GetType().
  • "同步块索引".这是本机WORD大小的整数类型,是CLR的"同步块"内部数组的索引.

同步块结构包含一个可以标记为锁定的字段.基本上,锁定对象时会打开此字段.释放锁定后,它会关闭(基本上-我对SSCLI的了解还不够长,无法深入研究这种操作的工作原理-我相信它是基于EnterCriticalSection..).

MethodImplOptions.InternalCall您传递给上述属性的参数意味着该方法的实际实现位于CLR中.这就是为什么您无法进一步浏览代码的原因.

速聊1:
谢谢,我听到了一些类似的内容,但是在system.object中看不到任何字段.您知道我在哪里可以找到更多信息吗?
速聊2:
主要是书籍.通过C#进行CLR处理..Pro .NET Performance刚好在页面上..共享源CLI(ROTOR)对其进行了实现..在这里,您可以找到的实现ReliableEnterTimeout(SSCLI将那些在内部将功能名称转换为其他名称.如果确实要查找,请搜索"ReliableEnterTimeout"并查看其对应的内容).它还具有一个同步块实现(尽管我一生都无法记住该结构的名称).
速聊3:
也许也可以研究WinAPI函数EnterCriticalSection.我相信SSCLI在某些锁定机制中使用了它(无论如何在Windows上).
速聊4:
它如何与类似的值类型一起使用int a = 42; Monitor.Enter(a)?
速聊5:
他们被装箱了.签名是:Monitor.Enter(Object).

I've been doing some investigation to find exactly how Monitor.Enter works internally. I looked through the code and it seems the actual mechanism for entering is inside an external function:

[System.Security.SecuritySafeCritical]
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void ReliableEnterTimeout(Object obj, int timeout, ref bool lockTaken);

Does anyone know what this function does or what the general mechanism of Monitor.Enter is? How does it know which objects are locked?

Solutions1

How does it know which objects are locked?

Every object in .NET has two extra (hidden.. you can't see them) overhead members.

  • A "type object pointer". This is just a reference to the Type instance of the object. In fact, you can "access" this by calling GetType().
  • A "sync block index". This is a native WORD size integral type which is an index into the CLR's internal array of "Sync Blocks".

The Sync Block structure contains a field that can be marked for locking. Basically, when you lock an object, this field is switched on. When the lock is released, it is switched off (basically - I haven't looked at the SSCLI for long enough to delve deeper into how that sort of operation works - I believe it is based on EnterCriticalSection though..).

The MethodImplOptions.InternalCall arguments you've passed to the attribute above means that the actual implementation of that method resides in the CLR.. which is why you can't browse any further through the code.

Talk1:
thanks, i heard something along those lines but couldn't see any fields in system.object. Do you know where I can find more info about this?
Talk2:
Books mostly. CLR via C# touches on it.. Pro .NET Performance has just over a page on it.. The Shared Source CLI (ROTOR) has an implementation of it.. and that's where you'll find an implementation of ReliableEnterTimeout (the SSCLI maps those functions names to other ones internally. If you do look for it, search for "ReliableEnterTimeout" and see what it maps to). It also has a sync block implementation (although I can't for the life of me remember what that structure was called).
Talk3:
Perhaps look into the WinAPI function EnterCriticalSection too.. I believe the SSCLI utilises that for some part of the locking mechanisms (on Windows anyway).
Talk4:
How does it work with value types, like int a = 42; Monitor.Enter(a)?
Talk5:
They're boxed. The signature is: Monitor.Enter(Object).
转载于:https://stackoverflow.com/questions/23692805/how-does-monitor-enter-work

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入