Kernel and buildscript improvements (#477)
* **New Features** * Centralized module management with global symbol resolution * Level-aware logging with colored prefixes and millisecond timestamps * **Breaking Changes** * ModuleParent hierarchy and getModuleParent() removed * Logging API and adapter model replaced; LogLevel-driven log_generic signature changed * **Improvements** * Unified, simplified module registration across build targets * Tests updated to reflect new module lifecycle and global symbol resolution
This commit is contained in:
committed by
GitHub
parent
1a61eac8e0
commit
a935410f82
@@ -4,88 +4,40 @@
|
||||
#include <tactility/concurrent/mutex.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
#define TAG LOG_TAG(module)
|
||||
#define TAG "module"
|
||||
|
||||
struct ModuleParentPrivate {
|
||||
struct ModuleLedger {
|
||||
std::vector<struct Module*> modules;
|
||||
struct Mutex mutex = { 0 };
|
||||
|
||||
ModuleLedger() { mutex_construct(&mutex); }
|
||||
~ModuleLedger() { mutex_destruct(&mutex); }
|
||||
};
|
||||
|
||||
static ModuleLedger ledger;
|
||||
|
||||
extern "C" {
|
||||
|
||||
#pragma region module_parent
|
||||
|
||||
error_t module_parent_construct(struct ModuleParent* parent) {
|
||||
parent->module_parent_private = new (std::nothrow) ModuleParentPrivate();
|
||||
if (!parent->module_parent_private) return ERROR_OUT_OF_MEMORY;
|
||||
|
||||
auto* data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
|
||||
mutex_construct(&data->mutex);
|
||||
|
||||
error_t module_construct(struct Module* module) {
|
||||
module->internal.started = false;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
error_t module_parent_destruct(struct ModuleParent* parent) {
|
||||
auto* data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
|
||||
if (data == nullptr) return ERROR_NONE;
|
||||
|
||||
mutex_lock(&data->mutex);
|
||||
if (!data->modules.empty()) {
|
||||
mutex_unlock(&data->mutex);
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
mutex_unlock(&data->mutex);
|
||||
|
||||
mutex_destruct(&data->mutex);
|
||||
delete data;
|
||||
parent->module_parent_private = nullptr;
|
||||
error_t module_destruct(struct Module* module) {
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
bool module_parent_resolve_symbol(ModuleParent* parent, const char* symbol_name, uintptr_t* symbol_address) {
|
||||
auto* data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
|
||||
mutex_lock(&data->mutex);
|
||||
for (auto* module : data->modules) {
|
||||
if (!module_is_started(module))
|
||||
continue;
|
||||
if (module_resolve_symbol(module, symbol_name, symbol_address)) {
|
||||
mutex_unlock(&data->mutex);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&data->mutex);
|
||||
return false;
|
||||
error_t module_add(struct Module* module) {
|
||||
mutex_lock(&ledger.mutex);
|
||||
ledger.modules.push_back(module);
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region module
|
||||
|
||||
error_t module_set_parent(struct Module* module, struct ModuleParent* parent) {
|
||||
if (module->internal.started) return ERROR_INVALID_STATE;
|
||||
if (module->internal.parent == parent) return ERROR_NONE;
|
||||
|
||||
// Remove from old parent
|
||||
if (module->internal.parent && module->internal.parent->module_parent_private) {
|
||||
auto* old_data = static_cast<ModuleParentPrivate*>(module->internal.parent->module_parent_private);
|
||||
mutex_lock(&old_data->mutex);
|
||||
auto it = std::find(old_data->modules.begin(), old_data->modules.end(), module);
|
||||
if (it != old_data->modules.end()) {
|
||||
old_data->modules.erase(it);
|
||||
}
|
||||
mutex_unlock(&old_data->mutex);
|
||||
}
|
||||
|
||||
module->internal.parent = parent;
|
||||
|
||||
// Add to new parent
|
||||
if (parent && parent->module_parent_private) {
|
||||
auto* new_data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
|
||||
mutex_lock(&new_data->mutex);
|
||||
new_data->modules.push_back(module);
|
||||
mutex_unlock(&new_data->mutex);
|
||||
}
|
||||
|
||||
error_t module_remove(struct Module* module) {
|
||||
mutex_lock(&ledger.mutex);
|
||||
ledger.modules.erase(std::remove(ledger.modules.begin(), ledger.modules.end(), module), ledger.modules.end());
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
@@ -93,7 +45,6 @@ error_t module_start(struct Module* module) {
|
||||
LOG_I(TAG, "start %s", module->name);
|
||||
|
||||
if (module->internal.started) return ERROR_NONE;
|
||||
if (!module->internal.parent) return ERROR_INVALID_STATE;
|
||||
|
||||
error_t error = module->start();
|
||||
module->internal.started = (error == ERROR_NONE);
|
||||
@@ -132,7 +83,19 @@ bool module_resolve_symbol(Module* module, const char* symbol_name, uintptr_t* s
|
||||
return false;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
bool module_resolve_symbol_global(const char* symbol_name, uintptr_t* symbol_address) {
|
||||
mutex_lock(&ledger.mutex);
|
||||
for (auto* module : ledger.modules) {
|
||||
if (!module_is_started(module))
|
||||
continue;
|
||||
if (module_resolve_symbol(module, symbol_name, symbol_address)) {
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
mutex_unlock(&ledger.mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user