History

change the ScriptEngine::waitTillDoneRunning() to wait for the script thread to complete

Brad Hefta-Gaub committed 10 years ago

Commit ce46c301 parent 5f2944c3 files · +26 −24Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+15−13
@@ -124,14 +124,9 @@ static bool hadUncaughtExceptions(QScriptEngine& engine, const QString& fileName · 123 unmodified lines
124124
125125 ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, bool wantSignals) :
126126 _scriptContents(scriptContents),
127− _isFinished(false),
128− _isRunning(false),
129− _isInitialized(false),
130127 _timerFunctionMap(),
131128 _wantSignals(wantSignals),
132129 _fileNameString(fileNameString),
133− _isUserLoaded(false),
134− _isReloading(false),
135130 _arrayBufferClass(new ArrayBufferClass(this))
136131 {
137132 _allScriptsMutex.lock();
@@ -140,6 +135,8 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 2 unmodified lines
140135 }
141136
142137 ScriptEngine::~ScriptEngine() {
138+ qDebug() << "Script Engine shutting down (destructor) for script:" << getFilename();
139+
143140 // If we're not already in the middle of stopping all scripts, then we should remove ourselves
144141 // from the list of running scripts. We don't do this if we're in the process of stopping all scripts
145142 // because that method removes scripts from its list as it iterates them
@@ -150,7 +147,13 @@ ScriptEngine::~ScriptEngine() { · 4 unmodified lines
150147 }
151148 }
152149
150+void ScriptEngine::disconnectNonEssentialSignals() {
151+ disconnect();
152+ connect(this, &ScriptEngine::doneRunning, thread(), &QThread::quit);
153+}
154+
153155 void ScriptEngine::runInThread() {
156+ _isThreaded = true;
154157 QThread* workerThread = new QThread(); // thread is not owned, so we need to manage the delete
155158 QString scriptEngineName = QString("Script Thread:") + getFilename();
156159 workerThread->setObjectName(scriptEngineName);
@@ -176,12 +179,13 @@ void ScriptEngine::runInThread() { · 19 unmodified lines
176179 QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
177180 QMutex ScriptEngine::_allScriptsMutex;
178181 bool ScriptEngine::_stoppingAllScripts = false;
179−bool ScriptEngine::_doneRunningThisScript = false;
180182
181183 void ScriptEngine::stopAllScripts(QObject* application) {
182184 _allScriptsMutex.lock();
183185 _stoppingAllScripts = true;
184186
187+ qDebug() << "Stopping all scripts.... currently known scripts:" << _allKnownScriptEngines.size();
188+
185189 QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines);
186190 while (i.hasNext()) {
187191 ScriptEngine* scriptEngine = i.next();
@@ -219,7 +223,9 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 31 unmodified lines
219223 // We need to wait for the engine to be done running before we proceed, because we don't
220224 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
221225 // any application state after we leave this stopAllScripts() method
226+ qDebug() << "waiting on script:" << scriptName;
222227 scriptEngine->waitTillDoneRunning();
228+ qDebug() << "done waiting on script:" << scriptName;
223229
224230 // If the script is stopped, we can remove it from our set
225231 i.remove();
@@ -227,21 +233,19 @@ void ScriptEngine::stopAllScripts(QObject* application) { · 1 unmodified line
227233 }
228234 _stoppingAllScripts = false;
229235 _allScriptsMutex.unlock();
236+ qDebug() << "DONE Stopping all scripts....";
230237 }
231238
232239
233240 void ScriptEngine::waitTillDoneRunning() {
234241 // 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) {
238243
239244 // NOTE: waitTillDoneRunning() will be called on the main Application thread, inside of stopAllScripts()
240245 // we want the application thread to continue to process events, because the scripts will likely need to
241246 // marshall messages across to the main thread. For example if they access Settings or Meny in any of their
242247 // shutdown code.
243− while (!_doneRunningThisScript) {
244−
248+ while (thread()->isRunning()) {
245249 // process events for the main application thread, allowing invokeMethod calls to pass between threads
246250 QCoreApplication::processEvents();
247251 }
@@ -752,8 +756,6 @@ void ScriptEngine::run() { · 504 unmodified lines
752756 emit runningStateChanged();
753757 emit doneRunning();
754758 }
755−
756− _doneRunningThisScript = true;
757759 }
758760
759761 // NOTE: This is private because it must be called on the same thread that created the timers, which is why