From c061675001713aca12661e255ea2f1afd3c95a91 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 11 Apr 2020 19:33:48 +0100 Subject: [PATCH] Allow modal progress sleep to finish early on completion --- src/gfx.cpp | 2 +- src/progress.cpp | 20 ++++++++++++++++++++ src/progress.h | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index 364fca20a2..8409e7a86a 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -1495,7 +1495,7 @@ void DrawDirtyBlocks() _modal_progress_work_mutex.unlock(); /* Wait a while and update _realtime_tick so we are given the rights */ - if (!is_first_modal_progress_loop) CSleep(MODAL_PROGRESS_REDRAW_TIMEOUT); + if (!is_first_modal_progress_loop) SleepWhileModalProgress(MODAL_PROGRESS_REDRAW_TIMEOUT); #if defined(__GNUC__) || defined(__clang__) __atomic_add_fetch(&_realtime_tick, MODAL_PROGRESS_REDRAW_TIMEOUT, __ATOMIC_RELAXED); #else diff --git a/src/progress.cpp b/src/progress.cpp index e45e5db078..57d96c3538 100644 --- a/src/progress.cpp +++ b/src/progress.cpp @@ -10,6 +10,14 @@ #include "stdafx.h" #include "progress.h" +#include +#include +#include +#if defined(__MINGW32__) +#include "3rdparty/mingw-std-threads/mingw.mutex.h" +#include "3rdparty/mingw-std-threads/mingw.condition_variable.h" +#endif + #include "safeguards.h" /** Are we in a modal progress or not? */ @@ -22,6 +30,9 @@ std::mutex _modal_progress_work_mutex; /** Rights for the painting. */ std::mutex _modal_progress_paint_mutex; +static std::mutex _modal_progress_cv_mutex; +static std::condition_variable _modal_progress_cv; + /** * Set the modal progress state. * @note Makes IsFirstModalProgressLoop return true for the next call. @@ -29,8 +40,11 @@ std::mutex _modal_progress_paint_mutex; */ void SetModalProgress(bool state) { + _modal_progress_cv_mutex.lock(); _in_modal_progress = state; _first_in_modal_loop = true; + _modal_progress_cv_mutex.unlock(); + if (!state) _modal_progress_cv.notify_all(); } /** @@ -44,3 +58,9 @@ bool IsFirstModalProgressLoop() _first_in_modal_loop = false; return ret; } + +void SleepWhileModalProgress(int milliseconds) +{ + std::unique_lock lk(_modal_progress_cv_mutex); + _modal_progress_cv.wait_for(lk, std::chrono::milliseconds(milliseconds), []{ return !_in_modal_progress; }); +} diff --git a/src/progress.h b/src/progress.h index e0fa977a44..92f6c89037 100644 --- a/src/progress.h +++ b/src/progress.h @@ -39,6 +39,7 @@ static inline bool UseThreadedModelProgress() bool IsFirstModalProgressLoop(); void SetModalProgress(bool state); +void SleepWhileModalProgress(int milliseconds); extern std::mutex _modal_progress_work_mutex; extern std::mutex _modal_progress_paint_mutex;