Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions cuda_core/cuda/core/_cpp/rt/internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <functional>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <thread>
#include <type_traits>
#include <unordered_map>
Expand Down Expand Up @@ -269,11 +270,13 @@ 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);
Lookup found;
released_.wait(lock, [&] {
found = find(lock, key);
return found.state != State::Releasing;
});
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;
Expand Down Expand Up @@ -316,6 +319,39 @@ 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
};

struct Lookup {
State state = State::Vacant;
Handle owner; // set only when state is Live
};

// Takes the caller's lock as proof that it holds mutex_, and checks it.
[[nodiscard]] Lookup
find(const std::unique_lock<std::mutex> &held, const Key &key) const
{
if (!held.owns_lock() || held.mutex() != &mutex_) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems unnecessary. find requiring the lock as parameter is sufficient.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That check is there b/c you can pass a reference to a mutex into a function. That does mean the thread has acquired the lock. That's what we are checking there.

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, {}};
}
Handle owner = it->second.handle.lock();
if (!owner) {
return {State::Releasing, {}};
}
return {State::Live, std::move(owner)};
}

std::mutex mutex_;
std::condition_variable released_;
std::unordered_map<Key, Entry, Hash> map_;
Expand Down
Loading