File locking refactored (#631)

Remove FileMutex and wire locking into driver subsystems.
This commit is contained in:
Ken Van Hoeylandt
2026-08-28 01:06:53 +02:00
committed by GitHub
parent 92ca046681
commit 020aa471e2
44 changed files with 766 additions and 993 deletions
-12
View File
@@ -42,8 +42,6 @@ bool listDirectory(
const std::string& path,
std::function<void(const dirent&)> onEntry
) {
FileMutexGuard guard(path);
LOG_I(TAG, "listDir start %s", path.c_str());
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
@@ -68,8 +66,6 @@ int scandir(
ScandirFilter filterMethod,
ScandirSort sortMethod
) {
FileMutexGuard guard(path);
LOG_I(TAG, "scandir start");
DIR* dir = opendir(path.c_str());
if (dir == nullptr) {
@@ -193,8 +189,6 @@ bool writeString(const std::string& filepath, const std::string& content) {
}
static bool findOrCreateDirectoryInternal(std::string path, mode_t mode) {
FileMutexGuard guard(path);
struct stat dir_stat;
if (mkdir(path.c_str(), mode) == 0) {
return true;
@@ -310,29 +304,23 @@ bool deleteRecursively(const std::string& path) {
}
bool deleteFile(const std::string& path) {
FileMutexGuard guard(path);
return remove(path.c_str()) == 0;
}
bool deleteDirectory(const std::string& path) {
FileMutexGuard guard(path);
return rmdir(path.c_str()) == 0;
}
bool isFile(const std::string& path) {
FileMutexGuard guard(path);
return access(path.c_str(), F_OK) == 0;
}
bool isDirectory(const std::string& path) {
FileMutexGuard guard(path);
struct stat stat_result;
return stat(path.c_str(), &stat_result) == 0 && S_ISDIR(stat_result.st_mode);
}
bool readLines(const std::string& filePath, bool stripNewLine, std::function<void(const char* line)> callback) {
FileMutexGuard guard(filePath);
auto* file = fopen(filePath.c_str(), "r");
if (file == nullptr) {
return false;
-135
View File
@@ -1,135 +0,0 @@
#include <tactility/device.h>
#include <tactility/drivers/display.h>
#include <tactility/drivers/sdcard.h>
#include <tactility/drivers/spi_controller.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/filesystem/file_system.h>
#include <lvgl/lvgl.h>
#include <vector>
constexpr auto* TAG = "file_mutex_lvgl";
struct Device;
namespace {
std::vector<FileMutexId> registered_ids;
void wrapped_lvgl_lock() {
if (!lvgl_is_running()) return;
lvgl_lock();
}
bool wrapped_lvgl_try_lock(uint32_t timeout) {
// Return lock success, so the file operation can continue when LVGL is not running
// lvgl_try_lock() fails to lock if LVGL is not running
if (!lvgl_is_running()) return true;
return lvgl_try_lock(timeout);
}
void wrapped_lvgl_unlock() {
if (!lvgl_is_running()) return;
lvgl_unlock();
}
const FileMutex lvgl_mutex = {
.lock = wrapped_lvgl_lock,
.try_lock = wrapped_lvgl_try_lock,
.unlock = wrapped_lvgl_unlock,
};
}
namespace tt {
/**
* Finds file systems with a device (e.g. sd card) that is owned by a SPI controller.
* If the SPI controller has a display on the bus, we create an LVGL lock for the file system path.
*/
void initFileMutexForLvgl() {
file_system_for_each(&registered_ids, [](FileSystem* fs, void* context) {
char mount_path[64];
if (file_system_get_path(fs, mount_path, sizeof(mount_path)) != ERROR_NONE) {
return true;
}
LOG_D(TAG, "Mount path %s", mount_path);
// We only care about file system with a Device (owner)
auto* owner = file_system_get_owner(fs);
if (owner == nullptr) {
LOG_D(TAG, "Owner: none");
return true;
}
LOG_D(TAG, "Owner: %s", owner->name);
// Ignore devices without a parent (root)
auto* parent = device_get_parent(owner);
if (parent == nullptr) {
LOG_D(TAG, "Owner: no parent");
return true;
}
LOG_D(TAG, "Owner: parent %s", parent->name);
// If the FileSystem is on a SPI bus and there's more than 1 device, we assume the other one is the display.
auto* type = device_get_type(parent);
if (type != &SPI_CONTROLLER_TYPE || device_get_child_count(parent) <= 1) {
LOG_D(TAG, "Owner parent not SPI controller or not enough children");
return true;
}
struct Context {
const char* mountPath;
std::vector<FileMutexId>* registeredIds;
};
Context ctx = { .mountPath = mount_path, .registeredIds = static_cast<std::vector<FileMutexId>*>(context) };
device_for_each_child(parent, &ctx, [](Device* child, void* context) -> bool {
Context* ctx = static_cast<Context*>(context);
if (device_get_type(child) == &DISPLAY_TYPE) {
LOG_I(TAG, "Adding file mutex for %s as it shares a bus with a display", ctx->mountPath);
ctx->registeredIds->push_back(file_mutex_add(&lvgl_mutex, ctx->mountPath));
return false;
} else {
LOG_D(TAG, "child of parent, %s: not DISPLAY_TYPE", child->name);
}
return true;
});
return true;
});
// SDMMC-backed SD cards aren't parented under SPI_CONTROLLER_TYPE, so the pass above never
// sees them - but on some chips (classic ESP32) SDMMC and SPI still contend for DMA/bus
// access. Lock every SD card mount if a display exists anywhere, regardless of bus topology.
if (!device_exists_of_type(&DISPLAY_TYPE)) {
return;
}
file_system_for_each(&registered_ids, [](FileSystem* fs, void* context) {
char mount_path[64];
if (file_system_get_path(fs, mount_path, sizeof(mount_path)) != ERROR_NONE) {
return true;
}
auto* owner = file_system_get_owner(fs);
if (owner == nullptr || device_get_type(owner) != &SDCARD_TYPE) {
return true;
}
LOG_I(TAG, "Adding file mutex for %s (SD card) - a display is present and may contend for bus/DMA resources", mount_path);
auto* ids = static_cast<std::vector<FileMutexId>*>(context);
ids->push_back(file_mutex_add(&lvgl_mutex, mount_path));
return true;
});
}
void deinitFileMutexForLvgl() {
for (FileMutexId id : registered_ids) {
file_mutex_remove(id);
}
registered_ids.clear();
}
}