From 65e956a01c2230094e2609d3849ed786e8cf068f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 9 May 2022 20:11:30 +1000 Subject: [PATCH] Misc: Replace use of Threading::Mutex with std::mutex in core code --- common/EventSource.h | 4 ++-- common/EventSource.inl | 13 ++++++------- common/Linux/LnxHostSys.cpp | 6 ++++-- common/PageFaultSource.h | 6 ++++-- common/VirtualMemory.cpp | 6 ++---- common/Vulkan/Builders.cpp | 3 ++- common/Windows/WinHostSys.cpp | 3 ++- pcsx2/GS.h | 7 ++++--- pcsx2/MTGS.cpp | 19 ++++++++++--------- pcsx2/SPU2/Wavedump_wav.cpp | 10 ++++++---- pcsx2/SPU2/spu2.cpp | 6 +++--- pcsx2/SPU2/spu2.h | 4 ++-- 12 files changed, 47 insertions(+), 40 deletions(-) diff --git a/common/EventSource.h b/common/EventSource.h index 67f61ec35d..df6e5dd0c2 100644 --- a/common/EventSource.h +++ b/common/EventSource.h @@ -16,7 +16,7 @@ #pragma once #include -#include "common/Threading.h" +#include // -------------------------------------------------------------------------------------- // EventSource< template EvtType > @@ -41,7 +41,7 @@ protected: ListenerList m_cache_copy; bool m_cache_valid; - Threading::Mutex m_listeners_lock; + std::mutex m_listeners_lock; public: EventSource() diff --git a/common/EventSource.inl b/common/EventSource.inl index 3269c47ec9..bfee66a953 100644 --- a/common/EventSource.inl +++ b/common/EventSource.inl @@ -14,15 +14,14 @@ */ #pragma once - -#include "common/Threading.h" - -using Threading::ScopedLock; +#include "common/Pcsx2Defs.h" +#include "common/Exceptions.h" +#include "common/Console.h" template typename EventSource::ListenerIterator EventSource::Add(ListenerType& listener) { - ScopedLock locker(m_listeners_lock); + std::unique_lock locker(m_listeners_lock); // Check for duplicates before adding the event. if (IsDebugBuild) @@ -41,7 +40,7 @@ typename EventSource::ListenerIterator EventSource:: template void EventSource::Remove(ListenerType& listener) { - ScopedLock locker(m_listeners_lock); + std::unique_lock locker(m_listeners_lock); m_cache_valid = false; m_listeners.remove(&listener); } @@ -49,7 +48,7 @@ void EventSource::Remove(ListenerType& listener) template void EventSource::Remove(const ListenerIterator& listenerHandle) { - ScopedLock locker(m_listeners_lock); + std::unique_lock locker(m_listeners_lock); m_cache_valid = false; m_listeners.erase(listenerHandle); } diff --git a/common/Linux/LnxHostSys.cpp b/common/Linux/LnxHostSys.cpp index 757592d33e..cbee8f1ed2 100644 --- a/common/Linux/LnxHostSys.cpp +++ b/common/Linux/LnxHostSys.cpp @@ -14,13 +14,15 @@ */ #if !defined(_WIN32) -#include #include #include #include #include #include "common/PageFaultSource.h" +#include "common/Console.h" +#include "common/Exceptions.h" +#include "common/StringHelpers.h" // Apple uses the MAP_ANON define instead of MAP_ANONYMOUS, but they mean // the same thing. @@ -55,7 +57,7 @@ static void SysPageFaultSignalFilter(int signal, siginfo_t* siginfo, void*) // Note: This signal can be accessed by the EE or MTVU thread // Source_PageFault is a global variable with its own state information // so for now we lock this exception code unless someone can fix this better... - Threading::ScopedLock lock(PageFault_Mutex); + std::unique_lock lock(PageFault_Mutex); Source_PageFault->Dispatch(PageFaultInfo((uptr)siginfo->si_addr & ~m_pagemask)); diff --git a/common/PageFaultSource.h b/common/PageFaultSource.h index 5f055cd386..695e0f3d40 100644 --- a/common/PageFaultSource.h +++ b/common/PageFaultSource.h @@ -26,7 +26,10 @@ #include "EventSource.h" #include "General.h" +#include "Assertions.h" +#include "Dependencies.h" #include +#include struct PageFaultInfo { @@ -314,6 +317,5 @@ extern long __stdcall SysPageFaultExceptionFilter(struct _EXCEPTION_POINTERS* ep extern void pxInstallSignalHandler(); extern void _platform_InstallSignalHandler(); -#include "Threading.h" extern SrcType_PageFault* Source_PageFault; -extern Threading::Mutex PageFault_Mutex; +extern std::mutex PageFault_Mutex; diff --git a/common/VirtualMemory.cpp b/common/VirtualMemory.cpp index 90086901ef..b73beb901f 100644 --- a/common/VirtualMemory.cpp +++ b/common/VirtualMemory.cpp @@ -13,17 +13,15 @@ * If not, see . */ -#ifndef __WXMSW__ -#include -#endif #include "common/PageFaultSource.h" #include "common/EventSource.inl" #include "common/MemsetFast.inl" +#include "common/Console.h" template class EventSource; SrcType_PageFault* Source_PageFault = NULL; -Threading::Mutex PageFault_Mutex; +std::mutex PageFault_Mutex; void pxInstallSignalHandler() { diff --git a/common/Vulkan/Builders.cpp b/common/Vulkan/Builders.cpp index 83add10891..ab0ad20d41 100644 --- a/common/Vulkan/Builders.cpp +++ b/common/Vulkan/Builders.cpp @@ -16,6 +16,7 @@ #include "common/Vulkan/Builders.h" #include "common/Vulkan/Util.h" #include "common/Assertions.h" +#include namespace Vulkan { @@ -881,4 +882,4 @@ namespace Vulkan m_ci.offset = offset; m_ci.range = size; } -} // namespace Vulkan \ No newline at end of file +} // namespace Vulkan diff --git a/common/Windows/WinHostSys.cpp b/common/Windows/WinHostSys.cpp index 85be0f9f7b..3c001e65d5 100644 --- a/common/Windows/WinHostSys.cpp +++ b/common/Windows/WinHostSys.cpp @@ -17,6 +17,7 @@ #include "common/RedtapeWindows.h" #include "common/PageFaultSource.h" +#include "common/Console.h" static long DoSysPageFaultExceptionFilter(EXCEPTION_POINTERS* eps) { @@ -26,7 +27,7 @@ static long DoSysPageFaultExceptionFilter(EXCEPTION_POINTERS* eps) // Note: This exception can be accessed by the EE or MTVU thread // Source_PageFault is a global variable with its own state information // so for now we lock this exception code unless someone can fix this better... - Threading::ScopedLock lock(PageFault_Mutex); + std::unique_lock lock(PageFault_Mutex); Source_PageFault->Dispatch(PageFaultInfo((uptr)eps->ExceptionRecord->ExceptionInformation[1])); return Source_PageFault->WasHandled() ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH; } diff --git a/pcsx2/GS.h b/pcsx2/GS.h index 8413789038..d29f2a1a3e 100644 --- a/pcsx2/GS.h +++ b/pcsx2/GS.h @@ -20,6 +20,7 @@ #include "GS/GS.h" #include #include +#include #include extern double GetVerticalFrequency(); @@ -333,8 +334,8 @@ public: std::atomic m_QueuedFrameCount; std::atomic m_VsyncSignalListener; - Threading::Mutex m_mtx_RingBufferBusy2; // Gets released on semaXGkick waiting... - Threading::Mutex m_mtx_WaitGS; + std::mutex m_mtx_RingBufferBusy2; // Gets released on semaXGkick waiting... + std::mutex m_mtx_WaitGS; Threading::WorkSema m_sem_event; Threading::KernelSemaphore m_sem_OnRingReset; Threading::KernelSemaphore m_sem_Vsync; @@ -355,7 +356,7 @@ public: uint m_packet_writepos; // index of the data location in the ringbuffer. #ifdef RINGBUF_DEBUG_STACK - Threading::Mutex m_lock_Stack; + std::mutex m_lock_Stack; #endif std::thread m_thread; diff --git a/pcsx2/MTGS.cpp b/pcsx2/MTGS.cpp index 8b2832336a..7e7b23b8bb 100644 --- a/pcsx2/MTGS.cpp +++ b/pcsx2/MTGS.cpp @@ -276,7 +276,7 @@ void SysMtgsThread::MainLoop() PacketTagType prevCmd; #endif - ScopedLock mtvu_lock(m_mtx_RingBufferBusy2); + std::unique_lock mtvu_lock(m_mtx_RingBufferBusy2); while (true) { @@ -285,14 +285,13 @@ void SysMtgsThread::MainLoop() // is very optimized (only 1 instruction test in most cases), so no point in trying // to avoid it. - m_mtx_RingBufferBusy2.Release(); - + mtvu_lock.unlock(); m_sem_event.WaitForWork(); + mtvu_lock.lock(); + if (!m_open_flag.load(std::memory_order_acquire)) break; - m_mtx_RingBufferBusy2.Acquire(); - // note: m_ReadPos is intentionally not volatile, because it should only // ever be modified by this thread. while (m_ReadPos.load(std::memory_order_relaxed) != m_WritePos.load(std::memory_order_acquire)) @@ -413,10 +412,10 @@ void SysMtgsThread::MainLoop() MTVU_LOG("MTGS - Waiting on semaXGkick!"); if (!vu1Thread.semaXGkick.TryWait()) { - mtvu_lock.Release(); + mtvu_lock.unlock(); // Wait for MTVU to complete vu1 program vu1Thread.semaXGkick.Wait(); - mtvu_lock.Acquire(); + mtvu_lock.lock(); } Gif_Path& path = gifUnit.gifPath[GIF_PATH_1]; GS_Packet gsPack = path.GetGSPacketMTVU(); // Get vu1 program's xgkick packet(s) @@ -605,7 +604,9 @@ void SysMtgsThread::WaitGS(bool syncRegs, bool weakWait, bool isMTVU) { while (true) { - m_mtx_RingBufferBusy2.Wait(); + // m_mtx_RingBufferBusy2.Wait(); + m_mtx_RingBufferBusy2.lock(); + m_mtx_RingBufferBusy2.unlock(); if (path.GetPendingGSPackets() != startP1Packs) break; } @@ -619,7 +620,7 @@ void SysMtgsThread::WaitGS(bool syncRegs, bool weakWait, bool isMTVU) if (syncRegs) { - ScopedLock lock(m_mtx_WaitGS); + std::unique_lock lock(m_mtx_WaitGS); // Completely synchronize GS and MTGS register states. memcpy(RingBuffer.Regs, PS2MEM_GS, sizeof(RingBuffer.Regs)); } diff --git a/pcsx2/SPU2/Wavedump_wav.cpp b/pcsx2/SPU2/Wavedump_wav.cpp index 4c40560921..3a84e22d20 100644 --- a/pcsx2/SPU2/Wavedump_wav.cpp +++ b/pcsx2/SPU2/Wavedump_wav.cpp @@ -22,6 +22,8 @@ #include "common/StringUtil.h" #endif +#include + static WavOutFile* _new_WavOutFile(const char* destfile) { return new WavOutFile(destfile, 48000, 16, 2); @@ -109,13 +111,13 @@ using namespace Threading; bool WavRecordEnabled = false; static WavOutFile* m_wavrecord = nullptr; -static Mutex WavRecordMutex; +static std::mutex WavRecordMutex; bool RecordStart(const std::string* filename) { try { - ScopedLock lock(WavRecordMutex); + std::unique_lock lock(WavRecordMutex); safe_delete(m_wavrecord); if (filename) #ifdef _WIN32 @@ -142,13 +144,13 @@ bool RecordStart(const std::string* filename) void RecordStop() { WavRecordEnabled = false; - ScopedLock lock(WavRecordMutex); + std::unique_lock lock(WavRecordMutex); safe_delete(m_wavrecord); } void RecordWrite(const StereoOut16& sample) { - ScopedLock lock(WavRecordMutex); + std::unique_lock lock(WavRecordMutex); if (m_wavrecord == nullptr) return; m_wavrecord->write((s16*)&sample, 2); diff --git a/pcsx2/SPU2/spu2.cpp b/pcsx2/SPU2/spu2.cpp index f00b888ddd..8bf9bc888d 100644 --- a/pcsx2/SPU2/spu2.cpp +++ b/pcsx2/SPU2/spu2.cpp @@ -31,7 +31,7 @@ using namespace Threading; -MutexRecursive mtx_SPU2Status; +std::recursive_mutex mtx_SPU2Status; int SampleRate = 48000; @@ -259,7 +259,7 @@ uptr gsWindowHandle = 0; s32 SPU2open() { - ScopedLock lock(mtx_SPU2Status); + std::unique_lock lock(mtx_SPU2Status); if (IsOpened) return 0; @@ -307,7 +307,7 @@ s32 SPU2open() void SPU2close() { - ScopedLock lock(mtx_SPU2Status); + std::unique_lock lock(mtx_SPU2Status); if (!IsOpened) return; IsOpened = false; diff --git a/pcsx2/SPU2/spu2.h b/pcsx2/SPU2/spu2.h index 14f0bdb575..b9ffbe9e2f 100644 --- a/pcsx2/SPU2/spu2.h +++ b/pcsx2/SPU2/spu2.h @@ -15,11 +15,11 @@ #pragma once -#include "common/Threading.h" #include "SaveState.h" #include "IopCounters.h" +#include -extern Threading::MutexRecursive mtx_SPU2Status; +extern std::recursive_mutex mtx_SPU2Status; enum class PS2Modes {