Fix: muxer: handle CANCELED status
[babeltrace.git] / plugins / utils / muxer / muxer.c
index 265d653efa32d1d46af20f358898fb490c2230a0..e0553423a927ead2e2d0cf6a13bd06e24e349b82 100644 (file)
 #include <babeltrace/graph/private-port.h>
 #include <plugins-common.h>
 #include <glib.h>
+#include <stdbool.h>
 #include <assert.h>
 
+#define IGNORE_ABSOLUTE_PARAM_NAME     "ignore-absolute"
+
 struct muxer_comp {
        /* Array of struct bt_private_notification_iterator * (weak refs) */
        GPtrArray *muxer_notif_iters;
@@ -50,73 +53,60 @@ struct muxer_comp {
        unsigned int next_port_num;
        size_t available_input_ports;
        bool error;
+       bool initializing_muxer_notif_iter;
+       bool ignore_absolute;
 };
 
 struct muxer_upstream_notif_iter {
-       /* Owned by this */
+       /* Owned by this, NULL if ended */
        struct bt_notification_iterator *notif_iter;
 
-       /* Owned by this*/
+       /* Weak */
        struct bt_private_port *priv_port;
-};
 
+       /*
+        * This flag is true if the upstream notification iterator's
+        * current notification must be considered for the multiplexing
+        * operations. If the upstream iterator returns
+        * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN, then this object
+        * is considered invalid, because its current notification is
+        * still the previous one, but we already took it into account.
+        *
+        * The value of this flag is not important if notif_iter above
+        * is NULL (which means the upstream iterator is finished).
+        */
+       bool is_valid;
+};
 
 struct muxer_notif_iter {
-       /* Array of struct muxer_upstream_notif_iter * (owned by this) */
+       /*
+        * Array of struct muxer_upstream_notif_iter * (owned by this).
+        *
+        * NOTE: This array is searched in linearly to find the youngest
+        * current notification. Keep this until benchmarks confirm that
+        * another data structure is faster than this for our typical
+        * use cases.
+        */
        GPtrArray *muxer_upstream_notif_iters;
 
-       /* Array of struct muxer_upstream_notif_iter * (weak refs) */
-       GPtrArray *muxer_upstream_notif_iters_retry;
+       /*
+        * List of "recently" connected input ports (weak) to
+        * handle by this muxer notification iterator.
+        * muxer_port_connected() adds entries to this list, and the
+        * entries are removed when a notification iterator is created
+        * on the port's connection and put into
+        * muxer_upstream_notif_iters above by
+        * muxer_notif_iter_handle_newly_connected_ports().
+        */
+       GList *newly_connected_priv_ports;
 
        /* Next thing to return by the "next" method */
        struct bt_notification_iterator_next_return next_next_return;
-       int64_t next_next_return_ts_ns;
 
        /* Last time returned in a notification */
        int64_t last_returned_ts_ns;
 };
 
-static
-struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
-               struct muxer_notif_iter *muxer_notif_iter,
-               struct bt_notification_iterator *notif_iter,
-               struct bt_private_port *priv_port)
-{
-       struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
-               g_new0(struct muxer_upstream_notif_iter, 1);
-
-       if (!muxer_upstream_notif_iter) {
-               goto end;
-       }
-
-       muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter);
-       muxer_upstream_notif_iter->priv_port = bt_get(priv_port);
-       g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
-               muxer_upstream_notif_iter);
-
-end:
-       return muxer_upstream_notif_iter;
-}
-
-static inline
-bool muxer_notif_iter_has_upstream_notif_iter_to_retry(
-               struct muxer_notif_iter *muxer_notif_iter)
-{
-       assert(muxer_notif_iter);
-       return muxer_notif_iter->muxer_upstream_notif_iters_retry->len > 0;
-}
-
-static
-void muxer_notif_iter_add_upstream_notif_iter_to_retry(
-               struct muxer_notif_iter *muxer_notif_iter,
-               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
-{
-       assert(muxer_notif_iter);
-       assert(muxer_upstream_notif_iter);
-       g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters_retry,
-               muxer_upstream_notif_iter);
-}
-
 static
 void destroy_muxer_upstream_notif_iter(
                struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
@@ -126,31 +116,30 @@ void destroy_muxer_upstream_notif_iter(
        }
 
        bt_put(muxer_upstream_notif_iter->notif_iter);
-       bt_put(muxer_upstream_notif_iter->priv_port);
        g_free(muxer_upstream_notif_iter);
 }
 
 static
