Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all
Unified · Split
| @@ -143,6 +143,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 142 unmodified lines | ||
| 143 | 143 | |
| 144 | 144 | ScriptEngine::~ScriptEngine() { |
| 145 | 145 | qCDebug(scriptengine) << __FUNCTION__ << "--- BEGIN --- script:" << getFilename(); |
| 146 | + qCDebug(scriptengine) << __FUNCTION__ << "called on thread[" << QThread::currentThread() << "], object's thread [" << thread() << "] script:" << getFilename(); | |
| 146 | 147 | qCDebug(scriptengine) << "Script Engine shutting down (destructor) for script:" << getFilename(); |
| 147 | 148 | |
| 148 | 149 | auto scriptEngines = DependencyManager::get<ScriptEngines>(); |
| @@ -1051,7 +1052,10 @@ void ScriptEngine::forwardHandlerCall(const EntityItemID& entityID, const QStrin · 902 unmodified lines | ||
| 1051 | 1052 | |
| 1052 | 1053 | // since all of these operations can be asynch we will always do the actual work in the response handler |
| 1053 | 1054 | // for the download |
| 1054 | −void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { | |
| 1055 | +void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { | |
| 1056 | + | |
| 1057 | + | |
| 1058 | +/***** | |
| 1055 | 1059 | if (QThread::currentThread() != thread()) { |
| 1056 | 1060 | #ifdef THREAD_DEBUGGING |
| 1057 | 1061 | qDebug() << "*** WARNING *** ScriptEngine::loadEntityScript() called on wrong thread [" |
| @@ -1070,25 +1074,30 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 12 unmodified lines | ||
| 1070 | 1074 | "entityID:" << entityID << "entityScript:" << entityScript << "forceRedownload:" << forceRedownload; |
| 1071 | 1075 | #endif |
| 1072 | 1076 | |
| 1073 | − // If we've been called our known entityScripts should not know about us.. | |
| 1074 | − assert(!_entityScripts.contains(entityID)); | |
| 1077 | + // If we've been called our known entityScripts should not know about us.. | |
| 1078 | + assert(!_entityScripts.contains(entityID)); | |
| 1075 | 1079 | |
| 1076 | 1080 | #ifdef THREAD_DEBUGGING |
| 1077 | 1081 | qDebug() << "ScriptEngine::loadEntityScript() calling scriptCache->getScriptContents() on thread [" |
| 1078 | 1082 | << QThread::currentThread() << "] expected thread [" << thread() << "]"; |
| 1079 | 1083 | #endif |
| 1084 | +******/ | |
| 1080 | 1085 | |
| 1081 | − QPointer<ScriptEngine> theEngine(this); | |
| 1082 | − | |
| 1086 | + // NOTE: If the script content is not currently in the caceh, The LAMBDA here, will be called on the Main Thread | |
| 1087 | + // which means we're guarenteed that it's not the correct thread for the ScriptEngine. This means | |
| 1088 | + // when we get into entityScriptContentAvailable() we will likely invokeMethod() to get it over | |
| 1089 | + // to the "Entities" ScriptEngine thread. | |
| 1083 | 1090 | DependencyManager::get<ScriptCache>()->getScriptContents(entityScript, [theEngine, entityID](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) { |
| 1084 | −#ifdef THREAD_DEBUGGING | |
| 1085 | − qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread [" | |
| 1086 | − << QThread::currentThread() << "] expected thread [" << thread() << "]"; | |
| 1091 | + QSharedPointer<ScriptEngine> strongEngine = theEngine.toStrongRef(); | |
| 1092 | + if (strongEngine) { | |
| 1093 | + qDebug() << "ScriptCache::getScriptContents() returned ScriptEngine still active calling ... entityScriptContentAvailable()"; | |
| 1094 | +#if 1//def THREAD_DEBUGGING | |
| 1095 | + qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread [" | |
| 1096 | + << QThread::currentThread() << "] expected thread [" << strongEngine->thread() << "]"; | |
| 1087 | 1097 | #endif |
| 1088 | 1098 | |
| 1089 | − if (!theEngine.isNull()) { | |
| 1090 | − qDebug() << "ScriptCache::getScriptContents() returned ScriptEngine still active calling ... entityScriptContentAvailable()"; | |
| 1091 | − theEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success); | |
| 1099 | + | |
| 1100 | + strongEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success); | |
| 1092 | 1101 | } else { |
| 1093 | 1102 | qDebug() << "ScriptCache::getScriptContents() returned after our ScriptEngine was deleted..."; |
| 1094 | 1103 | } |
| @@ -1098,6 +1107,7 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 3 unmodified lines | ||
| 1098 | 1107 | // since all of these operations can be asynch we will always do the actual work in the response handler |
| 1099 | 1108 | // for the download |
| 1100 | 1109 | void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success) { |
| 1110 | + | |
| 1101 | 1111 | if (QThread::currentThread() != thread()) { |
| 1102 | 1112 | #ifdef THREAD_DEBUGGING |
| 1103 | 1113 | qDebug() << "*** WARNING *** ScriptEngine::entityScriptContentAvailable() called on wrong thread [" |