-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add managed blocking info for lock and monitor waits #101192
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadBlockingInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Threading | ||
{ | ||
// Tracks some kinds of blocking on the thread, like waiting on locks where it may be useful to know when debugging which | ||
// thread owns the lock. | ||
// | ||
// Notes: | ||
// - The type, some fields, and some other members may be used by debuggers (noted specifically below), so take care when | ||
// renaming them | ||
// - There is a native version of this struct in CoreCLR, used by Monitor to fold in its blocking info here. The struct is | ||
// blittable with sequential layout to support that. | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal unsafe struct ThreadBlockingInfo | ||
{ | ||
#if CORECLR | ||
// In CoreCLR, for the Monitor object kinds, the object ptr will be a pointer to a native AwareLock object. This | ||
// relative offset indicates the location of the field holding the lock owner OS thread ID (the field is of type | ||
// size_t), and is used to get that info by the LockOwnerOSThreadId property. The offset is not zero currently, so zero | ||
// is used to determine if the static field has been initialized. | ||
// | ||
// This mechanism is used instead of using an FCall in the property getter such that the property can be more easily | ||
// evaluated by a debugger. | ||
private static int s_monitorObjectOffsetOfLockOwnerOSThreadId; | ||
#endif | ||
|
||
// Points to the first (most recent) blocking info for the thread. The _next field points to the next-most-recent | ||
// blocking info for the thread, or null if there are no more. Blocking can be reentrant in some cases, such as on UI | ||
// threads where reentrant waits are used, or if a SynchronizationContext wait override is set. | ||
[ThreadStatic] | ||
private static ThreadBlockingInfo* t_first; // may be used by debuggers | ||
|
||
// This pointer can be used to obtain the object relevant to the blocking. For native object kinds, it points to the | ||
// native object (for Monitor object kinds in CoreCLR, it points to a native AwareLock object). For managed object | ||
// kinds, it points to a stack location containing the managed object reference. | ||
private void* _objectPtr; // may be used by debuggers | ||
|
||
// Indicates the type of object relevant to the blocking | ||
private ObjectKind _objectKind; // may be used by debuggers | ||
|
||
// The timeout in milliseconds for the wait, -1 for infinite timeout | ||
private int _timeoutMs; // may be used by debuggers | ||
|
||
// Points to the next-most-recent blocking info for the thread | ||
private ThreadBlockingInfo* _next; // may be used by debuggers | ||
|
||
private void Push(void* objectPtr, ObjectKind objectKind, int timeoutMs) | ||
{ | ||
Debug.Assert(objectPtr != null); | ||
|
||
_objectPtr = objectPtr; | ||
_objectKind = objectKind; | ||
_timeoutMs = timeoutMs; | ||
_next = t_first; | ||
t_first = (ThreadBlockingInfo*)Unsafe.AsPointer(ref this); | ||
} | ||
|
||
private void Pop() | ||
{ | ||
Debug.Assert(_objectPtr != null); | ||
Debug.Assert(t_first != null); | ||
Debug.Assert(t_first->_next == _next); | ||
|
||
t_first = _next; | ||
_objectPtr = null; | ||
} | ||
|
||
// If the blocking is associated with a lock of some kind that has thread affinity and tracks the owner's OS thread ID, | ||
// returns the OS thread ID of the thread that currently owns the lock. Otherwise, returns 0. A return value of 0 may | ||
// indicate that the associated lock is currently not owned by a thread, or that the information could not be | ||
// determined. | ||
// | ||
// Calls to native helpers are avoided in the property getter such that it can be more easily evaluated by a debugger. | ||
public ulong LockOwnerOSThreadId // the getter may be used by debuggers | ||
{ | ||
get | ||
{ | ||
Debug.Assert(_objectPtr != null); | ||
|
||
switch (_objectKind) | ||
{ | ||
case ObjectKind.MonitorLock: | ||
case ObjectKind.MonitorWait: | ||
// The Monitor object kinds are only used by CoreCLR, and only the OS thread ID is reported | ||
#if CORECLR | ||
if (s_monitorObjectOffsetOfLockOwnerOSThreadId != 0) | ||
{ | ||
return *(nuint*)((nint)_objectPtr + s_monitorObjectOffsetOfLockOwnerOSThreadId); | ||
} | ||
#endif | ||
return 0; | ||
|
||
case ObjectKind.Lock: | ||
return ((Lock)Unsafe.AsRef<object>(_objectPtr)).OwningOSThreadId; | ||
|
||
default: | ||
Debug.Assert(_objectKind == ObjectKind.Condition); | ||
#if NATIVEAOT | ||
return ((Condition)Unsafe.AsRef<object>(_objectPtr)).AssociatedLock.OwningOSThreadId; | ||
#else | ||
return 0; | ||
#endif | ||
} | ||
} | ||
} | ||
|
||
// If the blocking is associated with a lock of some kind that has thread affinity and tracks the owner's managed thread | ||
// ID, returns the managed thread ID of the thread that currently owns the lock. Otherwise, returns 0. A return value of | ||
// 0 may indicate that the associated lock is currently not owned by a thread, or that the information could not be | ||
// determined. | ||
// | ||
// Calls to native helpers are avoided in the property getter such that it can be more easily evaluated by a debugger. | ||
public int LockOwnerManagedThreadId // the getter may be used by debuggers | ||
{ | ||
get | ||
{ | ||
Debug.Assert(_objectPtr != null); | ||
|
||
switch (_objectKind) | ||
{ | ||
case ObjectKind.MonitorLock: | ||
case ObjectKind.MonitorWait: | ||
// The Monitor object kinds are only used by CoreCLR, and only the OS thread ID is reported | ||
return 0; | ||
|
||
case ObjectKind.Lock: | ||
return ((Lock)Unsafe.AsRef<object>(_objectPtr)).OwningManagedThreadId; | ||
|
||
default: | ||
Debug.Assert(_objectKind == ObjectKind.Condition); | ||
#if NATIVEAOT | ||
return ((Condition)Unsafe.AsRef<object>(_objectPtr)).AssociatedLock.OwningManagedThreadId; | ||
#else | ||
return 0; | ||
#endif | ||
} | ||
} | ||
} | ||
|
||
public unsafe ref struct Scope | ||
{ | ||
private object? _object; | ||
private ThreadBlockingInfo _blockingInfo; | ||
|
||
#pragma warning disable CS9216 // casting Lock to object | ||
public Scope(Lock lockObj, int timeoutMs) : this(lockObj, ObjectKind.Lock, timeoutMs) { } | ||
#pragma warning restore CS9216 | ||
|
||
#if NATIVEAOT | ||
public Scope(Condition condition, int timeoutMs) : this(condition, ObjectKind.Condition, timeoutMs) { } | ||
#endif | ||
|
||
private Scope(object obj, ObjectKind objectKind, int timeoutMs) | ||
{ | ||
_object = obj; | ||
_blockingInfo.Push(Unsafe.AsPointer(ref _object), objectKind, timeoutMs); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_object is not null) | ||
{ | ||
_blockingInfo.Pop(); | ||
_object = null; | ||
} | ||
} | ||
} | ||
|
||
public enum ObjectKind // may be used by debuggers | ||
{ | ||
MonitorLock, // maps to DebugBlockingItemType::DebugBlock_MonitorCriticalSection in coreclr | ||
MonitorWait, // maps to DebugBlockingItemType::DebugBlock_MonitorEvent in coreclr | ||
Lock, | ||
Condition | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a 2nd definition of these types? Could we update DebugBlockingItem and DebugBlockingItemType to match instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current definition appears to be used by debugger APIs to enumerate the blocking items. It seems there would need to be a second object on the stack for maintaining a separate linked list that contains info only about Monitor blocking, as the current APIs to get info from a blocking object would not work with Lock. I was thinking it might be easier to remove the current definitions in the future after debuggers switch to using the managed surface, and maybe deprecate some of the relevant debugger APIs. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The types that get exposed publicly should be CorDebugBlockingObject and CorDebugBlockingReason in the ICorDebug interface. The types exposed in the DAC IDacDbiInterface are not public and can be changed as needed. The code to convert from the internal to public types occurs here:
runtime/src/coreclr/debug/di/rsthread.cpp
Line 2750 in d92ac1f
However if we removed the domain field off the internal type we'd have to repopulate it from somewhere else and it looks like we've never done the work to convert ICorDebug to understand that CoreCLR just has a single AppDomain. Its probably not that hard, but I wouldn't feel right asking you to do that refactoring when the ground work isn't there yet. So I'd say lets not worry about this, it was just a potential for some minor deduplication.
In terms of deprecation we've rarely ever done it in the past but it is possible if supporting the old APIs is ongoing hassle and we've allowed some time to pass so that the new API is easily available. I'd think next release at the earliest unless there is some pressing need. We'd need to remove the usage from our tests, remove code from the runtime, and put out notifications of the breaking change. Thanks Kount!