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:
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?
How does it know which objects are locked?
Every object in .NET has two extra (hidden.. you can't see them) overhead members.
Typeinstance of the object. In fact, you can "access" this by calling
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
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.