Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
Add class ScopedDestroyLock to optimize the source code for lock/unlock
the corresponding mutex;
  • Loading branch information
mizhen authored and locke-lunarg committed Jan 10, 2024
1 parent 2ce1b9c commit 1353691
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 102 deletions.
7 changes: 1 addition & 6 deletions framework/encode/vulkan_handle_wrapper_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ GFXRECON_BEGIN_NAMESPACE(encode)

VulkanStateHandleTable state_handle_table_;

std::shared_mutex mutex_for_create_destroy_handle_;

std::unique_lock<std::shared_mutex> ScopedDestroyLock()
{
return std::move(std::unique_lock<std::shared_mutex>(mutex_for_create_destroy_handle_));
}
std::shared_mutex ScopedDestroyLock::mutex_for_create_destroy_handle_;

uint64_t GetWrappedId(uint64_t object, VkObjectType object_type)
{
Expand Down
45 changes: 41 additions & 4 deletions framework/encode/vulkan_handle_wrapper_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,46 @@ The functions LockForDestroyHandle and UnlockForDestroyHandle should be used dur
lock to the deletion of handles and their wrapper.
*/

extern std::shared_mutex mutex_for_create_destroy_handle_;
class ScopedDestroyLock
{
public:
ScopedDestroyLock(bool shared = false)
{
lock_shared_ = shared;
if (shared)
{
mutex_for_create_destroy_handle_.lock_shared();
}
else
{
mutex_for_create_destroy_handle_.lock();
}
};

std::unique_lock<std::shared_mutex> ScopedDestroyLock();
~ScopedDestroyLock()
{
if (lock_shared_)
{
mutex_for_create_destroy_handle_.unlock_shared();
}
else
{
mutex_for_create_destroy_handle_.unlock();
}
};

ScopedDestroyLock(const ScopedDestroyLock&) = delete;

ScopedDestroyLock(ScopedDestroyLock&&) = delete;

ScopedDestroyLock& operator=(const ScopedDestroyLock&) = delete;

ScopedDestroyLock& operator=(ScopedDestroyLock&&) = delete;

private:
bool lock_shared_ = false;
static std::shared_mutex mutex_for_create_destroy_handle_;
};

template <typename Wrapper>
format::HandleId GetTempWrapperId(const typename Wrapper::HandleType& handle)
Expand Down Expand Up @@ -207,7 +244,7 @@ void CreateWrappedDispatchHandle(typename ParentWrapper::HandleType parent,
typename Wrapper::HandleType* handle,
PFN_GetHandleId get_id)
{
const std::shared_lock<std::shared_mutex> lock(mutex_for_create_destroy_handle_);
ScopedDestroyLock shared_scoped_lock(true);
assert(handle != nullptr);
if ((*handle) != VK_NULL_HANDLE)
{
Expand Down Expand Up @@ -237,7 +274,7 @@ void CreateWrappedDispatchHandle(typename ParentWrapper::HandleType parent,
template <typename Wrapper>
void CreateWrappedNonDispatchHandle(typename Wrapper::HandleType* handle, PFN_GetHandleId get_id)
{
const std::shared_lock<std::shared_mutex> lock(mutex_for_create_destroy_handle_);
ScopedDestroyLock shared_scoped_lock(false);
assert(handle != nullptr);
if ((*handle) != VK_NULL_HANDLE)
{
Expand Down
Loading

0 comments on commit 1353691

Please sign in to comment.