Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 60 files). Show all
Unified · Split
| @@ -911,19 +911,11 @@ void ScriptEngine::run() { · 910 unmodified lines | |||
| 911 | break; | 911 | break; |
| 912 | } | 912 | } |
| 913 | 913 | ||
| 914 | // determine how long before the next timer should fire, we'd ideally like to sleep just | 914 | // We only want to sleep a small amount so that any pending events (like timers or invokeMethod events) |
| 915 | // that long, so the next processEvents() will allow the timers to fire on time. | 915 | // will be able to process quickly. |
| 916 | const std::chrono::microseconds minTimerTimeRemaining(USECS_PER_MSEC * getTimersRemainingTime()); | 916 | static const int SMALL_SLEEP_AMOUNT = 100; |
| 917 | 917 | auto smallSleepUntil = clock::now() + static_cast<std::chrono::microseconds>(SMALL_SLEEP_AMOUNT); | |
| 918 | // However, if we haven't yet slept at least as long as our average timer per frame, then we will | 918 | std::this_thread::sleep_until(smallSleepUntil); |
| 919 | // punish the timers to at least wait as long as the average run time of the timers. | ||
| 920 | auto untilTimer = std::max(minTimerTimeRemaining, averageTimerPerFrame); | ||
| 921 | |||
| 922 | // choose the closest time point, our | ||
| 923 | auto remainingSleepUntil = std::chrono::duration_cast<std::chrono::microseconds>(sleepUntil - clock::now()); | ||
| 924 | auto closestUntil = std::min(remainingSleepUntil, untilTimer); | ||
| 925 | auto thisSleepUntil = std::min(sleepUntil, clock::now() + closestUntil); | ||
| 926 | std::this_thread::sleep_until(thisSleepUntil); | ||
| 927 | } | 919 | } |
| 928 | 920 | ||
| 929 | #ifdef SCRIPT_DELAY_DEBUG | 921 | #ifdef SCRIPT_DELAY_DEBUG |
| @@ -1013,21 +1005,6 @@ void ScriptEngine::run() { · 83 unmodified lines | |||
| 1013 | emit doneRunning(); | 1005 | emit doneRunning(); |
| 1014 | } | 1006 | } |
| 1015 | 1007 | ||
| 1016 | quint64 ScriptEngine::getTimersRemainingTime() { | ||
| 1017 | quint64 minimumTime = USECS_PER_SECOND; // anything larger than this can be ignored | ||
| 1018 | QMutableHashIterator<QTimer*, CallbackData> i(_timerFunctionMap); | ||
| 1019 | while (i.hasNext()) { | ||
| 1020 | i.next(); | ||
| 1021 | QTimer* timer = i.key(); | ||
| 1022 | int remainingTime = timer->remainingTime(); | ||
| 1023 | if (remainingTime >= 0) { | ||
| 1024 | minimumTime = std::min((quint64)remainingTime, minimumTime); | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | return minimumTime; | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | |||
| 1031 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why | 1008 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why |
| 1032 | // we want to only call it in our own run "shutdown" processing. | 1009 | // we want to only call it in our own run "shutdown" processing. |
| 1033 | void ScriptEngine::stopAllTimers() { | 1010 | void ScriptEngine::stopAllTimers() { |
| @@ -1038,6 +1015,7 @@ void ScriptEngine::stopAllTimers() { · 4 unmodified lines | |||
| 1038 | stopTimer(timer); | 1015 | stopTimer(timer); |
| 1039 | } | 1016 | } |
| 1040 | } | 1017 | } |
| 1018 | |||
| 1041 | void ScriptEngine::stopAllTimersForEntityScript(const EntityItemID& entityID) { | 1019 | void ScriptEngine::stopAllTimersForEntityScript(const EntityItemID& entityID) { |
| 1042 | // We could maintain a separate map of entityID => QTimer, but someone will have to prove to me that it's worth the complexity. -HRS | 1020 | // We could maintain a separate map of entityID => QTimer, but someone will have to prove to me that it's worth the complexity. -HRS |
| 1043 | QVector<QTimer*> toDelete; | 1021 | QVector<QTimer*> toDelete; |