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
94 _isUserLoaded(false),94 _isUserLoaded(false),
95 _arrayBufferClass(new ArrayBufferClass(this))95 _arrayBufferClass(new ArrayBufferClass(this))
96{96{
97 qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << "[" << ((void*)this) << "]" << " -------- BEGIN -----------";
98 _allScriptsMutex.lock();
97 _allKnownScriptEngines.insert(this);99 _allKnownScriptEngines.insert(this);
100 _allScriptsMutex.unlock();
101 qDebug() << "ScriptEngine::ScriptEngine() " << getFilename() << " -------- DONE -----------";
98}102}
99 103
100ScriptEngine::~ScriptEngine() {104ScriptEngine::~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 -----------";
102}115}
103 116
104QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;117QSet<ScriptEngine*> ScriptEngine::_allKnownScriptEngines;
118QMutex ScriptEngine::_allScriptsMutex;
119bool ScriptEngine::_stoppingAllScripts = false;
105 120
106void ScriptEngine::stopAllScripts(QObject* application) {121void ScriptEngine::stopAllScripts(QObject* application) {
122
123 qDebug() << "ScriptEngine::stopAllScripts() -------- BEGIN -----------";
124 _allScriptsMutex.lock();
125 _stoppingAllScripts = true;
126
107 QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines);127 QMutableSetIterator<ScriptEngine*> i(_allKnownScriptEngines);
108 while (i.hasNext()) {128 while (i.hasNext()) {
109 ScriptEngine* scriptEngine = i.next();129 ScriptEngine* scriptEngine = i.next();
110 130
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
111 // NOTE: typically all script engines are running. But there's at least one known exception to this, the137 // NOTE: typically all script engines are running. But there's at least one known exception to this, the
112 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using138 // "entities sandbox" which is only used to evaluate entities scripts to test their validity before using
113 // them. We don't need to stop scripts that aren't running. 139 // them. We don't need to stop scripts that aren't running.
114 if (scriptEngine->isRunning()) {140 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
115 QEventLoop loop;151 QEventLoop loop;
116 QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);152 QObject::connect(scriptEngine, &ScriptEngine::doneRunning, &loop, &QEventLoop::quit);
117 153
154 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
118 scriptEngine->disconnect(application);155 scriptEngine->disconnect(application);
156 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
119 scriptEngine->stop();157 scriptEngine->stop();
120 158 qDebug() << "ScriptEngine::stopAllScripts() handling script: " << scriptName << "line:" << __LINE__;
121 loop.exec();159 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.";
122 }164 }
123 }165 }
166 _stoppingAllScripts = false;
167 _allScriptsMutex.unlock();
168 qDebug() << "ScriptEngine::stopAllScripts() -------- DONE -----------";
124}169}
125 170
126QString ScriptEngine::getFilename() const { 171QString ScriptEngine::getFilename() const {
@@ -347,6 +392,8 @@ void ScriptEngine::evaluate() { · 220 unmodified lines
347}392}
348 393
349QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {394QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) {
395 qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;
396 _evaluatePending = true;
350 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);397 QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber);
351 if (hasUncaughtException()) {398 if (hasUncaughtException()) {
352 int line = uncaughtExceptionLineNumber();399 int line = uncaughtExceptionLineNumber();
@@ -354,6 +401,8 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 1 unmodified line
354 }401 }
355 emit evaluationFinished(result, hasUncaughtException());402 emit evaluationFinished(result, hasUncaughtException());
356 clearExceptions();403 clearExceptions();
404 _evaluatePending = false;
405 qDebug() << "ScriptEngine::evaluate() " << getFilename() << " line:" << __LINE__;
357 return result;406 return result;
358}407}
359 408
@@ -370,30 +419,36 @@ void ScriptEngine::sendAvatarBillboardPacket() { · 10 unmodified lines
370}419}
371 420
372void ScriptEngine::run() {421void ScriptEngine::run() {
422 qDebug() << "ScriptEngine::run() " << getFilename() << " -------- BEGIN -----------";
423
373 if (!_isInitialized) {424 if (!_isInitialized) {
425 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
374 init();426 init();
427 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
375 }428 }
429 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
376 _isRunning = true;430 _isRunning = true;
377 _isFinished = false;431 _isFinished = false;
432 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
378 emit runningStateChanged();433 emit runningStateChanged();
434 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
379 435
436 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
380 QScriptValue result = evaluate(_scriptContents);437 QScriptValue result = evaluate(_scriptContents);
381 if (hasUncaughtException()) {438 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
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 }
387 439
440 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
388 QElapsedTimer startTime;441 QElapsedTimer startTime;
389 startTime.start();442 startTime.start();
390 443
391 int thisFrame = 0;444 int thisFrame = 0;
392 445
446 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
393 auto nodeList = DependencyManager::get<NodeList>();447 auto nodeList = DependencyManager::get<NodeList>();
394 448
395 qint64 lastUpdate = usecTimestampNow();449 qint64 lastUpdate = usecTimestampNow();
396 450
451 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
397 while (!_isFinished) {452 while (!_isFinished) {
398 int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec453 int usecToSleep = (thisFrame++ * SCRIPT_DATA_CALLBACK_USECS) - startTime.nsecsElapsed() / 1000; // nsec to usec
399 if (usecToSleep > 0) {454 if (usecToSleep > 0) {
@@ -535,6 +590,7 @@ void ScriptEngine::run() { · 135 unmodified lines
535 }590 }
536 lastUpdate = now;591 lastUpdate = now;
537 }592 }
593 qDebug() << "ScriptEngine::run() " << getFilename() << " line:" << __LINE__;
538 594
539 stopAllTimers(); // make sure all our timers are stopped if the script is ending595 stopAllTimers(); // make sure all our timers are stopped if the script is ending
540 596
@@ -565,6 +621,7 @@ void ScriptEngine::run() { · 24 unmodified lines
565 emit runningStateChanged();621 emit runningStateChanged();
566 622
567 emit doneRunning();623 emit doneRunning();
624 qDebug() << "ScriptEngine::run() " << getFilename() << " -------- DONE -----------" << " line:" << __LINE__;
568}625}
569 626
570// NOTE: This is private because it must be called on the same thread that created the timers, which is why627// 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
579}636}
580 637
581void ScriptEngine::stop() {638void ScriptEngine::stop() {
639 qDebug() << "ScriptEngine::stop() " << getFilename() << "[" << ((void*)this) << "]";
582 _isFinished = true;640 _isFinished = true;
583 emit runningStateChanged();641 emit runningStateChanged();
584}642}
@@ -710,8 +768,11 @@ void ScriptEngine::include(const QString& includeFile, QScriptValue callback) { · 125 unmodified lines
710}768}
711 769
712void ScriptEngine::load(const QString& loadFile) {770void ScriptEngine::load(const QString& loadFile) {
771 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
713 QUrl url = resolvePath(loadFile);772 QUrl url = resolvePath(loadFile);
773 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
714 emit loadScript(url.toString(), false);774 emit loadScript(url.toString(), false);
775 qDebug() << "ScriptEngine::load() " << getFilename() << "[" << ((void*)this) << "]" << "line:" << __LINE__;
715}776}
716 777
717void ScriptEngine::nodeKilled(SharedNodePointer node) {778void ScriptEngine::nodeKilled(SharedNodePointer node) {