Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
context hash
  • Loading branch information
jason-simmons committed May 28, 2025
commit d2ef65393806817dfcadda4b1e48cc8d94784fd7
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ static std::unordered_map<
std::weak_ptr<CommandPoolVK>>> g_all_pools_map
IPLR_GUARDED_BY(g_all_pools_map_mutex);

CommandPoolRecyclerVK::CommandPoolRecyclerVK(
const std::shared_ptr<ContextVK>& context)
: context_(context), context_hash_(context->GetHash()) {}

// Visible for testing.
// Returns the number of pools in g_all_pools_map for the given context.
int CommandPoolRecyclerVK::GetGlobalPoolCount(const ContextVK& context) {
Expand All @@ -196,8 +200,7 @@ std::shared_ptr<CommandPoolVK> CommandPoolRecyclerVK::Get() {
tls_command_pool_map.reset(new CommandPoolMap());
}
CommandPoolMap& pool_map = *tls_command_pool_map.get();
auto const hash = strong_context->GetHash();
auto const it = pool_map.find(hash);
auto const it = pool_map.find(context_hash_);
if (it != pool_map.end()) {
return it->second;
}
Expand All @@ -210,11 +213,11 @@ std::shared_ptr<CommandPoolVK> CommandPoolRecyclerVK::Get() {

auto const resource = std::make_shared<CommandPoolVK>(
std::move(data->pool), std::move(data->buffers), context_);
pool_map.emplace(hash, resource);
pool_map.emplace(context_hash_, resource);

{
Lock all_pools_lock(g_all_pools_map_mutex);
g_all_pools_map[hash][std::this_thread::get_id()] = resource;
g_all_pools_map[context_hash_][std::this_thread::get_id()] = resource;
}

return resource;
Expand Down Expand Up @@ -284,31 +287,31 @@ void CommandPoolRecyclerVK::Reclaim(
RecycledData{.pool = std::move(pool), .buffers = std::move(buffers)});
}

void CommandPoolRecyclerVK::Dispose(const ContextVK& context) {
void CommandPoolRecyclerVK::Dispose() {
CommandPoolMap* pool_map = tls_command_pool_map.get();
if (pool_map) {
pool_map->erase(context.GetHash());
pool_map->erase(context_hash_);
}

{
Lock all_pools_lock(g_all_pools_map_mutex);
auto found = g_all_pools_map.find(context.GetHash());
auto found = g_all_pools_map.find(context_hash_);
if (found != g_all_pools_map.end()) {
found->second.erase(std::this_thread::get_id());
}
}
}

void CommandPoolRecyclerVK::DestroyThreadLocalPools(const ContextVK& context) {
void CommandPoolRecyclerVK::DestroyThreadLocalPools() {
// Delete the context's entry in this thread's command pool map.
if (tls_command_pool_map.get()) {
tls_command_pool_map.get()->erase(context.GetHash());
tls_command_pool_map.get()->erase(context_hash_);
}

// Destroy all other thread-local CommandPoolVK instances associated with
// this context.
Lock all_pools_lock(g_all_pools_map_mutex);
auto found = g_all_pools_map.find(context.GetHash());
auto found = g_all_pools_map.find(context_hash_);
if (found != g_all_pools_map.end()) {
for (auto& [thread_id, weak_pool] : found->second) {
auto pool = weak_pool.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,13 @@ class CommandPoolRecyclerVK final
};

/// @brief Clean up resources held by all per-thread command pools
/// associated with the given context.
///
/// @param[in] context The context.
static void DestroyThreadLocalPools(const ContextVK& context);
/// associated with the context.
void DestroyThreadLocalPools();

/// @brief Creates a recycler for the given |ContextVK|.
///
/// @param[in] context The context to create the recycler for.
explicit CommandPoolRecyclerVK(std::weak_ptr<ContextVK> context)
: context_(std::move(context)) {}
explicit CommandPoolRecyclerVK(const std::shared_ptr<ContextVK>& context);

/// @brief Gets a command pool for the current thread.
///
Expand All @@ -136,13 +133,14 @@ class CommandPoolRecyclerVK final
bool should_trim = false);

/// @brief Clears this context's thread-local command pool.
void Dispose(const ContextVK& context);
void Dispose();

// Visible for testing.
static int GetGlobalPoolCount(const ContextVK& context);

private:
std::weak_ptr<ContextVK> context_;
uint64_t context_hash_;

Mutex recycled_mutex_;
std::vector<RecycledData> recycled_ IPLR_GUARDED_BY(recycled_mutex_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST(CommandPoolRecyclerVKTest, ReclaimMakesCommandPoolAvailable) {
auto const pool = recycler->Get();

// This normally is called at the end of a frame.
recycler->Dispose(*context);
recycler->Dispose();
}

// Add something to the resource manager and have it notify us when it's
Expand Down Expand Up @@ -124,7 +124,7 @@ TEST(CommandPoolRecyclerVKTest, CommandBuffersAreRecycled) {
pool->CollectCommandBuffer(std::move(buffer));

// This normally is called at the end of a frame.
recycler->Dispose(*context);
recycler->Dispose();
}

// Wait for the pool to be reclaimed.
Expand All @@ -148,7 +148,7 @@ TEST(CommandPoolRecyclerVKTest, CommandBuffersAreRecycled) {
pool->CollectCommandBuffer(std::move(buffer));

// This normally is called at the end of a frame.
recycler->Dispose(*context);
recycler->Dispose();
}

// Now check that we only ever created one pool and one command buffer.
Expand Down Expand Up @@ -177,7 +177,7 @@ TEST(CommandPoolRecyclerVKTest, ExtraCommandBufferAllocationsTriggerTrim) {
}

// This normally is called at the end of a frame.
recycler->Dispose(*context);
recycler->Dispose();
}

// Wait for the pool to be reclaimed.
Expand All @@ -203,7 +203,7 @@ TEST(CommandPoolRecyclerVKTest, ExtraCommandBufferAllocationsTriggerTrim) {
auto pool = recycler->Get();

// This normally is called at the end of a frame.
recycler->Dispose(*context);
recycler->Dispose();
}

// Wait for the pool to be reclaimed.
Expand Down Expand Up @@ -241,8 +241,10 @@ TEST(CommandPoolRecyclerVKTest, RecyclerGlobalPoolMapSize) {

// Disposing this thread's pool should remove it from the global map.
pool.reset();
recycler->Dispose(*context);
recycler->Dispose();
EXPECT_EQ(CommandPoolRecyclerVK::GetGlobalPoolCount(*context), 0);

context->Shutdown();
}

} // namespace testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ ContextVK::~ContextVK() {
if (device_holder_ && device_holder_->device) {
[[maybe_unused]] auto result = device_holder_->device->waitIdle();
}
CommandPoolRecyclerVK::DestroyThreadLocalPools(*this);
command_pool_recycler_->DestroyThreadLocalPools();
}

Context::BackendType ContextVK::GetBackendType() const {
Expand Down Expand Up @@ -421,7 +421,7 @@ void ContextVK::Setup(Settings settings) {
}

auto command_pool_recycler =
std::make_shared<CommandPoolRecyclerVK>(weak_from_this());
std::make_shared<CommandPoolRecyclerVK>(shared_from_this());
if (!command_pool_recycler) {
VALIDATION_LOG << "Could not create command pool recycler.";
return;
Expand Down Expand Up @@ -720,7 +720,7 @@ void ContextVK::DisposeThreadLocalCachedResources() {
Lock lock(desc_pool_mutex_);
cached_descriptor_pool_.erase(std::this_thread::get_id());
}
command_pool_recycler_->Dispose(*this);
command_pool_recycler_->Dispose();
}

const std::shared_ptr<YUVConversionLibraryVK>&
Expand Down