-bool muxer_notif_iter_has_upstream_notif_iter_on_port(
+struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
                struct muxer_notif_iter *muxer_notif_iter,
+               struct bt_notification_iterator *notif_iter,
                struct bt_private_port *priv_port)
 {
-       size_t i;
-       bool exists = false;
-
-       for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
-               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
-                       g_ptr_array_index(
-                               muxer_notif_iter->muxer_upstream_notif_iters, i);
+       struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
+               g_new0(struct muxer_upstream_notif_iter, 1);
 
-               if (muxer_upstream_notif_iter->priv_port == priv_port) {
-                       exists = true;
-                       goto end;
-               }
+       if (!muxer_upstream_notif_iter) {
+               goto end;
        }
 
+       muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter);
+       muxer_upstream_notif_iter->priv_port = priv_port;
+       muxer_upstream_notif_iter->is_valid = false;
+       g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
+               muxer_upstream_notif_iter);
+
 end:
-       return exists;
+       return muxer_upstream_notif_iter;
 }
 
 static
@@ -176,7 +165,7 @@ int ensure_available_input_port(struct bt_private_component *priv_comp)
 
        g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
        priv_port = bt_private_component_filter_add_input_private_port(
-               priv_comp, port_name->str);
+               priv_comp, port_name->str, NULL);
        if (!priv_port) {
                ret = -1;
                goto end;
@@ -190,36 +179,6 @@ end:
                g_string_free(port_name, TRUE);
        }
 
-       BT_PUT(priv_port);
-       return ret;
-}
-
-static
-int remove_default_ports(struct bt_private_component *priv_comp)
-{
-       struct bt_private_port *priv_port;
-       int ret = 0;
-
-       priv_port = bt_private_component_filter_get_default_input_private_port(
-               priv_comp);
-       if (priv_port) {
-               ret = bt_private_port_remove_from_component(priv_port);
-               if (ret) {
-                       goto end;
-               }
-       }
-
-       bt_put(priv_port);
-       priv_port = bt_private_component_filter_get_default_output_private_port(
-               priv_comp);
-       if (priv_port) {
-               ret = bt_private_port_remove_from_component(priv_port);
-               if (ret) {
-                       goto end;
-               }
-       }
-
-end:
        bt_put(priv_port);
        return ret;
 }
@@ -231,7 +190,7 @@ int create_output_port(struct bt_private_component *priv_comp)
        int ret = 0;
 
        priv_port = bt_private_component_filter_add_output_private_port(
-               priv_comp, "out");
+               priv_comp, "out", NULL);
        if (!priv_port) {
                ret = -1;
        }
@@ -254,6 +213,75 @@ void destroy_muxer_comp(struct muxer_comp *muxer_comp)
        g_free(muxer_comp);
 }
 
