mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-04-20 02:15:06 +00:00
Simplify file locking
This commit is contained in:
parent
63ef1027e1
commit
114062c96d
@ -76,7 +76,7 @@ private:
|
||||
assert(elfFileData == nullptr);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
|
||||
@ -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<void>(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<bool>(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
|
||||
|
||||
@ -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) {
|
||||
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());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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::unique_ptr<uint8_t[]>>(std::string(filepath), [filepath] {
|
||||
return file::readString(filepath);
|
||||
std::unique_ptr<uint8_t[]> text;
|
||||
file::getLock(filepath)->withLock([&text, filepath] {
|
||||
text = file::readString(filepath);
|
||||
});
|
||||
|
||||
if (text != nullptr) {
|
||||
|
||||
@ -54,18 +54,6 @@ std::shared_ptr<Lock> 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<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);
|
||||
|
||||
/** Read a file and return its data.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user