feat(module manager): add thread_init and thread_exit API

This commit is contained in:
yangwei
2024-10-18 15:02:36 +08:00
parent 722ae7483b
commit a7b79a0e22
6 changed files with 144 additions and 51 deletions

View File

@@ -29,9 +29,6 @@ struct stellar_module_manager *stellar_module_manager_new(const char *module_spe
int mod_num = 0;
toml_table_t *conf = toml_parse_file(fp, NULL, 0);
toml_array_t* mod_array = toml_array_in(conf, "module");
char *path = NULL;
char *init_func_name = NULL;
char *exit_func_name = NULL;
if(mod_array==NULL)goto MODULE_SPEC_LOAD_END;
@@ -45,44 +42,73 @@ struct stellar_module_manager *stellar_module_manager_new(const char *module_spe
const char *path_raw = toml_raw_in(toml_mod, "path");
const char *init_func_name_raw = toml_raw_in(toml_mod, "init");
const char *exit_func_name_raw = toml_raw_in(toml_mod, "exit");
if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) ||
toml_rtos(exit_func_name_raw, &exit_func_name))
{
goto MODULE_SPEC_LOAD_END;
}
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
if (!handle) {
fprintf(stderr, "Error loading module %s: %s\n", path, dlerror());
goto MODULE_SPEC_LOAD_END;
}
const char *thread_init_func_name_raw = toml_raw_in(toml_mod, "thread_init");
const char *thread_exit_func_name_raw = toml_raw_in(toml_mod, "thread_exit");
mod_mgr->module_specs[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (mod_mgr->module_specs[i].on_init_cb)
toml_rtos(path_raw, &mod_mgr->module_specs[i].path);
toml_rtos(init_func_name_raw, &mod_mgr->module_specs[i].instance_init_cb_name);
toml_rtos(exit_func_name_raw, &mod_mgr->module_specs[i].instance_exit_cb_name);
toml_rtos(thread_init_func_name_raw, &mod_mgr->module_specs[i].thread_init_cb_name);
toml_rtos(thread_exit_func_name_raw, &mod_mgr->module_specs[i].thread_exit_cb_name);
void* handle = dlopen(mod_mgr->module_specs[i].path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
if (!handle) {
fprintf(stderr, "Error loading module %s: %s\n", mod_mgr->module_specs[i].path, dlerror());
break;
}
if (mod_mgr->module_specs[i].instance_init_cb_name)
{
mod_mgr->module_specs[i].mod = mod_mgr->module_specs[i].on_init_cb(mod_mgr);
if (stellar_module_manager_get_module(mod_mgr, mod_mgr->module_specs[i].mod->name) == NULL)
mod_mgr->module_specs[i].on_instance_init_cb =
(module_on_instance_init_func *)dlsym(handle, mod_mgr->module_specs[i].instance_init_cb_name);
if (mod_mgr->module_specs[i].on_instance_init_cb)
{
mod_mgr->module_specs[i].is_init_succ = true;
mod_mgr->module_specs[i].mod = mod_mgr->module_specs[i].on_instance_init_cb(mod_mgr);
if (stellar_module_manager_get_module(mod_mgr, mod_mgr->module_specs[i].mod->name) == NULL)
{
mod_mgr->module_specs[i].init_succ = true;
}
else
{
fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name);
if (mod_mgr->module_specs[i].on_instance_exit_cb)
mod_mgr->module_specs[i].on_instance_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
}
}
else
{
fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name);
if (mod_mgr->module_specs[i].on_exit_cb)
mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
fprintf(stderr, "Could not load init function %s: %s\n", mod_mgr->module_specs[i].instance_init_cb_name, dlerror());
}
}
else
if (mod_mgr->module_specs[i].instance_exit_cb_name)
{
fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror());
mod_mgr->module_specs[i].on_instance_exit_cb =
(module_on_instance_exit_func *)dlsym(handle, mod_mgr->module_specs[i].instance_exit_cb_name);
if (!mod_mgr->module_specs[i].on_instance_exit_cb)
{
fprintf(stderr, "Could not load exit function %s: %s\n", mod_mgr->module_specs[i].instance_exit_cb_name, dlerror());
}
}
mod_mgr->module_specs[i].on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
if (!mod_mgr->module_specs[i].on_exit_cb) {
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
if (mod_mgr->module_specs[i].thread_init_cb_name)
{
mod_mgr->module_specs[i].on_thread_init_cb =
(module_on_thread_init_func *)dlsym(handle, mod_mgr->module_specs[i].thread_init_cb_name);
if (!mod_mgr->module_specs[i].on_thread_init_cb)
{
fprintf(stderr, "Could not load init function %s: %s\n", mod_mgr->module_specs[i].thread_init_cb_name, dlerror());
}
}
if (mod_mgr->module_specs[i].thread_exit_cb_name)
{
mod_mgr->module_specs[i].on_thread_exit_cb =
(module_on_thread_exit_func *)dlsym(handle, mod_mgr->module_specs[i].thread_exit_cb_name);
if (!mod_mgr->module_specs[i].on_thread_exit_cb)
{
fprintf(stderr, "Could not load exit function %s: %s\n", mod_mgr->module_specs[i].thread_exit_cb_name, dlerror());
}
}
mod_mgr->module_specs[i].path=path;
mod_mgr->module_specs[i].init_cb_name=init_func_name;
mod_mgr->module_specs[i].exit_cb_name=exit_func_name;
mod_mgr->load_module_num+=1;
}
@@ -104,14 +130,16 @@ void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
{
for (int i = 0; i < mod_mgr->load_module_num; i++)
{
if (mod_mgr->module_specs[i].on_exit_cb != NULL &&
mod_mgr->module_specs[i].is_init_succ)
if (mod_mgr->module_specs[i].on_instance_exit_cb != NULL &&
mod_mgr->module_specs[i].init_succ)
{
mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
mod_mgr->module_specs[i].on_instance_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
}
if(mod_mgr->module_specs[i].path)FREE(mod_mgr->module_specs[i].path);
if(mod_mgr->module_specs[i].init_cb_name)FREE(mod_mgr->module_specs[i].init_cb_name);
if(mod_mgr->module_specs[i].exit_cb_name)FREE(mod_mgr->module_specs[i].exit_cb_name);
if(mod_mgr->module_specs[i].instance_init_cb_name)FREE(mod_mgr->module_specs[i].instance_init_cb_name);
if(mod_mgr->module_specs[i].instance_exit_cb_name)FREE(mod_mgr->module_specs[i].instance_exit_cb_name);
if(mod_mgr->module_specs[i].thread_init_cb_name)FREE(mod_mgr->module_specs[i].thread_init_cb_name);
if(mod_mgr->module_specs[i].thread_exit_cb_name)FREE(mod_mgr->module_specs[i].thread_exit_cb_name);
}
FREE(mod_mgr->module_specs);
}
@@ -156,10 +184,35 @@ struct mq_runtime *stellar_module_manager_get_mq_runtime(struct stellar_module_m
return local_mq_rt;
}
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr __unused, int thread_id, struct mq_runtime *mq_rt)
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr, int thread_id, struct mq_runtime *mq_rt)
{
local_thread_id=thread_id;
local_mq_rt=mq_rt;
for(int i=0; i<mod_mgr->load_module_num; i++)
{
if(mod_mgr->module_specs[i].mod == NULL)break;
if(mod_mgr->module_specs[i].on_thread_init_cb && mod_mgr->module_specs[i].init_succ)
{
mod_mgr->module_specs[i].on_thread_init_cb(mod_mgr, thread_id, mod_mgr->module_specs[i].mod);
}
}
return;
}
void stellar_module_manager_unregister_thread(struct stellar_module_manager *mod_mgr, int thread_id)
{
assert(local_thread_id==thread_id);
for(int i=0; i<mod_mgr->load_module_num; i++)
{
if(mod_mgr->module_specs[i].mod == NULL)break;
if(mod_mgr->module_specs[i].on_thread_exit_cb && mod_mgr->module_specs[i].init_succ)
{
mod_mgr->module_specs[i].on_thread_exit_cb(mod_mgr, thread_id, mod_mgr->module_specs[i].mod);
}
}
local_thread_id=-1;
local_mq_rt=NULL;
return;
}
@@ -171,7 +224,7 @@ struct stellar_module *stellar_module_manager_get_module(struct stellar_module_m
for(int i=0; i<mod_mgr->load_module_num; i++)
{
if(mod_mgr->module_specs[i].mod == NULL)break;
if(strcmp(mod_mgr->module_specs[i].mod->name, module_name)==0 && mod_mgr->module_specs[i].is_init_succ)
if(strcmp(mod_mgr->module_specs[i].mod->name, module_name)==0 && mod_mgr->module_specs[i].init_succ)
{
return mod_mgr->module_specs[i].mod;
}