Rename wait and unload in best thread

This commit is contained in:
Zach Pomerantz 2016-05-13 16:14:22 -07:00
parent 806d06b552
commit 2140dc77b3
4 changed files with 13 additions and 15 deletions

View file

@ -88,16 +88,13 @@ void EntityTreeRenderer::resetEntitiesScriptEngine() {
public: public:
WaitRunnable(ScriptEngine* engine) : _engine(engine) {} WaitRunnable(ScriptEngine* engine) : _engine(engine) {}
virtual void run() override { virtual void run() override {
_engine->wait(); _engine->waitTillDoneRunning();
_engine->deleteLater(); _engine->deleteLater();
} }
private: private:
ScriptEngine* _engine; ScriptEngine* _engine;
}; };
engine->unloadAllEntityScripts();
engine->stop();
// Wait for the scripting thread from the thread pool to avoid hanging the main thread // Wait for the scripting thread from the thread pool to avoid hanging the main thread
QThreadPool::globalInstance()->start(new WaitRunnable(engine)); QThreadPool::globalInstance()->start(new WaitRunnable(engine));
}); });
@ -110,6 +107,13 @@ void EntityTreeRenderer::resetEntitiesScriptEngine() {
void EntityTreeRenderer::clear() { void EntityTreeRenderer::clear() {
leaveAllEntities(); leaveAllEntities();
if (_entitiesScriptEngine) {
// Unload and stop the engine here (instead of in its deleter) to
// avoid marshalling unload signals back to this thread
_entitiesScriptEngine->unloadAllEntityScripts();
_entitiesScriptEngine->stop();
}
if (_wantScripts && !_shuttingDown) { if (_wantScripts && !_shuttingDown) {
resetEntitiesScriptEngine(); resetEntitiesScriptEngine();
} }

View file

@ -284,7 +284,7 @@ void ScriptEngine::runInThread() {
workerThread->start(); workerThread->start();
} }
void ScriptEngine::wait() { void ScriptEngine::waitTillDoneRunning() {
auto workerThread = thread(); auto workerThread = thread();
if (_isThreaded && workerThread) { if (_isThreaded && workerThread) {
@ -303,8 +303,9 @@ void ScriptEngine::wait() {
// Process events for the main application thread, allowing invokeMethod calls to pass between threads. // Process events for the main application thread, allowing invokeMethod calls to pass between threads.
QCoreApplication::processEvents(); QCoreApplication::processEvents();
// If the final evaluation takes too long, then tell the script engine to stop evaluating // If the final evaluation takes too long, then tell the script engine to stop running
auto elapsedUsecs = usecTimestampNow() - startedWaiting; auto elapsedUsecs = usecTimestampNow() - startedWaiting;
static const auto MAX_SCRIPT_EVALUATION_TIME = USECS_PER_SECOND;
if (elapsedUsecs > MAX_SCRIPT_EVALUATION_TIME) { if (elapsedUsecs > MAX_SCRIPT_EVALUATION_TIME) {
workerThread->quit(); workerThread->quit();

View file

@ -67,10 +67,7 @@ public:
class ScriptEngine : public QScriptEngine, public ScriptUser, public EntitiesScriptEngineProvider { class ScriptEngine : public QScriptEngine, public ScriptUser, public EntitiesScriptEngineProvider {
Q_OBJECT Q_OBJECT
public: public:
static const auto MAX_SCRIPT_EVALUATION_TIME = USECS_PER_SECOND;
ScriptEngine(const QString& scriptContents = NO_SCRIPT, const QString& fileNameString = QString("")); ScriptEngine(const QString& scriptContents = NO_SCRIPT, const QString& fileNameString = QString(""));
~ScriptEngine(); ~ScriptEngine();
/// run the script in a dedicated thread. This will have the side effect of evalulating /// run the script in a dedicated thread. This will have the side effect of evalulating
@ -88,7 +85,7 @@ public:
Q_INVOKABLE void stop(); Q_INVOKABLE void stop();
// Stop any evaluating scripts and wait for the scripting thread to finish. // Stop any evaluating scripts and wait for the scripting thread to finish.
void wait(); void waitTillDoneRunning();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NOTE - these are NOT intended to be public interfaces available to scripts, the are only Q_INVOKABLE so we can // NOTE - these are NOT intended to be public interfaces available to scripts, the are only Q_INVOKABLE so we can
@ -169,7 +166,6 @@ public:
public slots: public slots:
void callAnimationStateHandler(QScriptValue callback, AnimVariantMap parameters, QStringList names, bool useNames, AnimVariantResultHandler resultHandler); void callAnimationStateHandler(QScriptValue callback, AnimVariantMap parameters, QStringList names, bool useNames, AnimVariantResultHandler resultHandler);
void updateMemoryCost(const qint64&); void updateMemoryCost(const qint64&);
void abort() { abortEvaluation(); }
signals: signals:
void scriptLoaded(const QString& scriptFilename); void scriptLoaded(const QString& scriptFilename);

View file

@ -158,9 +158,6 @@ void ScriptEngines::shutdownScripting() {
// and stop. We can safely short circuit this because we know we're in the "quitting" process // and stop. We can safely short circuit this because we know we're in the "quitting" process
scriptEngine->disconnect(this); scriptEngine->disconnect(this);
// If this is an entity script, we need to unload any entities
scriptEngine->unloadAllEntityScripts();
// Gracefully stop the engine's scripting thread // Gracefully stop the engine's scripting thread
scriptEngine->stop(); scriptEngine->stop();
@ -168,7 +165,7 @@ void ScriptEngines::shutdownScripting() {
// want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing // want any of the scripts final "scriptEnding()" or pending "update()" methods from accessing
// any application state after we leave this stopAllScripts() method // any application state after we leave this stopAllScripts() method
qCDebug(scriptengine) << "waiting on script:" << scriptName; qCDebug(scriptengine) << "waiting on script:" << scriptName;
scriptEngine->wait(); scriptEngine->waitTillDoneRunning();
qCDebug(scriptengine) << "done waiting on script:" << scriptName; qCDebug(scriptengine) << "done waiting on script:" << scriptName;
scriptEngine->deleteLater(); scriptEngine->deleteLater();