diff options
| author | 2018-04-20 14:42:29 -0500 | |
|---|---|---|
| committer | 2018-04-20 21:04:32 -0500 | |
| commit | 5fdfbfe25adafd2734a19fe94cccc58993cb12e7 (patch) | |
| tree | a4940a8231d08c5b96ce2d629acc04948ab1af3a /src | |
| parent | Kernel: Properly implemented svcWaitProcessWideKey and svcSignalProcessWideKey (diff) | |
| download | yuzu-5fdfbfe25adafd2734a19fe94cccc58993cb12e7.tar.gz yuzu-5fdfbfe25adafd2734a19fe94cccc58993cb12e7.tar.xz yuzu-5fdfbfe25adafd2734a19fe94cccc58993cb12e7.zip | |
Kernel: Remove old and unused Mutex code.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/kernel/mutex.cpp | 120 | ||||
| -rw-r--r-- | src/core/hle/kernel/mutex.h | 82 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.cpp | 3 | ||||
| -rw-r--r-- | src/core/hle/kernel/thread.h | 7 |
4 files changed, 3 insertions, 209 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index 50a9a0805..5cc0bd266 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp | |||
| @@ -40,126 +40,6 @@ static std::pair<SharedPtr<Thread>, u32> GetHighestPriorityMutexWaitingThread(VA | |||
| 40 | return {highest_priority_thread, num_waiters}; | 40 | return {highest_priority_thread, num_waiters}; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void ReleaseThreadMutexes(Thread* thread) { | ||
| 44 | for (auto& mtx : thread->held_mutexes) { | ||
| 45 | mtx->SetHasWaiters(false); | ||
| 46 | mtx->SetHoldingThread(nullptr); | ||
| 47 | mtx->WakeupAllWaitingThreads(); | ||
| 48 | } | ||
| 49 | thread->held_mutexes.clear(); | ||
| 50 | } | ||
| 51 | |||
| 52 | Mutex::Mutex() {} | ||
| 53 | Mutex::~Mutex() {} | ||
| 54 | |||
| 55 | SharedPtr<Mutex> Mutex::Create(SharedPtr<Kernel::Thread> holding_thread, VAddr guest_addr, | ||
| 56 | std::string name) { | ||
| 57 | SharedPtr<Mutex> mutex(new Mutex); | ||
| 58 | |||
| 59 | mutex->guest_addr = guest_addr; | ||
| 60 | mutex->name = std::move(name); | ||
| 61 | |||
| 62 | // If mutex was initialized with a holding thread, acquire it by the holding thread | ||
| 63 | if (holding_thread) { | ||
| 64 | mutex->Acquire(holding_thread.get()); | ||
| 65 | } | ||
| 66 | |||
| 67 | // Mutexes are referenced by guest address, so track this in the kernel | ||
| 68 | g_object_address_table.Insert(guest_addr, mutex); | ||
| 69 | |||
| 70 | return mutex; | ||
| 71 | } | ||
| 72 | |||
| 73 | bool Mutex::ShouldWait(Thread* thread) const { | ||
| 74 | auto holding_thread = GetHoldingThread(); | ||
| 75 | return holding_thread != nullptr && thread != holding_thread; | ||
| 76 | } | ||
| 77 | |||
| 78 | void Mutex::Acquire(Thread* thread) { | ||
| 79 | ASSERT_MSG(!ShouldWait(thread), "object unavailable!"); | ||
| 80 | |||
| 81 | priority = thread->current_priority; | ||
| 82 | thread->held_mutexes.insert(this); | ||
| 83 | SetHoldingThread(thread); | ||
| 84 | thread->UpdatePriority(); | ||
| 85 | Core::System::GetInstance().PrepareReschedule(); | ||
| 86 | } | ||
| 87 | |||
| 88 | ResultCode Mutex::Release(Thread* thread) { | ||
| 89 | auto holding_thread = GetHoldingThread(); | ||
| 90 | ASSERT(holding_thread); | ||
| 91 | |||
| 92 | // We can only release the mutex if it's held by the calling thread. | ||
| 93 | ASSERT(thread == holding_thread); | ||
| 94 | |||
| 95 | holding_thread->held_mutexes.erase(this); | ||
| 96 | holding_thread->UpdatePriority(); | ||
| 97 | SetHoldingThread(nullptr); | ||
| 98 | SetHasWaiters(!GetWaitingThreads().empty()); | ||
| 99 | WakeupAllWaitingThreads(); | ||
| 100 | Core::System::GetInstance().PrepareReschedule(); | ||
| 101 | |||
| 102 | return RESULT_SUCCESS; | ||
| 103 | } | ||
| 104 | |||
| 105 | void Mutex::AddWaitingThread(SharedPtr<Thread> thread) { | ||
| 106 | WaitObject::AddWaitingThread(thread); | ||
| 107 | thread->pending_mutexes.insert(this); | ||
| 108 | SetHasWaiters(true); | ||
| 109 | UpdatePriority(); | ||
| 110 | } | ||
| 111 | |||
| 112 | void Mutex::RemoveWaitingThread(Thread* thread) { | ||
| 113 | WaitObject::RemoveWaitingThread(thread); | ||
| 114 | thread->pending_mutexes.erase(this); | ||
| 115 | if (!GetHasWaiters()) | ||
| 116 | SetHasWaiters(!GetWaitingThreads().empty()); | ||
| 117 | UpdatePriority(); | ||
| 118 | } | ||
| 119 | |||
| 120 | void Mutex::UpdatePriority() { | ||
| 121 | if (!GetHoldingThread()) | ||
| 122 | return; | ||
| 123 | |||
| 124 | u32 best_priority = THREADPRIO_LOWEST; | ||
| 125 | for (auto& waiter : GetWaitingThreads()) { | ||
| 126 | if (waiter->current_priority < best_priority) | ||
| 127 | best_priority = waiter->current_priority; | ||
| 128 | } | ||
| 129 | |||
| 130 | if (best_priority != priority) { | ||
| 131 | priority = best_priority; | ||
| 132 | GetHoldingThread()->UpdatePriority(); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | Handle Mutex::GetOwnerHandle() const { | ||
| 137 | GuestState guest_state{Memory::Read32(guest_addr)}; | ||
| 138 | return guest_state.holding_thread_handle; | ||
| 139 | } | ||
| 140 | |||
| 141 | SharedPtr<Thread> Mutex::GetHoldingThread() const { | ||
| 142 | GuestState guest_state{Memory::Read32(guest_addr)}; | ||
| 143 | return g_handle_table.Get<Thread>(guest_state.holding_thread_handle); | ||
| 144 | } | ||
| 145 | |||
| 146 | void Mutex::SetHoldingThread(SharedPtr<Thread> thread) { | ||
| 147 | GuestState guest_state{Memory::Read32(guest_addr)}; | ||
| 148 | guest_state.holding_thread_handle.Assign(thread ? thread->guest_handle : 0); | ||
| 149 | Memory::Write32(guest_addr, guest_state.raw); | ||
| 150 | } | ||
| 151 | |||
| 152 | bool Mutex::GetHasWaiters() const { | ||
| 153 | GuestState guest_state{Memory::Read32(guest_addr)}; | ||
| 154 | return guest_state.has_waiters != 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | void Mutex::SetHasWaiters(bool has_waiters) { | ||
| 158 | GuestState guest_state{Memory::Read32(guest_addr)}; | ||
| 159 | guest_state.has_waiters.Assign(has_waiters ? 1 : 0); | ||
| 160 | Memory::Write32(guest_addr, guest_state.raw); | ||
| 161 | } | ||
| 162 | |||
| 163 | ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, | 43 | ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle, |
| 164 | Handle requesting_thread_handle) { | 44 | Handle requesting_thread_handle) { |
| 165 | // The mutex address must be 4-byte aligned | 45 | // The mutex address must be 4-byte aligned |
diff --git a/src/core/hle/kernel/mutex.h b/src/core/hle/kernel/mutex.h index 310923087..3117e7c70 100644 --- a/src/core/hle/kernel/mutex.h +++ b/src/core/hle/kernel/mutex.h | |||
| @@ -15,68 +15,8 @@ namespace Kernel { | |||
| 15 | 15 | ||
| 16 | class Thread; | 16 | class Thread; |
| 17 | 17 | ||
| 18 | class Mutex final : public WaitObject { | 18 | class Mutex final { |
| 19 | public: | 19 | public: |
| 20 | /** | ||
| 21 | * Creates a mutex. | ||
| 22 | * @param holding_thread Specifies a thread already holding the mutex. If not nullptr, this | ||
| 23 | * thread will acquire the mutex. | ||
| 24 | * @param guest_addr Address of the object tracking the mutex in guest memory. If specified, | ||
| 25 | * this mutex will update the guest object when its state changes. | ||
| 26 | * @param name Optional name of mutex | ||
| 27 | * @return Pointer to new Mutex object | ||
| 28 | */ | ||
| 29 | static SharedPtr<Mutex> Create(SharedPtr<Kernel::Thread> holding_thread, VAddr guest_addr = 0, | ||
| 30 | std::string name = "Unknown"); | ||
| 31 | |||
| 32 | std::string GetTypeName() const override { | ||
| 33 | return "Mutex"; | ||
| 34 | } | ||
| 35 | std::string GetName() const override { | ||
| 36 | return name; | ||
| 37 | } | ||
| 38 | |||
| 39 | static const HandleType HANDLE_TYPE = HandleType::Mutex; | ||
| 40 | HandleType GetHandleType() const override { | ||
| 41 | return HANDLE_TYPE; | ||
| 42 | } | ||
| 43 | |||
| 44 | u32 priority; ///< The priority of the mutex, used for priority inheritance. | ||
| 45 | std::string name; ///< Name of mutex (optional) | ||
| 46 | VAddr guest_addr; ///< Address of the guest mutex value | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Elevate the mutex priority to the best priority | ||
| 50 | * among the priorities of all its waiting threads. | ||
| 51 | */ | ||
| 52 | void UpdatePriority(); | ||
| 53 | |||
| 54 | bool ShouldWait(Thread* thread) const override; | ||
| 55 | void Acquire(Thread* thread) override; | ||
| 56 | |||
| 57 | void AddWaitingThread(SharedPtr<Thread> thread) override; | ||
| 58 | void RemoveWaitingThread(Thread* thread) override; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Attempts to release the mutex from the specified thread. | ||
| 62 | * @param thread Thread that wants to release the mutex. | ||
| 63 | * @returns The result code of the operation. | ||
| 64 | */ | ||
| 65 | ResultCode Release(Thread* thread); | ||
| 66 | |||
| 67 | /// Gets the handle to the holding process stored in the guest state. | ||
| 68 | Handle GetOwnerHandle() const; | ||
| 69 | |||
| 70 | /// Gets the Thread pointed to by the owner handle | ||
| 71 | SharedPtr<Thread> GetHoldingThread() const; | ||
| 72 | /// Sets the holding process handle in the guest state. | ||
| 73 | void SetHoldingThread(SharedPtr<Thread> thread); | ||
| 74 | |||
| 75 | /// Returns the has_waiters bit in the guest state. | ||
| 76 | bool GetHasWaiters() const; | ||
| 77 | /// Sets the has_waiters bit in the guest state. | ||
| 78 | void SetHasWaiters(bool has_waiters); | ||
| 79 | |||
| 80 | /// Flag that indicates that a mutex still has threads waiting for it. | 20 | /// Flag that indicates that a mutex still has threads waiting for it. |
| 81 | static constexpr u32 MutexHasWaitersFlag = 0x40000000; | 21 | static constexpr u32 MutexHasWaitersFlag = 0x40000000; |
| 82 | /// Mask of the bits in a mutex address value that contain the mutex owner. | 22 | /// Mask of the bits in a mutex address value that contain the mutex owner. |
| @@ -90,24 +30,8 @@ public: | |||
| 90 | static ResultCode Release(VAddr address); | 30 | static ResultCode Release(VAddr address); |
| 91 | 31 | ||
| 92 | private: | 32 | private: |
| 93 | Mutex(); | 33 | Mutex() = default; |
| 94 | ~Mutex() override; | 34 | ~Mutex() = default; |
| 95 | |||
| 96 | /// Object in guest memory used to track the mutex state | ||
| 97 | union GuestState { | ||
| 98 | u32_le raw; | ||
| 99 | /// Handle of the thread that currently holds the mutex, 0 if available | ||
| 100 | BitField<0, 30, u32_le> holding_thread_handle; | ||
| 101 | /// 1 when there are threads waiting for this mutex, otherwise 0 | ||
| 102 | BitField<30, 1, u32_le> has_waiters; | ||
| 103 | }; | ||
| 104 | static_assert(sizeof(GuestState) == 4, "GuestState size is incorrect"); | ||
| 105 | }; | 35 | }; |
| 106 | 36 | ||
| 107 | /** | ||
| 108 | * Releases all the mutexes held by the specified thread | ||
| 109 | * @param thread Thread that is holding the mutexes | ||
| 110 | */ | ||
| 111 | void ReleaseThreadMutexes(Thread* thread); | ||
| 112 | |||
| 113 | } // namespace Kernel | 37 | } // namespace Kernel |
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 0a0ad7cfb..8093c4496 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp | |||
| @@ -77,9 +77,6 @@ void Thread::Stop() { | |||
| 77 | } | 77 | } |
| 78 | wait_objects.clear(); | 78 | wait_objects.clear(); |
| 79 | 79 | ||
| 80 | // Release all the mutexes that this thread holds | ||
| 81 | ReleaseThreadMutexes(this); | ||
| 82 | |||
| 83 | // Mark the TLS slot in the thread's page as free. | 80 | // Mark the TLS slot in the thread's page as free. |
| 84 | u64 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE; | 81 | u64 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE; |
| 85 | u64 tls_slot = | 82 | u64 tls_slot = |
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index a3a6e6a64..74d5904b8 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h | |||
| @@ -55,7 +55,6 @@ enum class ThreadWakeupReason { | |||
| 55 | 55 | ||
| 56 | namespace Kernel { | 56 | namespace Kernel { |
| 57 | 57 | ||
| 58 | class Mutex; | ||
| 59 | class Process; | 58 | class Process; |
| 60 | 59 | ||
| 61 | class Thread final : public WaitObject { | 60 | class Thread final : public WaitObject { |
| @@ -206,12 +205,6 @@ public: | |||
| 206 | 205 | ||
| 207 | VAddr tls_address; ///< Virtual address of the Thread Local Storage of the thread | 206 | VAddr tls_address; ///< Virtual address of the Thread Local Storage of the thread |
| 208 | 207 | ||
| 209 | /// Mutexes currently held by this thread, which will be released when it exits. | ||
| 210 | boost::container::flat_set<SharedPtr<Mutex>> held_mutexes; | ||
| 211 | |||
| 212 | /// Mutexes that this thread is currently waiting for. | ||
| 213 | boost::container::flat_set<SharedPtr<Mutex>> pending_mutexes; | ||
| 214 | |||
| 215 | SharedPtr<Process> owner_process; ///< Process that owns this thread | 208 | SharedPtr<Process> owner_process; ///< Process that owns this thread |
| 216 | 209 | ||
| 217 | /// Objects that the thread is waiting on, in the same order as they were | 210 | /// Objects that the thread is waiting on, in the same order as they were |