lib: make the "port connected" method return a status
[babeltrace.git] / lib / graph / graph.c
index c7c27058c771b46e48d320735349f125ffb8d8af..6c88f55e0b5af00d34f1320fe07fb47527c4b85b 100644 (file)
@@ -35,6 +35,9 @@
 #include <babeltrace/graph/component-source.h>
 #include <babeltrace/graph/component-filter.h>
 #include <babeltrace/graph/port.h>
+#include <babeltrace/graph/notification-internal.h>
+#include <babeltrace/graph/notification-event-internal.h>
+#include <babeltrace/graph/notification-packet-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/types.h>
 #include <babeltrace/values.h>
@@ -115,7 +118,7 @@ void bt_graph_destroy(struct bt_object *obj)
         * ensures that this function is not called two times.
         */
        BT_LOGD("Destroying graph: addr=%p", graph);
-       obj->ref_count.count++;
+       obj->ref_count++;
 
        /*
         * Cancel the graph to disallow some operations, like creating
@@ -129,14 +132,20 @@ void bt_graph_destroy(struct bt_object *obj)
        call_remove_listeners(graph->listeners.ports_connected);
        call_remove_listeners(graph->listeners.ports_disconnected);
 
+       if (graph->notifications) {
+               g_ptr_array_free(graph->notifications, TRUE);
+       }
+
        if (graph->connections) {
                BT_LOGD_STR("Destroying connections.");
                g_ptr_array_free(graph->connections, TRUE);
        }
+
        if (graph->components) {
                BT_LOGD_STR("Destroying components.");
                g_ptr_array_free(graph->components, TRUE);
        }
+
        if (graph->sinks_to_consume) {
                g_queue_free(graph->sinks_to_consume);
        }
@@ -157,9 +166,39 @@ void bt_graph_destroy(struct bt_object *obj)
                g_array_free(graph->listeners.ports_disconnected, TRUE);
        }
 
+       bt_object_pool_finalize(&graph->event_notif_pool);
+       bt_object_pool_finalize(&graph->packet_begin_notif_pool);
+       bt_object_pool_finalize(&graph->packet_end_notif_pool);
        g_free(graph);
 }
 
+static
+void destroy_notification_event(struct bt_notification *notif,
+               struct bt_graph *graph)
+{
+       bt_notification_event_destroy(notif);
+}
+
+static
+void destroy_notification_packet_begin(struct bt_notification *notif,
+               struct bt_graph *graph)
+{
+       bt_notification_packet_begin_destroy(notif);
+}
+
+static
+void destroy_notification_packet_end(struct bt_notification *notif,
+               struct bt_graph *graph)
+{
+       bt_notification_packet_end_destroy(notif);
+}
+
+static
+void notify_notification_graph_is_destroyed(struct bt_notification *notif)
+{
+       bt_notification_unlink_graph(notif);
+}
+
 struct bt_graph *bt_graph_create(void)
 {
        struct bt_graph *graph;
@@ -172,14 +211,15 @@ struct bt_graph *bt_graph_create(void)
                goto end;
        }
 
-       bt_object_init(graph, bt_graph_destroy);
-
-       graph->connections = g_ptr_array_new_with_free_func(bt_object_release);
+       bt_object_init_shared(&graph->base, bt_graph_destroy);
+       graph->connections = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_object_try_spec_release);
        if (!graph->connections) {
                BT_LOGE_STR("Failed to allocate one GPtrArray.");
                goto error;
        }
-       graph->components = g_ptr_array_new_with_free_func(bt_object_release);
+       graph->components = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) bt_object_try_spec_release);
        if (!graph->components) {
                BT_LOGE_STR("Failed to allocate one GPtrArray.");
                goto error;
@@ -190,7 +230,7 @@ struct bt_graph *bt_graph_create(void)
                goto error;
        }
 
-       graph->can_consume = BT_TRUE;
+       bt_graph_set_can_consume(graph, BT_TRUE);
        ret = init_listeners_array(&graph->listeners.port_added);
        if (ret) {
                BT_LOGE_STR("Cannot create the \"port added\" listener array.");
@@ -215,6 +255,38 @@ struct bt_graph *bt_graph_create(void)
                goto error;
        }
 
+       ret = bt_object_pool_initialize(&graph->event_notif_pool,
+               (bt_object_pool_new_object_func) bt_notification_event_new,
+               (bt_object_pool_destroy_object_func) destroy_notification_event,
+               graph);
+       if (ret) {
+               BT_LOGE("Failed to initialize event notification pool: ret=%d",
+                       ret);
+               goto error;
+       }
+
+       ret = bt_object_pool_initialize(&graph->packet_begin_notif_pool,
+               (bt_object_pool_new_object_func) bt_notification_packet_begin_new,
+               (bt_object_pool_destroy_object_func) destroy_notification_packet_begin,
+               graph);
+       if (ret) {
+               BT_LOGE("Failed to initialize packet beginning notification pool: ret=%d",
+                       ret);
+               goto error;
+       }
+
+       ret = bt_object_pool_initialize(&graph->packet_end_notif_pool,
+               (bt_object_pool_new_object_func) bt_notification_packet_end_new,
+               (bt_object_pool_destroy_object_func) destroy_notification_packet_end,
+               graph);
+       if (ret) {
+               BT_LOGE("Failed to initialize packet end notification pool: ret=%d",
+                       ret);
+               goto error;
+       }
+
+       graph->notifications = g_ptr_array_new_with_free_func(
+               (GDestroyNotify) notify_notification_graph_is_destroyed);
        BT_LOGD("Created graph object: addr=%p", graph);
 
 end:
@@ -269,7 +341,7 @@ enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
                goto end;
        }
 
-       graph->can_consume = BT_FALSE;
+       bt_graph_set_can_consume(graph, BT_FALSE);
 
        /* Ensure appropriate types for upstream and downstream ports. */
        if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
