diff options
| author | 2015-01-20 18:33:23 -0500 | |
|---|---|---|
| committer | 2015-01-21 20:48:36 -0500 | |
| commit | 2f3020a10247a0cb47848a6f8c19fbde50a7e0a6 (patch) | |
| tree | eb664bee7c58bd5a2e93c686d6586ac2bf42cdd1 /src/core/hle/kernel/mutex.cpp | |
| parent | Kernel: Renamed some functions for clarity. (diff) | |
| download | yuzu-2f3020a10247a0cb47848a6f8c19fbde50a7e0a6.tar.gz yuzu-2f3020a10247a0cb47848a6f8c19fbde50a7e0a6.tar.xz yuzu-2f3020a10247a0cb47848a6f8c19fbde50a7e0a6.zip | |
Mutex: Cleanup and remove redundant code.
Diffstat (limited to 'src/core/hle/kernel/mutex.cpp')
| -rw-r--r-- | src/core/hle/kernel/mutex.cpp | 72 |
1 files changed, 27 insertions, 45 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index c170e55ff..cd05a1397 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp | |||
| @@ -23,9 +23,8 @@ public: | |||
| 23 | 23 | ||
| 24 | bool initial_locked; ///< Initial lock state when mutex was created | 24 | bool initial_locked; ///< Initial lock state when mutex was created |
| 25 | bool locked; ///< Current locked state | 25 | bool locked; ///< Current locked state |
| 26 | Handle lock_thread; ///< Handle to thread that currently has mutex | ||
| 27 | std::string name; ///< Name of mutex (optional) | 26 | std::string name; ///< Name of mutex (optional) |
| 28 | SharedPtr<Thread> current_thread; ///< Thread that has acquired the mutex | 27 | SharedPtr<Thread> holding_thread; ///< Thread that has acquired the mutex |
| 29 | 28 | ||
| 30 | bool ShouldWait() override; | 29 | bool ShouldWait() override; |
| 31 | void Acquire() override; | 30 | void Acquire() override; |
| @@ -33,18 +32,17 @@ public: | |||
| 33 | 32 | ||
| 34 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 33 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 35 | 34 | ||
| 36 | typedef std::multimap<Handle, Handle> MutexMap; | 35 | typedef std::multimap<SharedPtr<Thread>, SharedPtr<Mutex>> MutexMap; |
| 37 | static MutexMap g_mutex_held_locks; | 36 | static MutexMap g_mutex_held_locks; |
| 38 | 37 | ||
| 39 | /** | 38 | /** |
| 40 | * Acquires the specified mutex for the specified thread | 39 | * Acquires the specified mutex for the specified thread |
| 41 | * @param mutex Mutex that is to be acquired | 40 | * @param mutex Mutex that is to be acquired |
| 42 | * @param thread Thread that will acquired | 41 | * @param thread Thread that will acquire the mutex |
| 43 | */ | 42 | */ |
| 44 | void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThread()->GetHandle()) { | 43 | void MutexAcquireLock(Mutex* mutex, Thread* thread) { |
| 45 | g_mutex_held_locks.insert(std::make_pair(thread, mutex->GetHandle())); | 44 | g_mutex_held_locks.insert(std::make_pair(thread, mutex)); |
| 46 | mutex->lock_thread = thread; | 45 | mutex->holding_thread = thread; |
| 47 | mutex->current_thread = Kernel::g_handle_table.Get<Thread>(thread); | ||
| 48 | } | 46 | } |
| 49 | 47 | ||
| 50 | /** | 48 | /** |
| @@ -55,51 +53,39 @@ void ResumeWaitingThread(Mutex* mutex) { | |||
| 55 | // Find the next waiting thread for the mutex... | 53 | // Find the next waiting thread for the mutex... |
| 56 | auto next_thread = mutex->WakeupNextThread(); | 54 | auto next_thread = mutex->WakeupNextThread(); |
| 57 | if (next_thread != nullptr) { | 55 | if (next_thread != nullptr) { |
| 58 | MutexAcquireLock(mutex, next_thread->GetHandle()); | 56 | MutexAcquireLock(mutex, next_thread); |
| 59 | } else { | 57 | } else { |
| 60 | // Reset mutex lock thread handle, nothing is waiting | 58 | // Reset mutex lock thread handle, nothing is waiting |
| 61 | mutex->locked = false; | 59 | mutex->locked = false; |
| 62 | mutex->lock_thread = -1; | 60 | mutex->holding_thread = nullptr; |
| 63 | } | 61 | } |
| 64 | } | 62 | } |
| 65 | 63 | ||
| 66 | void MutexEraseLock(Mutex* mutex) { | 64 | void ReleaseThreadMutexes(Thread* thread) { |
| 67 | Handle handle = mutex->GetHandle(); | ||
| 68 | auto locked = g_mutex_held_locks.equal_range(mutex->lock_thread); | ||
| 69 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { | ||
| 70 | if (iter->second == handle) { | ||
| 71 | g_mutex_held_locks.erase(iter); | ||
| 72 | break; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | mutex->lock_thread = -1; | ||
| 76 | } | ||
| 77 | |||
| 78 | void ReleaseThreadMutexes(Handle thread) { | ||
| 79 | auto locked = g_mutex_held_locks.equal_range(thread); | 65 | auto locked = g_mutex_held_locks.equal_range(thread); |
| 80 | 66 | ||
| 81 | // Release every mutex that the thread holds, and resume execution on the waiting threads | 67 | // Release every mutex that the thread holds, and resume execution on the waiting threads |
| 82 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { | 68 | for (auto iter = locked.first; iter != locked.second; ++iter) { |
| 83 | Mutex* mutex = g_handle_table.Get<Mutex>(iter->second).get(); | 69 | ResumeWaitingThread(iter->second.get()); |
| 84 | ResumeWaitingThread(mutex); | ||
| 85 | } | 70 | } |
| 86 | 71 | ||
| 87 | // Erase all the locks that this thread holds | 72 | // Erase all the locks that this thread holds |
| 88 | g_mutex_held_locks.erase(thread); | 73 | g_mutex_held_locks.erase(thread); |
| 89 | } | 74 | } |
| 90 | 75 | ||
| 91 | bool LockMutex(Mutex* mutex) { | 76 | bool ReleaseMutex(Mutex* mutex) { |
| 92 | // Mutex alread locked? | ||
| 93 | if (mutex->locked) { | 77 | if (mutex->locked) { |
| 94 | return false; | 78 | auto locked = g_mutex_held_locks.equal_range(mutex->holding_thread); |
| 95 | } | ||
| 96 | MutexAcquireLock(mutex); | ||
| 97 | return true; | ||
| 98 | } | ||
| 99 | 79 | ||
| 100 | bool ReleaseMutex(Mutex* mutex) { | 80 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { |
| 101 | MutexEraseLock(mutex); | 81 | if (iter->second == mutex) { |
| 102 | ResumeWaitingThread(mutex); | 82 | g_mutex_held_locks.erase(iter); |
| 83 | break; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | ResumeWaitingThread(mutex); | ||
| 88 | } | ||
| 103 | return true; | 89 | return true; |
| 104 | } | 90 | } |
| 105 | 91 | ||
| @@ -134,16 +120,12 @@ Mutex* CreateMutex(Handle& handle, bool initial_locked, const std::string& name) | |||
| 134 | 120 | ||
| 135 | mutex->locked = mutex->initial_locked = initial_locked; | 121 | mutex->locked = mutex->initial_locked = initial_locked; |
| 136 | mutex->name = name; | 122 | mutex->name = name; |
| 137 | mutex->current_thread = nullptr; | 123 | mutex->holding_thread = nullptr; |
| 138 | 124 | ||
| 139 | // Acquire mutex with current thread if initialized as locked... | 125 | // Acquire mutex with current thread if initialized as locked... |
| 140 | if (mutex->locked) { | 126 | if (mutex->locked) |
| 141 | MutexAcquireLock(mutex); | 127 | MutexAcquireLock(mutex, GetCurrentThread()); |
| 142 | 128 | ||
| 143 | // Otherwise, reset lock thread handle | ||
| 144 | } else { | ||
| 145 | mutex->lock_thread = -1; | ||
| 146 | } | ||
| 147 | return mutex; | 129 | return mutex; |
| 148 | } | 130 | } |
| 149 | 131 | ||
| @@ -160,13 +142,13 @@ Handle CreateMutex(bool initial_locked, const std::string& name) { | |||
| 160 | } | 142 | } |
| 161 | 143 | ||
| 162 | bool Mutex::ShouldWait() { | 144 | bool Mutex::ShouldWait() { |
| 163 | return locked && current_thread != GetCurrentThread(); | 145 | return locked && holding_thread != GetCurrentThread(); |
| 164 | } | 146 | } |
| 165 | 147 | ||
| 166 | void Mutex::Acquire() { | 148 | void Mutex::Acquire() { |
| 167 | _assert_msg_(Kernel, !ShouldWait(), "object unavailable!"); | 149 | _assert_msg_(Kernel, !ShouldWait(), "object unavailable!"); |
| 168 | locked = true; | 150 | locked = true; |
| 169 | MutexAcquireLock(this); | 151 | MutexAcquireLock(this, GetCurrentThread()); |
| 170 | } | 152 | } |
| 171 | 153 | ||
| 172 | } // namespace | 154 | } // namespace |