From e094b7f1d629342ebbd82d321d3c9badbf8b6de4 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 5 Apr 2021 20:25:45 +0100 Subject: [PATCH] Partially fix thread safety issues around _cur_palette Replaces: 4c59dfb6 See also: https://github.com/OpenTTD/OpenTTD/issues/8712 --- src/gfx.cpp | 2 ++ src/openttd.cpp | 11 ++++++++++- src/video/video_driver.cpp | 7 ++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index e16ec1064c..6f165ab013 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -53,6 +53,7 @@ GameMode _game_mode; SwitchMode _switch_mode; ///< The next mainloop command. PauseMode _pause_mode; Palette _cur_palette; +std::mutex _cur_palette_mutex; std::string _switch_baseset; static byte _stringwidth_table[FS_END][224]; ///< Cache containing width of often used characters. @see GetCharacterWidth() @@ -1162,6 +1163,7 @@ void DoPaletteAnimations(); void GfxInitPalettes() { + std::lock_guard lock_state(_cur_palette_mutex); memcpy(&_cur_palette, &_palette, sizeof(_cur_palette)); DoPaletteAnimations(); } diff --git a/src/openttd.cpp b/src/openttd.cpp index 45eaaf2a19..5db3b75cc9 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -82,6 +82,11 @@ #include "linkgraph/linkgraphschedule.h" #include "tracerestrict.h" +#include +#if defined(__MINGW32__) +#include "../3rdparty/mingw-std-threads/mingw.mutex.h" +#endif + #include #include @@ -1971,7 +1976,11 @@ void GameLoop() StateGameLoop(); } - if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) DoPaletteAnimations(); + if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) { + extern std::mutex _cur_palette_mutex; + std::lock_guard lock_state(_cur_palette_mutex); + DoPaletteAnimations(); + } SoundDriver::GetInstance()->MainLoop(); MusicLoop(); diff --git a/src/video/video_driver.cpp b/src/video/video_driver.cpp index d3ac95e6ab..178a42044c 100644 --- a/src/video/video_driver.cpp +++ b/src/video/video_driver.cpp @@ -174,7 +174,12 @@ void VideoDriver::Tick() this->PopulateSystemSprites(); } - this->CheckPaletteAnim(); + { + extern std::mutex _cur_palette_mutex; + std::lock_guard lock_state(_cur_palette_mutex); + this->CheckPaletteAnim(); + } + this->Paint(); this->UnlockVideoBuffer();