Skip to content

Commit

Permalink
Initiate saving state from main thread
Browse files Browse the repository at this point in the history
This is architecturally more consistent, but also necessary for the
coming change of being able to reload bridges, to not get concurrent
access on the bridge configs.
  • Loading branch information
halfgaar committed Mar 20, 2024
1 parent 0a6f6e7 commit 9c24283
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions mainapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ MainApp::MainApp(const std::string &configFilePath) :
subscriptionStore->loadSessionsAndSubscriptions(settings.getSessionsDBFile());
}

auto fSaveState = std::bind(&MainApp::saveStateInThread, this);
auto fSaveState = std::bind(&MainApp::queueSaveStateInThread, this);
timer.addCallback(fSaveState, 900000, "Save state.");

auto fSendPendingWills = std::bind(&MainApp::queueSendQueuedWills, this);
Expand Down Expand Up @@ -280,8 +280,18 @@ void MainApp::saveStateInThread()

auto f = std::bind(&MainApp::saveState, this->settings, bridgeInfos, true);
this->bgWorker.addTask(f);
}


/**
* @brief MainApp::queueSaveStateInThread is a wrapper to be called from another thread, to make sure saveStateInThread() is called
* on the main loop.
*/
void MainApp::queueSaveStateInThread()
{
std::lock_guard<std::mutex> locker(eventMutex);
auto f = std::bind(&MainApp::saveStateInThread, this);
taskQueue.push_back(f);
wakeUpThread();
}

void MainApp::queueSendQueuedWills()
Expand Down
1 change: 1 addition & 0 deletions mainapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class MainApp
static void saveBridgeInfo(const std::string &filePath, const std::list<BridgeInfoForSerializing> &bridgeInfos);
void loadBridgeInfo();
void saveStateInThread();
void queueSaveStateInThread();
void queueSendQueuedWills();
void waitForWillsQueued();
void waitForDisconnectsInitiated();
Expand Down

0 comments on commit 9c24283

Please sign in to comment.