Cleanup BufferView/TextureView Locking Code

Renames the variable to be neater and less confusing alongside adding comments for `try_lock()` to make the goal of the function more apparent.
This commit is contained in:
PixelyIon 2021-12-08 02:42:20 +05:30
parent 7776ef2cd0
commit 45c7a89fc3
2 changed files with 28 additions and 24 deletions

View File

@ -104,16 +104,16 @@ namespace skyline::gpu {
BufferView::BufferView(std::shared_ptr<Buffer> backing, vk::DeviceSize offset, vk::DeviceSize range, vk::Format format) : buffer(std::move(backing)), offset(offset), range(range), format(format) {} BufferView::BufferView(std::shared_ptr<Buffer> backing, vk::DeviceSize offset, vk::DeviceSize range, vk::Format format) : buffer(std::move(backing)), offset(offset), range(range), format(format) {}
void BufferView::lock() { void BufferView::lock() {
auto currentBacking{std::atomic_load(&buffer)}; auto backing{std::atomic_load(&buffer)};
while (true) { while (true) {
currentBacking->lock(); backing->lock();
auto newBacking{std::atomic_load(&buffer)}; auto latestBacking{std::atomic_load(&buffer)};
if (currentBacking == newBacking) if (backing == latestBacking)
return; return;
currentBacking->unlock(); backing->unlock();
currentBacking = newBacking; backing = latestBacking;
} }
} }
@ -122,17 +122,19 @@ namespace skyline::gpu {
} }
bool BufferView::try_lock() { bool BufferView::try_lock() {
auto currentBacking{std::atomic_load(&buffer)}; auto backing{std::atomic_load(&buffer)};
while (true) { while (true) {
bool success{currentBacking->try_lock()}; bool success{backing->try_lock()};
auto newBacking{std::atomic_load(&buffer)}; auto latestBacking{std::atomic_load(&buffer)};
if (currentBacking == newBacking) if (backing == latestBacking)
// We want to ensure that the try_lock() was on the latest backing and not on an outdated one
return success; return success;
if (success) if (success)
currentBacking->unlock(); // We only unlock() if the try_lock() was successful and we acquired the mutex
currentBacking = newBacking; backing->unlock();
backing = latestBacking;
} }
} }
} }

View File

@ -9,16 +9,16 @@
namespace skyline::gpu { namespace skyline::gpu {
void TextureView::lock() { void TextureView::lock() {
auto currentBacking{std::atomic_load(&texture)}; auto backing{std::atomic_load(&texture)};
while (true) { while (true) {
currentBacking->lock(); backing->lock();
auto newBacking{std::atomic_load(&texture)}; auto latestBacking{std::atomic_load(&texture)};
if (currentBacking == newBacking) if (backing == latestBacking)
return; return;
currentBacking->unlock(); backing->unlock();
currentBacking = newBacking; backing = latestBacking;
} }
} }
@ -27,17 +27,19 @@ namespace skyline::gpu {
} }
bool TextureView::try_lock() { bool TextureView::try_lock() {
auto currentBacking{std::atomic_load(&texture)}; auto backing{std::atomic_load(&texture)};
while (true) { while (true) {
bool success{currentBacking->try_lock()}; bool success{backing->try_lock()};
auto newBacking{std::atomic_load(&texture)}; auto latestBacking{std::atomic_load(&texture)};
if (currentBacking == newBacking) if (backing == latestBacking)
// We want to ensure that the try_lock() was on the latest backing and not on an outdated one
return success; return success;
if (success) if (success)
currentBacking->unlock(); // We only unlock() if the try_lock() was successful and we acquired the mutex
currentBacking = newBacking; backing->unlock();
backing = latestBacking;
} }
} }