From 5c56011d8215bd519c2da1b709e9d503692ec02f Mon Sep 17 00:00:00 2001 From: Jeremy Gebben Date: Tue, 10 Sep 2024 09:25:37 -0600 Subject: [PATCH] cdl: Make checkpoint managers thread safe --- src/checkpoint.cpp | 20 ++++++++++++++++++-- src/checkpoint.h | 3 +++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/checkpoint.cpp b/src/checkpoint.cpp index 1de1fcb..19ef92f 100644 --- a/src/checkpoint.cpp +++ b/src/checkpoint.cpp @@ -55,33 +55,42 @@ std::unique_ptr BufferMarkerCheckpointMgr::Allocate(uint32_t initial return checkpoint; } -void BufferMarkerCheckpointMgr::Free(Checkpoint &c) {} +void BufferMarkerCheckpointMgr::Free(Checkpoint &c) { + std::lock_guard lock(checkpoint_mutex_); + checkpoint_data_.erase(c.Id()); +} void BufferMarkerCheckpointMgr::WriteTop(Checkpoint &c, VkCommandBuffer cmd, uint32_t value) { + std::lock_guard lock(checkpoint_mutex_); + auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); iter->second.top_marker->Write(cmd, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, value); } void BufferMarkerCheckpointMgr::WriteBottom(Checkpoint &c, VkCommandBuffer cmd, uint32_t value) { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); iter->second.bottom_marker->Write(cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, value); } uint32_t BufferMarkerCheckpointMgr::ReadTop(const Checkpoint &c) const { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); return iter->second.top_marker->Read(); } uint32_t BufferMarkerCheckpointMgr::ReadBottom(const Checkpoint &c) const { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); return iter->second.bottom_marker->Read(); } void BufferMarkerCheckpointMgr::Reset(Checkpoint &c) { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); iter->second.top_marker->Write(0); @@ -95,11 +104,15 @@ std::unique_ptr DiagnosticCheckpointMgr::Allocate(uint32_t initial_v Data data; data.top_value = initial_value; data.bottom_value = initial_value; + std::lock_guard lock(checkpoint_mutex_); checkpoint_data_.emplace(std::make_pair(checkpoint->Id(), std::move(data))); return checkpoint; } -void DiagnosticCheckpointMgr::Free(Checkpoint &c) { checkpoint_data_.erase(c.Id()); } +void DiagnosticCheckpointMgr::Free(Checkpoint &c) { + std::lock_guard lock(checkpoint_mutex_); + checkpoint_data_.erase(c.Id()); +} // NV checkpoints are both top and bottom markers. void DiagnosticCheckpointMgr::WriteTop(Checkpoint &c, VkCommandBuffer cmd, uint32_t value) {} @@ -110,18 +123,21 @@ void DiagnosticCheckpointMgr::WriteBottom(Checkpoint &c, VkCommandBuffer cmd, ui } uint32_t DiagnosticCheckpointMgr::ReadTop(const Checkpoint &c) const { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); return iter->second.top_value; } uint32_t DiagnosticCheckpointMgr::ReadBottom(const Checkpoint &c) const { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); return iter->second.bottom_value; } void DiagnosticCheckpointMgr::Reset(Checkpoint &c) { + std::lock_guard lock(checkpoint_mutex_); auto iter = checkpoint_data_.find(c.Id()); assert(iter != checkpoint_data_.end()); iter->second.top_value = 0; diff --git a/src/checkpoint.h b/src/checkpoint.h index 0b234ff..72f5a5c 100644 --- a/src/checkpoint.h +++ b/src/checkpoint.h @@ -17,6 +17,7 @@ #pragma once #include "marker.h" +#include namespace crash_diagnostic_layer { @@ -79,6 +80,7 @@ class BufferMarkerCheckpointMgr : public CheckpointMgr { BufferMarkerMgr markers_; + mutable std::mutex checkpoint_mutex_; std::unordered_map checkpoint_data_; uint32_t next_id_{1}; }; @@ -108,6 +110,7 @@ class DiagnosticCheckpointMgr : public CheckpointMgr { }; Device &device_; + mutable std::mutex checkpoint_mutex_; std::unordered_map checkpoint_data_; uint32_t next_id_{1}; };