From d595a334b324ebd03e828e3af8d6f012d5e153a3 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Tue, 23 Apr 2024 13:58:06 -0700 Subject: [PATCH 1/2] Notify the debugger of a cross-thread dependency in Lock When a FuncEval tries to acquire a Lock that is held by a different thread, notify the debugger of a cross-thread dependency. --- .../src/System/Threading/Lock.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs index b7961869eac5d..532e33d2aa436 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs @@ -334,6 +334,17 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId) return new ThreadId(0); } + // + // At this point, a full lock attempt has been made, and it's time to retry or wait for the lock. + // + + // Notify the debugger that this thread is about to waiting for a lock that is likely owned by another thread. The + // debugger may choose to enable other threads to run to help resolve the dependency, or it may choose to abort the + // FuncEval here. The lock state is consistent here for an abort, whereas letting a FuncEval continue to run could + // lead to the FuncEval timing out and potentially aborting at an arbitrary place where the lock state may not be + // consistent. + Debugger.NotifyOfCrossThreadDependency(); + if (LazyInitializeOrEnter() == TryLockResult.Locked) { goto Locked; From 4ada5401a21ce253c9aea1e55e514c5c1ffee258 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Wed, 24 Apr 2024 11:19:50 -0700 Subject: [PATCH 2/2] Fix wording --- .../System.Private.CoreLib/src/System/Threading/Lock.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs index 532e33d2aa436..c9a38ef64329d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs @@ -338,7 +338,7 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId) // At this point, a full lock attempt has been made, and it's time to retry or wait for the lock. // - // Notify the debugger that this thread is about to waiting for a lock that is likely owned by another thread. The + // Notify the debugger that this thread is about to wait for a lock that is likely held by another thread. The // debugger may choose to enable other threads to run to help resolve the dependency, or it may choose to abort the // FuncEval here. The lock state is consistent here for an abort, whereas letting a FuncEval continue to run could // lead to the FuncEval timing out and potentially aborting at an arbitrary place where the lock state may not be