History

* Update per CR feedback / code standards * Cull log spam * Add tryLock to deferred processor to preempt unnecessary loadEntityContent round-trips

humbletim committed 9 years ago

Commit 417587d1 parent b1595625 files · +32 −27Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+18−13
@@ -172,7 +172,6 @@ ScriptEngine::ScriptEngine(Context context, const QString& scriptContents, const · 171 unmodified lines
172 qCDebug(scriptengine) << "isEntityServerScript() -- limiting maxRetries to 1";172 qCDebug(scriptengine) << "isEntityServerScript() -- limiting maxRetries to 1";
173 processLevelMaxRetries = 1;173 processLevelMaxRetries = 1;
174 }174 }
175 //qCDebug(scriptengine) << getContext() << "processLevelMaxRetries =" << processLevelMaxRetries << fileNameString;
176 175
177 // this is where all unhandled exceptions end up getting logged176 // this is where all unhandled exceptions end up getting logged
178 connect(this, &BaseScriptEngine::unhandledException, this, [this](const QScriptValue& err) {177 connect(this, &BaseScriptEngine::unhandledException, this, [this](const QScriptValue& err) {
@@ -1185,7 +1184,6 @@ void ScriptEngine::timerFired() { · 1006 unmodified lines
1185 _timerFunctionMap.remove(callingTimer);1184 _timerFunctionMap.remove(callingTimer);
1186 delete callingTimer;1185 delete callingTimer;
1187 }1186 }
1188 //qCWarning(scriptengine) << "timerFired" << timerData.function.toString();
1189 1187
1190 // call the associated JS function, if it exists1188 // call the associated JS function, if it exists
1191 if (timerData.function.isValid()) {1189 if (timerData.function.isValid()) {
@@ -1500,18 +1498,14 @@ const auto RETRY_ATTEMPT_BATCH = 10; //glm::clamp(qgetenv("RETRY_ATTEMPT_BATCH") · 308 unmodified lines
1500const auto RETRY_ATTEMPT_MSECS = 250; //glm::clamp(qgetenv("RETRY_ATTEMPT_MSECS").toInt(), 50, 1000);1498const auto RETRY_ATTEMPT_MSECS = 250; //glm::clamp(qgetenv("RETRY_ATTEMPT_MSECS").toInt(), 50, 1000);
1501 1499
1502void ScriptEngine::deferLoadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {1500void ScriptEngine::deferLoadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1503 // forceRedownload doesn't make sense yet in this scenario (which only happens when another entity has taken lead on entityScript)
1504 if (forceRedownload) {
1505 qCDebug(scriptengine) << "deferLoadEntityScript -- TODO: ignoring forceRedownload =" << forceRedownload;
1506 }
1507
1508 const auto needTimer = _pendingLoadEntities.isEmpty();1501 const auto needTimer = _pendingLoadEntities.isEmpty();
1502 // NOTE: forceRedownload isn't forwarded here because deferLoadEntityScript() is only ever called when another entity has taken lead for the script
1509 _pendingLoadEntities.push_back({ entityID, entityScript/*, forceRedownload*/ });1503 _pendingLoadEntities.push_back({ entityID, entityScript/*, forceRedownload*/ });
1510 1504
1511 auto processDeferredEntities = [this](QScriptContext* ctx, QScriptEngine*) -> QScriptValue {1505 auto processDeferredEntities = [this](QScriptContext* context, QScriptEngine*) -> QScriptValue {
1512 //qCDebug(scriptengine) << "deferLoadEntityScript.retry #" << _pendingLoadEntities.size() << " pending loads | " << QThread::currentThread();1506 QList<DeferredLoadEntity> stillPending;
1513 1507
1514 for(int i=0; i < RETRY_ATTEMPT_BATCH && !_pendingLoadEntities.isEmpty(); i++) {1508 for (int i=0; i < RETRY_ATTEMPT_BATCH && !_pendingLoadEntities.isEmpty(); i++) {
1515 auto retry = _pendingLoadEntities.takeFirst();1509 auto retry = _pendingLoadEntities.takeFirst();
1516 qCDebug(scriptengine) << "deferLoadEntityScript.retry[" << i << "]:" << retry.entityID << retry.entityScript;1510 qCDebug(scriptengine) << "deferLoadEntityScript.retry[" << i << "]:" << retry.entityID << retry.entityScript;
1517 if(!_entityScripts.contains(retry.entityID)) {1511 if(!_entityScripts.contains(retry.entityID)) {
@@ -1521,12 +1515,21 @@ void ScriptEngine::deferLoadEntityScript(const EntityItemID& entityID, const QSt · 3 unmodified lines
1521 if (details.status != EntityScriptStatus::PENDING) {1515 if (details.status != EntityScriptStatus::PENDING) {
1522 qCDebug(scriptengine) << "deferLoadEntityScript.retry -- entity status no longer PENDING; " << details.status;1516 qCDebug(scriptengine) << "deferLoadEntityScript.retry -- entity status no longer PENDING; " << details.status;
1523 } else {1517 } else {
1524 loadEntityScript(retry.entityID, retry.entityScript, false);1518 auto &locker = _lockPerUniqueScriptURL[retry.entityScript];
1519 if (locker.tryLock()) {
1520 locker.unlock();
1521 loadEntityScript(retry.entityID, retry.entityScript, false);
1522 } else {
1523 stillPending << retry;
1524 }
1525 }1525 }
1526 }1526 }
1527 }1527 }
1528 foreach(const DeferredLoadEntity& retry, stillPending) {
1529 _pendingLoadEntities.push_back(retry);
1530 }
1528 if (_pendingLoadEntities.isEmpty()) {1531 if (_pendingLoadEntities.isEmpty()) {
1529 QObject *interval = ctx->callee().property("interval").toQObject();1532 QObject *interval = context->callee().property("interval").toQObject();
1530 // we're done processing so can kill the timer1533 // we're done processing so can kill the timer
1531 qCDebug(scriptengine) << "loadEntityScript.retry queue processing complete -- exiting" << interval;1534 qCDebug(scriptengine) << "loadEntityScript.retry queue processing complete -- exiting" << interval;
1532 if (interval) {1535 if (interval) {
@@ -1610,7 +1613,9 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 77 unmodified lines
1610 return;1613 return;
1611 }1614 }
1612 executeOnScriptThread([=, &locker]{1615 executeOnScriptThread([=, &locker]{
1613 //qCDebug(scriptengine) << "loadEntityScript.contentAvailable" << status << QUrl(url).fileName() << entityID.toString();1616#ifdef DEBUG_ENTITY_STATES
1617 qCDebug(scriptengine) << "loadEntityScript.contentAvailable" << status << QUrl(url).fileName() << entityID.toString();
1618#endif
1614 if (!isStopping() && _entityScripts.contains(entityID)) {1619 if (!isStopping() && _entityScripts.contains(entityID)) {
1615 entityScriptContentAvailable(entityID, url, contents, isURL, success, status);1620 entityScriptContentAvailable(entityID, url, contents, isURL, success, status);
1616 } else {1621 } else {