Skip to content

Commit

Permalink
Avoid using yet another hashmap to store module_load handlers. Use RT…
Browse files Browse the repository at this point in the history
…LD_NOLOAD flag instead.
  • Loading branch information
FedeDP committed Jul 9, 2019
1 parent 7e938f6 commit f31e6af
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
19 changes: 8 additions & 11 deletions Lib/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ static module_ret_code init_ctx(const char *ctx_name, m_context **context) {

(*context)->modules = map_new();
(*context)->topics = map_new();
(*context)->loaded = map_new();

(*context)->name = ctx_name;
if ((*context)->topics && (*context)->modules && (*context)->loaded &&
if ((*context)->topics && (*context)->modules &&
map_put(ctx, (*context)->name, *context, false, false) == MAP_OK) {

return MOD_OK;
Expand All @@ -48,7 +47,6 @@ static void destroy_ctx(m_context *context) {
MODULE_DEBUG("Destroying context '%s'.\n", context->name);
map_free(context->modules);
map_free(context->topics);
map_free(context->loaded);
poll_close(context->fd, &context->pevents, &context->max_events);
map_remove(ctx, context->name);
memhook._free(context);
Expand Down Expand Up @@ -364,25 +362,24 @@ module_ret_code module_load(const char *module_path, const char *ctx_name) {
* Check that requested module has been created in requested ctx,
* by looking at requested ctx number of modules
*/
if (module_size == map_length(c->modules) || // no new module registered in requested ctx
map_put(c->loaded, module_path, handle, false, false) != MAP_OK) {

if (module_size == map_length(c->modules)) {
dlclose(handle);
return MOD_ERR;
}
return MOD_OK;
}

module_ret_code module_unload(const char *module_path, const char *ctx_name) {
module_ret_code module_unload(const char *module_path) {
MOD_PARAM_ASSERT(module_path);
FIND_CTX(ctx_name);

void *handle = map_get(c->loaded, module_path);
if (handle && map_remove(c->loaded, module_path) == MAP_OK) {
void *handle = dlopen(module_path, RTLD_NOW | RTLD_NOLOAD);
if (handle) {
/* RTLD_NOLOAD does still increment refcounter... */
dlclose(handle);
dlclose(handle);

return MOD_OK;
}
MODULE_DEBUG("Dlopen failed with error: %s\n", dlerror());
return MOD_ERR;
}

Expand Down
3 changes: 1 addition & 2 deletions Lib/priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "stack.h"

#ifndef NDEBUG
#define MODULE_DEBUG printf("Libmodule: "); printf
#define MODULE_DEBUG printf("Libmodule @ %s:%d| ", __func__, __LINE__); printf
#else
#define MODULE_DEBUG (void)
#endif
Expand Down Expand Up @@ -118,7 +118,6 @@ struct _context {
void *pevents;
int max_events;
map_t *topics;
map_t *loaded;
};

/* Defined in module.c */
Expand Down
2 changes: 1 addition & 1 deletion Lib/public/module/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ _public_ module_ret_code module_deregister(self_t **self);

/* External shared object module runtime loading */
_public_ module_ret_code module_load(const char *module_path, const char *ctx_name);
_public_ module_ret_code module_unload(const char *module_path, const char *ctx_name);
_public_ module_ret_code module_unload(const char *module_path);

/* Module state getters */
_public_ _pure_ bool module_is(const self_t *self, const enum module_states st);
Expand Down
2 changes: 1 addition & 1 deletion Lib/public/module/module_easy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void _ctor2_ module_pre_start(void)

/* Defines for easy API (with no need bothering with both _self and ctx) */
#define m_load(path) module_load(path, MODULES_DEFAULT_CTX)
#define m_unload(path) module_unload(path, MODULES_DEFAULT_CTX)
#define m_unload(path) module_unload(path) // just an alias

#define m_is(state) module_is(self(), state)
#define m_dump() module_dump(self())
Expand Down
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
- [x] Provide a default (weak symbol) main() that just runs modules_ctx_loop() on any ctx it found
- [x] Rename MODULE_DEFAULT_CTX and MODULE_MODULE_MAX_EVENTS to MODULES_*
- [x] Actually call init() callback first time module is started, even without passing from evaluate_module (thus without looping ctx)
- [x] module_load/unload should use RTLD_NOLOAD flag instead of yet another hashmap

- [ ] Rename pubsub_msg to ps_msg inside msg_t?
- [ ] Rename pubsub_msg_t to ps_msg_t?

### Doc
- [x] module_dump
Expand Down
6 changes: 2 additions & 4 deletions docs/src/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Again, where not specified, these functions return a :ref:`module_ret_code <modu
:param self: pointer to module's handler. It is set to NULL after this call.
:type self: :c:type:`self_t **`

.. c:function:: m_load(path, ctx_name)
.. c:function:: module_load(path, ctx_name)
Attaches a new module from a .so file to ctx_name context. If module.so has a different context, this will be an error.

Expand All @@ -270,14 +270,12 @@ Again, where not specified, these functions return a :ref:`module_ret_code <modu
:type path: :c:type:`const char *`
:type ctx_name: :c:type:`const char *`

.. c:function:: m_unload(path, ctx_name)
.. c:function:: module_unload(path)
Detaches a module loaded from a .so file.

:param path: shared object path.
:param ctx_name: module's context name.
:type path: :c:type:`const char *`
:type ctx_name: :c:type:`const char *`

.. c:function:: module_is(self, state)
Expand Down

0 comments on commit f31e6af

Please sign in to comment.