History

send quit event during shutdown on thread, with timeout

Stephen Birarda committed 9 years ago

Commit 40dfcb11 parent f5d732f5 files · +63 −32Browse files

Showing the changes to interface/src/Application.cpp (1 of 5 files). Show all

Unified · Split

interface/src/Application.cpp+18−3
@@ -11,6 +11,8 @@ · 10 unmodified lines
1111
1212 #include "Application.h"
1313
14+#include <thread>
15+
1416 #include <gl/Config.h>
1517 #include <glm/glm.hpp>
1618 #include <glm/gtx/component_wise.hpp>
@@ -1570,9 +1572,13 @@ void Application::aboutToQuit() { · 1553 unmodified lines
15701572
15711573 getActiveDisplayPlugin()->deactivate();
15721574
1573− // use the ClosureEventSender via an std::thread (to not use QThread while the application is going down)
1574− // to send an event that says the user asked for the app to close
1575− _userQuitThread = std::thread { &ClosureEventSender::sendQuitStart, DependencyManager::get<ClosureEventSender>() };
1575+ // use the ClosureEventSender via a QThread to send an event that says the user asked for the app to close
1576+ auto closureEventSender = DependencyManager::get<ClosureEventSender>();
1577+ QThread* closureEventThread = new QThread(this);
1578+ closureEventSender->moveToThread(closureEventThread);
1579+ // sendQuitEventAsync will bail immediately if the UserActivityLogger is not enabled
1580+ connect(closureEventThread, &QThread::started, closureEventSender.data(), &ClosureEventSender::sendQuitEventAsync);
1581+ closureEventThread->start();
15761582
15771583 // Hide Running Scripts dialog so that it gets destroyed in an orderly manner; prevents warnings at shutdown.
15781584 DependencyManager::get<OffscreenUi>()->hide("RunningScripts");
@@ -1738,6 +1744,15 @@ Application::~Application() { · 159 unmodified lines
17381744
17391745 _window->deleteLater();
17401746
1747+ // make sure that the quit event has finished sending before we take the application down
1748+ auto closureEventSender = DependencyManager::get<ClosureEventSender>();
1749+ while (!closureEventSender->hasFinishedQuitEvent() && !closureEventSender->hasTimedOutQuitEvent()) {
1750+ // yield so we're not spinning
1751+ std::this_thread::yield();
1752+ }
1753+ // quit the thread used by the closure event sender
1754+ closureEventSender->thread()->quit();
1755+
17411756 // Can't log to file passed this point, FileLogger about to be deleted
17421757 qInstallMessageHandler(LogHandler::verboseMessageHandler);
17431758 }