QTest::qWaitFor: move ceil<> to after the check

There's no reason to check whether the timer has expired after
rounding the timer's native nanoseconds up to milliseconds, so delay
the ceil<ms> operation to after the timer expiry check.

As a drive-by, protect the std::min call from Windows macros with the
parentheses trick and employ C++17 if-with-initializer.

Remove the break-if-expired, as it now obviously duplicates the while
exit condition.

Change-Id: If30421012143640c75a9a44fe711d0c1c7cd23b9
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 59549657a3699b5ea963b7cdc0bd69f08ab6513a)
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Marc Mutz 2024-03-11 11:42:35 +01:00
parent 2a6015914c
commit 449551eea1

View File

@ -43,11 +43,9 @@ qWaitFor(Functor predicate, QDeadlineTimer deadline = QDeadlineTimer(std::chrono
if (predicate())
return true;
const auto remaining = ceil<milliseconds>(deadline.remainingTimeAsDuration());
if (remaining == 0ms)
break;
if (const auto remaining = deadline.remainingTimeAsDuration(); remaining > 0ns)
qSleep((std::min)(10ms, ceil<milliseconds>(remaining)));
qSleep(std::min(10ms, remaining));
} while (!deadline.hasExpired());
return predicate(); // Last chance