don't allow punished scripts to slow stop or shutdown

This commit is contained in:
Brad Hefta-Gaub 2016-07-28 10:01:05 -07:00
parent 17a5fa040d
commit 755989b8ff

View file

@ -845,14 +845,24 @@ void ScriptEngine::run() {
// it means our script is taking too long in it's updates, and we want to punish the
// script a little bit. So we will force the sleepUntil to be at least our averageUpdate
// time into the future.
auto wouldSleep = (sleepUntil - clock::now());
auto wouldSleep = (sleepUntil - beforeSleep);
auto avgerageUpdate = totalUpdates / thisFrame;
if (wouldSleep < avgerageUpdate) {
sleepUntil = beforeSleep + avgerageUpdate;
}
std::this_thread::sleep_until(sleepUntil);
// We don't want to actually sleep for too long, because it causes our scripts to hang
// 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
while (!_isFinished && clock::now() < sleepUntil) {
auto wouldSleepSlice = (sleepUntil - clock::now());
auto thisSleepUntil = sleepUntil;
if (wouldSleepSlice > FRAME_DURATION) {
thisSleepUntil = clock::now() + FRAME_DURATION;
}
std::this_thread::sleep_until(thisSleepUntil);
}
#ifdef SCRIPT_DELAY_DEBUG
{