potential fix for Windows 7 freeze on exit

This commit is contained in:
Ryzerth 2021-07-29 15:07:22 +02:00
parent 7079ddb74e
commit b7a0f849cf
4 changed files with 38 additions and 30 deletions

View File

@ -47,51 +47,49 @@ void ConfigManager::save(bool lock) {
} }
void ConfigManager::enableAutoSave() { void ConfigManager::enableAutoSave() {
if (!autoSaveEnabled) { if (autoSaveEnabled) { return; }
autoSaveEnabled = true; autoSaveEnabled = true;
termFlag = false; termFlag = false;
autoSaveThread = std::thread(autoSaveWorker, this); autoSaveThread = std::thread(&ConfigManager::autoSaveWorker, this);
}
} }
void ConfigManager::disableAutoSave() { void ConfigManager::disableAutoSave() {
if (autoSaveEnabled) { if (!autoSaveEnabled) { return; }
{ {
std::lock_guard<std::mutex> lock(termMtx); std::lock_guard<std::mutex> lock(termMtx);
autoSaveEnabled = false; autoSaveEnabled = false;
termFlag = true; termFlag = true;
}
termCond.notify_one();
if (autoSaveThread.joinable()) { autoSaveThread.join(); }
} }
termCond.notify_one();
if (autoSaveThread.joinable()) { autoSaveThread.join(); }
} }
void ConfigManager::acquire() { void ConfigManager::acquire() {
mtx.lock(); mtx.lock();
} }
void ConfigManager::release(bool changed) { void ConfigManager::release(bool modified) {
this->changed |= changed; changed |= modified;
mtx.unlock(); mtx.unlock();
} }
void ConfigManager::autoSaveWorker(ConfigManager* _this) { void ConfigManager::autoSaveWorker() {
while (_this->autoSaveEnabled) { while (autoSaveEnabled) {
if (!_this->mtx.try_lock()) { if (!mtx.try_lock()) {
spdlog::warn("ConfigManager locked, waiting..."); spdlog::warn("ConfigManager locked, waiting...");
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
continue; continue;
} }
if (_this->changed) { if (changed) {
_this->changed = false; changed = false;
_this->save(false); save(false);
} }
_this->mtx.unlock(); mtx.unlock();
// Sleep but listen for wakeup call // Sleep but listen for wakeup call
{ {
std::unique_lock<std::mutex> lock(_this->termMtx); std::unique_lock<std::mutex> lock(termMtx);
_this->termCond.wait_for(lock, std::chrono::milliseconds(1000), [_this]() { return _this->termFlag; } ); termCond.wait_for(lock, std::chrono::milliseconds(1000), [this]() { return termFlag; } );
} }
} }
} }

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <atomic>
using nlohmann::json; using nlohmann::json;
@ -17,21 +18,21 @@ public:
void enableAutoSave(); void enableAutoSave();
void disableAutoSave(); void disableAutoSave();
void acquire(); void acquire();
void release(bool changed = false); void release(bool modified = false);
json conf; json conf;
private: private:
static void autoSaveWorker(ConfigManager* _this); void autoSaveWorker();
std::string path = ""; std::string path = "";
bool changed = false; volatile bool changed = false;
bool autoSaveEnabled = false; volatile bool autoSaveEnabled = false;
std::thread autoSaveThread; std::thread autoSaveThread;
std::mutex mtx; std::mutex mtx;
std::mutex termMtx; std::mutex termMtx;
std::condition_variable termCond; std::condition_variable termCond;
bool termFlag = false; volatile bool termFlag = false;
}; };

View File

@ -463,6 +463,11 @@ int sdrpp_main(int argc, char *argv[]) {
glfwSwapBuffers(core::window); glfwSwapBuffers(core::window);
} }
// Shut down all modules
for (auto& [name, mod] : core::moduleManager.modules) {
mod.end();
}
// Cleanup // Cleanup
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
@ -473,5 +478,8 @@ int sdrpp_main(int argc, char *argv[]) {
sigpath::signalPath.stop(); sigpath::signalPath.stop();
core::configManager.disableAutoSave();
core::configManager.save();
return 0; return 0;
} }

View File

@ -866,5 +866,6 @@ MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
} }
MOD_EXPORT void _END_() { MOD_EXPORT void _END_() {
// Nothing here config.disableAutoSave();
config.save();
} }