From 5224ba67483889bcf26d4fca3ca48e0a34af6b03 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Tue, 24 Nov 2026 09:08:56 -0700 Subject: [PATCH 1/4] refactor(cuda.core): name the states of a ReleasingHandleRegistry entry get_or_create() told "release in flight" apart from "live" by whether an entry's weak_ptr had expired, inside a hand-written wait loop. Name the three states (Vacant, Live, Releasing) and wait on a predicate. No behavior change. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_core/cuda/core/_cpp/rt/internal.hpp | 33 ++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/cuda_core/cuda/core/_cpp/rt/internal.hpp b/cuda_core/cuda/core/_cpp/rt/internal.hpp index 355f4445ff6e..93b7cad85954 100644 --- a/cuda_core/cuda/core/_cpp/rt/internal.hpp +++ b/cuda_core/cuda/core/_cpp/rt/internal.hpp @@ -269,11 +269,14 @@ class ReleasingHandleRegistry { get_or_create(const Key &key, Factory &&create) { std::unique_lock lock(mutex_); - for (auto it = map_.find(key); it != map_.end(); it = map_.find(key)) { - if (Handle h = it->second.handle.lock()) { - return h; - } - released_.wait(lock); + Handle owner; + State state; + released_.wait(lock, [&] { + state = find(key, owner); + return state != State::Releasing; + }); + if (state == State::Live) { + return owner; } // Insert before creating, so a failed insertion has nothing to undo. auto it = map_.try_emplace(key).first; @@ -316,6 +319,26 @@ class ReleasingHandleRegistry { const typename Handle::element_type *resource = nullptr; }; + // What is registered under a key. An entry outlives its owner's last + // reference, so an entry whose owner has expired is a release in flight. + enum class State { + Vacant, // no entry: the caller may create an owner + Live, // an owner holds the resource + Releasing, // the owner's deleter has not called release() yet + }; + + // Requires mutex_. Sets owner only when the state is Live. + State + find(const Key &key, Handle &owner) const + { + auto it = map_.find(key); + if (it == map_.end()) { + return State::Vacant; + } + owner = it->second.handle.lock(); + return owner ? State::Live : State::Releasing; + } + std::mutex mutex_; std::condition_variable released_; std::unordered_map map_; From 3828ea358ecce7257870b4991ee208c50fd032dd Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Tue, 24 Nov 2026 09:33:06 -0700 Subject: [PATCH 2/4] refactor(cuda.core): return the registry lookup as a struct find() returned the state and filled the owner through an out parameter. Return both in a Lookup struct. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_core/cuda/core/_cpp/rt/internal.hpp | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/cuda_core/cuda/core/_cpp/rt/internal.hpp b/cuda_core/cuda/core/_cpp/rt/internal.hpp index 93b7cad85954..0cd178968bd0 100644 --- a/cuda_core/cuda/core/_cpp/rt/internal.hpp +++ b/cuda_core/cuda/core/_cpp/rt/internal.hpp @@ -269,14 +269,13 @@ class ReleasingHandleRegistry { get_or_create(const Key &key, Factory &&create) { std::unique_lock lock(mutex_); - Handle owner; - State state; + Lookup found; released_.wait(lock, [&] { - state = find(key, owner); - return state != State::Releasing; + found = find(key); + return found.state != State::Releasing; }); - if (state == State::Live) { - return owner; + if (found.state == State::Live) { + return found.owner; } // Insert before creating, so a failed insertion has nothing to undo. auto it = map_.try_emplace(key).first; @@ -327,16 +326,24 @@ class ReleasingHandleRegistry { Releasing, // the owner's deleter has not called release() yet }; - // Requires mutex_. Sets owner only when the state is Live. - State - find(const Key &key, Handle &owner) const + struct Lookup { + State state = State::Vacant; + Handle owner; // set only when state is Live + }; + + // Requires mutex_. + Lookup + find(const Key &key) const { auto it = map_.find(key); if (it == map_.end()) { - return State::Vacant; + return {State::Vacant, {}}; + } + Handle owner = it->second.handle.lock(); + if (!owner) { + return {State::Releasing, {}}; } - owner = it->second.handle.lock(); - return owner ? State::Live : State::Releasing; + return {State::Live, std::move(owner)}; } std::mutex mutex_; From b22104ce7e04cef9b3294c176c1ef18c8c91dae5 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Tue, 24 Nov 2026 09:40:38 -0700 Subject: [PATCH 3/4] refactor(cuda.core): make the registry lookup check its caller's lock find() said "requires mutex_" in a comment. It now takes the caller's unique_lock and raises an internal error unless that lock is held and locks this registry's mutex. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_core/cuda/core/_cpp/rt/internal.hpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cuda_core/cuda/core/_cpp/rt/internal.hpp b/cuda_core/cuda/core/_cpp/rt/internal.hpp index 0cd178968bd0..d9c45a0b2cc4 100644 --- a/cuda_core/cuda/core/_cpp/rt/internal.hpp +++ b/cuda_core/cuda/core/_cpp/rt/internal.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -271,7 +272,7 @@ class ReleasingHandleRegistry { std::unique_lock lock(mutex_); Lookup found; released_.wait(lock, [&] { - found = find(key); + found = find(lock, key); return found.state != State::Releasing; }); if (found.state == State::Live) { @@ -331,10 +332,15 @@ class ReleasingHandleRegistry { Handle owner; // set only when state is Live }; - // Requires mutex_. + // Takes the caller's lock as proof that it holds mutex_, and checks it. Lookup - find(const Key &key) const + find(const std::unique_lock &held, const Key &key) const { + if (!held.owns_lock() || held.mutex() != &mutex_) { + throw std::runtime_error( + "internal cuda.core error, please report: registry lookup without the registry's lock" + ); + } auto it = map_.find(key); if (it == map_.end()) { return {State::Vacant, {}}; From 1f80ea2e4e56d7d5fed8d3ac017e5daa692c3082 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Tue, 24 Nov 2026 09:55:01 -0700 Subject: [PATCH 4/4] fix(cuda.core): mark the registry lookup [[nodiscard]] clang-tidy's modernize-use-nodiscard rejects find() in the Windows build. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_core/cuda/core/_cpp/rt/internal.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cuda_core/cuda/core/_cpp/rt/internal.hpp b/cuda_core/cuda/core/_cpp/rt/internal.hpp index d9c45a0b2cc4..2a0d9d7d9c64 100644 --- a/cuda_core/cuda/core/_cpp/rt/internal.hpp +++ b/cuda_core/cuda/core/_cpp/rt/internal.hpp @@ -333,7 +333,7 @@ class ReleasingHandleRegistry { }; // Takes the caller's lock as proof that it holds mutex_, and checks it. - Lookup + [[nodiscard]] Lookup find(const std::unique_lock &held, const Key &key) const { if (!held.owns_lock() || held.mutex() != &mutex_) {