History

first cut at using wait signals to improve RPC timing

Brad Hefta-Gaub committed 9 years ago

Commit 7c7d2391 parent 1fcfc944 files · +42 −1Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+37−1
@@ -573,6 +573,7 @@ void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) { · 572 unmodified lines
573573 QMetaObject::invokeMethod(this, "registerValue",
574574 Q_ARG(const QString&, valueName),
575575 Q_ARG(QScriptValue, value));
576+ _hasInvokeMethod.wakeAll();
576577 return;
577578 }
578579
@@ -603,6 +604,7 @@ void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { · 24 unmodified lines
603604 QMetaObject::invokeMethod(this, "registerGlobalObject",
604605 Q_ARG(const QString&, name),
605606 Q_ARG(QObject*, object));
607+ _hasInvokeMethod.wakeAll();
606608 return;
607609 }
608610 #ifdef THREAD_DEBUGGING
@@ -628,6 +630,7 @@ void ScriptEngine::registerFunction(const QString& name, QScriptEngine::Function · 19 unmodified lines
628630 Q_ARG(const QString&, name),
629631 Q_ARG(QScriptEngine::FunctionSignature, functionSignature),
630632 Q_ARG(int, numArguments));
633+ _hasInvokeMethod.wakeAll();
631634 return;
632635 }
633636 #ifdef THREAD_DEBUGGING
@@ -647,6 +650,7 @@ void ScriptEngine::registerFunction(const QString& parent, const QString& name, · 13 unmodified lines
647650 Q_ARG(const QString&, name),
648651 Q_ARG(QScriptEngine::FunctionSignature, functionSignature),
649652 Q_ARG(int, numArguments));
653+ _hasInvokeMethod.wakeAll();
650654 return;
651655 }
652656 #ifdef THREAD_DEBUGGING
@@ -672,6 +676,7 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 19 unmodified lines
672676 Q_ARG(QScriptEngine::FunctionSignature, getter),
673677 Q_ARG(QScriptEngine::FunctionSignature, setter),
674678 Q_ARG(const QString&, parent));
679+ _hasInvokeMethod.wakeAll();
675680 return;
676681 }
677682 #ifdef THREAD_DEBUGGING
@@ -704,6 +709,7 @@ void ScriptEngine::removeEventHandler(const EntityItemID& entityID, const QStrin · 26 unmodified lines
704709 Q_ARG(const EntityItemID&, entityID),
705710 Q_ARG(const QString&, eventName),
706711 Q_ARG(QScriptValue, handler));
712+ _hasInvokeMethod.wakeAll();
707713 return;
708714 }
709715 #ifdef THREAD_DEBUGGING
@@ -735,6 +741,7 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 25 unmodified lines
735741 Q_ARG(const EntityItemID&, entityID),
736742 Q_ARG(const QString&, eventName),
737743 Q_ARG(QScriptValue, handler));
744+ _hasInvokeMethod.wakeAll();
738745 return;
739746 }
740747 #ifdef THREAD_DEBUGGING
@@ -821,6 +828,7 @@ QScriptValue ScriptEngine::evaluate(const QString& sourceCode, const QString& fi · 80 unmodified lines
821828 Q_ARG(const QString&, sourceCode),
822829 Q_ARG(const QString&, fileName),
823830 Q_ARG(int, lineNumber));
831+ _hasInvokeMethod.wakeAll();
824832 return result;
825833 }
826834
@@ -920,7 +928,25 @@ void ScriptEngine::run() { · 93 unmodified lines
920928 auto remainingSleepUntil = std::chrono::duration_cast<std::chrono::microseconds>(sleepUntil - clock::now());
921929 auto closestUntil = std::min(remainingSleepUntil, untilTimer);
922930 auto thisSleepUntil = std::min(sleepUntil, clock::now() + closestUntil);
923− std::this_thread::sleep_until(thisSleepUntil);
931+
932+ // if the sleep until is more than 1 ms away, then don't sleep, and instead use a wait condition
933+ // for any possible invoke methods that might also be pending.
934+ const std::chrono::microseconds MAX_USECS_TO_SLEEP(USECS_PER_MSEC);
935+ if (closestUntil < MAX_USECS_TO_SLEEP) {
936+ qDebug() << "about to sleep:" << closestUntil.count() << "usec";
937+ std::this_thread::sleep_until(thisSleepUntil);
938+ } else {
939+ // FIXME - we might want to make this 1 less ms to wait
940+ unsigned long maxWait = closestUntil.count() / USECS_PER_MSEC;
941+ qDebug() << "about to wait:" << maxWait << "ms --- closestUntil:" << closestUntil.count() << "usec";
942+ _waitingOnInvokeMethod.lock();
943+ bool signalled = _hasInvokeMethod.wait(&_waitingOnInvokeMethod, maxWait);
944+ _waitingOnInvokeMethod.unlock();
945+ if (signalled) {
946+ qDebug() << "SIGNALLED!! done waiting... now:" << usecTimestampNow();
947+ }
948+
949+ }
924950 }
925951
926952 #ifdef SCRIPT_DELAY_DEBUG
@@ -1058,6 +1084,7 @@ void ScriptEngine::stop(bool marshal) { · 131 unmodified lines
10581084
10591085 if (marshal) {
10601086 QMetaObject::invokeMethod(this, "stop");
1087+ _hasInvokeMethod.wakeAll();
10611088 return;
10621089 }
10631090 if (!_isFinished) {
@@ -1078,6 +1105,7 @@ void ScriptEngine::callAnimationStateHandler(QScriptValue callback, AnimVariantM · 14 unmodified lines
10781105 Q_ARG(QStringList, names),
10791106 Q_ARG(bool, useNames),
10801107 Q_ARG(AnimVariantResultHandler, resultHandler));
1108+ _hasInvokeMethod.wakeAll();
10811109 return;
10821110 }
10831111 QScriptValue javascriptParameters = parameters.animVariantMapToScriptValue(this, names, useNames);
@@ -1428,6 +1456,7 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 344 unmodified lines
14281456 Q_ARG(const QString&, contents),
14291457 Q_ARG(bool, isURL),
14301458 Q_ARG(bool, success));
1459+ _hasInvokeMethod.wakeAll();
14311460 return;
14321461 }
14331462
@@ -1526,6 +1555,7 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { · 92 unmodified lines
15261555
15271556 QMetaObject::invokeMethod(this, "unloadEntityScript",
15281557 Q_ARG(const EntityItemID&, entityID));
1558+ _hasInvokeMethod.wakeAll();
15291559 return;
15301560 }
15311561 #ifdef THREAD_DEBUGGING
@@ -1547,6 +1577,7 @@ void ScriptEngine::unloadAllEntityScripts() { · 15 unmodified lines
15471577 #endif
15481578
15491579 QMetaObject::invokeMethod(this, "unloadAllEntityScripts");
1580+ _hasInvokeMethod.wakeAll();
15501581 return;
15511582 }
15521583 #ifdef THREAD_DEBUGGING
@@ -1639,10 +1670,13 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 86 unmodified lines
16391670 "entityID:" << entityID << "methodName:" << methodName;
16401671 #endif
16411672
1673+ qDebug() << "about to invokeMethod callEntityScriptMethod() now:" << usecTimestampNow();
1674+
16421675 QMetaObject::invokeMethod(this, "callEntityScriptMethod",
16431676 Q_ARG(const EntityItemID&, entityID),
16441677 Q_ARG(const QString&, methodName),
16451678 Q_ARG(const QStringList&, params));
1679+ _hasInvokeMethod.wakeAll();
16461680 return;
16471681 }
16481682 #ifdef THREAD_DEBUGGING
@@ -1675,6 +1709,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 26 unmodified lines
16751709 Q_ARG(const EntityItemID&, entityID),
16761710 Q_ARG(const QString&, methodName),
16771711 Q_ARG(const PointerEvent&, event));
1712+ _hasInvokeMethod.wakeAll();
16781713 return;
16791714 }
16801715 #ifdef THREAD_DEBUGGING
@@ -1708,6 +1743,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 27 unmodified lines
17081743 Q_ARG(const QString&, methodName),
17091744 Q_ARG(const EntityItemID&, otherID),
17101745 Q_ARG(const Collision&, collision));
1746+ _hasInvokeMethod.wakeAll();
17111747 return;
17121748 }
17131749 #ifdef THREAD_DEBUGGING