diff --git a/Utilities/SMutex.cpp b/Utilities/SMutex.cpp index e253a52346..807a55c268 100644 --- a/Utilities/SMutex.cpp +++ b/Utilities/SMutex.cpp @@ -8,7 +8,14 @@ __forceinline void SM_Sleep() { - Sleep(1); + if (NamedThreadBase* t = GetCurrentNamedThread()) + { + t->WaitForAnySignal(); + } + else + { + Sleep(1); + } } thread_local size_t g_this_thread_id = 0; diff --git a/Utilities/SMutex.h b/Utilities/SMutex.h index 4023641c73..1e129809fc 100644 --- a/Utilities/SMutex.h +++ b/Utilities/SMutex.h @@ -123,7 +123,7 @@ public: default: return res; } - wait(); + if (wait) wait(); if (timeout && counter++ > timeout) { @@ -133,14 +133,14 @@ public: } }; -template +template class SMutexLockerBase { - SMutexBase& sm; + T& sm; public: - const T tid; + const Tid tid; - __forceinline SMutexLockerBase(SMutexBase& _sm) + __forceinline SMutexLockerBase(T& _sm) : sm(_sm) , tid(get_tid()) { @@ -169,9 +169,9 @@ typedef SMutexBase typedef SMutexBase> SMutexBE; -typedef SMutexLockerBase +typedef SMutexLockerBase SMutexGeneralLocker; -typedef SMutexLockerBase +typedef SMutexLockerBase SMutexLocker; -typedef SMutexLockerBase, SM_GetCurrentCPUThreadIdBE> +typedef SMutexLockerBase, SM_GetCurrentCPUThreadIdBE> SMutexBELocker; diff --git a/Utilities/SQueue.h b/Utilities/SQueue.h index dd0b7787fc..c6e4f7c01a 100644 --- a/Utilities/SQueue.h +++ b/Utilities/SQueue.h @@ -3,7 +3,7 @@ template class SQueue { - SMutexGeneral m_mutex; + std::mutex m_mutex; u32 m_pos; u32 m_count; T m_data[SQSize]; @@ -24,11 +24,6 @@ public: { while (true) { - if (m_mutex.GetOwner() == m_mutex.GetDeadValue()) - { - return false; - } - if (m_count >= SQSize) { if (Emu.IsStopped()) @@ -40,7 +35,7 @@ public: } { - SMutexGeneralLocker lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_count >= SQSize) continue; @@ -55,11 +50,6 @@ public: { while (true) { - if (m_mutex.GetOwner() == m_mutex.GetDeadValue()) - { - return false; - } - if (!m_count) { if (Emu.IsStopped()) @@ -71,7 +61,7 @@ public: } { - SMutexGeneralLocker lock(m_mutex); + std::lock_guard lock(m_mutex); if (!m_count) continue; @@ -96,7 +86,7 @@ public: void Clear() { - SMutexGeneralLocker lock(m_mutex); + std::lock_guard lock(m_mutex); m_count = 0; } @@ -104,11 +94,6 @@ public: { while (true) { - if (m_mutex.GetOwner() == m_mutex.GetDeadValue()) - { - break; - } - if (!m_count) { if (Emu.IsStopped()) @@ -120,7 +105,7 @@ public: } { - SMutexGeneralLocker lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_count) break; } } diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index dba77485c2..ed54c45245 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -4,7 +4,7 @@ #include "Thread.h" thread_local NamedThreadBase* g_tls_this_thread = nullptr; -std::atomic g_thread_count = 0; +std::atomic g_thread_count(0); NamedThreadBase* GetCurrentNamedThread() { diff --git a/Utilities/Thread.h b/Utilities/Thread.h index f081b89951..7a4065015b 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -13,6 +13,9 @@ class NamedThreadBase { std::string m_name; + std::condition_variable m_signal_cv; + std::mutex m_signal_mtx; + public: NamedThreadBase(const std::string& name) : m_name(name) { @@ -24,6 +27,17 @@ public: virtual std::string GetThreadName() const; virtual void SetThreadName(const std::string& name); + + void WaitForAnySignal() // wait 1 ms for something + { + std::unique_lock lock(m_signal_mtx); + m_signal_cv.wait_for(lock, std::chrono::milliseconds(1)); + } + + void Notify() // wake up waiting thread or nothing + { + m_signal_cv.notify_one(); + } }; NamedThreadBase* GetCurrentNamedThread(); diff --git a/rpcs3/Emu/CPU/CPUThread.h b/rpcs3/Emu/CPU/CPUThread.h index 62a13d4a3c..5fd14c402b 100644 --- a/rpcs3/Emu/CPU/CPUThread.h +++ b/rpcs3/Emu/CPU/CPUThread.h @@ -3,9 +3,14 @@ #include "Emu/CPU/CPUDecoder.h" #include "Utilities/SMutex.h" +typedef SMutexBase SMutexR; +typedef SMutexLockerBase SMutexLockerR; + struct reservation_struct { - SMutex mutex; // mutex for updating reservation_owner and data + SMutexR mutex; // mutex for updating reservation_owner and data + // std::mutex doesn't work because it probably wakes up waiting threads in the most unwanted order + // and doesn't give a chance to finish some work before losing the reservation u32 owner; // id of thread that got reservation u32 addr; u32 size; diff --git a/rpcs3/Emu/Cell/PPUInterpreter.h b/rpcs3/Emu/Cell/PPUInterpreter.h index c8e5960d21..a28ee3a050 100644 --- a/rpcs3/Emu/Cell/PPUInterpreter.h +++ b/rpcs3/Emu/Cell/PPUInterpreter.h @@ -2367,7 +2367,7 @@ private: { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); reservation.owner = lock.tid; reservation.addr = addr; reservation.size = 4; @@ -2522,7 +2522,7 @@ private: { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); reservation.owner = lock.tid; reservation.addr = addr; reservation.size = 8; @@ -2641,7 +2641,7 @@ private: { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); if (lock.tid == reservation.owner && reservation.addr == addr && reservation.size == 4) { // Memory.Write32(addr, CPU.GPR[rs]); @@ -2702,7 +2702,7 @@ private: { const u64 addr = ra ? CPU.GPR[ra] + CPU.GPR[rb] : CPU.GPR[rb]; - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); if (lock.tid == reservation.owner && reservation.addr == addr && reservation.size == 8) { // Memory.Write64(addr, CPU.GPR[rs]); diff --git a/rpcs3/Emu/Cell/SPURecompiler.h b/rpcs3/Emu/Cell/SPURecompiler.h index 782f7c4d0a..b68a7c6cd1 100644 --- a/rpcs3/Emu/Cell/SPURecompiler.h +++ b/rpcs3/Emu/Cell/SPURecompiler.h @@ -1931,7 +1931,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._i32[3])); c.cmp(*addr, cpu_dword(GPR[rb]._i32[3])); c.mov(*addr, 0); - c.setg(*addr); + c.setg(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); @@ -2311,7 +2311,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._u32[3])); c.cmp(*addr, cpu_dword(GPR[rb]._u32[3])); c.mov(*addr, 0); - c.seta(*addr); + c.seta(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); @@ -2729,7 +2729,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._i32[3])); c.cmp(*addr, cpu_dword(GPR[rb]._i32[3])); c.mov(*addr, 0); - c.sete(*addr); + c.sete(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); @@ -3391,7 +3391,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._i32[3])); c.cmp(*addr, i10); c.mov(*addr, 0); - c.setg(*addr); + c.setg(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); @@ -3457,7 +3457,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._u32[3])); c.cmp(*addr, i10); c.mov(*addr, 0); - c.seta(*addr); + c.seta(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); @@ -3508,7 +3508,7 @@ private: c.mov(*addr, cpu_dword(GPR[ra]._u32[3])); c.cmp(*addr, i10); c.mov(*addr, 0); - c.sete(*addr); + c.sete(addr->r8()); c.neg(*addr); c.mov(*pos_var, (CPU.PC >> 2) + 1); c.xor_(*pos_var, *addr); diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 663e80d777..c454171422 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -115,7 +115,7 @@ void SPUThread::DoClose() for (u32 i = 0; i < 64; i++) { EventPort& port = SPUPs[i]; - SMutexLocker lock(port.mutex); + std::lock_guard lock(port.m_mutex); if (port.eq) { port.eq->ports.remove(&port); diff --git a/rpcs3/Emu/Cell/SPUThread.h b/rpcs3/Emu/Cell/SPUThread.h index c71b2d3d68..664a1e24f0 100644 --- a/rpcs3/Emu/Cell/SPUThread.h +++ b/rpcs3/Emu/Cell/SPUThread.h @@ -733,7 +733,7 @@ public: if (op == MFC_GETLLAR_CMD) // get reservation { - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); reservation.owner = lock.tid; reservation.addr = ea; reservation.size = 128; @@ -746,7 +746,7 @@ public: } else if (op == MFC_PUTLLC_CMD) // store conditional { - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); if (reservation.owner == lock.tid) // succeeded { if (reservation.addr == ea && reservation.size == 128) @@ -827,7 +827,7 @@ public: } else // store unconditional { - SMutexLocker lock(reservation.mutex); + SMutexLockerR lock(reservation.mutex); ProcessCmd(MFC_PUT_CMD, tag, lsa, ea, 128); if (op == MFC_PUTLLUC_CMD) { @@ -929,7 +929,7 @@ public: EventPort& port = SPUPs[spup]; - SMutexLocker lock(port.mutex); + std::lock_guard lock(port.m_mutex); if (!port.eq) { @@ -937,7 +937,7 @@ public: return; } - if (!port.eq->events.push(SYS_SPU_THREAD_EVENT_USER_KEY, lock.tid, ((u64)code << 32) | (v & 0x00ffffff), data)) + if (!port.eq->events.push(SYS_SPU_THREAD_EVENT_USER_KEY, GetCurrentCPUThread()->GetId(), ((u64)code << 32) | (v & 0x00ffffff), data)) { SPU.In_MBox.PushUncond(CELL_EBUSY); return; diff --git a/rpcs3/Emu/Event.cpp b/rpcs3/Emu/Event.cpp index 06d9abfe38..528b9703c6 100644 --- a/rpcs3/Emu/Event.cpp +++ b/rpcs3/Emu/Event.cpp @@ -6,7 +6,6 @@ void EventManager::Init() { - } void EventManager::Clear() @@ -17,7 +16,7 @@ void EventManager::Clear() bool EventManager::CheckKey(u64 key) { if (!key) return true; - SMutexLocker lock(m_lock); + std::lock_guard lock(m_lock); return key_map.find(key) != key_map.end(); } @@ -25,7 +24,7 @@ bool EventManager::CheckKey(u64 key) bool EventManager::RegisterKey(EventQueue* data, u64 key) { if (!key) return true; - SMutexLocker lock(m_lock); + std::lock_guard lock(m_lock); if (key_map.find(key) != key_map.end()) return false; @@ -43,7 +42,7 @@ bool EventManager::GetEventQueue(u64 key, EventQueue*& data) { data = nullptr; if (!key) return false; - SMutexLocker lock(m_lock); + std::lock_guard lock(m_lock); auto f = key_map.find(key); if (f != key_map.end()) @@ -57,7 +56,7 @@ bool EventManager::GetEventQueue(u64 key, EventQueue*& data) bool EventManager::UnregisterKey(u64 key) { if (!key) return false; - SMutexLocker lock(m_lock); + std::lock_guard lock(m_lock); auto f = key_map.find(key); if (f != key_map.end()) @@ -71,7 +70,7 @@ bool EventManager::UnregisterKey(u64 key) bool EventManager::SendEvent(u64 key, u64 source, u64 d1, u64 d2, u64 d3) { if (!key) return false; - SMutexLocker lock(m_lock); + std::lock_guard lock(m_lock); auto f = key_map.find(key); if (f == key_map.end()) diff --git a/rpcs3/Emu/SysCalls/Modules/cellAdec.cpp b/rpcs3/Emu/SysCalls/Modules/cellAdec.cpp index e5983c399e..d4ef62518b 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellAdec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellAdec.cpp @@ -8,7 +8,7 @@ #include "Emu/SysCalls/SysCalls.h" #include "cellPamf.h" -extern SMutexGeneral g_mutex_avcodec_open2; +extern std::mutex g_mutex_avcodec_open2; extern "C" { @@ -352,7 +352,7 @@ u32 adecOpen(AudioDecoder* data) AVDictionary* opts = nullptr; av_dict_set(&opts, "refcounted_frames", "1", 0); { - SMutexGeneralLocker lock(g_mutex_avcodec_open2); + std::lock_guard lock(g_mutex_avcodec_open2); // not multithread-safe err = avcodec_open2(adec.ctx, codec, &opts); } diff --git a/rpcs3/Emu/SysCalls/Modules/cellAudio.cpp b/rpcs3/Emu/SysCalls/Modules/cellAudio.cpp index 1e9d2af4ad..ca4d8072f0 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellAudio.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellAudio.cpp @@ -15,7 +15,7 @@ //Module cellAudio(0x0011, cellAudio_init); extern Module *cellAudio = nullptr; -static SMutexGeneral audioMutex; +static std::mutex audioMutex; AudioConfig m_config; @@ -394,7 +394,7 @@ int cellAudioInit() // send aftermix event (normal audio event) { - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); // update indexes: for (u32 i = 0; i < m_config.AUDIO_PORT_COUNT; i++) { @@ -694,7 +694,7 @@ int cellAudioGetPortTimestamp(u32 portNum, u64 tag, mem64_t stamp) AudioPortConfig& port = m_config.m_ports[portNum]; - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); stamp = m_config.start_time + (port.counter + (tag - port.tag)) * 256000000 / 48000; @@ -728,7 +728,7 @@ int cellAudioGetPortBlockTag(u32 portNum, u64 blockNo, mem64_t tag) return CELL_AUDIO_ERROR_PARAM; } - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); u64 tag_base = port.tag; if (tag_base % port.block > blockNo) @@ -756,7 +756,7 @@ int cellAudioCreateNotifyEventQueue(mem32_t id, mem64_t key) { cellAudio->Warning("cellAudioCreateNotifyEventQueue(id_addr=0x%x, key_addr=0x%x)", id.GetAddr(), key.GetAddr()); - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); u64 event_key = 0; while (Emu.GetEventManager().CheckKey((event_key << 48) | 0x80004d494f323221)) @@ -790,7 +790,7 @@ int cellAudioSetNotifyEventQueue(u64 key) { cellAudio->Warning("cellAudioSetNotifyEventQueue(key=0x%llx)", key); - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); for (u32 i = 0; i < m_config.m_keys.size(); i++) // check for duplicates { @@ -822,7 +822,7 @@ int cellAudioRemoveNotifyEventQueue(u64 key) { cellAudio->Warning("cellAudioRemoveNotifyEventQueue(key=0x%llx)", key); - SMutexGeneralLocker lock(audioMutex); + std::lock_guard lock(audioMutex); bool found = false; for (u32 i = 0; i < m_config.m_keys.size(); i++) diff --git a/rpcs3/Emu/SysCalls/Modules/cellDmux.h b/rpcs3/Emu/SysCalls/Modules/cellDmux.h index e57aca33c3..7d2f8082a7 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellDmux.h +++ b/rpcs3/Emu/SysCalls/Modules/cellDmux.h @@ -481,7 +481,7 @@ public: class ElementaryStream { - SMutex mutex; + std::mutex m_mutex; SQueue entries; // AU starting addresses u32 put_count; // number of AU written @@ -566,19 +566,19 @@ public: bool hasunseen() { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); return peek_count < put_count; } bool hasdata() { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); return size; } bool isfull() { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); return is_full(); } @@ -586,7 +586,7 @@ public: { u32 addr; { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); //if (fidMajor != 0xbd) ConLog.Write(">>> es::finish(): peek=0x%x, first=0x%x, put=0x%x, size=0x%x", peek, first, put, size); addr = put; @@ -621,7 +621,7 @@ public: void push(DemuxerStream& stream, u32 sz, PesHeader& pes) { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); if (is_full()) { @@ -673,7 +673,7 @@ public: bool release() { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); //if (fidMajor != 0xbd) ConLog.Write(">>> es::release(): peek=0x%x, first=0x%x, put=0x%x, size=0x%x", peek, first, put, size); if (released >= put_count) { @@ -720,7 +720,7 @@ public: bool peek(u32& out_data, bool no_ex, u32& out_spec, bool update_index) { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); //if (fidMajor != 0xbd) ConLog.Write(">>> es::peek(%sAu%s): peek=0x%x, first=0x%x, put=0x%x, size=0x%x", wxString(update_index ? "Get" : "Peek").wx_str(), //wxString(no_ex ? "" : "Ex").wx_str(), peek, first, put, size); if (peek_count >= put_count) return false; @@ -767,7 +767,7 @@ public: void reset() { - SMutexLocker lock(mutex); + std::lock_guard lock(m_mutex); //first = 0; //peek = 0; put = memAddr; diff --git a/rpcs3/Emu/SysCalls/Modules/cellVdec.cpp b/rpcs3/Emu/SysCalls/Modules/cellVdec.cpp index eda5a0511d..69a6256de3 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellVdec.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellVdec.cpp @@ -7,7 +7,7 @@ #include "Emu/SysCalls/Modules.h" #include "cellPamf.h" -SMutexGeneral g_mutex_avcodec_open2; +std::mutex g_mutex_avcodec_open2; extern "C" { @@ -284,7 +284,7 @@ u32 vdecOpen(VideoDecoder* data) AVDictionary* opts = nullptr; av_dict_set(&opts, "refcounted_frames", "1", 0); { - SMutexGeneralLocker lock(g_mutex_avcodec_open2); + std::lock_guard lock(g_mutex_avcodec_open2); // not multithread-safe err = avcodec_open2(vdec.ctx, codec, &opts); } diff --git a/rpcs3/Emu/SysCalls/Modules/libmixer.cpp b/rpcs3/Emu/SysCalls/Modules/libmixer.cpp index 02ed6425ad..a159e0eec1 100644 --- a/rpcs3/Emu/SysCalls/Modules/libmixer.cpp +++ b/rpcs3/Emu/SysCalls/Modules/libmixer.cpp @@ -17,7 +17,7 @@ CellSurMixerConfig surMixer; #define SUR_PORT (7) u32 surMixerCb = 0; u32 surMixerCbArg = 0; -SMutex mixer_mutex; +std::mutex mixer_mutex; float mixdata[8*256]; u64 mixcount = 0; @@ -53,7 +53,7 @@ int cellAANAddData(u32 aan_handle, u32 aan_port, u32 offset, u32 addr, u32 sampl return CELL_OK; } - SMutexLocker lock(mixer_mutex); + std::lock_guard lock(mixer_mutex); if (type == CELL_SURMIXER_CHSTRIP_TYPE1A) { @@ -316,7 +316,7 @@ int cellSurMixerSurBusAddData(u32 busNo, u32 offset, u32 addr, u32 samples) return CELL_OK; } - SMutexLocker lock(mixer_mutex); + std::lock_guard lock(mixer_mutex); for (u32 i = 0; i < samples; i++) { diff --git a/rpcs3/Emu/SysCalls/lv2/SC_Event.cpp b/rpcs3/Emu/SysCalls/lv2/SC_Event.cpp index 46bc43fcdb..b755a4d16a 100644 --- a/rpcs3/Emu/SysCalls/lv2/SC_Event.cpp +++ b/rpcs3/Emu/SysCalls/lv2/SC_Event.cpp @@ -280,20 +280,18 @@ int sys_event_port_destroy(u32 eport_id) return CELL_ESRCH; } - u32 tid = GetCurrentPPUThread().GetId(); - - if (eport->mutex.trylock(tid) != SMR_OK) + if (!eport->m_mutex.try_lock()) { return CELL_EISCONN; } if (eport->eq) { - eport->mutex.unlock(tid); + eport->m_mutex.unlock(); return CELL_EISCONN; } - eport->mutex.unlock(tid, ~0); + eport->m_mutex.unlock(); Emu.GetIdManager().RemoveID(eport_id); return CELL_OK; } @@ -308,16 +306,14 @@ int sys_event_port_connect_local(u32 eport_id, u32 equeue_id) return CELL_ESRCH; } - u32 tid = GetCurrentPPUThread().GetId(); - - if (eport->mutex.trylock(tid) != SMR_OK) + if (!eport->m_mutex.try_lock()) { return CELL_EISCONN; } if (eport->eq) { - eport->mutex.unlock(tid); + eport->m_mutex.unlock(); return CELL_EISCONN; } @@ -325,7 +321,7 @@ int sys_event_port_connect_local(u32 eport_id, u32 equeue_id) if (!Emu.GetIdManager().GetIDData(equeue_id, equeue)) { sys_event.Error("sys_event_port_connect_local: event_queue(%d) not found!", equeue_id); - eport->mutex.unlock(tid); + eport->m_mutex.unlock(); return CELL_ESRCH; } else @@ -334,7 +330,7 @@ int sys_event_port_connect_local(u32 eport_id, u32 equeue_id) } eport->eq = equeue; - eport->mutex.unlock(tid); + eport->m_mutex.unlock(); return CELL_OK; } @@ -353,16 +349,14 @@ int sys_event_port_disconnect(u32 eport_id) return CELL_ENOTCONN; } - u32 tid = GetCurrentPPUThread().GetId(); - - if (eport->mutex.trylock(tid) != SMR_OK) + if (!eport->m_mutex.try_lock()) { return CELL_EBUSY; } eport->eq->ports.remove(eport); eport->eq = nullptr; - eport->mutex.unlock(tid); + eport->m_mutex.unlock(); return CELL_OK; } @@ -377,7 +371,7 @@ int sys_event_port_send(u32 eport_id, u64 data1, u64 data2, u64 data3) return CELL_ESRCH; } - SMutexLocker lock(eport->mutex); + std::lock_guard lock(eport->m_mutex); EventQueue* eq = eport->eq; if (!eq) diff --git a/rpcs3/Emu/SysCalls/lv2/SC_SPU_Thread.cpp b/rpcs3/Emu/SysCalls/lv2/SC_SPU_Thread.cpp index b71e02889c..3a4ebb5837 100644 --- a/rpcs3/Emu/SysCalls/lv2/SC_SPU_Thread.cpp +++ b/rpcs3/Emu/SysCalls/lv2/SC_SPU_Thread.cpp @@ -604,7 +604,7 @@ int sys_spu_thread_connect_event(u32 id, u32 eq_id, u32 et, u8 spup) EventPort& port = spu.SPUPs[spup]; - SMutexLocker lock(port.mutex); + std::lock_guard lock(port.m_mutex); if (port.eq) { @@ -645,7 +645,7 @@ int sys_spu_thread_disconnect_event(u32 id, u32 et, u8 spup) EventPort& port = spu.SPUPs[spup]; - SMutexLocker lock(port.mutex); + std::lock_guard lock(port.m_mutex); if (!port.eq) { diff --git a/rpcs3/Emu/event.h b/rpcs3/Emu/event.h index a9a3630c45..cb0e0a77c1 100644 --- a/rpcs3/Emu/event.h +++ b/rpcs3/Emu/event.h @@ -58,7 +58,7 @@ struct EventPort { u64 name; // generated or user-specified code that is passed to sys_event_data struct EventQueue* eq; // event queue this port has been connected to - SMutex mutex; // may be locked until the event sending is finished + std::mutex m_mutex; // may be locked until the event sending is finished EventPort(u64 name = 0) : eq(nullptr) @@ -70,7 +70,7 @@ struct EventPort class EventRingBuffer { std::vector data; - SMutex m_lock; + std::mutex m_mutex; u32 buf_pos; u32 buf_count; @@ -87,14 +87,14 @@ public: void clear() { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); buf_count = 0; buf_pos = 0; } bool push(u64 name, u64 d1, u64 d2, u64 d3) { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); if (buf_count >= size) return false; sys_event_data& ref = data[(buf_pos + buf_count++) % size]; @@ -108,7 +108,7 @@ public: bool pop(sys_event_data& ref) { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); if (!buf_count) return false; sys_event_data& from = data[buf_pos]; @@ -124,7 +124,7 @@ public: u32 pop_all(sys_event_data* ptr, u32 max) { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); u32 res = 0; while (buf_count && max) @@ -152,16 +152,16 @@ public: class EventPortList { std::vector data; - SMutex m_lock; + std::mutex m_mutex; public: void clear() { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); for (u32 i = 0; i < data.size(); i++) { - SMutexLocker lock2(data[i]->mutex); + std::lock_guard lock2(data[i]->m_mutex); data[i]->eq = nullptr; // force all ports to disconnect } data.clear(); @@ -169,13 +169,13 @@ public: void add(EventPort* port) { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); data.push_back(port); } void remove(EventPort* port) { - SMutexLocker lock(m_lock); + std::lock_guard lock(m_mutex); for (u32 i = 0; i < data.size(); i++) { if (data[i] == port) @@ -215,7 +215,7 @@ struct EventQueue class EventManager { - SMutex m_lock; + std::mutex m_lock; std::unordered_map key_map; public: diff --git a/rpcs3/emucore.vcxproj b/rpcs3/emucore.vcxproj index 5f751eda79..4e8b8bd3ab 100644 --- a/rpcs3/emucore.vcxproj +++ b/rpcs3/emucore.vcxproj @@ -267,6 +267,7 @@ + diff --git a/rpcs3/emucore.vcxproj.filters b/rpcs3/emucore.vcxproj.filters index 7534b4b0b8..a5db320871 100644 --- a/rpcs3/emucore.vcxproj.filters +++ b/rpcs3/emucore.vcxproj.filters @@ -1039,5 +1039,8 @@ Emu\Io\XInput + + Emu\SysCalls + \ No newline at end of file