Repository navigation
fix(cuda.core): share ownership of repeat imports of an IPC memory pool #3511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a4a6922
4a0537f
8a5f50d
96a6a5c
ffe0ab0
ec5a3a9
35e75c8
8de80bb
6b11e5e
c098961
8fe6d05
86b2ac8
dc522f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include "types.hpp" | ||
| #include "driver_api.hpp" | ||
| #include "error.hpp" | ||
| #include <condition_variable> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <functional> | ||
|
|
@@ -192,7 +193,6 @@ class HandleRegistry { | |
| std::scoped_lock lock(mutex_); | ||
| map_.erase(key); | ||
| } | ||
|
|
||
| Handle | ||
| lookup(const Key &key) | ||
| { | ||
|
|
@@ -244,4 +244,81 @@ class HandleRegistry { | |
| MapType map_; | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
| // Handle registry that waits for in-flight releases | ||
| // | ||
| // Like HandleRegistry, but an entry outlives its handle's last reference: only | ||
| // the owner's deleter removes it, by calling release() once its cleanup is | ||
| // done. get_or_create() waits while the entry under its key has expired, so a | ||
| // new owner is never created while the previous owner is still releasing the | ||
| // resource. Used where the driver hands the same resource to repeat imports | ||
| // (IPC pools and IPC pointers), so a new owner and an old owner's cleanup | ||
| // would act on one driver resource. See REGISTRY_DESIGN.md. | ||
| // ============================================================================ | ||
|
|
||
| template <typename Key, typename Handle, typename Hash = std::hash<Key>> | ||
| class ReleasingHandleRegistry { | ||
| public: | ||
| // Return the live handle registered under key, waiting first while the | ||
| // previous owner's release is in flight. Otherwise register and return the | ||
| // handle that create() returns; an empty handle is returned unregistered. | ||
| // create() runs under the registry's mutex, so it must not acquire the GIL | ||
| // or release a handle whose deleter calls release(). | ||
| template <typename Factory> | ||
| Handle | ||
| 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)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: template <typename Factory>
Handle get_or_create(const Key &key, Factory &&create)
{
std::unique_lock lock(mutex_);
Handle h;
released_.wait(lock, [&] {
auto it = map_.find(key);
if (it == map_.end()) return true; // nothing registered
h = it->second.handle.lock();
return bool(h); // live owner; else keep waiting
});
if (h) return h;
// Insert first. If create() ran first and the insert threw, ~h would run
// the deleter, which calls release(), which takes this mutex.
auto it = map_.try_emplace(key).first;
try { h = create(); } catch (...) { map_.erase(it); throw; }
if (h) it->second = Entry{h, h.get()}; else map_.erase(it);
return h;
}
If every factory is I haven't compiled or tested this. |
||
| if (Handle h = it->second.handle.lock()) { | ||
| return h; | ||
| } | ||
| released_.wait(lock); | ||
| } | ||
| // Insert before creating, so a failed insertion has nothing to undo. | ||
| auto it = map_.try_emplace(key).first; | ||
| Handle h; | ||
| try { | ||
| h = create(); | ||
| } | ||
| catch (...) { | ||
| map_.erase(it); | ||
| throw; | ||
| } | ||
| if (h) { | ||
| it->second = Entry{h, h.get()}; | ||
| } | ||
| else { | ||
| map_.erase(it); | ||
| } | ||
| return h; | ||
| } | ||
|
|
||
| // Called by an owner's deleter after its last driver call on the resource: | ||
| // erase key's entry if it belongs to the handle that pointed to resource, | ||
| // and wake the importers waiting for it. | ||
| void | ||
| release(const Key &key, const typename Handle::element_type *resource) noexcept | ||
| { | ||
| { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additional scopes aren't required. Its legal to hold the mutex when calling notify on the CV. |
||
| std::scoped_lock lock(mutex_); | ||
| auto it = map_.find(key); | ||
| if (it != map_.end() && it->second.resource == resource) { | ||
| map_.erase(it); | ||
| } | ||
| } | ||
| released_.notify_all(); | ||
| } | ||
|
|
||
| private: | ||
| struct Entry { | ||
| std::weak_ptr<typename Handle::element_type> handle; | ||
| const typename Handle::element_type *resource = nullptr; | ||
| }; | ||
|
|
||
| std::mutex mutex_; | ||
| std::condition_variable released_; | ||
| std::unordered_map<Key, Entry, Hash> map_; | ||
| }; | ||
|
|
||
| } // namespace cuda_core::rt::detail | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juenglin / @Andy-Jost : Is this a testing only symbol that we are shipping as part of the runtime?
If those we should limit these types of test-only helper utilities to the testing module and not ship them as part of the public facing API.