From 71e9c462d8603df86105935649e9c2275e3e91b6 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Thu, 2 May 2024 09:51:50 -0700 Subject: [PATCH] Notify the debugger of a cross-thread dependency in Lock (#101501) * 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 8658ab6caafb1..4dadf49bdfd4a 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 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 + // consistent. + Debugger.NotifyOfCrossThreadDependency(); + if (LazyInitializeOrEnter() == TryLockResult.Locked) { goto Locked;