Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all
Unified · Split
| @@ -124,14 +124,9 @@ static bool hadUncaughtExceptions(QScriptEngine& engine, const QString& fileName · 123 unmodified lines | ||
| 124 | 124 | |
| 125 | 125 | ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, bool wantSignals) : |
| 126 | 126 | _scriptContents(scriptContents), |
| 127 | − _isFinished(false), | |
| 128 | − _isRunning(false), | |
| 129 | − _isInitialized(false), | |
| 130 | 127 | _timerFunctionMap(), |
| 131 | 128 | _wantSignals(wantSignals), |
| 132 | 129 | _fileNameString(fileNameString), |
| 133 | − _isUserLoaded(false), | |
| 134 | − _isReloading(false), | |
| 135 | 130 | _arrayBufferClass(new ArrayBufferClass(this)) |
| 136 | 131 | { |
| 137 | 132 | _allScriptsMutex.lock(); |
| @@ -140,6 +135,8 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 2 unmodified lines | ||
| 140 | 135 | } |
| 141 | 136 | |
| 142 | 137 | ScriptEngine::~ScriptEngine() { |
| 138 | + qDebug() << "Script Engine shutting down (destructor) for script:" << getFilename(); | |
| 139 | + | |
| 143 | 140 | // If we're not already in the middle of stopping all scripts, then we should remove ourselves |
| 144 | 141 | // from the list of running scripts. We don't do this if we're in the process of stopping all scripts |
| 145 | 142 | // because that method removes scripts from its list as it iterates them |
| @@ -150,7 +147,13 @@ ScriptEngine::~ScriptEngine() { · 4 unmodified lines | ||
| 150 | 147 | } |
| 151 | 148 | } |
| 152 | 149 | |
| 150 | +void ScriptEngine::disconnectNonEssentialSignals() { | |
| 151 | + disconnect(); | |
| 152 | + connect(this, &ScriptEngine::doneRunning, thread(), &QThread::quit); | |
| 153 | +} | |
| 154 | + | |
| 153 | 155 | void ScriptEngine::runInThread() { |
| 156 | + _isThreaded = true; | |
| 154 | 157 | QThread* workerThread = new QThread(); // thread is not owned, so we need to manage the delete |
| 155 | 158 | QString scriptEngineName = QString("Script Thread:") + getFilename(); |
| 156 | 159 | workerThread->setObjectName(scriptEngineName); |
| @@ -176,12 +179,13 @@ void ScriptEngine::runInThread() { · 19 unmodified lines | ||
| 176 | 179 | QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines; |
| 177 | 180 | QMutex ScriptEngine::_allScriptsMutex; |
| 178 | 181 | bool ScriptEngine::_stoppingAllScripts = false; |
| 179 | −bool ScriptEngine::_doneRunningThisScript = false; | |
| 180 | 182 | |
| 181 | 183 | void ScriptEngine::stopAllScripts(QObject* application) { |
| 182 | 184 | _allScriptsMutex.lock(); |
| 183 | 185 | _stoppingAllScripts = true; |
| 184 | 186 | |
| 187 | + qDebug() << "Stopping all scripts.... currently known scripts:" << _allKnownScriptEngines.size(); | |
| 188 | + | |
| 185 | 189 | QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines); |
| 186 | 190 | while (i.hasNext()) { |
| 187 | 191 | ScriptEngine* scriptEngine = i.next(); |
| @@ -219,7 +223,9 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 31 unmodified lines | ||
| 219 | 223 | // We need to wait for the engine to be done running before we proceed, because we don't |
| 220 | 224 | // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing |
| 221 | 225 | // any application state after we leave this stopAllScripts() method |
| 226 | + qDebug() << "waiting on script:" << scriptName; | |
| 222 | 227 | scriptEngine->waitTillDoneRunning(); |
| 228 | + qDebug() << "done waiting on script:" << scriptName; | |
| 223 | 229 | |
| 224 | 230 | // If the script is stopped, we can remove it from our set |
| 225 | 231 | i.remove(); |
| @@ -227,21 +233,19 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 1 unmodified line | ||
| 227 | 233 | } |
| 228 | 234 | _stoppingAllScripts = false; |
| 229 | 235 | _allScriptsMutex.unlock(); |
| 236 | + qDebug() << "DONE Stopping all scripts...."; | |
| 230 | 237 | } |
| 231 | 238 | |
| 232 | 239 | |
| 233 | 240 | void ScriptEngine::waitTillDoneRunning() { |
| 234 | 241 | // If the script never started running or finished running before we got here, we don't need to wait for it |
| 235 | − if (_isRunning) { | |
| 236 | − | |
| 237 | − _doneRunningThisScript = false; // NOTE: this is static, we serialize our waiting for scripts to finish | |
| 242 | + if (_isRunning && _isThreaded) { | |
| 238 | 243 | |
| 239 | 244 | // NOTE: waitTillDoneRunning() will be called on the main Application thread, inside of stopAllScripts() |
| 240 | 245 | // we want the application thread to continue to process events, because the scripts will likely need to |
| 241 | 246 | // marshall messages across to the main thread. For example if they access Settings or Meny in any of their |
| 242 | 247 | // shutdown code. |
| 243 | − while (!_doneRunningThisScript) { | |
| 244 | − | |
| 248 | + while (thread()->isRunning()) { | |
| 245 | 249 | // process events for the main application thread, allowing invokeMethod calls to pass between threads |
| 246 | 250 | QCoreApplication::processEvents(); |
| 247 | 251 | } |
| @@ -752,8 +756,6 @@ void ScriptEngine::run() { · 504 unmodified lines | ||
| 752 | 756 | emit runningStateChanged(); |
| 753 | 757 | emit doneRunning(); |
| 754 | 758 | } |
| 755 | − | |
| 756 | − _doneRunningThisScript = true; | |
| 757 | 759 | } |
| 758 | 760 | |
| 759 | 761 | // NOTE: This is private because it must be called on the same thread that created the timers, which is why |