Codechange: Replace custom mutex code with C++11 mutex'es.

A conforming compiler with a valid <mutex>-header is expected.
Most parts of the code assume that locking a mutex will never fail unexpectedly,
which is generally true on all common platforms that don't just pretend to
be C++11. The use of condition variables in driver code is checked.
This commit is contained in:
Michael Lutz
2019-03-11 00:45:39 +01:00
parent 3b86f54fc7
commit 05f4e73608
19 changed files with 187 additions and 426 deletions

View File

@@ -34,6 +34,7 @@
#include "game/game.hpp"
#include "game/game_instance.hpp"
#include "string_func.h"
#include "thread/thread.h"
#include "safeguards.h"
@@ -98,9 +99,10 @@ static void _GenerateWorld(void *)
/* Make sure everything is done via OWNER_NONE. */
Backup<CompanyByte> _cur_company(_current_company, OWNER_NONE, FILE_LINE);
std::unique_lock<std::mutex> lock(_modal_progress_work_mutex, std::defer_lock);
try {
_generating_world = true;
_modal_progress_work_mutex->BeginCritical();
lock.lock();
if (_network_dedicated) DEBUG(net, 1, "Generating map, please wait...");
/* Set the Random() seed to generation_seed so we produce the same map with the same seed */
if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
@@ -194,7 +196,7 @@ static void _GenerateWorld(void *)
IncreaseGeneratingWorldProgress(GWP_GAME_START);
CleanupGeneration();
_modal_progress_work_mutex->EndCritical();
lock.unlock();
ShowNewGRFError();
@@ -210,7 +212,6 @@ static void _GenerateWorld(void *)
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true);
if (_cur_company.IsValid()) _cur_company.Restore();
_generating_world = false;
_modal_progress_work_mutex->EndCritical();
throw;
}
}
@@ -243,15 +244,15 @@ void WaitTillGeneratedWorld()
{
if (_gw.thread == NULL) return;
_modal_progress_work_mutex->EndCritical();
_modal_progress_paint_mutex->EndCritical();
_modal_progress_work_mutex.unlock();
_modal_progress_paint_mutex.unlock();
_gw.quit_thread = true;
_gw.thread->Join();
delete _gw.thread;
_gw.thread = NULL;
_gw.threaded = false;
_modal_progress_work_mutex->BeginCritical();
_modal_progress_paint_mutex->BeginCritical();
_modal_progress_work_mutex.lock();
_modal_progress_paint_mutex.lock();
}
/**
@@ -331,12 +332,12 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
_gw.thread = NULL;
}
if (!VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread, "ottd:genworld")) {
if (!UseThreadedModelProgress() || !VideoDriver::GetInstance()->HasGUI() || !ThreadObject::New(&_GenerateWorld, NULL, &_gw.thread, "ottd:genworld")) {
DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
_gw.threaded = false;
_modal_progress_work_mutex->EndCritical();
_modal_progress_work_mutex.unlock();
_GenerateWorld(NULL);
_modal_progress_work_mutex->BeginCritical();
_modal_progress_work_mutex.lock();
return;
}