Skip to content

Commit

Permalink
cdl: Make checkpoint managers thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyg-lunarg committed Sep 10, 2024
1 parent ce17656 commit 5c56011
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,42 @@ std::unique_ptr<Checkpoint> BufferMarkerCheckpointMgr::Allocate(uint32_t initial
return checkpoint;
}

void BufferMarkerCheckpointMgr::Free(Checkpoint &c) {}
void BufferMarkerCheckpointMgr::Free(Checkpoint &c) {
std::lock_guard<std::mutex> lock(checkpoint_mutex_);
checkpoint_data_.erase(c.Id());
}

void BufferMarkerCheckpointMgr::WriteTop(Checkpoint &c, VkCommandBuffer cmd, uint32_t value) {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(checkpoint_mutex_);
auto iter = checkpoint_data_.find(c.Id());
assert(iter != checkpoint_data_.end());
iter->second.top_marker->Write(0);
Expand All @@ -95,11 +104,15 @@ std::unique_ptr<Checkpoint> DiagnosticCheckpointMgr::Allocate(uint32_t initial_v
Data data;
data.top_value = initial_value;
data.bottom_value = initial_value;
std::lock_guard<std::mutex> 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<std::mutex> 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) {}
Expand All @@ -110,18 +123,21 @@ void DiagnosticCheckpointMgr::WriteBottom(Checkpoint &c, VkCommandBuffer cmd, ui
}

uint32_t DiagnosticCheckpointMgr::ReadTop(const Checkpoint &c) const {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(checkpoint_mutex_);
auto iter = checkpoint_data_.find(c.Id());
assert(iter != checkpoint_data_.end());
iter->second.top_value = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include "marker.h"
#include <mutex>

namespace crash_diagnostic_layer {

Expand Down Expand Up @@ -79,6 +80,7 @@ class BufferMarkerCheckpointMgr : public CheckpointMgr {

BufferMarkerMgr markers_;

mutable std::mutex checkpoint_mutex_;
std::unordered_map<CheckpointId, Data> checkpoint_data_;
uint32_t next_id_{1};
};
Expand Down Expand Up @@ -108,6 +110,7 @@ class DiagnosticCheckpointMgr : public CheckpointMgr {
};

Device &device_;
mutable std::mutex checkpoint_mutex_;
std::unordered_map<CheckpointId, Data> checkpoint_data_;
uint32_t next_id_{1};
};
Expand Down

0 comments on commit 5c56011

Please sign in to comment.