plugin-so: use list of components instead of glib hash table
[babeltrace.git] / lib / plugin / plugin-so.c
index 38f887463b0478f66031e11d5a0f512c851ce6bc..44aa967ed603b18820456e263468004178c319c1 100644 (file)
 #include <babeltrace/plugin/plugin-internal.h>
 #include <babeltrace/graph/component-class-internal.h>
 #include <babeltrace/types.h>
+#include <babeltrace/list-internal.h>
 #include <string.h>
 #include <stdlib.h>
 #include <glib.h>
 #include <gmodule.h>
 
-#define NATIVE_PLUGIN_SUFFIX           ".so"
+#define NATIVE_PLUGIN_SUFFIX           "." G_MODULE_SUFFIX
 #define NATIVE_PLUGIN_SUFFIX_LEN       sizeof(NATIVE_PLUGIN_SUFFIX)
 #define LIBTOOL_PLUGIN_SUFFIX          ".la"
 #define LIBTOOL_PLUGIN_SUFFIX_LEN      sizeof(LIBTOOL_PLUGIN_SUFFIX)
@@ -65,61 +66,49 @@ DECLARE_SECTION(struct __bt_plugin_component_class_descriptor const *, __bt_plug
 DECLARE_SECTION(struct __bt_plugin_component_class_descriptor_attribute const *, __bt_plugin_component_class_descriptor_attributes);
 
 /*
- * This hash table, global to the library, maps component class pointers
- * to shared library handles.
+ * This list, global to the library, keeps all component classes that
+ * have a reference to their shared library handles. It allows iteration
+ * on all component classes still present when the destructor executes
+ * to release the shared library handle references they might still have.
  *
- * The keys (component classes) are NOT owned by this hash table, whereas
- * the values (shared library handles) are owned by this hash table.
- *
- * The keys are the component classes created with
+ * The list items are the component classes created with
  * bt_plugin_add_component_class(). They keep the shared library handle
  * object created by their plugin alive so that the plugin's code is
  * not discarded when it could still be in use by living components
  * created from those component classes:
  *
- *     [component] --ref-> [component class] --through this HT-> [shlib handle]
- *
- * This hash table exists for two reasons:
+ *     [component] --ref-> [component class]-> [shlib handle]
  *
- * 1. To allow this application:
+ * It allows this use-case:
  *
  *        my_plugins = bt_plugin_create_all_from_file("/path/to/my-plugin.so");
  *        // instantiate components from a plugin's component classes
  *        // put plugins and free my_plugins here
  *        // user code of instantiated components still exists
  *
- * 2. To decouple the plugin subsystem from the component subsystem:
- *    while plugins objects need to know component class objects, the
- *    opposite is not necessary, thus it makes no sense for a component
- *    class to keep a reference to the plugin object from which it was
- *    created.
- *
- * An entry is removed from this HT when a component class is destroyed
- * thanks to a custom destroy listener. When the entry is removed, the
- * GLib function calls the value destroy notifier of the HT, which is
- * bt_put(). This decreases the reference count of the mapped shared
- * library handle. Assuming the original plugin object which contained
- * some component classes is put first, when the last component class is
- * removed from this HT, the shared library handle object's reference
- * count falls to zero and the shared library is finally closed.
+ * An entry is removed from this list when a component class is
+ * destroyed thanks to a custom destroy listener. When the entry is
+ * removed, the entry is removed from the list, and we release the
+ * reference on the shlib handle. Assuming the original plugin object
+ * which contained some component classes is put first, when the last
+ * component class is removed from this list, the shared library handle
+ * object's reference count falls to zero and the shared library is
+ * finally closed.
  */
+
 static
-GHashTable *comp_classes_to_shlib_handles;
-
-__attribute__((constructor)) static
-void init_comp_classes_to_shlib_handles(void) {
-       comp_classes_to_shlib_handles = g_hash_table_new_full(g_direct_hash,
-               g_direct_equal, NULL, bt_put);
-       assert(comp_classes_to_shlib_handles);
-       BT_LOGD_STR("Initialized component class to shared library handle hash table.");
-}
+BT_LIST_HEAD(component_class_list);
 
 __attribute__((destructor)) static
-void fini_comp_classes_to_shlib_handles(void) {
-       if (comp_classes_to_shlib_handles) {
-               g_hash_table_destroy(comp_classes_to_shlib_handles);
-               BT_LOGD_STR("Destroyed component class to shared library handle hash table.");
+void fini_comp_class_list(void)
+{
+       struct bt_component_class *comp_class, *tmp;
+
+       bt_list_for_each_entry_safe(comp_class, tmp, &component_class_list, node) {
+               bt_list_del(&comp_class->node);
+               BT_PUT(comp_class->so_handle);
        }
+       BT_LOGD_STR("Released references from all component classes to shared library handles.");
 }
 
 static
@@ -210,7 +199,14 @@ struct bt_plugin_so_shared_lib_handle *bt_plugin_so_shared_lib_handle_create(
 
        shared_lib_handle->module = g_module_open(path, 0);
        if (!shared_lib_handle->module) {
-               BT_LOGW("Cannot open GModule: %s: path=\"%s\"",
+               /*
+                * DEBUG-level logging because we're only _trying_ to
+                * open this file as a Babeltrace plugin: if it's not,
+                * it's not an error. And because this can be tried
+                * during bt_plugin_create_all_from_dir(), it's not even
+                * a warning.
+                */
+               BT_LOGD("Cannot open GModule: %s: path=\"%s\"",
                        g_module_error(), path);
                goto error;
        }
@@ -368,6 +364,13 @@ enum bt_plugin_status bt_plugin_so_init(
                                cur_attr->value.version.extra);
                        break;
                default:
+                       /*
+                        * WARN-level logging because this should not
+                        * happen with the appropriate ABI version. If
+                        * we're here, we know that for the reported
+                        * version of the ABI, this attribute is
+                        * unknown.
+                        */
                        BT_LOGW("Ignoring unknown plugin descriptor attribute: "
                                "plugin-path=\"%s\", plugin-name=\"%s\", "
                                "attr-type-name=\"%s\", attr-type-id=%d",
@@ -467,6 +470,15 @@ enum bt_plugin_status bt_plugin_so_init(
                                                cur_cc_descr_attr->value.notif_iter_seek_time_method;
                                        break;
                                default:
+                                       /*
+                                        * WARN-level logging because
+                                        * this should not happen with
+                                        * the appropriate ABI version.
+                                        * If we're here, we know that
+                                        * for the reported version of
+                                        * the ABI, this attribute is
+                                        * unknown.
+                                        */
                                        BT_LOGW("Ignoring unknown component class descriptor attribute: "
                                                "plugin-path=\"%s\", "
                                                "plugin-name=\"%s\", "
@@ -539,6 +551,13 @@ enum bt_plugin_status bt_plugin_so_init(
                                cc_full_descr->descriptor->methods.sink.consume);
                        break;
                default:
+                       /*
+                        * WARN-level logging because this should not
+                        * happen with the appropriate ABI version. If
+                        * we're here, we know that for the reported
+                        * version of the ABI, this component class type
+                        * is unknown.
+                        */
                        BT_LOGW("Ignoring unknown component class type: "
                                "plugin-path=\"%s\", plugin-name=\"%s\", "
                                "comp-class-name=\"%s\", comp-class-type=%d",
@@ -731,7 +750,7 @@ enum bt_plugin_status bt_plugin_so_init(
                 *
                 * This will call back
                 * bt_plugin_so_on_add_component_class() so that we can
-                * add a mapping in comp_classes_to_shlib_handles when
+                * add a mapping in the component class list when
                 * we know the component class is successfully added.
                 */
                status = bt_plugin_add_component_class(plugin,
@@ -842,7 +861,15 @@ struct bt_plugin_set *bt_plugin_so_create_all_from_sections(
                        descriptor->name, descriptor->major, descriptor->minor);
 
                if (descriptor->major > __BT_PLUGIN_VERSION_MAJOR) {
-                       BT_LOGW("Unknown ABI major version: abi-major=%d",
+                       /*
+                        * DEBUG-level logging because we're only
+                        * _trying_ to open this file as a compatible
+                        * Babeltrace plugin: if it's not, it's not an
+                        * error. And because this can be tried during
+                        * bt_plugin_create_all_from_dir(), it's not
+                        * even a warning.
+                        */
+                       BT_LOGD("Unknown ABI major version: abi-major=%d",
                                descriptor->major);
                        goto error;
                }
@@ -861,7 +888,15 @@ struct bt_plugin_set *bt_plugin_so_create_all_from_sections(
                        attrs_end, cc_descriptors_begin, cc_descriptors_end,
                        cc_descr_attrs_begin, cc_descr_attrs_end);
                if (status < 0) {
-                       BT_LOGW_STR("Cannot initialize SO plugin object from sections.");
+                       /*
+                        * DEBUG-level logging because we're only
+                        * _trying_ to open this file as a compatible
+                        * Babeltrace plugin: if it's not, it's not an
+                        * error. And because this can be tried during
+                        * bt_plugin_create_all_from_dir(), it's not
+                        * even a warning.
+                        */
+                       BT_LOGD_STR("Cannot initialize SO plugin object from sections.");
                        BT_PUT(plugin);
                        goto error;
                }
@@ -956,7 +991,7 @@ struct bt_plugin_set *bt_plugin_so_create_all_from_file(const char *path)
 
        shared_lib_handle = bt_plugin_so_shared_lib_handle_create(path);
        if (!shared_lib_handle) {
-               BT_LOGW_STR("Cannot create shared library handle.");
+               BT_LOGD_STR("Cannot create shared library handle.");
                goto end;
        }
 
@@ -1067,10 +1102,9 @@ static
 void plugin_comp_class_destroy_listener(struct bt_component_class *comp_class,
                void *data)
 {
-       gboolean exists = g_hash_table_remove(comp_classes_to_shlib_handles,
-               comp_class);
-       assert(exists);
-       BT_LOGV("Component class destroyed: removed entry from hash table: "
+       bt_list_del(&comp_class->node);
+       BT_PUT(comp_class->so_handle);
+       BT_LOGV("Component class destroyed: removed entry from list: "
                "comp-cls-addr=%p", comp_class);
 }
 
@@ -1083,9 +1117,8 @@ void bt_plugin_so_on_add_component_class(struct bt_plugin *plugin,
        assert(plugin->spec_data);
        assert(plugin->type == BT_PLUGIN_TYPE_SO);
 
-       /* Map component class pointer to shared lib handle in global HT */
-       g_hash_table_insert(comp_classes_to_shlib_handles, comp_class,
-               bt_get(spec->shared_lib_handle));
+       bt_list_add(&comp_class->node, &component_class_list);
+       comp_class->so_handle = bt_get(spec->shared_lib_handle);
 
        /* Add our custom destroy listener */
        bt_component_class_add_destroy_listener(comp_class,
This page took 0.026755 seconds and 4 git commands to generate.