Codechange: Replace custom thread code with C++11 thread objects.

We assume a conforming C++11 compiler environment that has a valid <thread>-header.
Failure to run a real thread is handled gracefully.
This commit is contained in:
Michael Lutz
2019-03-17 01:59:46 +01:00
parent 05f4e73608
commit 05bc2ed7cb
35 changed files with 191 additions and 577 deletions

View File

@@ -26,7 +26,7 @@
#include "../stdafx.h"
#include "../debug.h"
#include "../station_base.h"
#include "../thread/thread.h"
#include "../thread.h"
#include "../town.h"
#include "../network/network.h"
#include "../window_func.h"
@@ -372,7 +372,7 @@ void NORETURN SlErrorCorruptFmt(const char *format, ...)
typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished.
static AsyncSaveFinishProc _async_save_finish = NULL; ///< Callback to call when the savegame loading is finished.
static ThreadObject *_save_thread; ///< The thread we're using to compress and write a savegame
static std::thread _save_thread; ///< The thread we're using to compress and write a savegame
/**
* Called by save thread to tell we finished saving.
@@ -397,10 +397,8 @@ void ProcessAsyncSaveFinish()
_async_save_finish = NULL;
if (_save_thread != NULL) {
_save_thread->Join();
delete _save_thread;
_save_thread = NULL;
if (_save_thread.joinable()) {
_save_thread.join();
}
}
@@ -2486,19 +2484,11 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
}
}
/** Thread run function for saving the file to disk. */
static void SaveFileToDiskThread(void *arg)
{
SaveFileToDisk(true);
}
void WaitTillSaved()
{
if (_save_thread == NULL) return;
if (!_save_thread.joinable()) return;
_save_thread->Join();
delete _save_thread;
_save_thread = NULL;
_save_thread.join();
/* Make sure every other state is handled properly as well. */
ProcessAsyncSaveFinish();
@@ -2525,7 +2515,8 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
SlSaveChunks();
SaveFileStart();
if (!threaded || !ThreadObject::New(&SaveFileToDiskThread, NULL, &_save_thread, "ottd:savegame")) {
if (!threaded || !StartNewThread(&_save_thread, "ottd:savegame", &SaveFileToDisk, true)) {
if (threaded) DEBUG(sl, 1, "Cannot create savegame thread, reverting to single-threaded mode...");
SaveOrLoadResult result = SaveFileToDisk(false);