Showing the changes to libraries/shared/src/SettingInterface.cpp (1 of 5 files). Show all
Unified · Split
| @@ -23,25 +23,27 @@ · 22 unmodified lines | ||
| 23 | 23 | #include "SharedUtil.h" |
| 24 | 24 | |
| 25 | 25 | 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() { | |
| 28 | 28 | auto globalManager = DependencyManager::get<Manager>(); |
| 29 | 29 | Q_ASSERT(qApp && globalManager); |
| 30 | 30 | |
| 31 | − // grab the thread before we nuke the instance | |
| 31 | + // Grab the settings thread to shut it down | |
| 32 | 32 | QThread* settingsManagerThread = globalManager->thread(); |
| 33 | 33 | |
| 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 | |
| 35 | 36 | settingsManagerThread->quit(); |
| 36 | 37 | settingsManagerThread->wait(); |
| 37 | 38 | |
| 38 | − // Save all settings | |
| 39 | + // [IMPORTANT] Save all settings when the QApplication goes down | |
| 39 | 40 | globalManager->saveAll(); |
| 40 | 41 | |
| 41 | 42 | qCDebug(shared) << "Settings thread stopped."; |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | − void setupPrivateInstance() { | |
| 45 | + // This should only run as a pre-routine in the QCoreApplication constructor | |
| 46 | + void setupSettingsSaveThread() { | |
| 45 | 47 | auto globalManager = DependencyManager::get<Manager>(); |
| 46 | 48 | Q_ASSERT(qApp && globalManager); |
| 47 | 49 | |
| @@ -55,6 +57,9 @@ namespace Setting { · 7 unmodified lines | ||
| 55 | 57 | QObject::connect(thread, &QThread::finished, globalManager.data(), &Manager::stopTimer); |
| 56 | 58 | |
| 57 | 59 | // 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 | |
| 58 | 63 | globalManager->moveToThread(thread); |
| 59 | 64 | QObject::connect(thread, &QThread::finished, globalManager.data(), [] { |
| 60 | 65 | auto globalManager = DependencyManager::get<Manager>(); |
| @@ -63,15 +68,17 @@ namespace Setting { · 2 unmodified lines | ||
| 63 | 68 | // Move manager back to the main thread (has to be done on owning thread) |
| 64 | 69 | globalManager->moveToThread(qApp->thread()); |
| 65 | 70 | }); |
| 66 | − | |
| 71 | + | |
| 72 | + // Start the settings save thread | |
| 67 | 73 | thread->start(); |
| 68 | 74 | qCDebug(shared) << "Settings thread started."; |
| 69 | 75 | |
| 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); | |
| 72 | 79 | } |
| 73 | 80 | |
| 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. | |
| 75 | 82 | void init() { |
| 76 | 83 | // Set settings format |
| 77 | 84 | QSettings::setDefaultFormat(JSON_FORMAT); |
| @@ -91,11 +98,11 @@ namespace Setting { · 13 unmodified lines | ||
| 91 | 98 | qCDebug(shared) << (deleted ? "Deleted" : "Failed to delete") << "settings lock file" << settingsLockFilename; |
| 92 | 99 | } |
| 93 | 100 | |
| 94 | − // Setup settings manager | |
| 101 | + // Setup settings manager, the manager will live until the process shuts down | |
| 95 | 102 | DependencyManager::set<Manager>(); |
| 96 | 103 | |
| 97 | 104 | // Add pre-routine to setup threading |
| 98 | − qAddPreRoutine(setupPrivateInstance); | |
| 105 | + qAddPreRoutine(setupSettingsSaveThread); | |
| 99 | 106 | } |
| 100 | 107 | |
| 101 | 108 | void Interface::init() { |