From 3c71f875fa51ef79b75468e728a135d02a992563 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Tue, 28 Nov 2023 15:14:15 -0500 Subject: [PATCH] netreplay: Use wzThread* functions instead of std::thread To avoid std::thread::~thread() calling std::terminate() upon non-graceful shutdown. --- lib/netplay/netreplay.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/netplay/netreplay.cpp b/lib/netplay/netreplay.cpp index c1fcb7f6623..8eefe44112c 100644 --- a/lib/netplay/netreplay.cpp +++ b/lib/netplay/netreplay.cpp @@ -59,14 +59,15 @@ typedef std::vector SerializedNetMessagesBuffer; static moodycamel::BlockingReaderWriterQueue serializedBufferWriteQueue(256); static SerializedNetMessagesBuffer latestWriteBuffer; static size_t minBufferSizeToQueue = DefaultReplayBufferSize; -static std::unique_ptr saveThread; +static WZ_THREAD *saveThread = nullptr; // This function is run in its own thread! Do not call any non-threadsafe functions! -static void replaySaveThreadFunc(PHYSFS_file *pSaveHandle) +static int replaySaveThreadFunc(void *data) { + PHYSFS_file *pSaveHandle = (PHYSFS_file *)data; if (pSaveHandle == nullptr) { - return; + return 1; } SerializedNetMessagesBuffer item; while (true) @@ -79,6 +80,7 @@ static void replaySaveThreadFunc(PHYSFS_file *pSaveHandle) } WZ_PHYSFS_writeBytes(pSaveHandle, item.data(), item.size()); } + return 0; } bool NETreplaySaveStart(std::string const& subdir, ReplayOptionsHandler const &optionsHandler, int maxReplaysSaved, bool appendPlayerToFilename) @@ -185,11 +187,12 @@ bool NETreplaySaveStart(std::string const& subdir, ReplayOptionsHandler const &o debug(LOG_INFO, "Started writing replay file \"%s\".", filename.c_str()); // Create a background thread and hand off all responsibility for writing to the file handle to it - ASSERT(saveThread.get() == nullptr, "Failed to release prior thread"); + ASSERT(saveThread == nullptr, "Failed to release prior thread"); latestWriteBuffer.reserve(minBufferSizeToQueue); if (desiredBufferSize != std::numeric_limits::max()) { - saveThread = std::make_unique(replaySaveThreadFunc, replaySaveHandle); + saveThread = wzThreadCreate(replaySaveThreadFunc, replaySaveHandle, "replaySaveThread"); + wzThreadStart(saveThread); } else { @@ -225,8 +228,8 @@ bool NETreplaySaveStop() // Wait for writing thread to finish if (saveThread) { - saveThread->join(); - saveThread.reset(); + wzThreadJoin(saveThread); + saveThread = nullptr; } else {