From 84c476cea59d8c82085ab17332edad4a977f297e Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 20 Mar 2024 17:39:46 +0000 Subject: [PATCH] Avoid data race in calls to VideoDriver::GetGameInterval --- src/video/video_driver.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/video/video_driver.cpp b/src/video/video_driver.cpp index 2c0d8ce57e..c446afb972 100644 --- a/src/video/video_driver.cpp +++ b/src/video/video_driver.cpp @@ -27,15 +27,17 @@ bool _video_vsync; ///< Whether we should use vsync (only if active video driver void VideoDriver::GameLoop() { - this->next_game_tick += this->GetGameInterval(); - - /* Avoid next_game_tick getting behind more and more if it cannot keep up. */ auto now = std::chrono::steady_clock::now(); - if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now; { std::lock_guard lock(this->game_state_mutex); + const auto interval = this->GetGameInterval(); + this->next_game_tick += interval; + + /* Avoid next_game_tick getting behind more and more if it cannot keep up. */ + if (this->next_game_tick < now - ALLOWED_DRIFT * interval) this->next_game_tick = now; + ::GameLoop(); } }