summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorLiam <byteslice@airmail.cc>2024-01-17 05:35:48 +0100
committerFernando Sahmkow <fsahmkow27@gmail.com>2024-01-31 16:38:51 +0100
commit8f848f43e979ac4049237d3b6a161696dd85372b (patch)
treee218fcb939560362b4293ab035669d776d6e0cb0 /src/common
parentTexture Cache: make sparse texture table per channel (diff)
downloadyuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar.gz
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar.bz2
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar.lz
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar.xz
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.tar.zst
yuzu-8f848f43e979ac4049237d3b6a161696dd85372b.zip
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/range_mutex.h93
2 files changed, 94 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index e30fea268..85926fc8f 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -106,6 +106,7 @@ add_library(common STATIC
precompiled_headers.h
quaternion.h
range_map.h
+ range_mutex.h
reader_writer_queue.h
ring_buffer.h
${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp
diff --git a/src/common/range_mutex.h b/src/common/range_mutex.h
new file mode 100644
index 000000000..d6c949811
--- /dev/null
+++ b/src/common/range_mutex.h
@@ -0,0 +1,93 @@
+// SPDX-FileCopyrightText: 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <condition_variable>
+#include <mutex>
+
+#include "common/intrusive_list.h"
+
+namespace Common {
+
+class ScopedRangeLock;
+
+class RangeMutex {
+public:
+ explicit RangeMutex() = default;
+ ~RangeMutex() = default;
+
+private:
+ friend class ScopedRangeLock;
+
+ void Lock(ScopedRangeLock& l);
+ void Unlock(ScopedRangeLock& l);
+ bool HasIntersectionLocked(ScopedRangeLock& l);
+
+private:
+ std::mutex m_mutex;
+ std::condition_variable m_cv;
+
+ using LockList = Common::IntrusiveListBaseTraits<ScopedRangeLock>::ListType;
+ LockList m_list;
+};
+
+class ScopedRangeLock : public Common::IntrusiveListBaseNode<ScopedRangeLock> {
+public:
+ explicit ScopedRangeLock(RangeMutex& mutex, u64 address, u64 size)
+ : m_mutex(mutex), m_address(address), m_size(size) {
+ if (m_size > 0) {
+ m_mutex.Lock(*this);
+ }
+ }
+ ~ScopedRangeLock() {
+ if (m_size > 0) {
+ m_mutex.Unlock(*this);
+ }
+ }
+
+ u64 GetAddress() const {
+ return m_address;
+ }
+
+ u64 GetSize() const {
+ return m_size;
+ }
+
+private:
+ RangeMutex& m_mutex;
+ const u64 m_address{};
+ const u64 m_size{};
+};
+
+inline void RangeMutex::Lock(ScopedRangeLock& l) {
+ std::unique_lock lk{m_mutex};
+ m_cv.wait(lk, [&] { return !HasIntersectionLocked(l); });
+ m_list.push_back(l);
+}
+
+inline void RangeMutex::Unlock(ScopedRangeLock& l) {
+ {
+ std::scoped_lock lk{m_mutex};
+ m_list.erase(m_list.iterator_to(l));
+ }
+ m_cv.notify_all();
+}
+
+inline bool RangeMutex::HasIntersectionLocked(ScopedRangeLock& l) {
+ const auto cur_begin = l.GetAddress();
+ const auto cur_last = l.GetAddress() + l.GetSize() - 1;
+
+ for (const auto& other : m_list) {
+ const auto other_begin = other.GetAddress();
+ const auto other_last = other.GetAddress() + other.GetSize() - 1;
+
+ if (cur_begin <= other_last && other_begin <= cur_last) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+} // namespace Common