Skip to content

Commit

Permalink
Tune timers for script.wait
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov committed Oct 3, 2024
1 parent 980efa0 commit ad0efec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions tests/trikJsRunnerTests/trikJsRunnerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ TEST_F(TrikJsRunnerTest, scriptWaitQuit)

for (auto &&t: { 3, 10, 20, 50, 100, 200, 500, 1000 }) {
auto err = runDirectCommandAndWaitForQuit(test.arg(t));
mStdOut.clear();
ASSERT_EQ(err, EXIT_SCRIPT_SUCCESS);
}

Check notice on line 167 in tests/trikJsRunnerTests/trikJsRunnerTest.cpp

View check run for this annotation

codefactor.io / CodeFactor

tests/trikJsRunnerTests/trikJsRunnerTest.cpp#L167

Redundant blank line at the end of a code block should be deleted. (whitespace/blank_line)

Expand Down
30 changes: 18 additions & 12 deletions trikScriptRunner/src/scriptExecutionControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,32 @@ void ScriptExecutionControl::wait(const int &milliseconds)
auto precision = 0;

QCoreApplication::sendPostedEvents();
QCoreApplication::processEvents();
auto diff = milliseconds - elapsed.elapsed();
if (diff <= 1) {
if (diff <= precision) {
return;
}

constexpr auto preciseTimerDelta =
QOperatingSystemVersion::currentType() != QOperatingSystemVersion::Windows ? 10 : 5;
QCoreApplication::processEvents();
diff = milliseconds - elapsed.elapsed();
if (diff <= precision) {
return;
}

constexpr auto preciseTimerDelta = 20;

if (milliseconds > 80
&& waitWithTimerType(this, std::max(milliseconds - preciseTimerDelta, 0), Qt::TimerType::CoarseTimer)) {
if (diff > 100
&& waitWithTimerType(this, std::max(diff - preciseTimerDelta, 0ll), Qt::TimerType::CoarseTimer)) {
return;
}
diff = milliseconds - elapsed.elapsed();

// QThread::usleep does not work for Windows, sleeps too long, about 20 ms
const auto usleepDelta =
QOperatingSystemVersion::currentType() != QOperatingSystemVersion::Windows ? 3 : 0;
constexpr auto usleepDelta = QOperatingSystemVersion::currentType() != QOperatingSystemVersion::Windows ? 3 : 0;
constexpr auto spinLockDelta = QOperatingSystemVersion::currentType() != QOperatingSystemVersion::Windows ? 2 : 3;

if (diff >= preciseTimerDelta && waitWithTimerType(this, diff - usleepDelta, Qt::TimerType::PreciseTimer)) {
static_assert(preciseTimerDelta > usleepDelta, "Use timer for longer sleep");

if (waitWithTimerType(this, std::max(0ll, diff - (usleepDelta+spinLockDelta)), Qt::TimerType::PreciseTimer)) {
return;
}

Expand All @@ -106,11 +111,12 @@ void ScriptExecutionControl::wait(const int &milliseconds)
return;
}

if (diff > usleepDelta && usleepDelta > 0) {
QThread::usleep( (diff - usleepDelta) * 1000);

if (diff > usleepDelta && usleepDelta > spinLockDelta) {
QThread::usleep( (diff - spinLockDelta) * 1000);
}
// Ok, spin-lock to wait for a few milliseconds
while ((diff = milliseconds - elapsed.elapsed()) > precision ) {
while ((milliseconds - elapsed.elapsed()) > precision ) {
/* do nothing */
}
}
Expand Down

0 comments on commit ad0efec

Please sign in to comment.