From 114062c96d6c24301d1c023e681f00e858887d4c Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Fri, 10 Oct 2025 16:53:28 +0200 Subject: [PATCH] Simplify file locking --- Tactility/Source/app/ElfApp.cpp | 2 +- Tactility/Source/app/notes/Notes.cpp | 10 +++++----- Tactility/Source/file/PropertiesFile.cpp | 8 +++++--- Tactility/Source/lvgl/LabelUtils.cpp | 5 +++-- TactilityCore/Include/Tactility/file/File.h | 12 ------------ 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Tactility/Source/app/ElfApp.cpp b/Tactility/Source/app/ElfApp.cpp index 2fc629fa..927aedd3 100644 --- a/Tactility/Source/app/ElfApp.cpp +++ b/Tactility/Source/app/ElfApp.cpp @@ -76,7 +76,7 @@ private: assert(elfFileData == nullptr); size_t size = 0; - file::withLock(elf_path, [this, &elf_path, &size]{ + file::getLock(elf_path)->withLock([this, &elf_path, &size]{ elfFileData = file::readBinary(elf_path, size); }); diff --git a/Tactility/Source/app/notes/Notes.cpp b/Tactility/Source/app/notes/Notes.cpp index 2a6bef17..f40f0679 100644 --- a/Tactility/Source/app/notes/Notes.cpp +++ b/Tactility/Source/app/notes/Notes.cpp @@ -83,7 +83,7 @@ class NotesApp final : public App { void openFile(const std::string& path) { // We might be reading from the SD card, which could share a SPI bus with other devices (display) - file::withLock(path, [this, path] { + file::getLock(path)->withLock([this, path] { auto data = file::readString(path); if (data != nullptr) { auto lock = lvgl::getSyncLock()->asScopedLock(); @@ -98,15 +98,15 @@ class NotesApp final : public App { bool saveFile(const std::string& path) { // We might be writing to SD card, which could share a SPI bus with other devices (display) - return file::withLock(path, [this, path] { + bool result = false; + file::getLock(path)->withLock([&result, this, path] { if (file::writeString(path, saveBuffer.c_str())) { TT_LOG_I(TAG, "Saved to %s", path.c_str()); filePath = path; - return true; - } else { - return false; + result = true; } }); + return result; } #pragma endregion Open_Events_Functions diff --git a/Tactility/Source/file/PropertiesFile.cpp b/Tactility/Source/file/PropertiesFile.cpp index d78a114d..5335a8be 100644 --- a/Tactility/Source/file/PropertiesFile.cpp +++ b/Tactility/Source/file/PropertiesFile.cpp @@ -49,20 +49,22 @@ bool loadPropertiesFile(const std::string& filePath, std::map& properties) { - return file::withLock(filePath, [filePath, &properties] { + bool result = false; + getLock(filePath)->withLock([&result, filePath, &properties] { TT_LOG_I(TAG, "Saving properties file %s", filePath.c_str()); FILE* file = fopen(filePath.c_str(), "w"); if (file == nullptr) { TT_LOG_E(TAG, "Failed to open %s", filePath.c_str()); - return false; + return; } for (const auto& [key, value]: properties) { fprintf(file, "%s=%s\n", key.c_str(), value.c_str()); } fclose(file); - return true; + result = true; }); + return result; } } diff --git a/Tactility/Source/lvgl/LabelUtils.cpp b/Tactility/Source/lvgl/LabelUtils.cpp index 0a9965f1..9f6ffd82 100644 --- a/Tactility/Source/lvgl/LabelUtils.cpp +++ b/Tactility/Source/lvgl/LabelUtils.cpp @@ -7,8 +7,9 @@ namespace tt::lvgl { constexpr auto* TAG = "LabelUtils"; bool label_set_text_file(lv_obj_t* label, const char* filepath) { - auto text = file::withLock>(std::string(filepath), [filepath] { - return file::readString(filepath); + std::unique_ptr text; + file::getLock(filepath)->withLock([&text, filepath] { + text = file::readString(filepath); }); if (text != nullptr) { diff --git a/TactilityCore/Include/Tactility/file/File.h b/TactilityCore/Include/Tactility/file/File.h index e4e26001..a2b45002 100644 --- a/TactilityCore/Include/Tactility/file/File.h +++ b/TactilityCore/Include/Tactility/file/File.h @@ -54,18 +54,6 @@ std::shared_ptr getLock(const std::string& path); void setFindLockFunction(const FindLockFunction& function); -/** - * Acquires a lock, calls the function, then releases the lock. - * @param[in] path the path to find a lock for - * @param[in] fn the code to execute while the lock is acquired - */ -template -ReturnType withLock(const std::string& path, std::function fn) { - const auto lock = getLock(path)->asScopedLock(); - lock.lock(); - return fn(); -} - long getSize(FILE* file); /** Read a file and return its data.