diff --git a/src/Microsoft.VisualStudio.Threading/AsyncReaderWriterLock.cs b/src/Microsoft.VisualStudio.Threading/AsyncReaderWriterLock.cs index 4960f2b84..c0ea76470 100644 --- a/src/Microsoft.VisualStudio.Threading/AsyncReaderWriterLock.cs +++ b/src/Microsoft.VisualStudio.Threading/AsyncReaderWriterLock.cs @@ -1067,6 +1067,8 @@ private void CheckSynchronizationContextAppropriateForLock(Awaiter? awaiter) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] private bool TryIssueLock(Awaiter awaiter, bool previouslyQueued, bool skipPendingWriteLockCheck = false) { + bool issued = false; + lock (this.syncObject) { if (this.completeInvoked && !previouslyQueued) @@ -1079,7 +1081,6 @@ private bool TryIssueLock(Awaiter awaiter, bool previouslyQueued, bool skipPendi } } - bool issued = false; if (this.reenterConcurrencyPrepRunning is null) { if (this.issuedWriteLocks.Count == 0 && this.issuedUpgradeableReadLocks.Count == 0 && this.issuedReadLocks.Count == 0) @@ -1166,20 +1167,23 @@ private bool TryIssueLock(Awaiter awaiter, bool previouslyQueued, bool skipPendi if (issued) { this.GetActiveLockSet(awaiter.Kind).Add(awaiter); - this.etw.Issued(awaiter); } + } - if (!issued) - { - this.etw.WaitStart(awaiter); - - // If the lock is immediately available, we don't need to coordinate with other threads. - // But if it is NOT available, we'd have to wait potentially for other threads to do more work. - Debugger.NotifyOfCrossThreadDependency(); - } + if (issued) + { + this.etw.Issued(awaiter); + } + else + { + this.etw.WaitStart(awaiter); - return issued; + // If the lock is immediately available, we don't need to coordinate with other threads. + // But if it is NOT available, we'd have to wait potentially for other threads to do more work. + Debugger.NotifyOfCrossThreadDependency(); } + + return issued; } ///