Fix: only add components to graph if they are not already present
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 10 Mar 2017 19:16:46 +0000 (14:16 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:38 +0000 (12:57 -0400)
bt_graph_connect() creates a connection and adds the components
on both ends of the connection to the graph. This fix ensures
that the components are not added a second time if multiple
connections involving the same components are established.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
lib/component/graph.c

index d0ce1dfbcb79ab6ee5028cb8fee6112faa714fba..9982f5d076210d6d825f6d703d56be95a24e0ef1 100644 (file)
@@ -94,7 +94,8 @@ struct bt_connection *bt_graph_connect(struct bt_graph *graph,
        struct bt_component *upstream_component = NULL;
        struct bt_component *downstream_component = NULL;
        enum bt_component_status component_status;
-       bool components_added = false;
+       bool upstream_was_already_in_graph;
+       bool downstream_was_already_in_graph;
 
        if (!graph || !upstream_port || !downstream_port) {
                goto end;
@@ -115,6 +116,7 @@ struct bt_connection *bt_graph_connect(struct bt_graph *graph,
                fprintf(stderr, "Upstream component is already part of another graph\n");
                goto error;
        }
+       upstream_was_already_in_graph = (graph == upstream_graph);
 
        downstream_component = bt_port_get_component(downstream_port);
        assert(downstream_component);
@@ -123,6 +125,7 @@ struct bt_connection *bt_graph_connect(struct bt_graph *graph,
                fprintf(stderr, "Downstream component is already part of another graph\n");
                goto error;
        }
+       downstream_was_already_in_graph = (graph == downstream_graph);
 
        connection = bt_connection_create(graph, upstream_port,
                        downstream_port);
@@ -135,25 +138,25 @@ struct bt_connection *bt_graph_connect(struct bt_graph *graph,
         * transferred to the graph.
         */
        g_ptr_array_add(graph->connections, connection);
-       g_ptr_array_add(graph->components, upstream_component);
-       g_ptr_array_add(graph->components, downstream_component);
-       if (bt_component_get_class_type(downstream_component) ==
-                       BT_COMPONENT_CLASS_TYPE_SINK) {
-               g_queue_push_tail(graph->sinks_to_consume,
-                               downstream_component);
+
+       if (!upstream_was_already_in_graph) {
+               g_ptr_array_add(graph->components, upstream_component);
+               bt_component_set_graph(upstream_component, graph);
+       }
+       if (!downstream_was_already_in_graph) {
+               g_ptr_array_add(graph->components, downstream_component);
+               bt_component_set_graph(downstream_component, graph);
+               if (bt_component_get_class_type(downstream_component) ==
+                               BT_COMPONENT_CLASS_TYPE_SINK) {
+                       g_queue_push_tail(graph->sinks_to_consume,
+                                       downstream_component);
+               }
        }
 
        /*
         * The graph is now the parent of these components which garantees their
         * existence for the duration of the graph's lifetime.
         */
-       bt_component_set_graph(upstream_component, graph);
-       bt_put(upstream_component);
-       bt_component_set_graph(downstream_component, graph);
-       bt_put(downstream_component);
-
-       /* Rollback the connection from this point on. */
-       components_added = true;
 
        /*
         * The components and connection are added to the graph before invoking
This page took 0.027145 seconds and 4 git commands to generate.