Skip to content

Commit

Permalink
new(core/mod): added a m_mod_bind() API to bind a module lifecycle …
Browse files Browse the repository at this point in the history
…to that of another one.

Note that dependency cycle is not checked.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed May 1, 2022
1 parent b3df734 commit 1b32343
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
54 changes: 50 additions & 4 deletions Lib/core/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static void module_dtor(void *data) {
m_stack_free(&mod->recvs);
m_queue_free(&mod->stashed);
m_queue_free(&mod->batch.events);
m_list_free(&mod->bound_mods);
for (int i = 0; i < M_SRC_TYPE_END; i++) {
m_bst_free(&mod->srcs[i]);
}
Expand Down Expand Up @@ -514,6 +515,11 @@ _public_ int m_mod_register(const char *name, m_ctx_t *c, m_mod_t **mod_ref, con
break;
}

mod->bound_mods = m_list_new(NULL, mem_dtor);
if (!mod->bound_mods) {
break;
}

if (m_map_put(c->modules, mod->name, mod) == 0) {
mod->state = M_MOD_IDLE;

Expand Down Expand Up @@ -609,23 +615,63 @@ _public_ m_ctx_t *m_mod_ctx(const m_mod_t *mod) {
_public_ int m_mod_start(m_mod_t *mod) {
M_MOD_ASSERT_STATE(mod, M_MOD_IDLE | M_MOD_STOPPED);

return start(mod, true);
int ret = start(mod, true);
if (ret == 0) {
m_itr_foreach(mod->bound_mods, {
m_mod_t *bmod = m_itr_get(m_itr);
m_mod_start(bmod);
});
}
return ret;
}

_public_ int m_mod_pause(m_mod_t *mod) {
M_MOD_ASSERT_STATE(mod, M_MOD_RUNNING);

return stop(mod, false);
int ret = stop(mod, false);
if (ret == 0) {
m_itr_foreach(mod->bound_mods, {
m_mod_t *bmod = m_itr_get(m_itr);
m_mod_pause(bmod);
});
}
return ret;
}

_public_ int m_mod_resume(m_mod_t *mod) {
M_MOD_ASSERT_STATE(mod, M_MOD_PAUSED);

return start(mod, false);
int ret = start(mod, false);
if (ret == 0) {
m_itr_foreach(mod->bound_mods, {
m_mod_t *bmod = m_itr_get(m_itr);
m_mod_resume(bmod);
});
}
return ret;
}

_public_ int m_mod_stop(m_mod_t *mod) {
M_MOD_ASSERT_STATE(mod, M_MOD_RUNNING | M_MOD_PAUSED);

return stop(mod, true);
int ret = stop(mod, true);
if (ret == 0) {
m_itr_foreach(mod->bound_mods, {
m_mod_t *bmod = m_itr_get(m_itr);
m_mod_stop(bmod);
});
}
return ret;
}

_public_ int m_mod_bind(m_mod_t *mod, m_mod_t *ref) {
M_MOD_ASSERT(mod);
M_MOD_ASSERT(ref);

/*
* NOTE: dependency cycle is not avoided.
* This is deliberately not too smart to avoid costly checks.
* Be careful.
*/
return m_list_insert(ref->bound_mods, m_mem_ref(mod));
}
1 change: 1 addition & 0 deletions Lib/core/mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct _mod {
m_bst_t *srcs[M_SRC_TYPE_END]; // module's event sources
m_map_t *subscriptions; // module's subscriptions (map of ev_src_t*)
m_queue_t *stashed; // module's stashed messages
m_list_t *bound_mods; // modules that are bound to this module's state
CONST m_ctx_t *ctx; // Module's ctx
};

Expand Down
2 changes: 2 additions & 0 deletions Lib/core/public/module/mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ int m_mod_pause(m_mod_t *mod);
int m_mod_resume(m_mod_t *mod);
int m_mod_stop(m_mod_t *mod);

int m_mod_bind(m_mod_t *mod, m_mod_t *ref);

/* Module generic functions */
int m_mod_log(const m_mod_t *mod, const char *fmt, ...);
int m_mod_dump(const m_mod_t *mod);
Expand Down
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Maybe try to m_mod_load() the newly created file?
- [x] enable logging only when env LIBMODULE_LOG=debug/info/warn/error are enabled
- [x] Make use of new M_WARN, M_INFO, M_ERR macros
- [x] Getenv (LIBMODULE_LOG_OUTPUT) and default to stdout/stderr when nonset
- [ ] Improve logging: per-namespace -> LIBMODULE_LOG="mod/debug" "ctx/debug"; can be a list; "debug" remains to enable all debugs
- [ ] Improve logging: per-namespace -> LIBMODULE_MOD_LOG="debug" LIBMODULE_CTX_LOG="debug"; LIBMODULE_LOG="debug" remains to enable all debugs

- [x] Split some more private headers from priv.h
- [x] split libmodule_structs, libmodule_thpool, libmodule_mem ...
Expand Down Expand Up @@ -104,6 +104,8 @@ Maybe try to m_mod_load() the newly created file?
- [x] add a m_ctx_finalize() API that once set, deny any new module loaded in the context
- [x] add a set of m_mod_flags passed to m_ctx_register() that will be forwarded to each module registered

- [x] add a m_mod_bind() to bind a module's lifecycle to that of another one

### Reference-counted objects' life management

- [x] Keep objects alive as long as someone references them
Expand Down

0 comments on commit 1b32343

Please sign in to comment.