Skip to content

Commit

Permalink
Move writing settings to thread. (#1488)
Browse files Browse the repository at this point in the history
I had a problem with volume control. I think it was because SD card is
not a fast enough. And writing settings are executes synchrony. I tryed
to move it into another thread.

How it works:
When settings changed we are start writing to SD card asynchrony instead
of synchrony. If new changes try to run cycle again it will be delayed
until thread not finished.
Then settings will be writen one more time. It can be not a thread safe,
when main thread re-write settings variables and the second thread try
to read them for store to SD card.
In all cases, even if writen to SD card settigns will be changed in
"writing" moment, it will be re-writen one more time.

Video how it works before with slow SD card and how it works after
moving saving settings to thread:
https://www.youtube.com/watch?v=fIy-bYyZLBk

---------

Co-authored-by: andrei nikulin <nikulin.aa@kvanta.pro>
  • Loading branch information
hypercyclist and andrei nikulin authored May 9, 2024
1 parent 7775842 commit 1a7c2e1
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/keymon/keymon.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ void turnOffScreen(void)
suspend_exec(stay_awake ? -1 : timeout);
}

static void *runWritingSettingsThread(void* param)
{
bool *isWritingSettingsThreadActive = (bool*)param;
*isWritingSettingsThreadActive = true;
settings_shm_write();
settings_save();
*isWritingSettingsThreadActive = false;
return 0;
}

//
// Main
//
Expand Down Expand Up @@ -386,6 +396,10 @@ int main(void)
bool delete_flag = false;
bool settings_changed = false;

volatile bool needWriteSettings = false;
volatile bool isWritingSettingsThreadActive = false;
pthread_t writingSettingsThread;

time_t fav_last_modified = time(NULL);

while (1) {
Expand Down Expand Up @@ -736,9 +750,13 @@ int main(void)
else if (volDown_state == RELEASED && volUp_state == RELEASED)
comboKey_volume = false;

if (settings_changed) {
settings_shm_write();
settings_save();
if (settings_changed || needWriteSettings) {
needWriteSettings = true;
if (!isWritingSettingsThreadActive)
{
needWriteSettings = false;
pthread_create(&writingSettingsThread, NULL, runWritingSettingsThread, &isWritingSettingsThreadActive);
}
}

if ((val == PRESSED) && (system_state == MODE_MAIN_UI)) {
Expand Down

0 comments on commit 1a7c2e1

Please sign in to comment.