lib: do not allow port to be removed when message iterators are active
[babeltrace.git] / lib / graph / port.c
index aa8ed98f26f03d865e670ded59fc9b8c459f2278..86d01b92ac82e0bc1ee6023772e375bc8629468c 100644 (file)
@@ -1,8 +1,7 @@
 /*
+ * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
  * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
 #define BT_LOG_TAG "PORT"
 #include <babeltrace/lib-logging-internal.h>
 
-#include <babeltrace/graph/port.h>
+#include <babeltrace/assert-internal.h>
+#include <babeltrace/assert-pre-internal.h>
+#include <babeltrace/graph/port-const.h>
+#include <babeltrace/graph/port-input-const.h>
+#include <babeltrace/graph/port-output-const.h>
 #include <babeltrace/graph/self-component-port.h>
 #include <babeltrace/graph/self-component-port-input.h>
 #include <babeltrace/graph/self-component-port-output.h>
 #include <babeltrace/graph/port-internal.h>
 #include <babeltrace/graph/connection-internal.h>
 #include <babeltrace/object-internal.h>
-#include <babeltrace/object.h>
 #include <babeltrace/compiler-internal.h>
-#include <babeltrace/assert-internal.h>
-#include <babeltrace/assert-pre-internal.h>
 
 static
 void destroy_port(struct bt_object *obj)
@@ -90,28 +90,30 @@ end:
        return port;
 }
 
-const char *bt_port_get_name(struct bt_port *port)
+const char *bt_port_get_name(const struct bt_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
        return port->name->str;
 }
 
-enum bt_port_type bt_port_get_type(struct bt_port *port)
+enum bt_port_type bt_port_get_type(const struct bt_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
        return port->type;
 }
 
-struct bt_connection *bt_port_borrow_connection(struct bt_port *port)
+const struct bt_connection *bt_port_borrow_connection_const(
+               const struct bt_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
        return port->connection;
 }
 
-struct bt_component *bt_port_borrow_component(struct bt_port *port)
+const struct bt_component *bt_port_borrow_component_const(
+               const struct bt_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
-       return (struct bt_component *) bt_object_borrow_parent(&port->base);
+       return bt_port_borrow_component_inline(port);
 }
 
 struct bt_self_component *bt_self_component_port_borrow_component(
@@ -135,6 +137,36 @@ void bt_port_set_connection(struct bt_port *port,
                connection);
 }
 
+static inline
+bool port_connection_iterators_are_finalized(struct bt_port *port)
+{
+       bool ret = true;
+       struct bt_connection *conn = port->connection;
+       uint64_t i;
+
+       if (!conn) {
+               goto end;
+       }
+
+       for (i = 0; i < conn->iterators->len; i++) {
+               struct bt_self_component_port_input_message_iterator *iterator =
+                       conn->iterators->pdata[i];
+
+               BT_ASSERT(iterator);
+
+               if (iterator->state != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING &&
+                               iterator->state != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED) {
+                       BT_ASSERT_PRE_MSG("Message iterator is not being finalized or finalized: "
+                               "%!+i", iterator);
+                       ret = false;
+                       goto end;
+               }
+       }
+
+end:
+       return ret;
+}
+
 enum bt_self_component_port_status bt_self_component_port_remove_from_component(
                struct bt_self_component_port *self_port)
 {
@@ -142,7 +174,8 @@ enum bt_self_component_port_status bt_self_component_port_remove_from_component(
        struct bt_component *comp = NULL;
 
        BT_ASSERT_PRE_NON_NULL(port, "Port");
-
+       BT_ASSERT_PRE(port_connection_iterators_are_finalized(port),
+               "At least one message iterator using this port has the wrong state.");
        comp = (void *) bt_object_borrow_parent(&port->base);
        if (!comp) {
                BT_LIB_LOGV("Port already removed from its component: %!+p",
@@ -157,14 +190,44 @@ end:
        return BT_SELF_PORT_STATUS_OK;
 }
 
-bt_bool bt_port_is_connected(struct bt_port *port)
+bt_bool bt_port_is_connected(const struct bt_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
        return port->connection ? BT_TRUE : BT_FALSE;
 }
 
-void *bt_self_component_port_get_data(struct bt_self_component_port *port)
+void *bt_self_component_port_get_data(const struct bt_self_component_port *port)
 {
        BT_ASSERT_PRE_NON_NULL(port, "Port");
        return ((struct bt_port *) port)->user_data;
 }
+
+void bt_port_get_ref(const struct bt_port *port)
+{
+       bt_object_get_ref(port);
+}
+
+void bt_port_put_ref(const struct bt_port *port)
+{
+       bt_object_put_ref(port);
+}
+
+void bt_port_input_get_ref(const struct bt_port_input *port_input)
+{
+       bt_object_get_ref(port_input);
+}
+
+void bt_port_input_put_ref(const struct bt_port_input *port_input)
+{
+       bt_object_put_ref(port_input);
+}
+
+void bt_port_output_get_ref(const struct bt_port_output *port_output)
+{
+       bt_object_get_ref(port_output);
+}
+
+void bt_port_output_put_ref(const struct bt_port_output *port_output)
+{
+       bt_object_put_ref(port_output);
+}
This page took 0.035274 seconds and 4 git commands to generate.