mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-04-22 03:15:05 +00:00
Simplify file locking
This commit is contained in:
parent
63ef1027e1
commit
114062c96d
@ -76,7 +76,7 @@ private:
|
|||||||
assert(elfFileData == nullptr);
|
assert(elfFileData == nullptr);
|
||||||
|
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
file::withLock<void>(elf_path, [this, &elf_path, &size]{
|
file::getLock(elf_path)->withLock([this, &elf_path, &size]{
|
||||||
elfFileData = file::readBinary(elf_path, size);
|
elfFileData = file::readBinary(elf_path, size);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -83,7 +83,7 @@ class NotesApp final : public App {
|
|||||||
|
|
||||||
void openFile(const std::string& path) {
|
void openFile(const std::string& path) {
|
||||||
// We might be reading from the SD card, which could share a SPI bus with other devices (display)
|
// We might be reading from the SD card, which could share a SPI bus with other devices (display)
|
||||||
file::withLock<void>(path, [this, path] {
|
file::getLock(path)->withLock([this, path] {
|
||||||
auto data = file::readString(path);
|
auto data = file::readString(path);
|
||||||
if (data != nullptr) {
|
if (data != nullptr) {
|
||||||
auto lock = lvgl::getSyncLock()->asScopedLock();
|
auto lock = lvgl::getSyncLock()->asScopedLock();
|
||||||
@ -98,15 +98,15 @@ class NotesApp final : public App {
|
|||||||
|
|
||||||
bool saveFile(const std::string& path) {
|
bool saveFile(const std::string& path) {
|
||||||
// We might be writing to SD card, which could share a SPI bus with other devices (display)
|
// We might be writing to SD card, which could share a SPI bus with other devices (display)
|
||||||
return file::withLock<bool>(path, [this, path] {
|
bool result = false;
|
||||||
|
file::getLock(path)->withLock([&result, this, path] {
|
||||||
if (file::writeString(path, saveBuffer.c_str())) {
|
if (file::writeString(path, saveBuffer.c_str())) {
|
||||||
TT_LOG_I(TAG, "Saved to %s", path.c_str());
|
TT_LOG_I(TAG, "Saved to %s", path.c_str());
|
||||||
filePath = path;
|
filePath = path;
|
||||||
return true;
|
result = true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma endregion Open_Events_Functions
|
#pragma endregion Open_Events_Functions
|
||||||
|
|||||||
@ -49,20 +49,22 @@ bool loadPropertiesFile(const std::string& filePath, std::map<std::string, std::
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool savePropertiesFile(const std::string& filePath, const std::map<std::string, std::string>& properties) {
|
bool savePropertiesFile(const std::string& filePath, const std::map<std::string, std::string>& properties) {
|
||||||
return file::withLock<bool>(filePath, [filePath, &properties] {
|
bool result = false;
|
||||||
|
getLock(filePath)->withLock([&result, filePath, &properties] {
|
||||||
TT_LOG_I(TAG, "Saving properties file %s", filePath.c_str());
|
TT_LOG_I(TAG, "Saving properties file %s", filePath.c_str());
|
||||||
|
|
||||||
FILE* file = fopen(filePath.c_str(), "w");
|
FILE* file = fopen(filePath.c_str(), "w");
|
||||||
if (file == nullptr) {
|
if (file == nullptr) {
|
||||||
TT_LOG_E(TAG, "Failed to open %s", filePath.c_str());
|
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()); }
|
for (const auto& [key, value]: properties) { fprintf(file, "%s=%s\n", key.c_str(), value.c_str()); }
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return true;
|
result = true;
|
||||||
});
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,8 +7,9 @@ namespace tt::lvgl {
|
|||||||
constexpr auto* TAG = "LabelUtils";
|
constexpr auto* TAG = "LabelUtils";
|
||||||
|
|
||||||
bool label_set_text_file(lv_obj_t* label, const char* filepath) {
|
bool label_set_text_file(lv_obj_t* label, const char* filepath) {
|
||||||
auto text = file::withLock<std::unique_ptr<uint8_t[]>>(std::string(filepath), [filepath] {
|
std::unique_ptr<uint8_t[]> text;
|
||||||
return file::readString(filepath);
|
file::getLock(filepath)->withLock([&text, filepath] {
|
||||||
|
text = file::readString(filepath);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (text != nullptr) {
|
if (text != nullptr) {
|
||||||
|
|||||||
@ -54,18 +54,6 @@ std::shared_ptr<Lock> getLock(const std::string& path);
|
|||||||
|
|
||||||
void setFindLockFunction(const FindLockFunction& function);
|
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<typename ReturnType>
|
|
||||||
ReturnType withLock(const std::string& path, std::function<ReturnType()> fn) {
|
|
||||||
const auto lock = getLock(path)->asScopedLock();
|
|
||||||
lock.lock();
|
|
||||||
return fn();
|
|
||||||
}
|
|
||||||
|
|
||||||
long getSize(FILE* file);
|
long getSize(FILE* file);
|
||||||
|
|
||||||
/** Read a file and return its data.
|
/** Read a file and return its data.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user