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
172172 qCDebug(scriptengine) << "isEntityServerScript() -- limiting maxRetries to 1";
173173 processLevelMaxRetries = 1;
174174 }
175− //qCDebug(scriptengine) << getContext() << "processLevelMaxRetries =" << processLevelMaxRetries << fileNameString;
176175
177176 // this is where all unhandled exceptions end up getting logged
178177 connect(this, &BaseScriptEngine::unhandledException, this, [this](const QScriptValue& err) {
@@ -1185,7 +1184,6 @@ void ScriptEngine::timerFired() { · 1006 unmodified lines
11851184 _timerFunctionMap.remove(callingTimer);
11861185 delete callingTimer;
11871186 }
1188− //qCWarning(scriptengine) << "timerFired" << timerData.function.toString();
11891187
11901188 // call the associated JS function, if it exists
11911189 if (timerData.function.isValid()) {
@@ -1500,18 +1498,14 @@ const auto RETRY_ATTEMPT_BATCH = 10; //glm::clamp(qgetenv("RETRY_ATTEMPT_BATCH") · 308 unmodified lines
15001498 const auto RETRY_ATTEMPT_MSECS = 250; //glm::clamp(qgetenv("RETRY_ATTEMPT_MSECS").toInt(), 50, 1000);
15011499
15021500 void 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−
15081501 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
15091503 _pendingLoadEntities.push_back({ entityID, entityScript/*, forceRedownload*/ });
15101504
1511− auto processDeferredEntities = [this](QScriptContext* ctx, QScriptEngine*) -> QScriptValue {
1512− //qCDebug(scriptengine) << "deferLoadEntityScript.retry #" << _pendingLoadEntities.size() << " pending loads | " << QThread::currentThread();
1505+ auto processDeferredEntities = [this](QScriptContext* context, QScriptEngine*) -> QScriptValue {
1506+ QList<DeferredLoadEntity> stillPending;
15131507
1514− for(int i=0; i < RETRY_ATTEMPT_BATCH && !_pendingLoadEntities.isEmpty(); i++) {
1508+ for (int i=0; i < RETRY_ATTEMPT_BATCH && !_pendingLoadEntities.isEmpty(); i++) {
15151509 auto retry = _pendingLoadEntities.takeFirst();
15161510 qCDebug(scriptengine) << "deferLoadEntityScript.retry[" << i << "]:" << retry.entityID << retry.entityScript;
15171511 if(!_entityScripts.contains(retry.entityID)) {
@@ -1521,12 +1515,21 @@ void ScriptEngine::deferLoadEntityScript(const EntityItemID& entityID, const QSt · 3 unmodified lines
15211515 if (details.status != EntityScriptStatus::PENDING) {
15221516 qCDebug(scriptengine) << "deferLoadEntityScript.retry -- entity status no longer PENDING; " << details.status;
15231517 } 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+ }
15251525 }
15261526 }
15271527 }
1528+ foreach(const DeferredLoadEntity& retry, stillPending) {
1529+ _pendingLoadEntities.push_back(retry);
1530+ }
15281531 if (_pendingLoadEntities.isEmpty()) {
1529− QObject *interval = ctx->callee().property("interval").toQObject();
1532+ QObject *interval = context->callee().property("interval").toQObject();
15301533 // we're done processing so can kill the timer
15311534 qCDebug(scriptengine) << "loadEntityScript.retry queue processing complete -- exiting" << interval;
15321535 if (interval) {
@@ -1610,7 +1613,9 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 77 unmodified lines
16101613 return;
16111614 }
16121615 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
16141619 if (!isStopping() && _entityScripts.contains(entityID)) {
16151620 entityScriptContentAvailable(entityID, url, contents, isURL, success, status);
16161621 } else {