@@ -378,17 +450,56 @@ enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
         * Notify both components that their port is connected.
         */
        BT_LOGD_STR("Notifying upstream component that its port is connected.");
-       bt_component_port_connected(upstream_component, upstream_port,
-               downstream_port);
+       component_status = bt_component_port_connected(upstream_component,
+               upstream_port, downstream_port);
+       if (component_status != BT_COMPONENT_STATUS_OK) {
+               BT_LOGW("Error while notifying upstream component that its port is connected: "
+                       "status=%s, graph-addr=%p, "
+                       "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
+                       "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
+                       "upstream-port-addr=%p, upstream-port-name=\"%s\", "
+                       "downstream-port-addr=%p, downstream-port-name=\"%s\"",
+                       bt_component_status_string(component_status), graph,
+                       upstream_component, bt_component_get_name(upstream_component),
+                       downstream_component, bt_component_get_name(downstream_component),
+                       upstream_port, bt_port_get_name(upstream_port),
+                       downstream_port, bt_port_get_name(downstream_port));
+               bt_connection_end(connection, true);
+               status = bt_graph_status_from_component_status(
+                       component_status);
+               goto end;
+       }
+
+       connection->notified_upstream_port_connected = true;
        BT_LOGD_STR("Notifying downstream component that its port is connected.");
-       bt_component_port_connected(downstream_component, downstream_port,
-               upstream_port);
+       component_status = bt_component_port_connected(downstream_component,
+               downstream_port, upstream_port);
+       if (component_status != BT_COMPONENT_STATUS_OK) {
+               BT_LOGW("Error while notifying downstream component that its port is connected: "
+                       "status=%s, graph-addr=%p, "
+                       "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
+                       "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
+                       "upstream-port-addr=%p, upstream-port-name=\"%s\", "
+                       "downstream-port-addr=%p, downstream-port-name=\"%s\"",
+                       bt_component_status_string(component_status), graph,
+                       upstream_component, bt_component_get_name(upstream_component),
+                       downstream_component, bt_component_get_name(downstream_component),
+                       upstream_port, bt_port_get_name(upstream_port),
+                       downstream_port, bt_port_get_name(downstream_port));
+               bt_connection_end(connection, true);
+               status = bt_graph_status_from_component_status(
+                       component_status);
+               goto end;
+       }
+
+       connection->notified_downstream_port_connected = true;
 
        /*
         * Notify the graph's creator that both ports are connected.
         */
        BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
        bt_graph_notify_ports_connected(graph, upstream_port, downstream_port);
