diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/range_mutex.h | 93 | ||||
| -rw-r--r-- | src/common/settings.h | 12 |
3 files changed, 100 insertions, 6 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 | |||
| 106 | precompiled_headers.h | 106 | precompiled_headers.h |
| 107 | quaternion.h | 107 | quaternion.h |
| 108 | range_map.h | 108 | range_map.h |
| 109 | range_mutex.h | ||
| 109 | reader_writer_queue.h | 110 | reader_writer_queue.h |
| 110 | ring_buffer.h | 111 | ring_buffer.h |
| 111 | ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp | 112 | ${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 @@ | |||
| 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 | ||
diff --git a/src/common/settings.h b/src/common/settings.h index 16749ab68..f1b1add56 100644 --- a/src/common/settings.h +++ b/src/common/settings.h | |||
| @@ -134,12 +134,12 @@ struct Values { | |||
| 134 | Linkage linkage{}; | 134 | Linkage linkage{}; |
| 135 | 135 | ||
| 136 | // Audio | 136 | // Audio |
| 137 | Setting<AudioEngine> sink_id{linkage, AudioEngine::Auto, "output_engine", Category::Audio, | 137 | SwitchableSetting<AudioEngine> sink_id{linkage, AudioEngine::Auto, "output_engine", |
| 138 | Specialization::RuntimeList}; | 138 | Category::Audio, Specialization::RuntimeList}; |
| 139 | Setting<std::string> audio_output_device_id{linkage, "auto", "output_device", Category::Audio, | 139 | SwitchableSetting<std::string> audio_output_device_id{ |
| 140 | Specialization::RuntimeList}; | 140 | linkage, "auto", "output_device", Category::Audio, Specialization::RuntimeList}; |
| 141 | Setting<std::string> audio_input_device_id{linkage, "auto", "input_device", Category::Audio, | 141 | SwitchableSetting<std::string> audio_input_device_id{ |
| 142 | Specialization::RuntimeList}; | 142 | linkage, "auto", "input_device", Category::Audio, Specialization::RuntimeList}; |
| 143 | SwitchableSetting<AudioMode, true> sound_index{ | 143 | SwitchableSetting<AudioMode, true> sound_index{ |
| 144 | linkage, AudioMode::Stereo, AudioMode::Mono, AudioMode::Surround, | 144 | linkage, AudioMode::Stereo, AudioMode::Mono, AudioMode::Surround, |
| 145 | "sound_index", Category::SystemAudio, Specialization::Default, true, | 145 | "sound_index", Category::SystemAudio, Specialization::Default, true, |