potential fix for Windows 7 freeze on exit

pull/190/head
Ryzerth 2021-07-29 15:07:22 +02:00
rodzic 7079ddb74e
commit b7a0f849cf
4 zmienionych plików z 38 dodań i 30 usunięć

Wyświetl plik

@ -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<std::mutex> lock(termMtx);
autoSaveEnabled = false;
termFlag = true;
}
termCond.notify_one();
if (autoSaveThread.joinable()) { autoSaveThread.join(); }
if (!autoSaveEnabled) { return; }
{
std::lock_guard<std::mutex> 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<std::mutex> lock(_this->termMtx);
_this->termCond.wait_for(lock, std::chrono::milliseconds(1000), [_this]() { return _this->termFlag; } );
std::unique_lock<std::mutex> lock(termMtx);
termCond.wait_for(lock, std::chrono::milliseconds(1000), [this]() { return termFlag; } );
}
}
}

Wyświetl plik

@ -4,6 +4,7 @@
#include <string>
#include <mutex>
#include <condition_variable>
#include <atomic>
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;
};

Wyświetl plik

@ -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;
}

Wyświetl plik

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