History

Eliminate file IO contentions for the settings

Brad Davis committed 10 years ago

Commit 39dcd1f1 parent 5805c0c8 files · +186 −45Browse files

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

Unified · Split

libraries/shared/src/SettingInterface.cpp+38−25
@@ -9,27 +9,33 @@ · 8 unmodified lines
99 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1010 //
1111
12+#include "SettingInterface.h"
13+
1214 #include <QCoreApplication>
1315 #include <QDebug>
1416 #include <QFile>
1517 #include <QThread>
1618
1719 #include "PathUtils.h"
18−#include "SettingInterface.h"
1920 #include "SettingManager.h"
2021 #include "SharedLogging.h"
2122
2223 namespace Setting {
23− static Manager* privateInstance = nullptr;
24+ static QSharedPointer<Manager> globalManager;
2425
26+ const QString Interface::FIRST_RUN { "firstRun" };
27+
2528 // cleans up the settings private instance. Should only be run once at closing down.
2629 void cleanupPrivateInstance() {
2730 // grab the thread before we nuke the instance
28− QThread* settingsManagerThread = privateInstance->thread();
29−
31+ QThread* settingsManagerThread = DependencyManager::get<Manager>()->thread();
32+
3033 // tell the private instance to clean itself up on its thread
31− privateInstance->deleteLater();
32− privateInstance = NULL;
34+ DependencyManager::destroy<Manager>();
35+
36+ //
37+ globalManager->deleteLater();
38+ globalManager.reset();
3339
3440 // quit the settings manager thread and wait on it to make sure it's gone
3541 settingsManagerThread->quit();
@@ -63,14 +69,13 @@ namespace Setting { · 27 unmodified lines
6369 QThread* thread = new QThread();
6470 Q_CHECK_PTR(thread);
6571 thread->setObjectName("Settings Thread");
66−
67− privateInstance = new Manager();
68− Q_CHECK_PTR(privateInstance);
6972
70− QObject::connect(privateInstance, SIGNAL(destroyed()), thread, SLOT(quit()));
71− QObject::connect(thread, SIGNAL(started()), privateInstance, SLOT(startTimer()));
73+ globalManager = DependencyManager::set<Manager>();
74+
75+ QObject::connect(globalManager.data(), SIGNAL(destroyed()), thread, SLOT(quit()));
76+ QObject::connect(thread, SIGNAL(started()), globalManager.data(), SLOT(startTimer()));
7277 QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
73− privateInstance->moveToThread(thread);
78+ globalManager->moveToThread(thread);
7479 thread->start();
7580 qCDebug(shared) << "Settings thread started.";
7681
@@ -79,7 +84,7 @@ namespace Setting { · 2 unmodified lines
7984 }
8085
8186 void Interface::init() {
82− if (!privateInstance) {
87+ if (!DependencyManager::isSet<Manager>()) {
8388 // WARNING: As long as we are using QSettings this should always be triggered for each Setting::Handle
8489 // in an assignment-client - the QSettings backing we use for this means persistence of these
8590 // settings from an AC (when there can be multiple terminating at same time on one machine)
@@ -87,9 +92,13 @@ namespace Setting { · 1 unmodified line
8792 qWarning() << "Setting::Interface::init() for key" << _key << "- Manager not yet created." <<
8893 "Settings persistence disabled.";
8994 } else {
90− // Register Handle
91− privateInstance->registerHandle(this);
92− _isInitialized = true;
95+ _manager = DependencyManager::get<Manager>();
96+ auto manager = _manager.lock();
97+ if (manager) {
98+ // Register Handle
99+ manager->registerHandle(this);
100+ _isInitialized = true;
101+ }
93102
94103 // Load value from disk
95104 load();
@@ -97,11 +106,13 @@ namespace Setting { · 1 unmodified line
97106 }
98107
99108 void Interface::deinit() {
100− if (_isInitialized && privateInstance) {
101− // Save value to disk
102− save();
103−
104− privateInstance->removeHandle(_key);
109+ if (_isInitialized && _manager) {
110+ auto manager = _manager.lock();
111+ if (manager) {
112+ // Save value to disk
113+ save();
114+ manager->removeHandle(_key);
115+ }
105116 }
106117 }
107118
@@ -113,14 +124,16 @@ namespace Setting { · 5 unmodified lines
113124 }
114125
115126 void Interface::save() {
116− if (privateInstance) {
117− privateInstance->saveSetting(this);
127+ auto manager = _manager.lock();
128+ if (manager) {
129+ manager->saveSetting(this);
118130 }
119131 }
120132
121133 void Interface::load() {
122− if (privateInstance) {
123− privateInstance->loadSetting(this);
134+ auto manager = _manager.lock();
135+ if (manager) {
136+ manager->loadSetting(this);
124137 }
125138 }
126139 }