lib: update and simplify the `bt_object` API
[babeltrace.git] / lib / graph / connection.c
index 96eed0f329aaaaf9e9ea394a1ebe08e8f67c66b8..10f8074762f3d14c84438c884cabc38271b696bf 100644 (file)
@@ -39,6 +39,8 @@
 #include <babeltrace/graph/port-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/compiler-internal.h>
+#include <babeltrace/assert-internal.h>
+#include <stdlib.h>
 #include <glib.h>
 
 static
@@ -46,7 +48,6 @@ void bt_connection_destroy(struct bt_object *obj)
 {
        struct bt_connection *connection = container_of(obj,
                        struct bt_connection, base);
-       size_t i;
 
        BT_LOGD("Destroying connection: addr=%p", connection);
 
@@ -59,32 +60,18 @@ void bt_connection_destroy(struct bt_object *obj)
         * Because connections are destroyed before components within a
         * graph, this ensures that notification iterators are always
         * finalized before their upstream component.
+        *
+        * Ending the connection does exactly this. We pass `false` to
+        * bt_connection_end() here to avoid removing this connection
+        * from the graph: if we're here, we're already in the graph's
+        * destructor.
         */
-       if (connection->iterators) {
-               for (i = 0; i < connection->iterators->len; i++) {
-                       struct bt_notification_iterator *iterator =
-                               g_ptr_array_index(connection->iterators, i);
-
-                       BT_LOGD("Finalizing notification iterator created by this connection: "
-                               "iter-addr=%p", iterator);
-                       bt_notification_iterator_finalize(iterator);
-
-                       /*
-                        * Make sure this iterator does not try to
-                        * remove itself from this connection's
-                        * iterators on destruction because this
-                        * connection won't exist anymore.
-                        */
-                       bt_notification_iterator_set_connection(iterator,
-                               NULL);
-               }
-
-               g_ptr_array_free(connection->iterators, TRUE);
-       }
+       bt_connection_end(connection, false);
+       g_ptr_array_free(connection->iterators, TRUE);
 
        /*
-        * No bt_put on ports as a connection only holds _weak_ references
-        * to them.
+        * No bt_put on ports as a connection only holds _weak_
+        * references to them.
         */
        g_free(connection);
 }