+       connection->notified_graph_ports_connected = true;
        BT_LOGD("Connected component ports within graph: "
                "graph-addr=%p, "
                "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
@@ -414,15 +525,15 @@ end:
        bt_put(downstream_component);
        bt_put(connection);
        if (graph) {
-               graph->can_consume = init_can_consume;
+               (void) init_can_consume;
+               bt_graph_set_can_consume(graph, init_can_consume);
        }
        return status;
 }
 
-static
+static inline
 enum bt_graph_status consume_graph_sink(struct bt_component *sink)
 {
-       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
        enum bt_component_status comp_status;
 
        BT_ASSERT(sink);
@@ -430,25 +541,14 @@ enum bt_graph_status consume_graph_sink(struct bt_component *sink)
        BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
                sink, bt_component_get_name(sink),
                bt_component_status_string(comp_status));
-
-       switch (comp_status) {
-       case BT_COMPONENT_STATUS_OK:
-               break;
-       case BT_COMPONENT_STATUS_END:
-               status = BT_GRAPH_STATUS_END;
-               break;
-       case BT_COMPONENT_STATUS_AGAIN:
-               status = BT_GRAPH_STATUS_AGAIN;
-               break;
-       case BT_COMPONENT_STATUS_INVALID:
-               status = BT_GRAPH_STATUS_INVALID;
-               break;
-       default:
-               status = BT_GRAPH_STATUS_ERROR;
-               break;
-       }
-
-       return status;
+       BT_ASSERT_PRE(comp_status == BT_COMPONENT_STATUS_OK ||
+               comp_status == BT_COMPONENT_STATUS_END ||
+               comp_status == BT_COMPONENT_STATUS_AGAIN ||
+               comp_status == BT_COMPONENT_STATUS_ERROR ||
+               comp_status == BT_COMPONENT_STATUS_NOMEM,
+               "Invalid component status returned by consuming function: "
+               "status=%s", bt_component_status_string(comp_status));
+       return (enum bt_graph_status) comp_status;
 }
 
 /*
@@ -456,7 +556,7 @@ enum bt_graph_status consume_graph_sink(struct bt_component *sink)
  * this function. This function adds it back to the queue if there's
  * still something to consume afterwards.
  */
