History

Add comments to startup processes

Atlante45 committed 8 years ago

Commit 2a204531 parent 800d15b5 files · +56 −18Browse files

Showing the changes to libraries/shared/src/SettingInterface.cpp (1 of 5 files). Show all

Unified · Split

libraries/shared/src/SettingInterface.cpp+19−12
@@ -23,25 +23,27 @@ · 22 unmodified lines
2323 #include "SharedUtil.h"
2424
2525 namespace Setting {
26− // cleans up the settings private instance. Should only be run once at closing down.
27− void cleanupPrivateInstance() {
26+ // This should only run as a post-routine in the QCoreApplication destructor
27+ void cleanupSettingsSaveThread() {
2828 auto globalManager = DependencyManager::get<Manager>();
2929 Q_ASSERT(qApp && globalManager);
3030
31− // grab the thread before we nuke the instance
31+ // Grab the settings thread to shut it down
3232 QThread* settingsManagerThread = globalManager->thread();
3333
34− // quit the settings manager thread
34+ // Quit the settings manager thread and wait for it so we
35+ // don't get concurrent accesses when we save all settings below
3536 settingsManagerThread->quit();
3637 settingsManagerThread->wait();
3738
38− // Save all settings
39+ // [IMPORTANT] Save all settings when the QApplication goes down
3940 globalManager->saveAll();
4041
4142 qCDebug(shared) << "Settings thread stopped.";
4243 }
4344
44− void setupPrivateInstance() {
45+ // This should only run as a pre-routine in the QCoreApplication constructor
46+ void setupSettingsSaveThread() {
4547 auto globalManager = DependencyManager::get<Manager>();
4648 Q_ASSERT(qApp && globalManager);
4749
@@ -55,6 +57,9 @@ namespace Setting { · 7 unmodified lines
5557 QObject::connect(thread, &QThread::finished, globalManager.data(), &Manager::stopTimer);
5658
5759 // Setup manager threading affinity
60+ // This makes the timer fire on the settings thread so we don't block the main
61+ // thread with a lot of file I/O.
62+ // We bring back the manager to the main thread when the QApplication goes down
5863 globalManager->moveToThread(thread);
5964 QObject::connect(thread, &QThread::finished, globalManager.data(), [] {
6065 auto globalManager = DependencyManager::get<Manager>();
@@ -63,15 +68,17 @@ namespace Setting { · 2 unmodified lines
6368 // Move manager back to the main thread (has to be done on owning thread)
6469 globalManager->moveToThread(qApp->thread());
6570 });
66−
71+
72+ // Start the settings save thread
6773 thread->start();
6874 qCDebug(shared) << "Settings thread started.";
6975
70− // Register cleanupPrivateInstance to run inside QCoreApplication's destructor.
71− qAddPostRoutine(cleanupPrivateInstance);
76+ // Register cleanupSettingsSaveThread to run inside QCoreApplication's destructor.
77+ // This will cleanup the settings thread and save all settings before shut down.
78+ qAddPostRoutine(cleanupSettingsSaveThread);
7279 }
7380
74− // Sets up the settings private instance. Should only be run once at startup. preInit() must be run beforehand,
81+ // Sets up the settings private instance. Should only be run once at startup.
7582 void init() {
7683 // Set settings format
7784 QSettings::setDefaultFormat(JSON_FORMAT);
@@ -91,11 +98,11 @@ namespace Setting { · 13 unmodified lines
9198 qCDebug(shared) << (deleted ? "Deleted" : "Failed to delete") << "settings lock file" << settingsLockFilename;
9299 }
93100
94− // Setup settings manager
101+ // Setup settings manager, the manager will live until the process shuts down
95102 DependencyManager::set<Manager>();
96103
97104 // Add pre-routine to setup threading
98− qAddPreRoutine(setupPrivateInstance);
105+ qAddPreRoutine(setupSettingsSaveThread);
99106 }
100107
101108 void Interface::init() {