+static
+struct bt_value *get_default_params(void)
+{
+       struct bt_value *params;
+       int ret;
+
+       params = bt_value_map_create();
+       if (!params) {
+               goto error;
+       }
+
+       ret = bt_value_map_insert_bool(params, IGNORE_ABSOLUTE_PARAM_NAME,
+               false);
+       if (ret) {
+               goto error;
+       }
+
+       goto end;
+
+error:
+       BT_PUT(params);
+
+end:
+       return params;
+}
+
+static
+int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params)
+{
+       struct bt_value *default_params = NULL;
+       struct bt_value *real_params = NULL;
+       struct bt_value *ignore_absolute = NULL;
+       int ret = 0;
+       bt_bool bool_val;
+
+       default_params = get_default_params();
+       if (!default_params) {
+               goto error;
+       }
+
+       real_params = bt_value_map_extend(default_params, params);
+       if (!real_params) {
+               goto error;
+       }
+
+       ignore_absolute = bt_value_map_get(real_params,
+               IGNORE_ABSOLUTE_PARAM_NAME);
+       if (!bt_value_is_bool(ignore_absolute)) {
+               goto error;
+       }
+
+       if (bt_value_bool_get(ignore_absolute, &bool_val)) {
+               goto error;
+       }
+
+       muxer_comp->ignore_absolute = (bool) bool_val;
+
+       goto end;
+
+error:
+       ret = -1;
+
+end:
+       bt_put(default_params);
+       bt_put(real_params);
+       bt_put(ignore_absolute);
+       return ret;
+}
+
 BT_HIDDEN
 enum bt_component_status muxer_init(
                struct bt_private_component *priv_comp,
@@ -267,6 +295,11 @@ enum bt_component_status muxer_init(
                goto error;
        }
 
+       ret = configure_muxer_comp(muxer_comp, params);
+       if (ret) {
+               goto error;
+       }
+
        muxer_comp->muxer_notif_iters = g_ptr_array_new();
        if (!muxer_comp->muxer_notif_iters) {
                goto error;
@@ -275,11 +308,6 @@ enum bt_component_status muxer_init(
        muxer_comp->priv_comp = priv_comp;
        ret = bt_private_component_set_user_data(priv_comp, muxer_comp);
        assert(ret == 0);
-       ret = remove_default_ports(priv_comp);
-       if (ret) {
-               goto error;
-       }
-
        ret = ensure_available_input_port(priv_comp);
        if (ret) {
                goto error;
@@ -330,8 +358,11 @@ struct bt_notification_iterator *create_notif_iter_on_input_port(
                goto end;
        }
 
+       // TODO: Advance the iterator to >= the time of the latest
+       //       returned notification by the muxer notification
+       //       iterator which creates it.
        notif_iter = bt_private_connection_create_notification_iterator(
-               priv_conn);
+               priv_conn, NULL);
        if (!notif_iter) {
                *ret = -1;
                goto end;
@@ -343,6 +374,128 @@ end:
        return notif_iter;
 }
 
+static
+enum bt_notification_iterator_status muxer_upstream_notif_iter_next(
+               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
+{
+       enum bt_notification_iterator_status status;
+
+       status = bt_notification_iterator_next(
+               muxer_upstream_notif_iter->notif_iter);
+
+       switch (status) {
+       case BT_NOTIFICATION_ITERATOR_STATUS_OK:
+               /*
+                * Notification iterator's current notification is valid:
+                * it must be considered for muxing operations.
+                */
+               muxer_upstream_notif_iter->is_valid = true;
+               break;
+       case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
+               /*
+                * Notification iterator's current notification is not
+                * valid anymore. Return
+                * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
+                * immediately.
+                */
+               muxer_upstream_notif_iter->is_valid = false;
+               break;
+       case BT_NOTIFICATION_ITERATOR_STATUS_END:       /* Fall-through. */
+       case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
+               /*
+                * Notification iterator reached the end: release it. It
+                * won't be considered again to find the youngest
+                * notification.
+                */
+               BT_PUT(muxer_upstream_notif_iter->notif_iter);
+               muxer_upstream_notif_iter->is_valid = false;
+               status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
+               break;
+       default:
+               /* Error or unsupported status code */
+               status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+               break;
+       }
+
+       return status;
+}
+
+static
+int muxer_notif_iter_handle_newly_connected_ports(
+               struct muxer_notif_iter *muxer_notif_iter)
+{
+       int ret = 0;
+
+       /*
+        * Here we create one upstream notification iterator for each
+        * newly connected port. We do not perform an initial "next" on
+        * those new upstream notification iterators: they are
+        * invalidated, to be validated later. The list of newly
+        * connected ports to handle here is updated by
+        * muxer_port_connected().
+        */
+       while (true) {
+               GList *node = muxer_notif_iter->newly_connected_priv_ports;
+               struct bt_private_port *priv_port;
+               struct bt_port *port;
+               struct bt_notification_iterator *upstream_notif_iter = NULL;
+               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
+
+               if (!node) {
+                       break;
+               }
+
+               priv_port = node->data;
+               port = bt_port_from_private_port(priv_port);
+               assert(port);
+
+               if (!bt_port_is_connected(port)) {
+                       /*
+                        * Looks like this port is not connected
+                        * anymore: we can't create an upstream
+                        * notification iterator on its (non-existing)
+                        * connection in this case.
+                        */
+                       goto remove_node;
+               }
+
+               BT_PUT(port);
+               upstream_notif_iter = create_notif_iter_on_input_port(priv_port,
+                       &ret);
+               if (ret) {
+                       assert(!upstream_notif_iter);
+                       goto error;
+               }
+
+               muxer_upstream_notif_iter =
+                       muxer_notif_iter_add_upstream_notif_iter(
+                               muxer_notif_iter, upstream_notif_iter,
+                               priv_port);
+               BT_PUT(upstream_notif_iter);
+               if (!muxer_upstream_notif_iter) {
+                       goto error;
+               }
+
+remove_node:
+               bt_put(upstream_notif_iter);
+               bt_put(port);
+               muxer_notif_iter->newly_connected_priv_ports =
+                       g_list_delete_link(
+                               muxer_notif_iter->newly_connected_priv_ports,
+                               node);
+       }
+
+       goto end;
+
+error:
+       if (ret >= 0) {
+               ret = -1;
+       }
+
+end:
+       return ret;
+}
+
 static
 int get_notif_ts_ns(struct muxer_comp *muxer_comp,
                struct bt_notification *notif, int64_t last_returned_ts_ns,
@@ -366,14 +519,11 @@ int get_notif_ts_ns(struct muxer_comp *muxer_comp,
 
        case BT_NOTIFICATION_TYPE_INACTIVITY:
                cc_prio_map =
-                       bt_notification_event_get_clock_class_priority_map(
+                       bt_notification_inactivity_get_clock_class_priority_map(
                                notif);
                break;
        default:
-               /*
-                * All the other notifications have a higher
-                * priority.
-                */
+               /* All the other notifications have a higher priority */
                *ts_ns = last_returned_ts_ns;
                goto end;
        }
@@ -399,17 +549,15 @@ int get_notif_ts_ns(struct muxer_comp *muxer_comp,
                goto error;
        }
 
-       if (!bt_ctf_clock_class_get_is_absolute(clock_class)) {
+       if (!muxer_comp->ignore_absolute &&
+                       !bt_ctf_clock_class_is_absolute(clock_class)) {
                goto error;
        }
 
        switch (bt_notification_get_type(notif)) {
        case BT_NOTIFICATION_TYPE_EVENT:
                event = bt_notification_event_get_event(notif);
-               if (!event) {
-                       goto error;
-               }
-
+               assert(event);
                clock_value = bt_ctf_event_get_clock_value(event,
                        clock_class);
                break;
@@ -443,6 +591,23 @@ end:
        return ret;
 }
 
+/*
+ * This function finds the youngest available notification amongst the
+ * non-ended upstream notification iterators and returns the upstream
+ * notification iterator which has it, or
+ * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available
+ * notification.
+ *
+ * This function does NOT:
+ *
+ * * Update any upstream notification iterator.
+ * * Check for newly connected ports.
+ * * Check the upstream notification iterators to retry.
+ *
+ * On sucess, this function sets *muxer_upstream_notif_iter to the
+ * upstream notification iterator of which the current notification is
+ * the youngest, and sets *ts_ns to its time.
+ */
 static
 enum bt_notification_iterator_status
 muxer_notif_iter_youngest_upstream_notif_iter(
@@ -469,10 +634,11 @@ muxer_notif_iter_youngest_upstream_notif_iter(
                int64_t notif_ts_ns;
 
                if (!cur_muxer_upstream_notif_iter->notif_iter) {
-                       /* This upstream notification iterator is done */
+                       /* This upstream notification iterator is ended */
                        continue;
                }
 
+               assert(cur_muxer_upstream_notif_iter->is_valid);
                notif = bt_notification_iterator_get_notification(
                        cur_muxer_upstream_notif_iter->notif_iter);
                assert(notif);
@@ -503,191 +669,193 @@ end:
 }
 
 static
-int muxer_notif_iter_set_next_next_return(struct muxer_comp *muxer_comp,
+enum bt_notification_iterator_status validate_muxer_upstream_notif_iter(
+       struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
+{
+       enum bt_notification_iterator_status status =
+               BT_NOTIFICATION_ITERATOR_STATUS_OK;
+
+       if (muxer_upstream_notif_iter->is_valid ||
+                       !muxer_upstream_notif_iter->notif_iter) {
+               goto end;
+       }
+
+       status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter);
+
+end:
+       return status;
+}
+
+static
+enum bt_notification_iterator_status validate_muxer_upstream_notif_iters(
+       struct muxer_notif_iter *muxer_notif_iter)
+{
+       enum bt_notification_iterator_status status =
+               BT_NOTIFICATION_ITERATOR_STATUS_OK;
+       size_t i;
+
+       for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
+               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
+                       g_ptr_array_index(
+                               muxer_notif_iter->muxer_upstream_notif_iters,
+                               i);
+
+               status = validate_muxer_upstream_notif_iter(
+                       muxer_upstream_notif_iter);
+               if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
+                       goto end;
+               }
+       }
+
+end:
+       return status;
+}
+
+static
+struct bt_notification_iterator_next_return muxer_notif_iter_do_next(
+               struct muxer_comp *muxer_comp,
                struct muxer_notif_iter *muxer_notif_iter)
 {
-       struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
-       struct bt_notification *notif = NULL;
-       enum bt_notification_iterator_status notif_iter_status;
-       int ret = 0;
+       struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL;
+       struct bt_notification_iterator_next_return next_return = {
+               .notification = NULL,
+               .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
+       };
+       int64_t next_return_ts;
+
+       while (true) {
+               int ret = muxer_notif_iter_handle_newly_connected_ports(
+                       muxer_notif_iter);
+
+               if (ret) {
+                       next_return.status =
+                               BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+                       goto end;
+               }
+
+               next_return.status =
+                       validate_muxer_upstream_notif_iters(muxer_notif_iter);
+               if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
+                       goto end;
+               }
 
-       if (muxer_notif_iter_has_upstream_notif_iter_to_retry(muxer_notif_iter)) {
                /*
-                * At least one upstream notification iterator to retry:
-                * try again later.
+                * At this point, we know that all the existing upstream
+                * notification iterators are valid. However the
+                * operations to validate them (during
+                * validate_muxer_upstream_notif_iters()) may have
+                * connected new ports. If no ports were connected
+                * during this operation, exit the loop.
                 */
-               muxer_notif_iter->next_next_return.status =
-                       BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
-               BT_PUT(muxer_notif_iter->next_next_return.notification);
-               goto end;
+               if (!muxer_notif_iter->newly_connected_priv_ports) {
+                       break;
+               }
        }
 
+       assert(!muxer_notif_iter->newly_connected_priv_ports);
+
        /*
-        * Pick the current youngest notification and advance this
-        * upstream notification iterator.
+        * At this point we know that all the existing upstream
+        * notification iterators are valid. We can find the one,
+        * amongst those, of which the current notification is the
+        * youngest.
         */
-       notif_iter_status =
+       next_return.status =
                muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp,
                        muxer_notif_iter, &muxer_upstream_notif_iter,
-                       &muxer_notif_iter->next_next_return_ts_ns);
-       if (notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
-               /* No more active upstream notification iterator */
-               muxer_notif_iter->next_next_return.status =
-                       BT_NOTIFICATION_ITERATOR_STATUS_END;
-               BT_PUT(muxer_notif_iter->next_next_return.notification);
+                       &next_return_ts);
+       if (next_return.status < 0 ||
+                       next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_END ||
+                       next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_CANCELED) {
                goto end;
        }
 
-       if (notif_iter_status < 0) {
-               ret = -1;
+       if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) {
+               next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
                goto end;
        }
 
+       assert(next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK);
        assert(muxer_upstream_notif_iter);
-       notif = bt_notification_iterator_get_notification(
-               muxer_upstream_notif_iter->notif_iter);
-       assert(notif);
-       muxer_notif_iter->next_next_return.status =
-               BT_NOTIFICATION_ITERATOR_STATUS_OK;
-       BT_MOVE(muxer_notif_iter->next_next_return.notification, notif);
-       notif_iter_status = bt_notification_iterator_next(
+       next_return.notification = bt_notification_iterator_get_notification(
                muxer_upstream_notif_iter->notif_iter);
-       if (notif_iter_status < 0) {
-               ret = -1;
-               goto end;
-       }
+       assert(next_return.notification);
 
-       if (notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
-               /* This upstream notification iterator is done */
-               BT_PUT(muxer_upstream_notif_iter->notif_iter);
-               goto ensure_monotonic;
-       }
+       /*
+        * We invalidate the upstream notification iterator so that, the
+        * next time this function is called,
+        * validate_muxer_upstream_notif_iters() will make it valid.
+        */
+       muxer_upstream_notif_iter->is_valid = false;
+       muxer_notif_iter->last_returned_ts_ns = next_return_ts;
 
-       assert(notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK ||
-               notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN);
+end:
+       return next_return;
+}
 
-       if (notif_iter_status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN) {
-               muxer_notif_iter_add_upstream_notif_iter_to_retry(
-                       muxer_notif_iter, muxer_upstream_notif_iter);
+static
+void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
+{
+       if (!muxer_notif_iter) {
+               return;
        }
 
-ensure_monotonic:
-       /*
-        * Here we have the next "next" return value. It won't change
-        * until it is returned by the next call to our "next" method.
-        * If its time is less than the time of the last notification
-        * that our "next" method returned, then fail because the
-        * muxer's output wouldn't be monotonic.
-        */
-       if (muxer_notif_iter->next_next_return_ts_ns <
-                       muxer_notif_iter->last_returned_ts_ns) {
-               ret = -1;
-               goto end;
+       if (muxer_notif_iter->muxer_upstream_notif_iters) {
+               g_ptr_array_free(
+                       muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
        }
 
-       /*
-        * We are now sure that the next "next" return value will not
-        * change until it is returned by this muxer notification
-        * iterator. It is now safe to set the last returned time
-        * to this one.
-        */
-       muxer_notif_iter->last_returned_ts_ns =
-               muxer_notif_iter->next_next_return_ts_ns;
-
-end:
-       return ret;
+       g_list_free(muxer_notif_iter->newly_connected_priv_ports);
+       g_free(muxer_notif_iter);
 }
 
 static
-int muxer_notif_iter_update_upstream_notif_iters(struct muxer_comp *muxer_comp,
+int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp,
                struct muxer_notif_iter *muxer_notif_iter)
 {
-       struct bt_component *comp = NULL;
+       struct bt_component *comp;
+       int64_t count;
+       int64_t i;
        int ret = 0;
-       uint64_t count;
-       size_t i;
 
+       /*
+        * Add the connected input ports to this muxer notification
+        * iterator's list of newly connected ports. They will be
+        * handled by muxer_notif_iter_handle_newly_connected_ports().
+        */
        comp = bt_component_from_private_component(muxer_comp->priv_comp);
        assert(comp);
-       ret = bt_component_filter_get_input_port_count(comp, &count);
-       assert(ret == 0);
+       count = bt_component_filter_get_input_port_count(comp);
+       if (count < 0) {
+               goto end;
+       }
 
        for (i = 0; i < count; i++) {
                struct bt_private_port *priv_port =
-                       bt_private_component_filter_get_input_private_port_at_index(
+                       bt_private_component_filter_get_input_private_port_by_index(
                                muxer_comp->priv_comp, i);
                struct bt_port *port;
-               struct bt_notification_iterator *upstream_notif_iter;
-               enum bt_notification_iterator_status next_status;
-               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
 
                assert(priv_port);
-
-               if (muxer_notif_iter_has_upstream_notif_iter_on_port(
-                               muxer_notif_iter, priv_port)) {
-                       bt_put(priv_port);
-                       continue;
-               }
-
                port = bt_port_from_private_port(priv_port);
+               assert(port);
 
                if (!bt_port_is_connected(port)) {
-                       bt_put(port);
                        bt_put(priv_port);
+                       bt_put(port);
                        continue;
                }
 
                bt_put(port);
-               upstream_notif_iter = create_notif_iter_on_input_port(priv_port,
-                       &ret);
-               if (ret) {
-                       assert(!upstream_notif_iter);
-                       bt_put(priv_port);
-                       goto error;
-               }
-
-               next_status = bt_notification_iterator_next(
-                       upstream_notif_iter);
-               if (next_status < 0) {
-                       bt_put(priv_port);
-                       bt_put(upstream_notif_iter);
-                       ret = next_status;
-                       goto error;
-               }
-
-               if (next_status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
-                       /* Already the end: do not even keep it */
-                       bt_put(priv_port);
-                       bt_put(upstream_notif_iter);
-                       continue;
-               }
-
-               assert(next_status == BT_NOTIFICATION_ITERATOR_STATUS_OK ||
-                       next_status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN);
-               muxer_upstream_notif_iter =
-                       muxer_notif_iter_add_upstream_notif_iter(
-                               muxer_notif_iter, upstream_notif_iter,
+               bt_put(priv_port);
+               muxer_notif_iter->newly_connected_priv_ports =
+                       g_list_append(
+                               muxer_notif_iter->newly_connected_priv_ports,
                                priv_port);
-               if (!muxer_upstream_notif_iter) {
-                       bt_put(priv_port);
-                       bt_put(upstream_notif_iter);
-                       goto error;
-               }
-
-               if (next_status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN) {
-                       muxer_notif_iter_add_upstream_notif_iter_to_retry(
-                               muxer_notif_iter, muxer_upstream_notif_iter);
+               if (!muxer_notif_iter->newly_connected_priv_ports) {
+                       ret = -1;
+                       goto end;
                }
-
-               bt_put(priv_port);
-               bt_put(upstream_notif_iter);
-       }
-
-       goto end;
-
-error:
-       if (ret >= 0) {
-               ret = -1;
        }
 
 end:
@@ -695,27 +863,6 @@ end:
        return ret;
 }
 
-static
-void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
-{
-       if (!muxer_notif_iter) {
-               return;
-       }
-
-       if (muxer_notif_iter->muxer_upstream_notif_iters) {
-               g_ptr_array_free(
-                       muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
-       }
-
-       if (muxer_notif_iter->muxer_upstream_notif_iters_retry) {
-               g_ptr_array_free(
-                       muxer_notif_iter->muxer_upstream_notif_iters_retry,
-                       TRUE);
-       }
-
-       g_free(muxer_notif_iter);
-}
-
 BT_HIDDEN
 enum bt_notification_iterator_status muxer_notif_iter_init(
                struct bt_private_notification_iterator *priv_notif_iter,
@@ -733,6 +880,17 @@ enum bt_notification_iterator_status muxer_notif_iter_init(
        assert(priv_comp);
        muxer_comp = bt_private_component_get_user_data(priv_comp);
        assert(muxer_comp);
+
+       if (muxer_comp->initializing_muxer_notif_iter) {
+               /*
+                * Weird, unhandled situation detected: downstream
+                * creates a muxer notification iterator while creating
+                * another muxer notification iterator (same component).
+                */
+               goto error;
+       }
+
+       muxer_comp->initializing_muxer_notif_iter = true;
        muxer_notif_iter = g_new0(struct muxer_notif_iter, 1);
        if (!muxer_notif_iter) {
                goto error;
@@ -746,29 +904,16 @@ enum bt_notification_iterator_status muxer_notif_iter_init(
                goto error;
        }
 
-       muxer_notif_iter->muxer_upstream_notif_iters_retry = g_ptr_array_new();
-       if (!muxer_notif_iter->muxer_upstream_notif_iters_retry) {
-               goto error;
-       }
-
        /*
-        * Initial upstream notification iterator update: this creates
-        * one upstream notification iterator for each connected port
-        * without an upstream notification iterator (for this muxer
-        * notification iterator).
-        *
-        * At this point the next "next" return value is not set yet.
+        * Add the muxer notification iterator to the component's array
+        * of muxer notification iterators here because
+        * muxer_notif_iter_init_newly_connected_ports() can cause
+        * muxer_port_connected() to be called, which adds the newly
+        * connected port to each muxer notification iterator's list of
+        * newly connected ports.
         */
-       ret = muxer_notif_iter_update_upstream_notif_iters(muxer_comp,
-               muxer_notif_iter);
-       if (ret) {
-               goto error;
-       }
-
-       /*
-        * Set the initial "next" return value.
-        */
-       ret = muxer_notif_iter_set_next_next_return(muxer_comp,
+       g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
+       ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp,
                muxer_notif_iter);
        if (ret) {
                goto error;
@@ -777,10 +922,15 @@ enum bt_notification_iterator_status muxer_notif_iter_init(
        ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
                muxer_notif_iter);
        assert(ret == 0);
-       g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
        goto end;
 
 error:
+       if (g_ptr_array_index(muxer_comp->muxer_notif_iters,
+                       muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) {
+               g_ptr_array_remove_index(muxer_comp->muxer_notif_iters,
+                       muxer_comp->muxer_notif_iters->len - 1);
+       }
+
        destroy_muxer_notif_iter(muxer_notif_iter);
        ret = bt_private_notification_iterator_set_user_data(priv_notif_iter,
                NULL);
@@ -788,6 +938,7 @@ error:
        status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
 
 end:
+       muxer_comp->initializing_muxer_notif_iter = false;
        bt_put(priv_comp);
        return status;
 }
@@ -819,15 +970,11 @@ BT_HIDDEN
 struct bt_notification_iterator_next_return muxer_notif_iter_next(
                struct bt_private_notification_iterator *priv_notif_iter)
 {
-       struct bt_notification_iterator_next_return next_ret = {
-               .notification = NULL,
-       };
+       struct bt_notification_iterator_next_return next_ret;
        struct muxer_notif_iter *muxer_notif_iter =
                bt_private_notification_iterator_get_user_data(priv_notif_iter);
        struct bt_private_component *priv_comp = NULL;
        struct muxer_comp *muxer_comp = NULL;
-       size_t i;
-       int ret;
 
        assert(muxer_notif_iter);
        priv_comp = bt_private_notification_iterator_get_private_component(
@@ -838,83 +985,12 @@ struct bt_notification_iterator_next_return muxer_notif_iter_next(
 
        /* Are we in an error state set elsewhere? */
        if (unlikely(muxer_comp->error)) {
-               goto error;
-       }
-
-       /*
-        * If we have upstream notification iterators to retry, retry
-        * them now. Each one we find which now has a notification or
-        * is in "end" state, we set it to NULL in this array. Then
-        * we remove all the NULL values from this array.
-        */
-       for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters_retry->len; i++) {
-               struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
-                       g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters_retry, i);
-               enum bt_notification_iterator_status status;
-
-               assert(muxer_upstream_notif_iter->notif_iter);
-               status = bt_notification_iterator_next(
-                       muxer_upstream_notif_iter->notif_iter);
-               if (status < 0) {
-                       /*
-                        * Technically we have a next "next" return
-                        * value which is ready for this call, but we're
-                        * failing within this call, so discard this
-                        * buffer.
-                        */
-                       goto error;
-               }
-
-               if (status == BT_NOTIFICATION_ITERATOR_STATUS_END) {
-                       /*
-                        * This upstream notification iterator is done.
-                        * Set it to NULL so that it's removed later.
-                        */
-                       g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters_retry,
-                               i) = NULL;
-                       BT_PUT(muxer_upstream_notif_iter->notif_iter);
-                       continue;
-               }
-
-               assert(status == BT_NOTIFICATION_ITERATOR_STATUS_OK ||
-                       status == BT_NOTIFICATION_ITERATOR_STATUS_AGAIN);
-
-               if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
-                       /*
-                        * This upstream notification iterator now has.
-                        * a notification. Remove it from this array.
-                        */
-                       g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters_retry,
-                               i) = NULL;
-                       continue;
-               }
-       }
-
-       /*
-        * Remove NULL values from the array of upstream notification
-        * iterators to retry.
-        */
-       while (g_ptr_array_remove_fast(
-               muxer_notif_iter->muxer_upstream_notif_iters_retry, NULL));
-
-       /* Take our next "next" next return value */
-       next_ret = muxer_notif_iter->next_next_return;
-       muxer_notif_iter->next_next_return.status =
-               BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
-       muxer_notif_iter->next_next_return.notification = NULL;
-
-       /* Set the next "next" return value */
-       ret = muxer_notif_iter_set_next_next_return(muxer_comp,
-               muxer_notif_iter);
-       if (ret) {
-               goto error;
+               next_ret.notification = NULL;
+               next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+               goto end;
        }
 
-       goto end;
-
-error:
-       BT_PUT(next_ret.notification);
-       next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
+       next_ret = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter);
 
 end:
        bt_put(priv_comp);
@@ -937,16 +1013,8 @@ void muxer_port_connected(
        assert(self_port);
        assert(muxer_comp);
 
-       if (bt_port_get_type(self_port) == BT_PORT_TYPE_INPUT) {
-               int ret;
-
-               /* One less available input port */
-               muxer_comp->available_input_ports--;
-               ret = ensure_available_input_port(priv_comp);
-               if (ret) {
-                       muxer_comp->error = true;
-                       goto end;
-               }
+       if (bt_port_get_type(self_port) == BT_PORT_TYPE_OUTPUT) {
+               goto end;
        }
 
        for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) {
@@ -954,21 +1022,37 @@ void muxer_port_connected(
                        g_ptr_array_index(muxer_comp->muxer_notif_iters, i);
 
                /*
-                * Here we update the list of upstream notification
-                * iterators, but we do NOT call
-                * muxer_notif_iter_set_next_next_return() because we
-                * already have a next "next" return value at this point
-                * (right after the muxer notification iterator
-                * initialization, and always after).
+                * Add this port to the list of newly connected ports
+                * for this muxer notification iterator. We append at
+                * the end of this list while
+                * muxer_notif_iter_handle_newly_connected_ports()
+                * removes the nodes from the beginning.
+                *
+                * The list node owns the private port.
                 */
-               ret = muxer_notif_iter_update_upstream_notif_iters(muxer_comp,
-                       muxer_notif_iter);
-               if (ret) {
+               muxer_notif_iter->newly_connected_priv_ports =
+                       g_list_append(
+                               muxer_notif_iter->newly_connected_priv_ports,
+                               self_private_port);
+               if (!muxer_notif_iter->newly_connected_priv_ports) {
+                       /* Put reference taken by bt_get() above */
                        muxer_comp->error = true;
                        goto end;
                }
        }
 
+       /* One less available input port */
+       muxer_comp->available_input_ports--;
+       ret = ensure_available_input_port(priv_comp);
+       if (ret) {
+               /*
+                * Only way to report an error later since this
+                * method does not return anything.
+                */
+               muxer_comp->error = true;
+               goto end;
+       }
+
 end:
        bt_put(self_port);
 }
@@ -984,6 +1068,17 @@ void muxer_port_disconnected(struct bt_private_component *priv_comp,
        assert(port);
        assert(muxer_comp);
 
+       /*
+        * There's nothing special to do when a port is disconnected
+        * because this component deals with upstream notification
+        * iterators which were already created thanks to connected
+        * ports. The fact that the port is disconnected does not cancel
+        * the upstream notification iterators created using its
+        * connection: they still exist, even if the connection is dead.
+        * The only way to remove an upstream notification iterator is
+        * for its "next" operation to return
+        * BT_NOTIFICATION_ITERATOR_STATUS_END.
+        */
        if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
                /* One more available input port */
                muxer_comp->available_input_ports++;
This page took 0.035688 seconds and 4 git commands to generate.