Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all
Unified · Split
| @@ -573,6 +573,7 @@ void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) { · 572 unmodified lines | ||
| 573 | 573 | QMetaObject::invokeMethod(this, "registerValue", |
| 574 | 574 | Q_ARG(const QString&, valueName), |
| 575 | 575 | Q_ARG(QScriptValue, value)); |
| 576 | + _hasInvokeMethod.wakeAll(); | |
| 576 | 577 | return; |
| 577 | 578 | } |
| 578 | 579 | |
| @@ -603,6 +604,7 @@ void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { · 24 unmodified lines | ||
| 603 | 604 | QMetaObject::invokeMethod(this, "registerGlobalObject", |
| 604 | 605 | Q_ARG(const QString&, name), |
| 605 | 606 | Q_ARG(QObject*, object)); |
| 607 | + _hasInvokeMethod.wakeAll(); | |
| 606 | 608 | return; |
| 607 | 609 | } |
| 608 | 610 | #ifdef THREAD_DEBUGGING |
| @@ -628,6 +630,7 @@ void ScriptEngine::registerFunction(const QString& name, QScriptEngine::Function · 19 unmodified lines | ||
| 628 | 630 | Q_ARG(const QString&, name), |
| 629 | 631 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), |
| 630 | 632 | Q_ARG(int, numArguments)); |
| 633 | + _hasInvokeMethod.wakeAll(); | |
| 631 | 634 | return; |
| 632 | 635 | } |
| 633 | 636 | #ifdef THREAD_DEBUGGING |
| @@ -647,6 +650,7 @@ void ScriptEngine::registerFunction(const QString& parent, const QString& name, · 13 unmodified lines | ||
| 647 | 650 | Q_ARG(const QString&, name), |
| 648 | 651 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), |
| 649 | 652 | Q_ARG(int, numArguments)); |
| 653 | + _hasInvokeMethod.wakeAll(); | |
| 650 | 654 | return; |
| 651 | 655 | } |
| 652 | 656 | #ifdef THREAD_DEBUGGING |
| @@ -672,6 +676,7 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 19 unmodified lines | ||
| 672 | 676 | Q_ARG(QScriptEngine::FunctionSignature, getter), |
| 673 | 677 | Q_ARG(QScriptEngine::FunctionSignature, setter), |
| 674 | 678 | Q_ARG(const QString&, parent)); |
| 679 | + _hasInvokeMethod.wakeAll(); | |
| 675 | 680 | return; |
| 676 | 681 | } |
| 677 | 682 | #ifdef THREAD_DEBUGGING |
| @@ -704,6 +709,7 @@ void ScriptEngine::removeEventHandler(const EntityItemID& entityID, const QStrin · 26 unmodified lines | ||
| 704 | 709 | Q_ARG(const EntityItemID&, entityID), |
| 705 | 710 | Q_ARG(const QString&, eventName), |
| 706 | 711 | Q_ARG(QScriptValue, handler)); |
| 712 | + _hasInvokeMethod.wakeAll(); | |
| 707 | 713 | return; |
| 708 | 714 | } |
| 709 | 715 | #ifdef THREAD_DEBUGGING |
| @@ -735,6 +741,7 @@ void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& · 25 unmodified lines | ||
| 735 | 741 | Q_ARG(const EntityItemID&, entityID), |
| 736 | 742 | Q_ARG(const QString&, eventName), |
| 737 | 743 | Q_ARG(QScriptValue, handler)); |
| 744 | + _hasInvokeMethod.wakeAll(); | |
| 738 | 745 | return; |
| 739 | 746 | } |
| 740 | 747 | #ifdef THREAD_DEBUGGING |
| @@ -821,6 +828,7 @@ QScriptValue ScriptEngine::evaluate(const QString& sourceCode, const QString& fi · 80 unmodified lines | ||
| 821 | 828 | Q_ARG(const QString&, sourceCode), |
| 822 | 829 | Q_ARG(const QString&, fileName), |
| 823 | 830 | Q_ARG(int, lineNumber)); |
| 831 | + _hasInvokeMethod.wakeAll(); | |
| 824 | 832 | return result; |
| 825 | 833 | } |
| 826 | 834 | |
| @@ -920,7 +928,25 @@ void ScriptEngine::run() { · 93 unmodified lines | ||
| 920 | 928 | auto remainingSleepUntil = std::chrono::duration_cast<std::chrono::microseconds>(sleepUntil - clock::now()); |
| 921 | 929 | auto closestUntil = std::min(remainingSleepUntil, untilTimer); |
| 922 | 930 | 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 | + } | |
| 924 | 950 | } |
| 925 | 951 | |
| 926 | 952 | #ifdef SCRIPT_DELAY_DEBUG |
| @@ -1058,6 +1084,7 @@ void ScriptEngine::stop(bool marshal) { · 131 unmodified lines | ||
| 1058 | 1084 | |
| 1059 | 1085 | if (marshal) { |
| 1060 | 1086 | QMetaObject::invokeMethod(this, "stop"); |
| 1087 | + _hasInvokeMethod.wakeAll(); | |
| 1061 | 1088 | return; |
| 1062 | 1089 | } |
| 1063 | 1090 | if (!_isFinished) { |
| @@ -1078,6 +1105,7 @@ void ScriptEngine::callAnimationStateHandler(QScriptValue callback, AnimVariantM · 14 unmodified lines | ||
| 1078 | 1105 | Q_ARG(QStringList, names), |
| 1079 | 1106 | Q_ARG(bool, useNames), |
| 1080 | 1107 | Q_ARG(AnimVariantResultHandler, resultHandler)); |
| 1108 | + _hasInvokeMethod.wakeAll(); | |
| 1081 | 1109 | return; |
| 1082 | 1110 | } |
| 1083 | 1111 | QScriptValue javascriptParameters = parameters.animVariantMapToScriptValue(this, names, useNames); |
| @@ -1428,6 +1456,7 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 344 unmodified lines | ||
| 1428 | 1456 | Q_ARG(const QString&, contents), |
| 1429 | 1457 | Q_ARG(bool, isURL), |
| 1430 | 1458 | Q_ARG(bool, success)); |
| 1459 | + _hasInvokeMethod.wakeAll(); | |
| 1431 | 1460 | return; |
| 1432 | 1461 | } |
| 1433 | 1462 | |
| @@ -1526,6 +1555,7 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { · 92 unmodified lines | ||
| 1526 | 1555 | |
| 1527 | 1556 | QMetaObject::invokeMethod(this, "unloadEntityScript", |
| 1528 | 1557 | Q_ARG(const EntityItemID&, entityID)); |
| 1558 | + _hasInvokeMethod.wakeAll(); | |
| 1529 | 1559 | return; |
| 1530 | 1560 | } |
| 1531 | 1561 | #ifdef THREAD_DEBUGGING |
| @@ -1547,6 +1577,7 @@ void ScriptEngine::unloadAllEntityScripts() { · 15 unmodified lines | ||
| 1547 | 1577 | #endif |
| 1548 | 1578 | |
| 1549 | 1579 | QMetaObject::invokeMethod(this, "unloadAllEntityScripts"); |
| 1580 | + _hasInvokeMethod.wakeAll(); | |
| 1550 | 1581 | return; |
| 1551 | 1582 | } |
| 1552 | 1583 | #ifdef THREAD_DEBUGGING |
| @@ -1639,10 +1670,13 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 86 unmodified lines | ||
| 1639 | 1670 | "entityID:" << entityID << "methodName:" << methodName; |
| 1640 | 1671 | #endif |
| 1641 | 1672 | |
| 1673 | + qDebug() << "about to invokeMethod callEntityScriptMethod() now:" << usecTimestampNow(); | |
| 1674 | + | |
| 1642 | 1675 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", |
| 1643 | 1676 | Q_ARG(const EntityItemID&, entityID), |
| 1644 | 1677 | Q_ARG(const QString&, methodName), |
| 1645 | 1678 | Q_ARG(const QStringList&, params)); |
| 1679 | + _hasInvokeMethod.wakeAll(); | |
| 1646 | 1680 | return; |
| 1647 | 1681 | } |
| 1648 | 1682 | #ifdef THREAD_DEBUGGING |
| @@ -1675,6 +1709,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 26 unmodified lines | ||
| 1675 | 1709 | Q_ARG(const EntityItemID&, entityID), |
| 1676 | 1710 | Q_ARG(const QString&, methodName), |
| 1677 | 1711 | Q_ARG(const PointerEvent&, event)); |
| 1712 | + _hasInvokeMethod.wakeAll(); | |
| 1678 | 1713 | return; |
| 1679 | 1714 | } |
| 1680 | 1715 | #ifdef THREAD_DEBUGGING |
| @@ -1708,6 +1743,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 27 unmodified lines | ||
| 1708 | 1743 | Q_ARG(const QString&, methodName), |
| 1709 | 1744 | Q_ARG(const EntityItemID&, otherID), |
| 1710 | 1745 | Q_ARG(const Collision&, collision)); |
| 1746 | + _hasInvokeMethod.wakeAll(); | |
| 1711 | 1747 | return; |
| 1712 | 1748 | } |
| 1713 | 1749 | #ifdef THREAD_DEBUGGING |