diff options
Diffstat (limited to 'src/core/hle/kernel/mutex.cpp')
| -rw-r--r-- | src/core/hle/kernel/mutex.cpp | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/src/core/hle/kernel/mutex.cpp b/src/core/hle/kernel/mutex.cpp index d07e9761b..5a173e129 100644 --- a/src/core/hle/kernel/mutex.cpp +++ b/src/core/hle/kernel/mutex.cpp | |||
| @@ -27,21 +27,7 @@ public: | |||
| 27 | std::vector<Handle> waiting_threads; ///< Threads that are waiting for the mutex | 27 | std::vector<Handle> waiting_threads; ///< Threads that are waiting for the mutex |
| 28 | std::string name; ///< Name of mutex (optional) | 28 | std::string name; ///< Name of mutex (optional) |
| 29 | 29 | ||
| 30 | ResultVal<bool> SyncRequest() override { | 30 | ResultVal<bool> WaitSynchronization() override; |
| 31 | // TODO(bunnei): ImplementMe | ||
| 32 | locked = true; | ||
| 33 | return MakeResult<bool>(false); | ||
| 34 | } | ||
| 35 | |||
| 36 | ResultVal<bool> WaitSynchronization() override { | ||
| 37 | // TODO(bunnei): ImplementMe | ||
| 38 | bool wait = locked; | ||
| 39 | if (locked) { | ||
| 40 | Kernel::WaitCurrentThread(WAITTYPE_MUTEX, GetHandle()); | ||
| 41 | } | ||
| 42 | |||
| 43 | return MakeResult<bool>(wait); | ||
| 44 | } | ||
| 45 | }; | 31 | }; |
| 46 | 32 | ||
| 47 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 33 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| @@ -49,21 +35,46 @@ public: | |||
| 49 | typedef std::multimap<Handle, Handle> MutexMap; | 35 | typedef std::multimap<Handle, Handle> MutexMap; |
| 50 | static MutexMap g_mutex_held_locks; | 36 | static MutexMap g_mutex_held_locks; |
| 51 | 37 | ||
| 52 | void MutexAcquireLock(Mutex* mutex, Handle thread) { | 38 | /** |
| 39 | * Acquires the specified mutex for the specified thread | ||
| 40 | * @param mutex Mutex that is to be acquired | ||
| 41 | * @param thread Thread that will acquired | ||
| 42 | */ | ||
| 43 | void MutexAcquireLock(Mutex* mutex, Handle thread = GetCurrentThreadHandle()) { | ||
| 53 | g_mutex_held_locks.insert(std::make_pair(thread, mutex->GetHandle())); | 44 | g_mutex_held_locks.insert(std::make_pair(thread, mutex->GetHandle())); |
| 54 | mutex->lock_thread = thread; | 45 | mutex->lock_thread = thread; |
| 55 | } | 46 | } |
| 56 | 47 | ||
| 57 | void MutexAcquireLock(Mutex* mutex) { | 48 | bool ReleaseMutexForThread(Mutex* mutex, Handle thread) { |
| 58 | Handle thread = GetCurrentThreadHandle(); | ||
| 59 | MutexAcquireLock(mutex, thread); | 49 | MutexAcquireLock(mutex, thread); |
| 50 | Kernel::ResumeThreadFromWait(thread); | ||
| 51 | return true; | ||
| 52 | } | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Resumes a thread waiting for the specified mutex | ||
| 56 | * @param mutex The mutex that some thread is waiting on | ||
| 57 | */ | ||
| 58 | void ResumeWaitingThread(Mutex* mutex) { | ||
| 59 | // Find the next waiting thread for the mutex... | ||
| 60 | if (mutex->waiting_threads.empty()) { | ||
| 61 | // Reset mutex lock thread handle, nothing is waiting | ||
| 62 | mutex->locked = false; | ||
| 63 | mutex->lock_thread = -1; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | // Resume the next waiting thread and re-lock the mutex | ||
| 67 | std::vector<Handle>::iterator iter = mutex->waiting_threads.begin(); | ||
| 68 | ReleaseMutexForThread(mutex, *iter); | ||
| 69 | mutex->waiting_threads.erase(iter); | ||
| 70 | } | ||
| 60 | } | 71 | } |
| 61 | 72 | ||
| 62 | void MutexEraseLock(Mutex* mutex) { | 73 | void MutexEraseLock(Mutex* mutex) { |
| 63 | Handle handle = mutex->GetHandle(); | 74 | Handle handle = mutex->GetHandle(); |
| 64 | auto locked = g_mutex_held_locks.equal_range(mutex->lock_thread); | 75 | auto locked = g_mutex_held_locks.equal_range(mutex->lock_thread); |
| 65 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { | 76 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { |
| 66 | if ((*iter).second == handle) { | 77 | if (iter->second == handle) { |
| 67 | g_mutex_held_locks.erase(iter); | 78 | g_mutex_held_locks.erase(iter); |
| 68 | break; | 79 | break; |
| 69 | } | 80 | } |
| @@ -71,6 +82,19 @@ void MutexEraseLock(Mutex* mutex) { | |||
| 71 | mutex->lock_thread = -1; | 82 | mutex->lock_thread = -1; |
| 72 | } | 83 | } |
| 73 | 84 | ||
| 85 | void ReleaseThreadMutexes(Handle thread) { | ||
| 86 | auto locked = g_mutex_held_locks.equal_range(thread); | ||
| 87 | |||
| 88 | // Release every mutex that the thread holds, and resume execution on the waiting threads | ||
| 89 | for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) { | ||
| 90 | Mutex* mutex = g_object_pool.GetFast<Mutex>(iter->second); | ||
| 91 | ResumeWaitingThread(mutex); | ||
| 92 | } | ||
| 93 | |||
| 94 | // Erase all the locks that this thread holds | ||
| 95 | g_mutex_held_locks.erase(thread); | ||
| 96 | } | ||
| 97 | |||
| 74 | bool LockMutex(Mutex* mutex) { | 98 | bool LockMutex(Mutex* mutex) { |
| 75 | // Mutex alread locked? | 99 | // Mutex alread locked? |
| 76 | if (mutex->locked) { | 100 | if (mutex->locked) { |
| @@ -80,26 +104,9 @@ bool LockMutex(Mutex* mutex) { | |||
| 80 | return true; | 104 | return true; |
| 81 | } | 105 | } |
| 82 | 106 | ||
| 83 | bool ReleaseMutexForThread(Mutex* mutex, Handle thread) { | ||
| 84 | MutexAcquireLock(mutex, thread); | ||
| 85 | Kernel::ResumeThreadFromWait(thread); | ||
| 86 | return true; | ||
| 87 | } | ||
| 88 | |||
| 89 | bool ReleaseMutex(Mutex* mutex) { | 107 | bool ReleaseMutex(Mutex* mutex) { |
| 90 | MutexEraseLock(mutex); | 108 | MutexEraseLock(mutex); |
| 91 | 109 | ResumeWaitingThread(mutex); | |
| 92 | // Find the next waiting thread for the mutex... | ||
| 93 | while (!mutex->waiting_threads.empty()) { | ||
| 94 | std::vector<Handle>::iterator iter = mutex->waiting_threads.begin(); | ||
| 95 | ReleaseMutexForThread(mutex, *iter); | ||
| 96 | mutex->waiting_threads.erase(iter); | ||
| 97 | } | ||
| 98 | |||
| 99 | // Reset mutex lock thread handle, nothing is waiting | ||
| 100 | mutex->locked = false; | ||
| 101 | mutex->lock_thread = -1; | ||
| 102 | |||
| 103 | return true; | 110 | return true; |
| 104 | } | 111 | } |
| 105 | 112 | ||
| @@ -157,4 +164,17 @@ Handle CreateMutex(bool initial_locked, const std::string& name) { | |||
| 157 | return handle; | 164 | return handle; |
| 158 | } | 165 | } |
| 159 | 166 | ||
| 167 | ResultVal<bool> Mutex::WaitSynchronization() { | ||
| 168 | bool wait = locked; | ||
| 169 | if (locked) { | ||
| 170 | Kernel::WaitCurrentThread(WAITTYPE_MUTEX, GetHandle()); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | // Lock the mutex when the first thread accesses it | ||
| 174 | locked = true; | ||
| 175 | MutexAcquireLock(this); | ||
| 176 | } | ||
| 177 | |||
| 178 | return MakeResult<bool>(wait); | ||
| 179 | } | ||
| 160 | } // namespace | 180 | } // namespace |