Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all
Unified · Split
| @@ -845,14 +845,24 @@ void ScriptEngine::run() { · 844 unmodified lines | ||
| 845 | 845 | // it means our script is taking too long in it's updates, and we want to punish the |
| 846 | 846 | // script a little bit. So we will force the sleepUntil to be at least our averageUpdate |
| 847 | 847 | // time into the future. |
| 848 | − auto wouldSleep = (sleepUntil - clock::now()); | |
| 848 | + auto wouldSleep = (sleepUntil - beforeSleep); | |
| 849 | 849 | auto avgerageUpdate = totalUpdates / thisFrame; |
| 850 | 850 | |
| 851 | 851 | if (wouldSleep < avgerageUpdate) { |
| 852 | 852 | sleepUntil = beforeSleep + avgerageUpdate; |
| 853 | 853 | } |
| 854 | 854 | |
| 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 | + } | |
| 856 | 866 | |
| 857 | 867 | #ifdef SCRIPT_DELAY_DEBUG |
| 858 | 868 | { |