mirror of
https://github.com/overte-org/overte.git
synced 2025-08-13 02:15:42 +02:00
rework ScriptEngine and worker thread shutdown
This commit is contained in:
parent
0efc8d6628
commit
6521de8163
1 changed files with 16 additions and 5 deletions
|
@ -107,6 +107,7 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptEngine::~ScriptEngine() {
|
ScriptEngine::~ScriptEngine() {
|
||||||
|
qDebug() << "ScriptEngine::~ScriptEngine().... this:" << this << "my thread:" << thread();
|
||||||
// If we're not already in the middle of stopping all scripts, then we should remove ourselves
|
// If we're not already in the middle of stopping all scripts, then we should remove ourselves
|
||||||
// from the list of running scripts. We don't do this if we're in the process of stopping all scripts
|
// from the list of running scripts. We don't do this if we're in the process of stopping all scripts
|
||||||
// because that method removes scripts from its list as it iterates them
|
// because that method removes scripts from its list as it iterates them
|
||||||
|
@ -117,20 +118,28 @@ ScriptEngine::~ScriptEngine() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyWorkerThread : public QThread {
|
||||||
|
public:
|
||||||
|
MyWorkerThread(QObject* parent = nullptr) : QThread(parent) { qDebug() << "MyWorkerThread::MyWorkerThread() this:" << this; }
|
||||||
|
~MyWorkerThread() { qDebug() << "MyWorkerThread::~MyWorkerThread() this:" << this; }
|
||||||
|
};
|
||||||
|
|
||||||
void ScriptEngine::runInThread() {
|
void ScriptEngine::runInThread() {
|
||||||
QThread* workerThread = new QThread(this);
|
QThread* workerThread = new MyWorkerThread(); // thread is owned but ScriptEngine, so if the ScriptEngine is destroyed, the thread will be too.
|
||||||
QString scriptEngineName = QString("Script Thread:") + getFilename();
|
QString scriptEngineName = QString("Script Thread:") + getFilename();
|
||||||
workerThread->setObjectName(scriptEngineName);
|
workerThread->setObjectName(scriptEngineName);
|
||||||
|
|
||||||
// when the worker thread is started, call our engine's run..
|
// when the worker thread is started, call our engine's run..
|
||||||
connect(workerThread, &QThread::started, this, &ScriptEngine::run);
|
connect(workerThread, &QThread::started, this, &ScriptEngine::run);
|
||||||
|
|
||||||
// when the thread is terminated, add both scriptEngine and thread to the deleteLater queue
|
// tell the thread to stop when the script engine is done
|
||||||
connect(this, &ScriptEngine::doneRunning, this, &ScriptEngine::deleteLater);
|
connect(this, &ScriptEngine::doneRunning, workerThread, &QThread::quit);
|
||||||
|
|
||||||
|
// when the thread is finished, add thread to the deleteLater queue
|
||||||
connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
|
connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
|
||||||
|
|
||||||
// tell the thread to stop when the script engine is done
|
// when the thread is destroyed, add scriptEngine to the deleteLater queue
|
||||||
connect(this, &ScriptEngine::destroyed, workerThread, &QThread::quit);
|
connect(workerThread, &QThread::finished, this, &ScriptEngine::deleteLater);
|
||||||
|
|
||||||
moveToThread(workerThread);
|
moveToThread(workerThread);
|
||||||
|
|
||||||
|
@ -650,10 +659,12 @@ void ScriptEngine::run() {
|
||||||
_isRunning = false;
|
_isRunning = false;
|
||||||
if (_wantSignals) {
|
if (_wantSignals) {
|
||||||
emit runningStateChanged();
|
emit runningStateChanged();
|
||||||
|
qDebug() << "ScriptEngine::run().... about to emit doneRunning().... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread();
|
||||||
emit doneRunning();
|
emit doneRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
_doneRunningThisScript = true;
|
_doneRunningThisScript = true;
|
||||||
|
qDebug() << "ScriptEngine::run().... END OF RUN.... this:" << this << "my thread:" << thread() << "current thread:" << QThread::currentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: This is private because it must be called on the same thread that created the timers, which is why
|
// NOTE: This is private because it must be called on the same thread that created the timers, which is why
|
||||||
|
|
Loading…
Reference in a new issue