Skip to content

Replace object locks with System.Threading.Lock#1977

Merged
tyrielv merged 1 commit into
microsoft:masterfrom
tyrielv:tyrielv/lock-object-migration
May 18, 2026
Merged

Replace object locks with System.Threading.Lock#1977
tyrielv merged 1 commit into
microsoft:masterfrom
tyrielv:tyrielv/lock-object-migration

Conversation

@tyrielv
Copy link
Copy Markdown
Contributor

@tyrielv tyrielv commented May 18, 2026

Summary

Migrate all private lock fields from object + new object() to System.Threading.Lock + new Lock(), the dedicated lightweight synchronization primitive introduced in .NET 9.

Changes

  • 20 lock fields across 19 files updated: private [static] [readonly] object fooLock = new object()Lock fooLock = new Lock()
  • 2 Monitor.IsEntered() assertions in GitStatusCache.cs replaced with Lock.IsHeldByCurrentThread (Monitor APIs are incompatible with the Lock type)
  • Added using System.Threading; to 10 files that were missing it

Why

System.Threading.Lock is purpose-built for lock() usage — it avoids the overhead of locking on a general-purpose object and makes the intent explicit. Since VFSForGit now targets .NET 10, all lock fields can benefit from this.

No instances of Monitor.Wait/Pulse/Enter/Exit were found, so every lock object in the codebase was a straightforward migration candidate.

Validation

  • Build: ✅ succeeded
  • Unit tests: ✅ 803/803 passed

@tyrielv tyrielv force-pushed the tyrielv/lock-object-migration branch from 58ebd6f to f6a7199 Compare May 18, 2026 20:48
.NET 9+ introduced System.Threading.Lock as a dedicated, lightweight
synchronization primitive. Migrate all private lock fields from
`object` + `new object()` to `Lock` + `new Lock()`.

Also replace Monitor.IsEntered() assertions in GitStatusCache with
Lock.IsHeldByCurrentThread, since Lock is not compatible with Monitor
APIs.

20 fields across 19 files updated. No Monitor.Wait/Pulse usage found
in the codebase, so all instances are safe to migrate.

Assisted-by: Claude Opus 4.6
Signed-off-by: Tyrie Vella <tyrielv@gmail.com>
@tyrielv tyrielv force-pushed the tyrielv/lock-object-migration branch from f6a7199 to 5eeba9e Compare May 18, 2026 20:49
@tyrielv tyrielv marked this pull request as ready for review May 18, 2026 21:20
@tyrielv tyrielv enabled auto-merge May 18, 2026 21:21
@tyrielv tyrielv merged commit 1e748bb into microsoft:master May 18, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants