diff options
author | Michael Lutz <michi@icosahedron.de> | 2019-03-17 01:59:46 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2019-04-06 11:27:39 +0200 |
commit | 05bc2ed7cbe07cb4cd535932f10778b35f72e944 (patch) | |
tree | 0faaf12fd1bafb0786236ffc82052e8b83dfca60 /src/saveload | |
parent | 05f4e7360886e36b221ef5c3af4426625a3de686 (diff) | |
download | openttd-05bc2ed7cbe07cb4cd535932f10778b35f72e944.tar.xz |
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.
Diffstat (limited to 'src/saveload')
-rw-r--r-- | src/saveload/saveload.cpp | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index c97087224..95690f6af 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -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); |