@@ -92,9 +79,9 @@ void bt_connection_destroy(struct bt_object *obj)
 static
 void bt_connection_try_remove_from_graph(struct bt_connection *connection)
 {
-       void *graph = bt_object_borrow_parent(&connection->base);
+       void *graph = (void *) bt_object_borrow_parent(&connection->base);
 
-       if (connection->base.ref_count.count > 0 ||
+       if (connection->base.ref_count > 0 ||
                        connection->downstream_port ||
                        connection->upstream_port ||
                        connection->iterators->len > 0) {
@@ -130,10 +117,10 @@ void bt_connection_parent_is_owner(struct bt_object *obj)
        bt_connection_try_remove_from_graph(connection);
 }
 
-struct bt_connection *bt_connection_from_private_connection(
+struct bt_connection *bt_connection_borrow_from_private(
                struct bt_private_connection *private_connection)
 {
-       return bt_get(bt_connection_from_private(private_connection));
+       return (void *) private_connection;
 }
 
 BT_HIDDEN
@@ -164,8 +151,9 @@ struct bt_connection *bt_connection_create(
                goto end;
        }
 
-       bt_object_init(connection, bt_connection_destroy);
-       bt_object_set_parent_is_owner_listener(connection,
+       bt_object_init_shared_with_parent(&connection->base,
+               bt_connection_destroy);
+       bt_object_set_parent_is_owner_listener_func(&connection->base,
                bt_connection_parent_is_owner);
        connection->iterators = g_ptr_array_new();
        if (!connection->iterators) {
@@ -181,7 +169,7 @@ struct bt_connection *bt_connection_create(
        bt_port_set_connection(upstream_port, connection);
        BT_LOGD_STR("Setting downstream port's connection.");
        bt_port_set_connection(downstream_port, connection);
-       bt_object_set_parent(connection, &graph->base);
+       bt_object_set_parent(&connection->base, &graph->base);
        BT_LOGD("Created connection: "
                "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", "
                "downstream-port-addr=%p, downstream-port-name=\"%s\", "
@@ -195,22 +183,32 @@ end:
 }
 
 BT_HIDDEN
-void bt_connection_disconnect_ports(struct bt_connection *conn)
+void bt_connection_end(struct bt_connection *conn,
+               bool try_remove_from_graph)
 {
        struct bt_component *downstream_comp = NULL;
        struct bt_component *upstream_comp = NULL;
        struct bt_port *downstream_port = conn->downstream_port;
        struct bt_port *upstream_port = conn->upstream_port;
-       struct bt_graph *graph = (void *) bt_object_borrow_parent(conn);
+       struct bt_graph *graph = bt_connection_borrow_graph(conn);
        size_t i;
 
+       BT_LOGD("Ending connection: conn-addr=%p, try-remove-from-graph=%d",
+               conn, try_remove_from_graph);
+
        if (downstream_port) {
+               BT_LOGD("Disconnecting connection's downstream port: "
+                       "port-addr=%p, port-name=\"%s\"",
+                       downstream_port, bt_port_get_name(downstream_port));
                downstream_comp = bt_port_get_component(downstream_port);
                bt_port_set_connection(downstream_port, NULL);
                conn->downstream_port = NULL;
        }
 
        if (upstream_port) {
+               BT_LOGD("Disconnecting connection's upstream port: "
+                       "port-addr=%p, port-name=\"%s\"",
+                       upstream_port, bt_port_get_name(upstream_port));
                upstream_comp = bt_port_get_component(upstream_port);
                bt_port_set_connection(upstream_port, NULL);
                conn->upstream_port = NULL;
@@ -227,7 +225,7 @@ void bt_connection_disconnect_ports(struct bt_connection *conn)
                bt_component_port_disconnected(upstream_comp, upstream_port);
        }
 
-       assert(graph);
+       BT_ASSERT(graph);
        /* bt_graph_notify_ports_disconnected() logs details */
        bt_graph_notify_ports_disconnected(graph, upstream_comp,
                downstream_comp, upstream_port, downstream_port);
@@ -239,24 +237,27 @@ void bt_connection_disconnect_ports(struct bt_connection *conn)
         * notification iterator created from it.
         */
        for (i = 0; i < conn->iterators->len; i++) {
-               struct bt_notification_iterator *iterator =
+               struct bt_notification_iterator_private_connection *iterator =
                        g_ptr_array_index(conn->iterators, i);
 
                BT_LOGD("Finalizing notification iterator created by this ended connection: "
                        "conn-addr=%p, iter-addr=%p", conn, iterator);
-               bt_notification_iterator_finalize(iterator);
+               bt_private_connection_notification_iterator_finalize(iterator);
 
                /*
                 * Make sure this iterator does not try to remove itself
                 * from this connection's iterators on destruction
                 * because this connection won't exist anymore.
                 */
-               bt_notification_iterator_set_connection(iterator,
-                       NULL);
+               bt_private_connection_notification_iterator_set_connection(
+                       iterator, NULL);
        }
 
        g_ptr_array_set_size(conn->iterators, 0);
-       bt_connection_try_remove_from_graph(conn);
+
+       if (try_remove_from_graph) {
+               bt_connection_try_remove_from_graph(conn);
+       }
 }
 
 struct bt_port *bt_connection_get_upstream_port(
@@ -271,45 +272,60 @@ struct bt_port *bt_connection_get_downstream_port(
        return connection ? bt_get(connection->downstream_port) : NULL;
 }
 
-struct bt_notification_iterator *
+enum bt_connection_status
 bt_private_connection_create_notification_iterator(
                struct bt_private_connection *private_connection,
-               const enum bt_notification_type *notification_types)
+               struct bt_notification_iterator **user_iterator)
 {
-       enum bt_notification_iterator_status ret_iterator;
        enum bt_component_class_type upstream_comp_class_type;
-       struct bt_notification_iterator *iterator = NULL;
+       struct bt_notification_iterator_private_connection *iterator = NULL;
        struct bt_port *upstream_port = NULL;
        struct bt_component *upstream_component = NULL;
        struct bt_component_class *upstream_comp_class = NULL;
        struct bt_connection *connection = NULL;
        bt_component_class_notification_iterator_init_method init_method = NULL;
-       static const enum bt_notification_type all_notif_types[] = {
-               BT_NOTIFICATION_TYPE_ALL,
-               BT_NOTIFICATION_TYPE_SENTINEL,
-       };
+       enum bt_connection_status status;
 
        if (!private_connection) {
                BT_LOGW_STR("Invalid parameter: private connection is NULL.");
-               goto error;
+               status = BT_CONNECTION_STATUS_INVALID;
+               goto end;
        }
 
-       if (!notification_types) {
-               BT_LOGD_STR("No notification types: subscribing to all notifications.");
-               notification_types = all_notif_types;
+       if (!user_iterator) {
+               BT_LOGW_STR("Invalid parameter: notification iterator pointer is NULL.");
+               status = BT_CONNECTION_STATUS_INVALID;
+               goto end;
        }
 
-       connection = bt_connection_from_private(private_connection);
-       if (!connection->upstream_port || !connection->downstream_port) {
+       connection = bt_connection_borrow_from_private(private_connection);
+
+       if (bt_graph_is_canceled(bt_connection_borrow_graph(connection))) {
+               BT_LOGW("Cannot create notification iterator from connection: "
+                       "connection's graph is canceled: "
+                       "conn-addr=%p, upstream-port-addr=%p, "
+                       "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
+                       "upstream-comp-name=\"%s\", graph-addr=%p",
+                       connection, connection->upstream_port,
+                       bt_port_get_name(connection->upstream_port),
+                       upstream_component,
+                       bt_component_get_name(upstream_component),
+                       bt_connection_borrow_graph(connection));
+               status = BT_CONNECTION_STATUS_GRAPH_IS_CANCELED;
+               goto end;
+       }
+
+       if (bt_connection_is_ended(connection)) {
                BT_LOGW("Invalid parameter: connection is ended: "
                        "conn-addr=%p", connection);
-               goto error;
+               status = BT_CONNECTION_STATUS_IS_ENDED;
+               goto end;
        }
 
        upstream_port = connection->upstream_port;
-       assert(upstream_port);
+       BT_ASSERT(upstream_port);
        upstream_component = bt_port_get_component(upstream_port);
-       assert(upstream_component);
+       BT_ASSERT(upstream_component);
        upstream_comp_class = upstream_component->class;
        BT_LOGD("Creating notification iterator from connection: "
                "conn-addr=%p, upstream-port-addr=%p, "
@@ -320,20 +336,13 @@ bt_private_connection_create_notification_iterator(
                upstream_component, bt_component_get_name(upstream_component));
        upstream_comp_class_type =
                bt_component_get_class_type(upstream_component);
-       if (upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
-                       upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_FILTER) {
-               /* Unsupported operation. */
-               BT_LOGW("Upstream component's class is not a source or filter component class: "
-                       "comp-class-type=%s",
-                       bt_component_class_type_string(upstream_comp_class_type));
-               goto error;
-       }
-
-       iterator = bt_notification_iterator_create(upstream_component,
-               upstream_port, notification_types, connection);
-       if (!iterator) {
+       BT_ASSERT(upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
+                       upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_FILTER);
+       status = bt_private_connection_notification_iterator_create(upstream_component,
+               upstream_port, connection, &iterator);
+       if (status != BT_CONNECTION_STATUS_OK) {
                BT_LOGW("Cannot create notification iterator from connection.");
-               goto error;
+               goto end;
        }
 
        switch (upstream_comp_class_type) {
@@ -355,32 +364,30 @@ bt_private_connection_create_notification_iterator(
        }
        default:
                /* Unreachable. */
-               assert(0);
+               BT_LOGF("Unknown component class type: type=%d",
+                       upstream_comp_class_type);
+               abort();
        }
 
        if (init_method) {
-               enum bt_notification_iterator_status status;
+               enum bt_notification_iterator_status iter_status;
 
                BT_LOGD("Calling user's initialization method: iter-addr=%p",
                        iterator);
-               status = init_method(
-                       bt_private_notification_iterator_from_notification_iterator(iterator),
+               iter_status = init_method(
+                       bt_private_connection_private_notification_iterator_from_notification_iterator((void *) iterator),
                        bt_private_port_from_port(upstream_port));
                BT_LOGD("User method returned: status=%s",
-                       bt_notification_iterator_status_string(status));
-               if (status < 0) {
+                       bt_notification_iterator_status_string(iter_status));
+               if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
                        BT_LOGW_STR("Initialization method failed.");
-                       goto error;
+                       status = bt_connection_status_from_notification_iterator_status(
+                               iter_status);
+                       goto end;
                }
        }
 
-       ret_iterator = bt_notification_iterator_validate(iterator);
-       if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
-               BT_LOGW("Notification iterator is invalid: status=%s",
-                       bt_notification_iterator_status_string(ret_iterator));
-               goto error;
-       }
-
+       iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE;
        g_ptr_array_add(connection->iterators, iterator);
        BT_LOGD("Created notification iterator from connection: "
                "conn-addr=%p, upstream-port-addr=%p, "
@@ -390,22 +397,28 @@ bt_private_connection_create_notification_iterator(
                bt_port_get_name(connection->upstream_port),
                upstream_component, bt_component_get_name(upstream_component),
                iterator);
-       goto end;
 
-error:
-       BT_PUT(iterator);
+       /* Move reference to user */
+       *user_iterator = (void *) iterator;
+       iterator = NULL;
 
 end:
        bt_put(upstream_component);
-       return iterator;
+       bt_put(iterator);
+       return status;
 }
 
 BT_HIDDEN
 void bt_connection_remove_iterator(struct bt_connection *conn,
-               struct bt_notification_iterator *iterator)
+               struct bt_notification_iterator_private_connection *iterator)
 {
        g_ptr_array_remove(conn->iterators, iterator);
        BT_LOGV("Removed notification iterator from connection: "
                "conn-addr=%p, iter-addr=%p", conn, iterator);
        bt_connection_try_remove_from_graph(conn);
 }
+
+bt_bool bt_connection_is_ended(struct bt_connection *connection)
+{
+       return !connection->downstream_port && !connection->upstream_port;
+}
This page took 0.027405 seconds and 4 git commands to generate.