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/genworld.cpp | |
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/genworld.cpp')
-rw-r--r-- | src/genworld.cpp | 22 |
1 files changed, 8 insertions, 14 deletions
diff --git a/src/genworld.cpp b/src/genworld.cpp index 64caf3b89..ee8768b62 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -34,7 +34,7 @@ #include "game/game.hpp" #include "game/game_instance.hpp" #include "string_func.h" -#include "thread/thread.h" +#include "thread.h" #include "safeguards.h" @@ -94,7 +94,7 @@ static void CleanupGeneration() /** * The internal, real, generate function. */ -static void _GenerateWorld(void *) +static void _GenerateWorld() { /* Make sure everything is done via OWNER_NONE. */ Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE); @@ -242,14 +242,12 @@ void GenerateWorldSetAbortCallback(GWAbortProc *proc) */ void WaitTillGeneratedWorld() { - if (_gw.thread == NULL) return; + if (!_gw.thread.joinable()) return; _modal_progress_work_mutex.unlock(); _modal_progress_paint_mutex.unlock(); _gw.quit_thread = true; - _gw.thread->Join(); - delete _gw.thread; - _gw.thread = NULL; + _gw.thread.join(); _gw.threaded = false; _modal_progress_work_mutex.lock(); _modal_progress_paint_mutex.lock(); @@ -284,7 +282,7 @@ void HandleGeneratingWorldAbortion() CleanupGeneration(); - if (_gw.thread != NULL) _gw.thread->Exit(); + if (_gw.thread.joinable() && _gw.thread.get_id() == std::this_thread::get_id()) throw OTTDThreadExitSignal(); SwitchToMode(_switch_mode); } @@ -326,17 +324,13 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti SetupColoursAndInitialWindow(); SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); - if (_gw.thread != NULL) { - _gw.thread->Join(); - delete _gw.thread; - _gw.thread = NULL; - } + if (_gw.thread.joinable()) _gw.thread.join(); - if (!UseThreadedModelProgress() || !VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread, "ottd:genworld")) { + if (!UseThreadedModelProgress() || !VideoDriver::GetInstance()->HasGUI() || !StartNewThread(&_gw.thread, "ottd:genworld", &_GenerateWorld)) { DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode"); _gw.threaded = false; _modal_progress_work_mutex.unlock(); - _GenerateWorld(NULL); + _GenerateWorld(); _modal_progress_work_mutex.lock(); return; } |