History

more work on improving shutdown behavior

ZappoMan committed 11 years ago

Commit 6be8f4c1 parent 7e2d3554 files · +85 −11Browse 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+70−9
@@ -94,33 +94,78 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 93 unmodified lines
9494 _isUserLoaded(false),
9595 _arrayBufferClass(new ArrayBufferClass(this))
9696 {
97+ qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------";
98+ _allScriptsMutex.lock();
9799 _allKnownScriptEngines.insert(this);
100+ _allScriptsMutex.unlock();
101+ qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << " -------- DONE -----------";
98102 }
99103
100104 ScriptEngine::~ScriptEngine() {
101− _allKnownScriptEngines.remove(this);
105+ qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------";
106+ if (!_stoppingAllScripts) {
107+ _allScriptsMutex.lock();
108+ qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " removing from list of scripts";
109+ _allKnownScriptEngines.remove(this);
110+ _allScriptsMutex.unlock();
111+ } else {
112+ qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " NOT removing from list of scripts here...";
113+ }
114+ qDebug() << "ScriptEngine::~ScriptEngine() " << getFilename() << " -------- DONE -----------";
102115 }
103116
104117 QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
118+QMutex ScriptEngine::_allScriptsMutex;
119+bool ScriptEngine::_stoppingAllScripts = false;
105120
106121 void ScriptEngine::stopAllScripts(QObject* application) {
122+
123+ qDebug() << "ScriptEngine::stopAllScripts() -------- BEGIN -----------";
124+ _allScriptsMutex.lock();
125+ _stoppingAllScripts = true;
126+
107127 QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines);
108128 while (i.hasNext()) {
109129 ScriptEngine* scriptEngine = i.next();
110130
131+ qDebug() << "ScriptEngine::stopAllScripts() scriptEngine: " << (void*)scriptEngine;
132+
133+ QString scriptName = scriptEngine->getFilename();
134+
135+ qDebug() << "ScriptEngine::stopAllScripts() considering script: " << scriptName << "evaluatePending:" << scriptEngine->evaluatePending();
136+
111137 // NOTE: typically all script engines are running. But there's at least one known exception to this, the
112138 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
113139 // them. We don't need to stop scripts that aren't running.
114140 if (scriptEngine->isRunning()) {
141+ if (scriptEngine->evaluatePending()) {
142+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "waiting for evaluation to complete....";
143+ QEventLoop loop;
144+ QObject::connect(scriptEngine, &ScriptEngine::evaluationFinished, &loop, &QEventLoop::quit);
145+ loop.exec();
146+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__ << "evaluation complete....";
147+ }
148+
149+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
150+
115151 QEventLoop loop;
116152 QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);
117−
153+
154+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
118155 scriptEngine->disconnect(application);
156+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
119157 scriptEngine->stop();
120−
158+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
121159 loop.exec();
160+ qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
161+ i.remove();
162+ } else {
163+ qDebug() << "ScriptEngine::stopAllScripts() script: " << scriptName << " was not running... skipping it's stop() call.";
122164 }
123165 }
166+ _stoppingAllScripts = false;
167+ _allScriptsMutex.unlock();
168+ qDebug() << "ScriptEngine::stopAllScripts() -------- DONE -----------";
124169 }
125170
126171 QString ScriptEngine::getFilename() const {
@@ -347,6 +392,8 @@ void ScriptEngine::evaluate() { · 220 unmodified lines
347392 }
348393
349394 QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {
395+ qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;
396+ _evaluatePending = true;
350397 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
351398 if (hasUncaughtException()) {
352399 int line = uncaughtExceptionLineNumber();
@@ -354,6 +401,8 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 1 unmodified line
354401 }
355402 emit evaluationFinished(result, hasUncaughtException());
356403 clearExceptions();
404+ _evaluatePending = false;
405+ qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;
357406 return result;
358407 }
359408
@@ -370,30 +419,36 @@ void ScriptEngine::sendAvatarBillboardPacket() { · 10 unmodified lines
370419 }
371420
372421 void ScriptEngine::run() {
422+ qDebug() << "ScriptEngine::run() " << getFilename() << " -------- BEGIN -----------";
423+
373424 if (!_isInitialized) {
425+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
374426 init();
427+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
375428 }
429+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
376430 _isRunning = true;
377431 _isFinished = false;
432+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
378433 emit runningStateChanged();
434+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
379435
436+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
380437 QScriptValue result = evaluate(_scriptContents);
381− if (hasUncaughtException()) {
382− int line = uncaughtExceptionLineNumber();
383− qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString();
384− emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString());
385− clearExceptions();
386− }
438+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
387439
440+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
388441 QElapsedTimer startTime;
389442 startTime.start();
390443
391444 int thisFrame = 0;
392445
446+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
393447 auto nodeList = DependencyManager::get<NodeList>();
394448
395449 qint64 lastUpdate = usecTimestampNow();
396450
451+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
397452 while (!_isFinished) {
398453 int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec
399454 if (usecToSleep > 0) {
@@ -535,6 +590,7 @@ void ScriptEngine::run() { · 135 unmodified lines
535590 }
536591 lastUpdate = now;
537592 }
593+ qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
538594
539595 stopAllTimers(); // make sure all our timers are stopped if the script is ending
540596
@@ -565,6 +621,7 @@ void ScriptEngine::run() { · 24 unmodified lines
565621 emit runningStateChanged();
566622
567623 emit doneRunning();
624+ qDebug() << "ScriptEngine::run() " << getFilename() << " -------- DONE -----------" << " line:" << __LINE__;
568625 }
569626
570627 // NOTE: This is private because it must be called on the same thread that created the timers, which is why
@@ -579,6 +636,7 @@ void ScriptEngine::stopAllTimers() { · 8 unmodified lines
579636 }
580637
581638 void ScriptEngine::stop() {
639+ qDebug() << "ScriptEngine::stop() " << getFilename() << "[" << ((void*)this) << "]";
582640 _isFinished = true;
583641 emit runningStateChanged();
584642 }
@@ -710,8 +768,11 @@ void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { · 125 unmodified lines
710768 }
711769
712770 void ScriptEngine::load(const QString& loadFile) {
771+ qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
713772 QUrl url = resolvePath(loadFile);
773+ qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
714774 emit loadScript(url.toString(), false);
775+ qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
715776 }
716777
717778 void ScriptEngine::nodeKilled(SharedNodePointer node) {