History

switch to using QSharedPointer for _entitiesScriptEngine

Brad Hefta-Gaub committed 10 years ago

Commit 9db0fe01 parent a3221f34 files · +54 −23Browse 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+21−11
@@ -143,6 +143,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 142 unmodified lines
143 143
144ScriptEngine::~ScriptEngine() {144ScriptEngine::~ScriptEngine() {
145 qCDebug(scriptengine) << __FUNCTION__ << "--- BEGIN --- script:" << getFilename();145 qCDebug(scriptengine) << __FUNCTION__ << "--- BEGIN --- script:" << getFilename();
146 qCDebug(scriptengine) << __FUNCTION__ << "called on thread[" << QThread::currentThread() << "], object's thread [" << thread() << "] script:" << getFilename();
146 qCDebug(scriptengine) << "Script Engine shutting down (destructor) for script:" << getFilename();147 qCDebug(scriptengine) << "Script Engine shutting down (destructor) for script:" << getFilename();
147 148
148 auto scriptEngines = DependencyManager::get<ScriptEngines>();149 auto scriptEngines = DependencyManager::get<ScriptEngines>();
@@ -1051,7 +1052,10 @@ void ScriptEngine::forwardHandlerCall(const EntityItemID& entityID, const QStrin · 902 unmodified lines
1051 1052
1052// since all of these operations can be asynch we will always do the actual work in the response handler1053// since all of these operations can be asynch we will always do the actual work in the response handler
1053// for the download1054// for the download
1054void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {1055void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1056
1057
1058/*****
1055 if (QThread::currentThread() != thread()) {1059 if (QThread::currentThread() != thread()) {
1056#ifdef THREAD_DEBUGGING1060#ifdef THREAD_DEBUGGING
1057 qDebug() << "*** WARNING *** ScriptEngine::loadEntityScript() called on wrong thread ["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 "entityID:" << entityID << "entityScript:" << entityScript << "forceRedownload:" << forceRedownload;1074 "entityID:" << entityID << "entityScript:" << entityScript << "forceRedownload:" << forceRedownload;
1071#endif1075#endif
1072 1076
1073 // If we've been called our known entityScripts should not know about us..1077 // If we've been called our known entityScripts should not know about us..
1074 assert(!_entityScripts.contains(entityID));1078 assert(!_entityScripts.contains(entityID));
1075 1079
1076#ifdef THREAD_DEBUGGING1080#ifdef THREAD_DEBUGGING
1077 qDebug() << "ScriptEngine::loadEntityScript() calling scriptCache->getScriptContents() on thread ["1081 qDebug() << "ScriptEngine::loadEntityScript() calling scriptCache->getScriptContents() on thread ["
1078 << QThread::currentThread() << "] expected thread [" << thread() << "]";1082 << QThread::currentThread() << "] expected thread [" << thread() << "]";
1079#endif1083#endif
1084******/
1080 1085
1081 QPointer<ScriptEngine> theEngine(this);1086 // NOTE: If the script content is not currently in the caceh, The LAMBDA here, will be called on the Main Thread
1082 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 DependencyManager::get<ScriptCache>()->getScriptContents(entityScript, [theEngine, entityID](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {1090 DependencyManager::get<ScriptCache>()->getScriptContents(entityScript, [theEngine, entityID](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {
1084#ifdef THREAD_DEBUGGING1091 QSharedPointer<ScriptEngine> strongEngine = theEngine.toStrongRef();
1085 qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread ["1092 if (strongEngine) {
1086 << QThread::currentThread() << "] expected thread [" << thread() << "]";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#endif1097#endif
1088 1098
1089 if (!theEngine.isNull()) {1099
1090 qDebug() << "ScriptCache::getScriptContents() returned ScriptEngine still active calling ... entityScriptContentAvailable()";1100 strongEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success);
1091 theEngine->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success);
1092 } else {1101 } else {
1093 qDebug() << "ScriptCache::getScriptContents() returned after our ScriptEngine was deleted...";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// since all of these operations can be asynch we will always do the actual work in the response handler1107// since all of these operations can be asynch we will always do the actual work in the response handler
1099// for the download1108// for the download
1100void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {1109void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success) {
1110
1101 if (QThread::currentThread() != thread()) {1111 if (QThread::currentThread() != thread()) {
1102#ifdef THREAD_DEBUGGING1112#ifdef THREAD_DEBUGGING
1103 qDebug() << "*** WARNING *** ScriptEngine::entityScriptContentAvailable() called on wrong thread ["1113 qDebug() << "*** WARNING *** ScriptEngine::entityScriptContentAvailable() called on wrong thread ["