diff options
Diffstat (limited to 'src/common/range_mutex.h')
| -rw-r--r-- | src/common/range_mutex.h | 93 |
1 files changed, 93 insertions, 0 deletions
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 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2024 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <condition_variable> | ||
| 7 | #include <mutex> | ||
| 8 | |||
| 9 | #include "common/intrusive_list.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | class ScopedRangeLock; | ||
| 14 | |||
| 15 | class RangeMutex { | ||
| 16 | public: | ||
| 17 | explicit RangeMutex() = default; | ||
| 18 | ~RangeMutex() = default; | ||
| 19 | |||
| 20 | private: | ||
| 21 | friend class ScopedRangeLock; | ||
| 22 | |||
| 23 | void Lock(ScopedRangeLock& l); | ||
| 24 | void Unlock(ScopedRangeLock& l); | ||
| 25 | bool HasIntersectionLocked(ScopedRangeLock& l); | ||
| 26 | |||
| 27 | private: | ||
| 28 | std::mutex m_mutex; | ||
| 29 | std::condition_variable m_cv; | ||
| 30 | |||
| 31 | using LockList = Common::IntrusiveListBaseTraits<ScopedRangeLock>::ListType; | ||
| 32 | LockList m_list; | ||
| 33 | }; | ||
| 34 | |||
| 35 | class ScopedRangeLock : public Common::IntrusiveListBaseNode<ScopedRangeLock> { | ||
| 36 | public: | ||
| 37 | explicit ScopedRangeLock(RangeMutex& mutex, u64 address, u64 size) | ||
| 38 | : m_mutex(mutex), m_address(address), m_size(size) { | ||
| 39 | if (m_size > 0) { | ||
| 40 | m_mutex.Lock(*this); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | ~ScopedRangeLock() { | ||
| 44 | if (m_size > 0) { | ||
| 45 | m_mutex.Unlock(*this); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | u64 GetAddress() const { | ||
| 50 | return m_address; | ||
| 51 | } | ||
| 52 | |||
| 53 | u64 GetSize() const { | ||
| 54 | return m_size; | ||
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | RangeMutex& m_mutex; | ||
| 59 | const u64 m_address{}; | ||
| 60 | const u64 m_size{}; | ||
| 61 | }; | ||
| 62 | |||
| 63 | inline void RangeMutex::Lock(ScopedRangeLock& l) { | ||
| 64 | std::unique_lock lk{m_mutex}; | ||
| 65 | m_cv.wait(lk, [&] { return !HasIntersectionLocked(l); }); | ||
| 66 | m_list.push_back(l); | ||
| 67 | } | ||
| 68 | |||
| 69 | inline void RangeMutex::Unlock(ScopedRangeLock& l) { | ||
| 70 | { | ||
| 71 | std::scoped_lock lk{m_mutex}; | ||
| 72 | m_list.erase(m_list.iterator_to(l)); | ||
| 73 | } | ||
| 74 | m_cv.notify_all(); | ||
| 75 | } | ||
| 76 | |||
| 77 | inline bool RangeMutex::HasIntersectionLocked(ScopedRangeLock& l) { | ||
| 78 | const auto cur_begin = l.GetAddress(); | ||
| 79 | const auto cur_last = l.GetAddress() + l.GetSize() - 1; | ||
| 80 | |||
| 81 | for (const auto& other : m_list) { | ||
| 82 | const auto other_begin = other.GetAddress(); | ||
| 83 | const auto other_last = other.GetAddress() + other.GetSize() - 1; | ||
| 84 | |||
| 85 | if (cur_begin <= other_last && other_begin <= cur_last) { | ||
| 86 | return true; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return false; | ||
| 91 | } | ||
| 92 | |||
| 93 | } // namespace Common | ||