From 6907106b54404427458a97590be814f80008d842 Mon Sep 17 00:00:00 2001 From: Nikhil Narayana Date: Sun, 29 Oct 2023 22:51:22 -0700 Subject: [PATCH] fix: use SPSCQueue for thread safety --- Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp | 10 +++++----- Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h | 3 ++- Source/Core/Core/Slippi/SlippiNetplay.cpp | 8 ++++---- Source/Core/Core/Slippi/SlippiNetplay.h | 5 ++++- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp index e8435c1713a4..842e94bc4a0b 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp +++ b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.cpp @@ -475,19 +475,19 @@ void CEXISlippi::writeToFileAsync(u8* payload, u32 length, std::string file_opti write_msg->data = payload_data; write_msg->operation = file_option; - file_write_queue.push(std::move(write_msg)); + file_write_queue.Push(std::move(write_msg)); } void CEXISlippi::FileWriteThread(void) { Common::SetCurrentThreadName("Slippi File Write"); - while (write_thread_running || !file_write_queue.empty()) + while (write_thread_running || !file_write_queue.Empty()) { // Process all messages - while (!file_write_queue.empty()) + while (!file_write_queue.Empty()) { - writeToFile(std::move(file_write_queue.front())); - file_write_queue.pop(); + writeToFile(std::move(file_write_queue.Front())); + file_write_queue.Pop(); Common::SleepCurrentThread(0); } diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h index f5026fc8e2e5..54df079ba055 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h +++ b/Source/Core/Core/HW/EXI/EXI_DeviceSlippi.h @@ -7,6 +7,7 @@ #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/IOFile.h" +#include "Common/SPSCQueue.h" #include "Core/Slippi/SlippiDirectCodes.h" #include "Core/Slippi/SlippiExiTypes.h" #include "Core/Slippi/SlippiGame.h" @@ -247,7 +248,7 @@ class CEXISlippi : public IEXIDevice void FileWriteThread(void); - std::queue> file_write_queue; + Common::SPSCQueue, false> file_write_queue; bool write_thread_running = false; std::thread m_file_write_thread; diff --git a/Source/Core/Core/Slippi/SlippiNetplay.cpp b/Source/Core/Core/Slippi/SlippiNetplay.cpp index 6b8ee47335a2..22b0779482bb 100644 --- a/Source/Core/Core/Slippi/SlippiNetplay.cpp +++ b/Source/Core/Core/Slippi/SlippiNetplay.cpp @@ -724,7 +724,7 @@ void SlippiNetplayClient::SendAsync(std::unique_ptr packet) { { std::lock_guard lkq(m_crit.async_queue_write); - m_async_queue.emplace(std::move(packet)); + m_async_queue.Push(std::move(packet)); } ENetUtil::WakeupThread(m_client); } @@ -951,10 +951,10 @@ void SlippiNetplayClient::ThreadFunc() ENetEvent net_event; int net; net = enet_host_service(m_client, &net_event, 250); - while (!m_async_queue.empty()) + while (!m_async_queue.Empty()) { - Send(*(m_async_queue.front().get())); - m_async_queue.pop(); + Send(*(m_async_queue.Front().get())); + m_async_queue.Pop(); } if (net > 0) { diff --git a/Source/Core/Core/Slippi/SlippiNetplay.h b/Source/Core/Core/Slippi/SlippiNetplay.h index 129a13ab24fe..5dbb5a27e36d 100644 --- a/Source/Core/Core/Slippi/SlippiNetplay.h +++ b/Source/Core/Core/Slippi/SlippiNetplay.h @@ -18,11 +18,13 @@ #include "Common/CommonTypes.h" #include "Common/Event.h" +#include "Common/SPSCQueue.h" #include "Common/Timer.h" #include "Common/TraversalClient.h" #include "Core/NetPlayProto.h" #include "Core/Slippi/SlippiPad.h" #include "InputCommon/GCPadStatus.h" + #ifdef _WIN32 #include #endif @@ -207,7 +209,8 @@ class SlippiNetplayClient std::recursive_mutex async_queue_write; } m_crit; - std::queue> m_async_queue; + // SLIPPITODO: consider using AsyncQueueEntry like in NetPlayServer.h + Common::SPSCQueue, false> m_async_queue; ENetHost* m_client = nullptr; std::vector m_server;