History

don't allow punished scripts to slow stop or shutdown

Brad Hefta-Gaub committed 10 years ago

Commit 755989b1 parent 17a5fa01 file · +12 −2Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+12−2
@@ -845,14 +845,24 @@ void ScriptEngine::run() { · 844 unmodified lines
845845 // it means our script is taking too long in it's updates, and we want to punish the
846846 // script a little bit. So we will force the sleepUntil to be at least our averageUpdate
847847 // time into the future.
848− auto wouldSleep = (sleepUntil - clock::now());
848+ auto wouldSleep = (sleepUntil - beforeSleep);
849849 auto avgerageUpdate = totalUpdates / thisFrame;
850850
851851 if (wouldSleep < avgerageUpdate) {
852852 sleepUntil = beforeSleep + avgerageUpdate;
853853 }
854854
855− std::this_thread::sleep_until(sleepUntil);
855+ // We don't want to actually sleep for too long, because it causes our scripts to hang
856+ // on shutdown and stop... so we want to loop and sleep until we've spent our time in
857+ // purgatory, constantly checking to see if our script was asked to end
858+ while (!_isFinished && clock::now() < sleepUntil) {
859+ auto wouldSleepSlice = (sleepUntil - clock::now());
860+ auto thisSleepUntil = sleepUntil;
861+ if (wouldSleepSlice > FRAME_DURATION) {
862+ thisSleepUntil = clock::now() + FRAME_DURATION;
863+ }
864+ std::this_thread::sleep_until(thisSleepUntil);
865+ }
856866
857867 #ifdef SCRIPT_DELAY_DEBUG
858868 {