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
@@ -65,6 +65,19 @@ error_t spi_controller_try_lock(struct Device* device, TickType_t timeout);
*/
error_t spi_controller_unlock(struct Device* device);
/**
* @brief Locks device's parent bus when the parent is a SPI controller.
* @param[in] device the device whose parent may be a SPI controller
* @retval ERROR_NONE when the operation was successful, or the parent is not a SPI controller
*/
error_t spi_controller_lock_bus_of(struct Device* device);
/**
* @brief Unlocks a device's parent bus.
* @param[in] device the device whose parent may be a SPI controller
*/
void spi_controller_unlock_bus_of(struct Device* device);
extern const struct DeviceType SPI_CONTROLLER_TYPE;
#ifdef __cplusplus
@@ -1,63 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/freertos/freertos.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Set of lock/try_lock/unlock callbacks backing a filesystem mount's mutex.
* Any field left null is treated as a no-op by file_mutex_lock/try_lock/unlock.
*/
struct FileMutex {
void (*lock)();
bool (*try_lock)(uint32_t timeout);
void (*unlock)();
};
typedef uint32_t FileMutexId;
#define FILE_MUTEX_ID_INVALID ((FileMutexId)0)
/**
* @brief Registers a mutex for a mount path (e.g. "/sdcard") and its descendants.
* @param[in] mutex callbacks to associate with the path; a copy is stored
* @param[in] path mount path this mutex serializes access to
* @return the id of the new entry, or the id of the existing entry if path is already registered
* @note If a mutex is already registered for this exact path, no new entry is created and the
* existing entry's id is returned; the existing callbacks are left unchanged.
*/
FileMutexId file_mutex_add(const struct FileMutex* mutex, const char* path);
/**
* @brief Removes a previously added mutex registration.
* @param[in] id id returned by file_mutex_add(); a stale or unknown id is a no-op
*/
void file_mutex_remove(FileMutexId id);
/**
* @brief Looks up the mutex registered for path or one of its ancestor mount paths.
* @param[out] mutex receives the matching mutex, or an all-null (no-op) mutex if none matches
* @param[in] path file or directory path to look up
*/
void file_mutex_get(struct FileMutex* mutex, const char* path);
/** @brief Locks mutex. No-op if mutex->lock is null. */
void file_mutex_lock(const struct FileMutex* mutex);
/**
* @brief Attempts to lock mutex within timeout.
* @return true if locked (or mutex->try_lock is null), false on timeout
*/
bool file_mutex_try_lock(const struct FileMutex* mutex, TickType_t timeout);
/** @brief Unlocks mutex. No-op if mutex->unlock is null. */
void file_mutex_unlock(const struct FileMutex* mutex);
#ifdef __cplusplus
}
#endif
@@ -2,8 +2,6 @@
/**
* @brief Generic string key-value ".properties" file.
* @note Safely acquires/releases the filesystem mutex registered for the file's path (see
* tactility/filesystem/file_mutex.h) - manual locking isn't needed.
*/
#pragma once
@@ -22,6 +22,22 @@ error_t spi_controller_unlock(Device* device) {
return SPI_DRIVER_API(driver)->unlock(device);
}
error_t spi_controller_lock_bus_of(Device* device) {
Device* parent = device_get_parent(device);
if (parent == nullptr || device_get_type(parent) != &SPI_CONTROLLER_TYPE) {
return ERROR_NONE;
}
return spi_controller_lock(parent);
}
void spi_controller_unlock_bus_of(Device* device) {
Device* parent = device_get_parent(device);
if (parent == nullptr || device_get_type(parent) != &SPI_CONTROLLER_TYPE) {
return;
}
spi_controller_unlock(parent);
}
const DeviceType SPI_CONTROLLER_TYPE {
.name = "spi-controller"
};
@@ -1,123 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/filesystem/file_mutex.h>
#include <tactility/concurrent/mutex.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
static const FileMutex no_mutex = {
.lock = nullptr,
.try_lock = nullptr,
.unlock = nullptr,
};
struct FileMutexEntry {
FileMutexId id;
std::string path;
FileMutex mutex;
};
// Guards mutex_entries against concurrent add/get/remove; unrelated to whether a FileMutex's own
// lock/unlock is currently held (file_mutex_get() hands out a copy that stays valid regardless of
// later registry changes - see file_mutex_remove()).
struct FileMutexLedger {
std::vector<FileMutexEntry> entries;
FileMutexId next_id = 1;
Mutex mutex {};
FileMutexLedger() { mutex_construct(&mutex); }
~FileMutexLedger() { mutex_destruct(&mutex); }
void lock() { mutex_lock(&mutex); }
void unlock() { mutex_unlock(&mutex); }
};
static FileMutexLedger& get_ledger() {
static FileMutexLedger ledger;
return ledger;
}
extern "C" {
FileMutexId file_mutex_add(const FileMutex* mutex, const char* path) {
auto& ledger = get_ledger();
ledger.lock();
for (auto& entry : ledger.entries) {
if (entry.path == path) {
FileMutexId existing_id = entry.id;
ledger.unlock();
return existing_id;
}
}
FileMutexId new_id = ledger.next_id++;
ledger.entries.push_back({
.id = new_id,
.path = path,
.mutex = *mutex
});
ledger.unlock();
return new_id;
}
void file_mutex_remove(FileMutexId id) {
auto& ledger = get_ledger();
ledger.lock();
const auto iterator = std::ranges::find_if(ledger.entries, [id](const FileMutexEntry& entry) {
return entry.id == id;
});
if (iterator != ledger.entries.end()) {
// Plain erase, not swap-and-pop: file_mutex_get() matches first-registered-wins, so
// removal must preserve the relative order of the remaining entries.
ledger.entries.erase(iterator);
}
ledger.unlock();
}
void file_mutex_get(FileMutex* mutex, const char* path) {
auto& ledger = get_ledger();
std::string path_string = path;
ledger.lock();
for (auto& entry : ledger.entries) {
// Match the mount path itself, or a descendant (e.g. "/sdcard" registered, "/sdcard/config.json" requested).
bool is_match = path_string == entry.path ||
(entry.path == "/" && !path_string.empty() && path_string[0] == '/') ||
(path_string.rfind(entry.path, 0) == 0 && path_string[entry.path.size()] == '/');
if (is_match) {
memcpy(mutex, &entry.mutex, sizeof(FileMutex));
ledger.unlock();
return;
}
}
ledger.unlock();
*mutex = no_mutex;
}
void file_mutex_lock(const FileMutex* mutex) {
if (mutex->lock) {
mutex->lock();
}
}
bool file_mutex_try_lock(const FileMutex* mutex, TickType_t timeout) {
if (mutex->try_lock) {
return mutex->try_lock(timeout);
}
return true;
}
void file_mutex_unlock(const FileMutex* mutex) {
if (mutex->unlock) {
mutex->unlock();
}
}
}
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/properties_file.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/log.h>
#include <cerrno>
@@ -53,14 +52,9 @@ namespace {
// (fgetc()'s EOF return doesn't by itself distinguish clean end-of-file from a read error -
// ferror() after the loop does); true otherwise, including for a missing file (ENOENT).
bool load_from_file(PropertiesFile* file) {
FileMutex mutex {};
file_mutex_get(&mutex, file->path.c_str());
file_mutex_lock(&mutex);
FILE* handle = std::fopen(file->path.c_str(), "r");
if (handle == nullptr) {
const int open_error = errno;
file_mutex_unlock(&mutex);
if (open_error == ENOENT) {
return true;
}
@@ -105,7 +99,6 @@ bool load_from_file(PropertiesFile* file) {
bool read_ok = std::ferror(handle) == 0;
std::fclose(handle);
file_mutex_unlock(&mutex);
if (!read_ok) {
LOG_E(TAG, "Failed to read %s", file->path.c_str());
@@ -121,16 +114,11 @@ bool load_from_file(PropertiesFile* file) {
// @return true if the backing file was fully replaced with the current entries; false (leaving
// the previous on-disk content untouched) if any step failed.
bool save_to_file(const PropertiesFile* file) {
FileMutex mutex {};
file_mutex_get(&mutex, file->path.c_str());
file_mutex_lock(&mutex);
std::string temp_path = file->path + ".tmp";
FILE* handle = std::fopen(temp_path.c_str(), "w");
if (handle == nullptr) {
LOG_E(TAG, "Failed to open %s", temp_path.c_str());
file_mutex_unlock(&mutex);
return false;
}
@@ -146,7 +134,6 @@ bool save_to_file(const PropertiesFile* file) {
if (!write_ok || !flush_ok || !close_ok) {
LOG_E(TAG, "Failed to write %s", temp_path.c_str());
std::remove(temp_path.c_str());
file_mutex_unlock(&mutex);
return false;
}
@@ -157,11 +144,9 @@ bool save_to_file(const PropertiesFile* file) {
if (std::rename(temp_path.c_str(), file->path.c_str()) != 0) {
LOG_E(TAG, "Failed to replace %s", file->path.c_str());
std::remove(temp_path.c_str());
file_mutex_unlock(&mutex);
return false;
}
file_mutex_unlock(&mutex);
return true;
}
+2 -8
View File
@@ -44,7 +44,6 @@
#include <tactility/drivers/usb_msc_device.h>
#include <tactility/drivers/wifi.h>
#include <tactility/error.h>
#include <tactility/filesystem/file_mutex.h>
#include <tactility/filesystem/file_system.h>
#include <tactility/memory.h>
#include <tactility/module.h>
@@ -170,13 +169,6 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(display_get_frame_buffer_count),
DEFINE_MODULE_SYMBOL(display_get_backlight),
DEFINE_MODULE_SYMBOL(DISPLAY_TYPE),
// file_mutex
DEFINE_MODULE_SYMBOL(file_mutex_add),
DEFINE_MODULE_SYMBOL(file_mutex_remove),
DEFINE_MODULE_SYMBOL(file_mutex_get),
DEFINE_MODULE_SYMBOL(file_mutex_lock),
DEFINE_MODULE_SYMBOL(file_mutex_try_lock),
DEFINE_MODULE_SYMBOL(file_mutex_unlock),
// file system
DEFINE_MODULE_SYMBOL(file_system_mount),
DEFINE_MODULE_SYMBOL(file_system_unmount),
@@ -306,6 +298,8 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(spi_controller_lock),
DEFINE_MODULE_SYMBOL(spi_controller_try_lock),
DEFINE_MODULE_SYMBOL(spi_controller_unlock),
DEFINE_MODULE_SYMBOL(spi_controller_lock_bus_of),
DEFINE_MODULE_SYMBOL(spi_controller_unlock_bus_of),
DEFINE_MODULE_SYMBOL(SPI_CONTROLLER_TYPE),
// drivers/trackball
DEFINE_MODULE_SYMBOL(trackball_read_delta),
@@ -1,190 +0,0 @@
#include "doctest.h"
#include <tactility/filesystem/file_mutex.h>
namespace {
int lock_calls = 0;
int unlock_calls = 0;
int try_lock_calls = 0;
bool try_lock_result = true;
uint32_t try_lock_timeout_seen = 0;
void mock_lock() { lock_calls++; }
void mock_unlock() { unlock_calls++; }
bool mock_try_lock(uint32_t timeout) {
try_lock_calls++;
try_lock_timeout_seen = timeout;
return try_lock_result;
}
int lock_a_calls = 0;
int lock_b_calls = 0;
void mock_lock_a() { lock_a_calls++; }
void mock_lock_b() { lock_b_calls++; }
void reset_mocks() {
lock_calls = 0;
unlock_calls = 0;
try_lock_calls = 0;
try_lock_result = true;
try_lock_timeout_seen = 0;
lock_a_calls = 0;
lock_b_calls = 0;
}
} // namespace
TEST_CASE("file_mutex_get with zero registrations returns a no-op mutex") {
FileMutex mutex;
file_mutex_get(&mutex, "/nowhere/file.txt");
CHECK_EQ(mutex.lock, nullptr);
CHECK_EQ(mutex.try_lock, nullptr);
CHECK_EQ(mutex.unlock, nullptr);
// Calling through a no-op mutex must be safe, and try_lock must report success.
file_mutex_lock(&mutex);
CHECK_EQ(file_mutex_try_lock(&mutex, 123), true);
file_mutex_unlock(&mutex);
}
TEST_CASE("file_mutex_add/get with a single registration") {
reset_mocks();
FileMutex registered = { .lock = mock_lock, .try_lock = mock_try_lock, .unlock = mock_unlock };
file_mutex_add(&registered, "/mock1");
FileMutex mutex;
// Exact mount path match.
file_mutex_get(&mutex, "/mock1");
CHECK_EQ(mutex.lock, mock_lock);
CHECK_EQ(mutex.try_lock, mock_try_lock);
CHECK_EQ(mutex.unlock, mock_unlock);
// Descendant path match.
file_mutex_get(&mutex, "/mock1/nested/file.txt");
CHECK_EQ(mutex.lock, mock_lock);
// Unrelated path falls back to no-op.
FileMutex unrelated;
file_mutex_get(&unrelated, "/other/file.txt");
CHECK_EQ(unrelated.lock, nullptr);
// Prefix-but-not-descendant path (e.g. "/mock1x") must not match "/mock1".
FileMutex prefix_only;
file_mutex_get(&prefix_only, "/mock1x/file.txt");
CHECK_EQ(prefix_only.lock, nullptr);
// Exercise the resolved callbacks.
file_mutex_get(&mutex, "/mock1");
file_mutex_lock(&mutex);
CHECK_EQ(lock_calls, 1);
CHECK_EQ(file_mutex_try_lock(&mutex, 42), true);
CHECK_EQ(try_lock_calls, 1);
CHECK_EQ(try_lock_timeout_seen, 42);
file_mutex_unlock(&mutex);
CHECK_EQ(unlock_calls, 1);
// Re-registering the same path is a no-op: original callbacks remain in place.
FileMutex replacement = { .lock = nullptr, .try_lock = nullptr, .unlock = nullptr };
file_mutex_add(&replacement, "/mock1");
file_mutex_get(&mutex, "/mock1");
CHECK_EQ(mutex.lock, mock_lock);
}
TEST_CASE("file_mutex_add/get with two registrations resolves to the matching path") {
reset_mocks();
FileMutex mutex_a = { .lock = mock_lock_a, .try_lock = nullptr, .unlock = nullptr };
FileMutex mutex_b = { .lock = mock_lock_b, .try_lock = nullptr, .unlock = nullptr };
file_mutex_add(&mutex_a, "/mock2a");
file_mutex_add(&mutex_b, "/mock2b");
FileMutex resolved;
file_mutex_get(&resolved, "/mock2a/file.txt");
CHECK_EQ(resolved.lock, mock_lock_a);
file_mutex_get(&resolved, "/mock2b/file.txt");
CHECK_EQ(resolved.lock, mock_lock_b);
// Path matching neither registration falls back to no-op.
file_mutex_get(&resolved, "/mock2c/file.txt");
CHECK_EQ(resolved.lock, nullptr);
// Registration order matters: the first matching entry wins, not the longest
// prefix. A mount nested under an earlier one is shadowed by it.
FileMutex mutex_nested = { .lock = nullptr, .try_lock = nullptr, .unlock = nullptr };
file_mutex_add(&mutex_nested, "/mock2a/nested");
file_mutex_get(&resolved, "/mock2a/nested/file.txt");
CHECK_EQ(resolved.lock, mock_lock_a); // still /mock2a, registered first
}
TEST_CASE("file_mutex_add returns a valid id") {
reset_mocks();
FileMutex registered = { .lock = mock_lock, .try_lock = nullptr, .unlock = nullptr };
FileMutexId id = file_mutex_add(&registered, "/mockA");
CHECK_NE(id, FILE_MUTEX_ID_INVALID);
}
TEST_CASE("file_mutex_add with a duplicate path returns the existing id") {
reset_mocks();
FileMutex mutex_1 = { .lock = mock_lock, .try_lock = nullptr, .unlock = nullptr };
FileMutex mutex_2 = { .lock = mock_lock_a, .try_lock = nullptr, .unlock = nullptr };
FileMutexId id_1 = file_mutex_add(&mutex_1, "/mockB");
FileMutexId id_2 = file_mutex_add(&mutex_2, "/mockB");
CHECK_EQ(id_1, id_2);
FileMutex resolved;
file_mutex_get(&resolved, "/mockB");
CHECK_EQ(resolved.lock, mock_lock); // first registration's callbacks win, unchanged
}
TEST_CASE("file_mutex_remove removes a registration") {
reset_mocks();
FileMutex registered = { .lock = mock_lock, .try_lock = nullptr, .unlock = nullptr };
FileMutexId id = file_mutex_add(&registered, "/mockC");
FileMutex before;
file_mutex_get(&before, "/mockC");
CHECK_EQ(before.lock, mock_lock);
file_mutex_remove(id);
FileMutex after;
file_mutex_get(&after, "/mockC");
CHECK_EQ(after.lock, nullptr);
}
TEST_CASE("file_mutex_remove with an unknown id is a safe no-op") {
reset_mocks();
FileMutex registered = { .lock = mock_lock, .try_lock = nullptr, .unlock = nullptr };
file_mutex_add(&registered, "/mockD");
file_mutex_remove(999999); // never issued
file_mutex_remove(FILE_MUTEX_ID_INVALID);
FileMutex resolved;
file_mutex_get(&resolved, "/mockD");
CHECK_EQ(resolved.lock, mock_lock); // untouched
}
TEST_CASE("file_mutex_remove of one registration leaves others intact") {
reset_mocks();
FileMutex mutex_a = { .lock = mock_lock_a, .try_lock = nullptr, .unlock = nullptr };
FileMutex mutex_b = { .lock = mock_lock_b, .try_lock = nullptr, .unlock = nullptr };
FileMutexId id_a = file_mutex_add(&mutex_a, "/mockE1");
file_mutex_add(&mutex_b, "/mockE2");
file_mutex_remove(id_a);
FileMutex resolved_a;
file_mutex_get(&resolved_a, "/mockE1");
CHECK_EQ(resolved_a.lock, nullptr);
FileMutex resolved_b;
file_mutex_get(&resolved_b, "/mockE2");
CHECK_EQ(resolved_b.lock, mock_lock_b);
}