History

add executeOnScriptThread

humbletim committed 9 years ago

Commit 9b0bee91 parent 0f7652e2 files · +18 −12Browse files

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

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+17−12
@@ -356,6 +356,16 @@ void ScriptEngine::runInThread() { · 355 unmodified lines
356 workerThread->start();356 workerThread->start();
357}357}
358 358
359void ScriptEngine::executeOnScriptThread(std::function<void()> function, bool blocking ) {
360 if (QThread::currentThread() != thread()) {
361 QMetaObject::invokeMethod(this, "executeOnScriptThread", blocking ? Qt::BlockingQueuedConnection : Qt::QueuedConnection,
362 Q_ARG(std::function<void()>, function));
363 return;
364 }
365
366 function();
367}
368
359void ScriptEngine::waitTillDoneRunning() {369void ScriptEngine::waitTillDoneRunning() {
360 auto workerThread = thread();370 auto workerThread = thread();
361 371
@@ -1490,18 +1500,13 @@ void ScriptEngine::updateEntityScriptStatus(const EntityItemID& entityID, const · 1128 unmodified lines
1490// since all of these operations can be asynch we will always do the actual work in the response handler1500// since all of these operations can be asynch we will always do the actual work in the response handler
1491// for the download1501// for the download
1492void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {1502void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) {
1493 1503 auto engine = theEngine.data();
1494 {1504 engine->executeOnScriptThread([=]{
1495 // set EntityScriptDetails.status to LOADING (over on theEngine's thread)1505 EntityScriptDetails details = engine->_entityScripts[entityID];
1496 QObject threadPunter;1506 if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) {
1497 auto engine = theEngine.data();1507 engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName());
1498 connect(&threadPunter, &QObject::destroyed, engine, [=]() {1508 }
1499 EntityScriptDetails details = engine->_entityScripts[entityID];1509 });
1500 if (details.status == EntityScriptStatus::PENDING || details.status == EntityScriptStatus::UNLOADED) {
1501 engine->updateEntityScriptStatus(entityID, EntityScriptStatus::LOADING, QThread::currentThread()->objectName());
1502 }
1503 });
1504 }
1505 1510
1506 // NOTE: If the script content is not currently in the cache, the LAMBDA here will be called on the Main Thread1511 // NOTE: If the script content is not currently in the cache, the LAMBDA here will be called on the Main Thread
1507 // which means we're guaranteed that it's not the correct thread for the ScriptEngine. This means1512 // which means we're guaranteed that it's not the correct thread for the ScriptEngine. This means