mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 05:23:09 +02:00
use QEventLoop and a timer for a less CPU using sleep + event-processing
This commit is contained in:
parent
9ae390853c
commit
bf7da71eaf
1 changed files with 13 additions and 16 deletions
|
@ -1067,24 +1067,21 @@ void ScriptEngine::run() {
|
||||||
// on shutdown and stop... so we want to loop and sleep until we've spent our time in
|
// on shutdown and stop... so we want to loop and sleep until we've spent our time in
|
||||||
// purgatory, constantly checking to see if our script was asked to end
|
// purgatory, constantly checking to see if our script was asked to end
|
||||||
bool processedEvents = false;
|
bool processedEvents = false;
|
||||||
while (!_isFinished && clock::now() < sleepUntil) {
|
if (!_isFinished) {
|
||||||
|
PROFILE_RANGE(script, "processEvents-sleep");
|
||||||
{
|
std::chrono::milliseconds sleepFor =
|
||||||
PROFILE_RANGE(script, "processEvents-sleep");
|
std::chrono::duration_cast<std::chrono::milliseconds>(sleepUntil - clock::now());
|
||||||
QCoreApplication::processEvents(); // before we sleep again, give events a chance to process
|
if (sleepFor > std::chrono::milliseconds(0)) {
|
||||||
|
QEventLoop loop;
|
||||||
|
QTimer timer;
|
||||||
|
timer.setSingleShot(true);
|
||||||
|
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
||||||
|
timer.start(sleepFor);
|
||||||
|
loop.exec();
|
||||||
|
} else {
|
||||||
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
processedEvents = true;
|
processedEvents = true;
|
||||||
|
|
||||||
// If after processing events, we're past due, exit asap
|
|
||||||
if (clock::now() >= sleepUntil) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We only want to sleep a small amount so that any pending events (like timers or invokeMethod events)
|
|
||||||
// will be able to process quickly.
|
|
||||||
static const int SMALL_SLEEP_AMOUNT = 100;
|
|
||||||
auto smallSleepUntil = clock::now() + static_cast<std::chrono::microseconds>(SMALL_SLEEP_AMOUNT);
|
|
||||||
std::this_thread::sleep_until(smallSleepUntil);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PROFILE_RANGE(script, "ScriptMainLoop");
|
PROFILE_RANGE(script, "ScriptMainLoop");
|
||||||
|
|
Loading…
Reference in a new issue