Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netreplay: Use wzThread* functions instead of std::thread #3544

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/netplay/netreplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ typedef std::vector<uint8_t> SerializedNetMessagesBuffer;
static moodycamel::BlockingReaderWriterQueue<SerializedNetMessagesBuffer> serializedBufferWriteQueue(256);
static SerializedNetMessagesBuffer latestWriteBuffer;
static size_t minBufferSizeToQueue = DefaultReplayBufferSize;
static std::unique_ptr<wz::thread> saveThread;
static WZ_THREAD *saveThread = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason not to use std::unique_ptr<WZ_THREAD>?

Copy link
Member Author

@past-due past-due Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WZ_THREAD instances should only be created and managed by the wzThread* functions. (They wrap SDL Threads.)

Also, this is one of those things that we actually want to "leak" on a non-graceful shutdown (to avoid various downstream effects that could lead to crash logs that aren't very useful - like in the std::thread destructor case.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, you still should be able to adopt the raw pointer you get from wzThreadCreate and then just declare a std::unique_ptr with a custom deleter, which will call wzThreadJoin instead of the default delete.

Copy link
Member Author

@past-due past-due Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like my edit stepped on your comment - we actually want this to "leak" on a non-graceful shutdown. (i.e. We don't want to join + wait.)


// 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)
Expand All @@ -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)
Expand Down Expand Up @@ -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<size_t>::max())
{
saveThread = std::make_unique<wz::thread>(replaySaveThreadFunc, replaySaveHandle);
saveThread = wzThreadCreate(replaySaveThreadFunc, replaySaveHandle, "replaySaveThread");
wzThreadStart(saveThread);
}
else
{
Expand Down Expand Up @@ -225,8 +228,8 @@ bool NETreplaySaveStop()
// Wait for writing thread to finish
if (saveThread)
{
saveThread->join();
saveThread.reset();
wzThreadJoin(saveThread);
saveThread = nullptr;
}
else
{
Expand Down
Loading