-static
+static inline
 enum bt_graph_status consume_sink_node(struct bt_graph *graph,
                GList *node)
 {
@@ -465,7 +565,7 @@ enum bt_graph_status consume_sink_node(struct bt_graph *graph,
 
        sink = node->data;
        status = consume_graph_sink(sink);
-       if (status != BT_GRAPH_STATUS_END) {
+       if (unlikely(status != BT_GRAPH_STATUS_END)) {
                g_queue_push_tail_link(graph->sinks_to_consume, node);
                goto end;
        }
@@ -519,7 +619,7 @@ end:
        return status;
 }
 
-BT_HIDDEN
+static inline
 enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
 {
        enum bt_graph_status status = BT_GRAPH_STATUS_OK;
@@ -530,7 +630,7 @@ enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
        BT_ASSERT_PRE(graph->has_sink,
                "Graph has no sink component: %!+g", graph);
 
-       if (g_queue_is_empty(graph->sinks_to_consume)) {
+       if (unlikely(g_queue_is_empty(graph->sinks_to_consume))) {
                BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
                status = BT_GRAPH_STATUS_END;
                goto end;
@@ -554,10 +654,10 @@ enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
        BT_ASSERT_PRE(!graph->canceled, "Graph is canceled: %!+g", graph);
        BT_ASSERT_PRE(graph->can_consume,
                "Cannot consume graph in its current state: %!+g", graph);
-       graph->can_consume = BT_FALSE;
+       bt_graph_set_can_consume(graph, BT_FALSE);
        status = bt_graph_consume_no_check(graph);
-       graph->can_consume = BT_TRUE;
-       return BT_GRAPH_STATUS_OK;
+       bt_graph_set_can_consume(graph, BT_TRUE);
+       return status;
 }
 
 enum bt_graph_status bt_graph_run(struct bt_graph *graph)
@@ -577,13 +677,9 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                goto end;
        }
 
-       if (!graph->can_consume) {
-               BT_LOGW_STR("Cannot run graph in its current state.");
-               status = BT_GRAPH_STATUS_CANNOT_CONSUME;
-               goto end;
-       }
-
-       graph->can_consume = BT_FALSE;
+       BT_ASSERT_PRE(graph->can_consume,
+               "Cannot consume graph in its current state: %!+g", graph);
+       bt_graph_set_can_consume(graph, BT_FALSE);
        BT_LOGV("Running graph: addr=%p", graph);
 
        do {
@@ -593,7 +689,7 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                 * signal, this is not a warning nor an error, it was
                 * intentional: log with a DEBUG level only.
                 */
-               if (graph->canceled) {
+               if (unlikely(graph->canceled)) {
                        BT_LOGD("Stopping the graph: graph is canceled: "
                                "graph-addr=%p", graph);
                        status = BT_GRAPH_STATUS_CANCELED;
@@ -601,7 +697,7 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                }
 
                status = bt_graph_consume_no_check(graph);
-               if (status == BT_GRAPH_STATUS_AGAIN) {
+               if (unlikely(status == BT_GRAPH_STATUS_AGAIN)) {
                        /*
                         * If AGAIN is received and there are multiple
                         * sinks, go ahead and consume from the next
@@ -628,7 +724,7 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
 end:
        BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
        if (graph) {
-               graph->can_consume = BT_TRUE;
+               bt_graph_set_can_consume(graph, BT_TRUE);
        }
        return status;
 }
@@ -1115,7 +1211,7 @@ int bt_graph_remove_unconnected_component(struct bt_graph *graph,
 
        BT_ASSERT(graph);
        BT_ASSERT(component);
-       BT_ASSERT(component->base.ref_count.count == 0);
+       BT_ASSERT(component->base.ref_count == 0);
        BT_ASSERT(bt_component_borrow_graph(component) == graph);
 
        init_can_consume = graph->can_consume;
@@ -1173,8 +1269,8 @@ int bt_graph_remove_unconnected_component(struct bt_graph *graph,
        }
 
        /*
-        * This calls bt_object_release() on the component, and since
-        * its reference count is 0, its destructor is called. Its
+        * This calls bt_object_try_spec_release() on the component, and
+        * since its reference count is 0, its destructor is called. Its
         * destructor calls the user's finalization method (if set).
         */
        g_ptr_array_remove(graph->components, component);
@@ -1187,3 +1283,21 @@ end:
        graph->can_consume = init_can_consume;
        return ret;
 }
+
+BT_HIDDEN
+void bt_graph_add_notification(struct bt_graph *graph,
+               struct bt_notification *notif)
+{
+       BT_ASSERT(graph);
+       BT_ASSERT(notif);
+
+       /*
+        * It's okay not to take a reference because, when a
+        * notification's reference count drops to 0, either:
+        *
+        * * It is recycled back to one of this graph's pool.
+        * * It is destroyed because it doesn't have any link to any
+        *   graph, which means the original graph is already destroyed.
+        */
+       g_ptr_array_add(graph->notifications, notif);
+}
This page took 0.028255 seconds and 4 git commands to generate.