diff --git a/core/src/config.cpp b/core/src/config.cpp index 83a6fe1c..a638dbdc 100644 --- a/core/src/config.cpp +++ b/core/src/config.cpp @@ -47,51 +47,49 @@ void ConfigManager::save(bool lock) { } void ConfigManager::enableAutoSave() { - if (!autoSaveEnabled) { - autoSaveEnabled = true; - termFlag = false; - autoSaveThread = std::thread(autoSaveWorker, this); - } + if (autoSaveEnabled) { return; } + autoSaveEnabled = true; + termFlag = false; + autoSaveThread = std::thread(&ConfigManager::autoSaveWorker, this); } void ConfigManager::disableAutoSave() { - if (autoSaveEnabled) { - { - std::lock_guard lock(termMtx); - autoSaveEnabled = false; - termFlag = true; - } - termCond.notify_one(); - if (autoSaveThread.joinable()) { autoSaveThread.join(); } + if (!autoSaveEnabled) { return; } + { + std::lock_guard lock(termMtx); + autoSaveEnabled = false; + termFlag = true; } + termCond.notify_one(); + if (autoSaveThread.joinable()) { autoSaveThread.join(); } } void ConfigManager::acquire() { mtx.lock(); } -void ConfigManager::release(bool changed) { - this->changed |= changed; +void ConfigManager::release(bool modified) { + changed |= modified; mtx.unlock(); } -void ConfigManager::autoSaveWorker(ConfigManager* _this) { - while (_this->autoSaveEnabled) { - if (!_this->mtx.try_lock()) { +void ConfigManager::autoSaveWorker() { + while (autoSaveEnabled) { + if (!mtx.try_lock()) { spdlog::warn("ConfigManager locked, waiting..."); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); continue; } - if (_this->changed) { - _this->changed = false; - _this->save(false); + if (changed) { + changed = false; + save(false); } - _this->mtx.unlock(); + mtx.unlock(); // Sleep but listen for wakeup call { - std::unique_lock lock(_this->termMtx); - _this->termCond.wait_for(lock, std::chrono::milliseconds(1000), [_this]() { return _this->termFlag; } ); + std::unique_lock lock(termMtx); + termCond.wait_for(lock, std::chrono::milliseconds(1000), [this]() { return termFlag; } ); } } } \ No newline at end of file diff --git a/core/src/config.h b/core/src/config.h index 0ebf6fc7..c1bec251 100644 --- a/core/src/config.h +++ b/core/src/config.h @@ -4,6 +4,7 @@ #include #include #include +#include using nlohmann::json; @@ -17,21 +18,21 @@ public: void enableAutoSave(); void disableAutoSave(); void acquire(); - void release(bool changed = false); + void release(bool modified = false); json conf; private: - static void autoSaveWorker(ConfigManager* _this); + void autoSaveWorker(); std::string path = ""; - bool changed = false; - bool autoSaveEnabled = false; + volatile bool changed = false; + volatile bool autoSaveEnabled = false; std::thread autoSaveThread; std::mutex mtx; std::mutex termMtx; std::condition_variable termCond; - bool termFlag = false; + volatile bool termFlag = false; }; \ No newline at end of file diff --git a/core/src/core.cpp b/core/src/core.cpp index 8c21dbb8..1553a480 100644 --- a/core/src/core.cpp +++ b/core/src/core.cpp @@ -463,6 +463,11 @@ int sdrpp_main(int argc, char *argv[]) { glfwSwapBuffers(core::window); } + // Shut down all modules + for (auto& [name, mod] : core::moduleManager.modules) { + mod.end(); + } + // Cleanup ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); @@ -473,5 +478,8 @@ int sdrpp_main(int argc, char *argv[]) { sigpath::signalPath.stop(); + core::configManager.disableAutoSave(); + core::configManager.save(); + return 0; } diff --git a/frequency_manager/src/main.cpp b/frequency_manager/src/main.cpp index c7dd08ab..1c7880c8 100644 --- a/frequency_manager/src/main.cpp +++ b/frequency_manager/src/main.cpp @@ -866,5 +866,6 @@ MOD_EXPORT void _DELETE_INSTANCE_(void* instance) { } MOD_EXPORT void _END_() { - // Nothing here + config.disableAutoSave(); + config.save(); } \ No newline at end of file