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
124 124
125ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNameString, bool wantSignals) :125ScriptEngine::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
142ScriptEngine::~ScriptEngine() {137ScriptEngine::~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 ourselves140 // 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 scripts141 // 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 them142 // 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
150void ScriptEngine::disconnectNonEssentialSignals() {
151 disconnect();
152 connect(this, &ScriptEngine::doneRunning, thread(), &QThread::quit);
153}
154
153void ScriptEngine::runInThread() {155void ScriptEngine::runInThread() {
156 _isThreaded = true;
154 QThread* workerThread = new QThread(); // thread is not owned, so we need to manage the delete157 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
176QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;179QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
177QMutex ScriptEngine::_allScriptsMutex;180QMutex ScriptEngine::_allScriptsMutex;
178bool ScriptEngine::_stoppingAllScripts = false;181bool ScriptEngine::_stoppingAllScripts = false;
179bool ScriptEngine::_doneRunningThisScript = false;
180 182
181void ScriptEngine::stopAllScripts(QObject* application) {183void 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't223 // 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 accessing224 // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
221 // any application state after we leave this stopAllScripts() method225 // 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 set230 // 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
233void ScriptEngine::waitTillDoneRunning() {240void ScriptEngine::waitTillDoneRunning() {
234 // If the script never started running or finished running before we got here, we don't need to wait for it241 // 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 to245 // 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 their246 // 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 threads249 // 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 why761// NOTE: This is private because it must be called on the same thread that created the timers, which is why