File locking refactored (#631)
Remove FileMutex and wire locking into driver subsystems.
This commit is contained in:
committed by
GitHub
parent
92ca046681
commit
020aa471e2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user