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