History

rework ScriptEngine and worker thread shutdown

Brad Hefta-Gaub committed 11 years ago

Commit 6521de81 parent 0efc8d61 file · +16 −5Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+16−5
@@ -107,6 +107,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 106 unmodified lines
107107 }
108108
109109 ScriptEngine::~ScriptEngine() {
110+ qDebug() << "ScriptEngine::~ScriptEngine().... this:" << this << "my thread:" << thread();
110111 // If we're not already in the middle of stopping all scripts, then we should remove ourselves
111112 // from the list of running scripts. We don't do this if we're in the process of stopping all scripts
112113 // because that method removes scripts from its list as it iterates them
@@ -117,20 +118,28 @@ ScriptEngine::~ScriptEngine() { · 4 unmodified lines
117118 }
118119 }
119120
121+class MyWorkerThread : public QThread {
122+public:
123+ MyWorkerThread(QObject* parent = nullptr) : QThread(parent) { qDebug() << "MyWorkerThread::MyWorkerThread() this:" << this; }
124+ ~MyWorkerThread() { qDebug() << "MyWorkerThread::~MyWorkerThread() this:" << this; }
125+};
126+
120127 void ScriptEngine::runInThread() {
121− QThread* workerThread = new QThread(this);
128+ QThread* workerThread = new MyWorkerThread(); // thread is owned but ScriptEngine, so if the ScriptEngine is destroyed, the thread will be too.
122129 QString scriptEngineName = QString("Script Thread:") + getFilename();
123130 workerThread->setObjectName(scriptEngineName);
124131
125132 // when the worker thread is started, call our engine's run..
126133 connect(workerThread, &QThread::started, this, &ScriptEngine::run);
127134
128− // when the thread is terminated, add both scriptEngine and thread to the deleteLater queue
129− connect(this, &ScriptEngine::doneRunning, this, &ScriptEngine::deleteLater);
135+ // tell the thread to stop when the script engine is done
136+ connect(this, &ScriptEngine::doneRunning, workerThread, &QThread::quit);
137+
138+ // when the thread is finished, add thread to the deleteLater queue
130139 connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
131140
132− // tell the thread to stop when the script engine is done
133− connect(this, &ScriptEngine::destroyed, workerThread, &QThread::quit);
141+ // when the thread is destroyed, add scriptEngine to the deleteLater queue
142+ connect(workerThread, &QThread::finished, this, &ScriptEngine::deleteLater);
134143
135144 moveToThread(workerThread);
136145
@@ -650,10 +659,12 @@ void ScriptEngine::run() { · 513 unmodified lines
650659 _isRunning = false;
651660 if (_wantSignals) {
652661 emit runningStateChanged();
662+ qDebug() << "ScriptEngine::run().... about to emit doneRunning().... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread();
653663 emit doneRunning();
654664 }
655665
656666 _doneRunningThisScript = true;
667+ qDebug() << "ScriptEngine::run().... END OF RUN.... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread();
657668 }
658669
659670 // NOTE: This is private because it must be called on the same thread that created the timers, which is why