Add Synchronized guarded-value locks with RAII-style holder handles - #1704
Draft
erikhortsch wants to merge 1 commit into
Draft
Add Synchronized guarded-value locks with RAII-style holder handles#1704erikhortsch wants to merge 1 commit into
erikhortsch wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 07dd867 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
erikhortsch
force-pushed
the
erik/synchronized
branch
2 times, most recently
from
August 5, 2026 20:31
b720729 to
9b4ca88
Compare
Lock returns the guarded value and a Holder; Unlock is a method on the handle, so releases match acquisitions exactly (including cross- goroutine handoff). Exclusive-only: with at most one holder, the guarded mutex synchronizes all bookkeeping — no atomics except the waiter count — and the by-value Holder never escapes: 5.5ns per Lock/Unlock with zero allocations (tracked Mutex 6.3ns, native 4.0ns). ScanTrackedLocks reports these locks alongside the existing trackers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
erikhortsch
force-pushed
the
erik/synchronized
branch
from
August 5, 2026 20:48
9b4ca88 to
07dd867
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Stacked on #1701. Review there surfaced the root problem with any drop-in
sync.Mutextracker: release is keyed by goroutine id, so cross-goroutine unlocks need heuristics (arbitrary eviction — the same policy go-deadlock uses) and holder tracking needs a slot cap.Synchronized[T]is the design that eliminates the class:Lockreturns the guarded value plus aHolderhandle andUnlockis a method on the handle.Design
Exclusive mutex only — and that exclusivity is what keeps it simple: with at most one holder, all holder bookkeeping (gid, acquisition time, optional acquisition stack) is written under the guarded mutex itself, so nothing needs atomics except the waiter count (maintained outside the lock). No holder list. The
Holderis returned by value and never escapes — zero allocations.Synchronized[T]guards its value:v, h := s.Lock()…h.Unlock(). The zero value works.ScanTrackedLocksreports these locks alongside the existing tracker (holder gid,HeldSince, waiter count, acquisition stack whenToggleLockTrackerStackTracesis on), soPopulateHolderStacksand downstream stuck-lock logging work unchanged. The scanner reads bookkeeping without taking the lock, tolerating racy reads exactly like the existinglockTrackerscan.Numbers (Apple M5 Pro, uncontended Lock/Unlock pair)
Intent
Successor primitive for hot, incident-prone exclusive locks (room locks, dispatcher pools) to migrate to incrementally; #1701 remains the drop-in improvement for the existing
utils.Mutex/RWMutexfleet (RW locks are few and keep the slot-based tracking).🤖 Generated with Claude Code